Class: RubyUIAdmin::BaseAction

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#argumentsObject

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_userObject

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_valuesObject

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

#recordsObject

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

#resourceObject

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

#responseObject (readonly)

Returns the value of attribute response.



41
42
43
# File 'lib/ruby_ui_admin/base_action.rb', line 41

def response
  @response
end

#userObject

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

#viewObject

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_keyObject

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_nameObject



27
28
29
# File 'lib/ruby_ui_admin/base_action.rb', line 27

def action_name
  name
end

.nameObject



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_fieldsObject



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)
  add_message(: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, **options, &block)
  field_class = Fields.field_class_for(as)
  action_fields << field_class.new(id, **options, &block)
end

#fieldsObject

Overridden by subclasses to declare action form fields.



67
# File 'lib/ruby_ui_admin/base_action.rb', line 67

def fields; end

#get_fieldsObject



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

Returns:

  • (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)
  add_message(:info, text, **opts)
end

#keep_modal_openObject



167
168
169
# File 'lib/ruby_ui_admin/base_action.rb', line 167

def keep_modal_open
  @response[:keep_modal_open] = true
end

#main_appObject

---- 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

#messageObject

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 message
  msg = self.class.message
  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

#nameObject



86
87
88
# File 'lib/ruby_ui_admin/base_action.rb', line 86

def name
  self.class.name
end

#no_confirmation?Boolean

Returns:

  • (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

#reloadObject



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

Returns:

  • (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_adminObject



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

#silentObject



171
172
173
174
# File 'lib/ruby_ui_admin/base_action.rb', line 171

def silent
  @response[:messages] = []
  @response[:silent] = true
end

#standalone?Boolean

Returns:

  • (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)
  add_message(:success, text, **opts)
end

#visible_in_view?(current_view) ⇒ Boolean

Returns:

  • (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)
  add_message(:warning, text, **opts)
end