-
Notifications
You must be signed in to change notification settings - Fork 330
Adding custom actions
When you add custom actions to a controller which uses ActiveScaffold, probably you will want to keep look and feel. Also, you have to follow some conventions so your action behaves as default actions, and ActiveScaffold provides some methods to DRY your code.
One of the easiest actions you can add, is an action which update some records and refresh the list. It could be an action which acts over some marked records, or whatever you can imagine.
Action links will be rendered inline by default, but you must remember to set :position to false, because your action won't return html code. On the other hand, you must call list
method after doing your processing. List method will load records for last page you visited, obeying your last search if it's enabled.
If you want to process each record in last visited page you can use each_record_in_page
iterator. If you want to process all records, obeying search and custom conditions (conditions from params, conditions_from_collection, constraints and so on) you can use each_record_in_scope
iterator.
class InvoicesController < ApplicationController
active_scaffold do |config|
config.action_links.add :paid, :position => nil
[more configuration]
end
def paid
each_record_in_page { |record| record.paid! }
list
end
end
Another easy action you can add is one which do some processing and display flash messages. It could be an action which acts over some marked records, or a record action.
You must remember to set :position to false too, because your action won't return html code. You should set method to :put, because you will change some records, and :crud_type if you want to do security checks with that.
On the other hand, you should use process_action_link_action
method in your action. This method has two optional arguments:
- A name to override default responses (
action_update
), which are redirecting to index for html requests, and for ajax requests it will refresh the lists for collection action links and refresh the row, messages and calculations for member action links. If you set this argument,respond_to_action
method will be called with this name, and you will have to define<name>_respond_to_<format>
method for each format you want to respond. - The crud_type to check security when loading the record. By default it will try to guess it.
class InvoicesController < ApplicationController
active_scaffold do |config|
config.action_links.add :paid, :member => true, :crud_type => :update, :method => :put, :position => nil
[more configuration]
end
def paid
end
end