Class: RubyUIAdmin::BaseAction
- Inherits:
-
Object
- Object
- RubyUIAdmin::BaseAction
- Defined in:
- lib/ruby_ui_admin/base_action.rb
Overview
Base class for custom actions: declarative fields, response helpers and handle.
Action execution is wired through ActionsController.
Instance Attribute Summary collapse
-
#arguments ⇒ Object
NOTE:
fieldsis the DSL declaration method (below), so submitted values are stored separately asfield_values. -
#controller ⇒ Object
writeonly
The controller running the action, used to expose route helpers inside
handle. -
#current_user ⇒ Object
NOTE:
fieldsis the DSL declaration method (below), so submitted values are stored separately asfield_values. -
#field_values ⇒ Object
NOTE:
fieldsis the DSL declaration method (below), so submitted values are stored separately asfield_values. -
#records ⇒ Object
NOTE:
fieldsis the DSL declaration method (below), so submitted values are stored separately asfield_values. -
#resource ⇒ Object
NOTE:
fieldsis the DSL declaration method (below), so submitted values are stored separately asfield_values. -
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#user ⇒ Object
NOTE:
fieldsis the DSL declaration method (below), so submitted values are stored separately asfield_values. -
#view ⇒ Object
NOTE:
fieldsis the DSL declaration method (below), so submitted values are stored separately asfield_values.
Class Method Summary collapse
-
.action_key ⇒ Object
URL-safe identifier for this action, stable across namespaces, e.g.
- .action_name ⇒ Object
- .name ⇒ Object
-
.name=(value) ⇒ Object
Class-level
self.name =with fallback to the real class name.
Instance Method Summary collapse
- #action_fields ⇒ Object
- #download(content, filename) ⇒ Object
- #error(text, **opts) ⇒ Object
-
#field(id, as: :text, **options, &block) ⇒ Object
---- Action form fields DSL ----.
-
#fields ⇒ Object
Overridden by subclasses to declare action form fields.
- #get_fields ⇒ Object
-
#handle(**args) ⇒ Object
Subclasses implement
handle. - #has_fields? ⇒ Boolean
- #inform(text, **opts) ⇒ Object
-
#initialize(view: nil, resource: nil, user: nil, arguments: {}) ⇒ BaseAction
constructor
A new instance of BaseAction.
- #keep_modal_open ⇒ Object
-
#main_app ⇒ Object
---- Route helpers (available inside
handle) ---- Exposemain_app.*(host routes) and the engine route proxy (ruby_ui_admin.*), plus bare*_path/*_urlhelpers resolved against either. -
#message ⇒ Object
The confirmation message shown in the action modal.
-
#method_missing(name, *args, **kwargs, &block) ⇒ Object
Resolves bare
*_path/*_urlcalls inhandleagainst the engine routes first, then the host app (main_app), so e.g. - #name ⇒ Object
- #no_confirmation? ⇒ Boolean
- #redirect_to(path = nil, **opts, &block) ⇒ Object
- #reload ⇒ Object
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
- #ruby_ui_admin ⇒ Object
- #silent ⇒ Object
- #standalone? ⇒ Boolean
-
#succeed(text, **opts) ⇒ Object
---- Response helpers (mutating @response) ----.
- #visible_in_view?(current_view) ⇒ Boolean
- #warn(text, **opts) ⇒ Object
Constructor Details
#initialize(view: nil, resource: nil, user: nil, arguments: {}) ⇒ BaseAction
Returns a new instance of BaseAction.
45 46 47 48 49 50 51 52 53 |
# File 'lib/ruby_ui_admin/base_action.rb', line 45 def initialize(view: nil, resource: nil, user: nil, arguments: {}) @view = view @resource = resource @user = user @current_user = user @arguments = arguments || {} @records = [] @response = default_response end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **kwargs, &block) ⇒ Object
Resolves bare *_path/*_url calls in handle against the engine routes first,
then the host app (main_app), so e.g. resources_posts_path or a host
login_url(...) work without a prefix.
184 185 186 187 188 189 190 191 |
# File 'lib/ruby_ui_admin/base_action.rb', line 184 def method_missing(name, *args, **kwargs, &block) if route_helper?(name) [ruby_ui_admin, main_app].compact.each do |helpers| return helpers.public_send(name, *args, **kwargs) if helpers.respond_to?(name) end end super end |
Instance Attribute Details
#arguments ⇒ Object
NOTE: fields is the DSL declaration method (below), so submitted values are
stored separately as field_values.
40 41 42 |
# File 'lib/ruby_ui_admin/base_action.rb', line 40 def arguments @arguments end |
#controller=(value) ⇒ Object (writeonly)
The controller running the action, used to expose route helpers inside handle.
43 44 45 |
# File 'lib/ruby_ui_admin/base_action.rb', line 43 def controller=(value) @controller = value end |
#current_user ⇒ Object
NOTE: fields is the DSL declaration method (below), so submitted values are
stored separately as field_values.
40 41 42 |
# File 'lib/ruby_ui_admin/base_action.rb', line 40 def current_user @current_user end |
#field_values ⇒ Object
NOTE: fields is the DSL declaration method (below), so submitted values are
stored separately as field_values.
40 41 42 |
# File 'lib/ruby_ui_admin/base_action.rb', line 40 def field_values @field_values end |
#records ⇒ Object
NOTE: fields is the DSL declaration method (below), so submitted values are
stored separately as field_values.
40 41 42 |
# File 'lib/ruby_ui_admin/base_action.rb', line 40 def records @records end |
#resource ⇒ Object
NOTE: fields is the DSL declaration method (below), so submitted values are
stored separately as field_values.
40 41 42 |
# File 'lib/ruby_ui_admin/base_action.rb', line 40 def resource @resource end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
41 42 43 |
# File 'lib/ruby_ui_admin/base_action.rb', line 41 def response @response end |
#user ⇒ Object
NOTE: fields is the DSL declaration method (below), so submitted values are
stored separately as field_values.
40 41 42 |
# File 'lib/ruby_ui_admin/base_action.rb', line 40 def user @user end |
#view ⇒ Object
NOTE: fields is the DSL declaration method (below), so submitted values are
stored separately as field_values.
40 41 42 |
# File 'lib/ruby_ui_admin/base_action.rb', line 40 def view @view end |
Class Method Details
.action_key ⇒ Object
URL-safe identifier for this action, stable across namespaces, e.g. RubyUIAdmin::Actions::Users::ResetPassword -> "users_reset_password".
33 34 35 |
# File 'lib/ruby_ui_admin/base_action.rb', line 33 def action_key to_s.sub(/^RubyUIAdmin::Actions::/, "").gsub("::", "_").underscore end |
.action_name ⇒ Object
27 28 29 |
# File 'lib/ruby_ui_admin/base_action.rb', line 27 def action_name name end |
.name ⇒ Object
23 24 25 |
# File 'lib/ruby_ui_admin/base_action.rb', line 23 def name @display_name || super end |
.name=(value) ⇒ Object
Class-level self.name = with fallback to the real class name.
19 20 21 |
# File 'lib/ruby_ui_admin/base_action.rb', line 19 def name=(value) @display_name = value end |
Instance Method Details
#action_fields ⇒ Object
62 63 64 |
# File 'lib/ruby_ui_admin/base_action.rb', line 62 def action_fields @action_fields ||= [] end |
#download(content, filename) ⇒ Object
176 177 178 179 |
# File 'lib/ruby_ui_admin/base_action.rb', line 176 def download(content, filename) @response[:type] = :download @response[:download] = {content: content, filename: filename} end |
#error(text, **opts) ⇒ Object
145 146 147 |
# File 'lib/ruby_ui_admin/base_action.rb', line 145 def error(text, **opts) (:error, text, **opts) end |
#field(id, as: :text, **options, &block) ⇒ Object
---- Action form fields DSL ----
57 58 59 60 |
# File 'lib/ruby_ui_admin/base_action.rb', line 57 def field(id, as: :text, **, &block) field_class = Fields.field_class_for(as) action_fields << field_class.new(id, **, &block) end |
#fields ⇒ Object
Overridden by subclasses to declare action form fields.
67 |
# File 'lib/ruby_ui_admin/base_action.rb', line 67 def fields; end |
#get_fields ⇒ Object
69 70 71 72 73 |
# File 'lib/ruby_ui_admin/base_action.rb', line 69 def get_fields @action_fields = [] fields action_fields end |
#handle(**args) ⇒ Object
Subclasses implement handle. Two signatures are supported:
def handle(query:, fields:, current_user:, resource:, **); end
def handle(args); end # args[:records], args[:fields], args[:current_user]
84 |
# File 'lib/ruby_ui_admin/base_action.rb', line 84 def handle(**args); end |
#has_fields? ⇒ Boolean
75 76 77 |
# File 'lib/ruby_ui_admin/base_action.rb', line 75 def has_fields? get_fields.any? end |
#inform(text, **opts) ⇒ Object
149 150 151 |
# File 'lib/ruby_ui_admin/base_action.rb', line 149 def inform(text, **opts) (:info, text, **opts) end |
#keep_modal_open ⇒ Object
167 168 169 |
# File 'lib/ruby_ui_admin/base_action.rb', line 167 def keep_modal_open @response[:keep_modal_open] = true end |
#main_app ⇒ Object
---- Route helpers (available inside handle) ----
Expose main_app.* (host routes) and the engine route proxy (ruby_ui_admin.*),
plus bare *_path/*_url helpers resolved against either.
110 111 112 |
# File 'lib/ruby_ui_admin/base_action.rb', line 110 def main_app @controller&.main_app end |
#message ⇒ Object
The confirmation message shown in the action modal. Accepts a literal or a proc
evaluated with resource/record/records available (dynamic message).
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ruby_ui_admin/base_action.rb', line 92 def msg = self.class. return msg unless msg.respond_to?(:call) ExecutionContext.new( target: msg, view: View.wrap(@view), resource: resource, record: records&.first, records: records, current_user: current_user ).handle end |
#name ⇒ Object
86 87 88 |
# File 'lib/ruby_ui_admin/base_action.rb', line 86 def name self.class.name end |
#no_confirmation? ⇒ Boolean
124 125 126 |
# File 'lib/ruby_ui_admin/base_action.rb', line 124 def no_confirmation? !!self.class.no_confirmation end |
#redirect_to(path = nil, **opts, &block) ⇒ Object
157 158 159 160 161 |
# File 'lib/ruby_ui_admin/base_action.rb', line 157 def redirect_to(path = nil, **opts, &block) @response[:type] = :redirect @response[:path] = block || path @response[:redirect_options] = opts end |
#reload ⇒ Object
163 164 165 |
# File 'lib/ruby_ui_admin/base_action.rb', line 163 def reload @response[:type] = :reload end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
193 194 195 |
# File 'lib/ruby_ui_admin/base_action.rb', line 193 def respond_to_missing?(name, include_private = false) (route_helper?(name) && [ruby_ui_admin, main_app].compact.any? { |h| h.respond_to?(name) }) || super end |
#ruby_ui_admin ⇒ Object
114 115 116 117 118 |
# File 'lib/ruby_ui_admin/base_action.rb', line 114 def ruby_ui_admin return @controller.ruby_ui_admin if @controller.respond_to?(:ruby_ui_admin) RubyUIAdmin::Engine.routes.url_helpers if defined?(RubyUIAdmin::Engine) end |
#silent ⇒ Object
171 172 173 174 |
# File 'lib/ruby_ui_admin/base_action.rb', line 171 def silent @response[:messages] = [] @response[:silent] = true end |
#standalone? ⇒ Boolean
120 121 122 |
# File 'lib/ruby_ui_admin/base_action.rb', line 120 def standalone? !!self.class.standalone end |
#succeed(text, **opts) ⇒ Object
---- Response helpers (mutating @response) ----
141 142 143 |
# File 'lib/ruby_ui_admin/base_action.rb', line 141 def succeed(text, **opts) (:success, text, **opts) end |
#visible_in_view?(current_view) ⇒ Boolean
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ruby_ui_admin/base_action.rb', line 128 def visible_in_view?(current_view) return true if self.class.visible.nil? ExecutionContext.new( target: self.class.visible, view: View.wrap(current_view), resource: resource, record: (records&.first) ).handle end |
#warn(text, **opts) ⇒ Object
153 154 155 |
# File 'lib/ruby_ui_admin/base_action.rb', line 153 def warn(text, **opts) (:warning, text, **opts) end |