1

I'm currently writing a rails app that has your regular resource like objects. However I would like to make my resources syncable. My web application uses web offline storage to cache results from the server (user's cannot modify data on server making syncing easier). When I fetch from the server, it returns a hash response like:

{
  :new => [...]
  :updated => [...]
  :deleted => [...]
}

This is all well and good until I want to have a regular fetch method that doesn't do any sort of syncing and simply return an array of models

Now my question is I want to create a method in my routes.rb file that sets up routes so that I have a route to synced_index and index. Ideally, I'd be able to do something like this:

synced_resources :plans

And then all of the regular resource routes would be created plus a few extra ones like synced_index. Any ideas on how to best do this?

Note: I do know that you can modify resources with do...end syntax but I'd like to abstract that out into a function since I'd have to do it to a lot of models.

1 Answer 1

2

You can easily add more verbs to a restful route:

resources :plans do
  get 'synced_index', on: :collection
end

Check the guides for more information on this.

If you have several routes that are similar to this, then sure, you can add a 'synced_resources' helper:

def synced_resources(*res)
  res.each do |r|
    resources(r) do
      get 'synced_index', on: :collection
    end
  end
end

Wrap above method in a module to be included in ActionDispatch::Routing::Mapper.

Sign up to request clarification or add additional context in comments.

1 Comment

Ended up putting it in an initializer which is necessary since it needs to be executed before routes get drawn. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.