In my widgets_controller.rb I’ve got the following method
# GET /widgets/index_of_deleted
def index_of_deleted
@widgets = Widget.order("name").deleted
respond_to do |format|
format.html
end
end
And I’ve got many other models that will use this method. How can I create a module that has this method in a generic form, so that I can DRY up my controllers? I think the solution would be to call this..
index_of_deleted("widget")
..in my widgets_controller.rb. Or perhaps I can use the controller’s name and just include this module in each controller that would use this method?
Specifically, what would the implementation of index_of_deleted look like within my Module?
You can write modules and include them. Start by making a file in
lib, maybe like so:Then, in your
WidgetsController, near the top, simple add:You might want to more explicitly namespace your modules (just nest them).
Edit
I’m not sure it’s worth abstracting that really, how many models are there that need this functionality? You could write a helper instead to find deleted items:
But then why not just make a scope for ‘deleted
Modelby name’…Edit 2
You could write the implementation to set an instance variable…