module ActiveAdmin::StateMachine::DSL
Public Instance Methods
state_action(action, options={}, &controller_action)
click to toggle source
Easily tie into a state_machine action
@param [Symbol] state machine event, ie: :publish @param [Hash] options
- permission [Symbol] permission to check authorization against - http_verb [Symbol] :put, :post, :get, etc
Will call “resource.publish!”, if “resource.can_publish?” returns true
# File lib/active_admin/state_machine/dsl.rb 16 def state_action(action, options={}, &controller_action) 17 singular = config.resource_name.singular 18 plural = config.resource_name.plural 19 20 options[:permission] ||= controller.new.send(:action_to_permission, action) 21 confirmation = options.fetch(:confirm, false) 22 if confirmation == true 23 default = "Are you sure you want to #{action.to_s.humanize.downcase}?" 24 confirmation = ->{ I18n.t(:confirm, scope: "#{plural}.#{action}", default: default) } 25 end 26 27 http_verb = options.fetch :http_verb, :put 28 29 action_item_args = if ActiveAdmin::VERSION.start_with?('0.') 30 [{ only: :show }] 31 else 32 ["state_action_#{action}", { only: :show }] 33 end 34 action_item(*action_item_args) do 35 if resource.send("can_#{action}?") && authorized?(options[:permission], resource) 36 path = resource_path << "/#{action}" 37 label = I18n.t("#{plural}.#{action}.label", default: action.to_s.titleize) 38 39 link_options = {} 40 if confirmation.is_a?(Proc) 41 link_options[:data] ||= {} 42 link_options[:data][:confirm] = instance_exec(&confirmation) 43 end 44 45 link_options[:class] = "btn btn-large" 46 link_options[:method] = http_verb 47 48 link_to label, path, link_options 49 end 50 end 51 52 unless block_given? 53 controller_action = -> do 54 resource.send("#{action}!") 55 flash[:notice] = t("#{plural}.#{action}.flash.success") 56 redirect_to smart_resource_url 57 end 58 end 59 60 member_action action, method: http_verb, &controller_action 61 end