Reputation: 2517
I have a bunch of resources that all have the same route, called synced_index
. The only difference between the route in different resources is it has a different model name. For example:
# Page controller
def synced_index
render :json => Page.my_func
end
# Comment controller
def synced_index
render :json => Comment.my_func
end
This is a simplified example, but I basically want to abstract this into either a module or a Controller class to inherit from. So I can simply put:
include SyncableResource
In my controller class. My problem is determining the resource name. What's the best/most reliable way to do this? Right now I have:
self.class.to_s.delete('Controller').singularize.constantize
It feels a bit hacky to me though.
Upvotes: 1
Views: 710
Reputation: 3424
It depends on the complexity of your app and the classes/namespaces you use. You can take a look at the gem inherited_resources
that the do something like that https://github.com/josevalim/inherited_resources/blob/master/lib/inherited_resources/class_methods.rb#L312
Upvotes: 0
Reputation: 6025
This should do the job:
controller_name.classify
depending on the need, you can add .constantize after it
Upvotes: 4