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