Class: ActiveScaffold::DataStructures::ActionLink

Inherits:
Object
  • Object
show all
Defined in:
lib/active_scaffold/data_structures/action_link.rb

Direct Known Subclasses

FilterOption

Constant Summary collapse

NO_OPTIONS =
{}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, options = {}) ⇒ ActionLink

provides a quick way to set any property of the object from a hash



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
# File 'lib/active_scaffold/data_structures/action_link.rb', line 8

def initialize(action, options = {})
  # set defaults
  @action = action
  @label = action
  @confirm = false
  @type = :collection
  @method = :get
  @crud_type =
    case action&.to_sym
    when :destroy then :delete
    when :create, :new then :create
    when :update, :edit then :update
    else :read
    end
  @column = nil
  @image = nil
  @controller = nil
  @parameters = nil
  @dynamic_parameters = nil
  @html_options = nil
  @weight = 0
  self.inline = true

  # apply quick properties
  options.each_pair do |k, v|
    setter = "#{k}="
    send(setter, v) if respond_to? setter
  end
  self.toggle = self.action&.to_sym == :index && !position && (parameters.present? || dynamic_parameters) unless options.include? :toggle
end

Instance Attribute Details

#actionObject

the action-path for this link. what page to request? this is required!



56
57
58
# File 'lib/active_scaffold/data_structures/action_link.rb', line 56

def action
  @action
end

#columnObject

nested action_links are referencing a column



240
241
242
# File 'lib/active_scaffold/data_structures/action_link.rb', line 240

def column
  @column
end

#confirm(label = '') ⇒ Object



110
111
112
113
114
# File 'lib/active_scaffold/data_structures/action_link.rb', line 110

def confirm(label = '')
  return @confirm if !confirm? || @confirm.is_a?(String)

  ActiveScaffold::Registry.cache(:translations, @confirm) { as_(@confirm) } % {label: label}
end

#controllerObject



61
62
63
64
# File 'lib/active_scaffold/data_structures/action_link.rb', line 61

def controller
  @controller = @controller.call if @controller.is_a?(Proc)
  @controller
end

#controller_actionsObject

for links in singular associations, copied from column.actions_for_association_links, excluding actions not available in association’s controller



252
253
254
# File 'lib/active_scaffold/data_structures/action_link.rb', line 252

def controller_actions
  @controller_actions
end

#crud_typeObject

