-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrails_admin_manage_states.rb
87 lines (66 loc) · 2.31 KB
/
rails_admin_manage_states.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require "rails_admin_manage_states/engine"
module RailsAdminManageStates
end
module RailsAdmin
module Config
module Actions
class ManageStates < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)
# This ensures the action only shows up for Authorized Persons
register_instance_option :visible? do
authorized? # && bindings[:object].has_attribute?(:state)
end
# We want the action on members
register_instance_option :member do
true
end
register_instance_option :link_icon do
'icon-ok'
end
# You may or may not want pjax for your action
register_instance_option :pjax? do
false
end
register_instance_option :http_methods do
[:get, :post]
end
register_instance_option :controller do
Proc.new do
if request.get?
respond_to do |format|
format.html { render @action.template_name }
format.js { render @action.template_name, layout: false }
end
elsif request.post?
unless @authorization_adapter.nil? || @authorization_adapter.authorized?(:all_events, @abstract_model, @object)
@authorization_adapter.try(:authorize, params[:state_events].to_sym, @abstract_model, @object)
end
if params['id'].present?
# puts "STATE EVENTS #{params[@abstract_model.param_key][:state_events]} Abstract model #{@abstract_model}"
if @object.has_attribute?(:updater_id)
@object.updater = _current_user
puts @object.updater
@object.save
end
begin
if @object.send('fire_state_event', params[@abstract_model.param_key][:state_events].to_sym)
@object.save
flash[:success] = I18n.t('admin.actions.manage_states.event_fired', attr: params[:method], event: I18n.t("state_machines.events.#{params[@abstract_model.param_key][:state_events].to_s.mb_chars.downcase}"))
else
flash[:error] = obj.errors.full_messages.join(', ')
end
rescue StandardError => e
Rails.logger.error e
flash[:error] = I18n.t('admin.actions.manage_states.error', err: e.to_s)
end
else
flash[:error] = I18n.t('admin.actions.manage_states.no_id')
end
redirect_to :back
end
end
end
end
end
end
end