the crud type of the (eventual?) action. different than :method, because this crud action may not be imminent. this is used to determine record-level authorization (e.g. record.authorized_for?(crud_type: link.crud_type). options are :create, :read, :update, and :delete



163
164
165
# File 'lib/active_scaffold/data_structures/action_link.rb', line 163

def crud_type
  @crud_type
end

#dynamic_parametersObject

a block for dynamic_parameters



85
86
87
# File 'lib/active_scaffold/data_structures/action_link.rb', line 85

def dynamic_parameters
  @dynamic_parameters
end

#html_optionsObject



233
234
235
236
237
# File 'lib/active_scaffold/data_structures/action_link.rb', line 233

def html_options
  return @html_options || NO_OPTIONS if frozen?

  @html_options ||= NO_OPTIONS.dup
end

#ignore_methodObject

what method to call on the controller to see if this action_link should be visible if method return true, link won’t be displayed



158
159
160
# File 'lib/active_scaffold/data_structures/action_link.rb', line 158

def ignore_method
  @ignore_method
end

#imageObject

image to use ‘arrow.png’, size: ‘16x16’



105
106
107
# File 'lib/active_scaffold/data_structures/action_link.rb', line 105

def image
  @image
end

#keep_open=(value) ⇒ Object (writeonly)

don’t close the panel when another action link is open



243
244
245
# File 'lib/active_scaffold/data_structures/action_link.rb', line 243

def keep_open=(value)
  @keep_open = value
end

#label(record = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/active_scaffold/data_structures/action_link.rb', line 93

def label(record = nil)
  case @label
  when Symbol
    ActiveScaffold::Registry.cache(:translations, @label) { as_(@label) }
  when Proc
    @label.call(record)
  else
    @label
  end
end

#methodObject

the RESTful method



88
89
90
# File 'lib/active_scaffold/data_structures/action_link.rb', line 88

def method
  @method
end

#parametersObject



73
74
75
76
77
# File 'lib/active_scaffold/data_structures/action_link.rb', line 73

def parameters
  return @parameters || NO_OPTIONS if frozen?

  @parameters ||= NO_OPTIONS.dup
end

#positionObject



219
220
221
222
223
224
225
# File 'lib/active_scaffold/data_structures/action_link.rb', line 219

def position
  return @position unless @position.nil? || @position == true
  return :replace if type == :member
  return :top if type == :collection

  raise "what should the default position be for #{type}?"
end

#prompt(label = '') ⇒ Object



123
124
125
126
127
# File 'lib/active_scaffold/data_structures/action_link.rb', line 123

def prompt(label = '')
  return @prompt if !prompt? || @prompt.is_a?(String)

  ActiveScaffold::Registry.cache(:translations, @prompt) { as_(@prompt) } % {label: label}
end

#prompt_required=(value) ⇒ Object (writeonly)

if the prompt is required, empty value or cancel will prevent running the action



134
135
136
# File 'lib/active_scaffold/data_structures/action_link.rb', line 134

def prompt_required=(value)
  @prompt_required = value
end

#refresh_on_closeObject

enable it to refresh the parent row when the view is closed



154
155
156
# File 'lib/active_scaffold/data_structures/action_link.rb', line 154

def refresh_on_close
  @refresh_on_close
end

#security_methodObject



145
146
147
# File 'lib/active_scaffold/data_structures/action_link.rb', line 145

def security_method
  @security_method || "#{action}_authorized?"
end

#toggleObject

if active class is added to link when current request matches link enabled automatically for links to index with parameters or dynamic parameters disable when is not needed so current request match check is skipped



82
83
84
# File 'lib/active_scaffold/data_structures/action_link.rb', line 82

def toggle
  @toggle
end

#typeObject

what type of link this is. currently supported values are :collection and :member.



228
229
230
# File 'lib/active_scaffold/data_structures/action_link.rb', line 228

def type
  @type
end

#weightObject

the weight for this link in the action links collection, it will be used to sort the collection



53
54
55
# File 'lib/active_scaffold/data_structures/action_link.rb', line 53

def weight
  @weight
end

Instance Method Details

#confirm?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/active_scaffold/data_structures/action_link.rb', line 116

def confirm?
  @confirm.present?
end

#deep_dupObject



44
45
46
47
48
49
50
# File 'lib/active_scaffold/data_structures/action_link.rb', line 44

def deep_dup
  link = dup
  instance_variables.each do |var|
    link.instance_variable_set(var, link.instance_variable_get(var).deep_dup)
  end
  link
end

#freezeObject



272
273
274
275
276
# File 'lib/active_scaffold/data_structures/action_link.rb', line 272

def freeze
  # force generating cache_key, except for column's link without action, or polymorphic associations
  name_to_cache if action && !column&.association&.polymorphic?
  super
end

#initialize_copy(action_link) ⇒ Object



39
40
41
42
# File 'lib/active_scaffold/data_structures/action_link.rb', line 39

def initialize_copy(action_link)
  self.parameters = parameters.clone if action_link.instance_variable_get(:@parameters)
  self.html_options = html_options.clone if action_link.instance_variable_get(:@html_options)
end

#inline=(val) ⇒ Object

an “inline” link is inserted into the existing page exclusive with popup? and page?



167
168
169
170
# File 'lib/active_scaffold/data_structures/action_link.rb', line 167

def inline=(val)
  @inline = (val == true)
  self.popup = self.page = false if @inline
end

#inline?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/active_scaffold/data_structures/action_link.rb', line 172

def inline?
  @inline
end

#keep_open?Boolean

Returns:

  • (Boolean)


245
246
247
# File 'lib/active_scaffold/data_structures/action_link.rb', line 245

def keep_open?
  @keep_open
end

#name_to_cacheObject



259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/active_scaffold/data_structures/action_link.rb', line 259

def name_to_cache
  return @name_to_cache if defined? @name_to_cache

  [
    controller || 'self',
    type,
    action,
    *parameters.map { |k, v| "#{k}=#{v.is_a?(Array) ? v.join(',') : v}" }
  ].compact.join('_').tap do |name_to_cache|
    @name_to_cache = name_to_cache unless frozen?
  end
end

#nested_link?Boolean

indicates that this a nested_link

Returns:

  • (Boolean)


255
256
257
# File 'lib/active_scaffold/data_structures/action_link.rb', line 255

def nested_link?
  @column || parameters&.dig(:named_scope)
end

#page=(val) ⇒ Object

a “page” link displays by reloading the current page exclusive with inline? and popup?



195
196
197
198
# File 'lib/active_scaffold/data_structures/action_link.rb', line 195

def page=(val)
  @page = (val == true)
  self.inline = self.popup = false if @page
end

#page?Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/active_scaffold/data_structures/action_link.rb', line 200

def page?
  @page
end

#popup=(val) ⇒ Object

a “popup” link displays in a separate (browser?) window. this will eventually take arguments. exclusive with inline? and page?



178
179
180
181
182
183
184
185
186
187
# File 'lib/active_scaffold/data_structures/action_link.rb', line 178

def popup=(val)
  @popup = (val == true)
  return unless @popup

  self.inline = self.page = false

  # the :method parameter doesn't mix with the :popup parameter
  # when/if we start using DHTML popups, we can bring :method back
  self.method = nil
end

#popup?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/active_scaffold/data_structures/action_link.rb', line 189

def popup?
  @popup
end

#prompt?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/active_scaffold/data_structures/action_link.rb', line 129

def prompt?
  @prompt.present?
end

#prompt_required?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/active_scaffold/data_structures/action_link.rb', line 136

def prompt_required?
  @prompt_required
end

#security_method_set?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/active_scaffold/data_structures/action_link.rb', line 149

def security_method_set?
  @security_method.present?
end

#static_controller?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/active_scaffold/data_structures/action_link.rb', line 66

def static_controller?
  !(@controller.is_a?(Proc) || (@controller == :polymorph))
end