Module: ActiveScaffold::DataStructures::Column::ProxyableMethods

Extended by:
ActiveSupport::Concern
Included in:
ActiveScaffold::DataStructures::Column, ProxyColumn
Defined in:
lib/active_scaffold/data_structures/column.rb

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object



193
194
195
196
# File 'lib/active_scaffold/data_structures/column.rb', line 193

def <=>(other)
  order_weight = weight <=> other.weight
  order_weight.nonzero? ? order_weight : name.to_s <=> other.name.to_s
end

#associated_number?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/active_scaffold/data_structures/column.rb', line 178

def associated_number?
  @associated_number
end

#attributes=(opts) ⇒ Object



340
341
342
343
344
# File 'lib/active_scaffold/data_structures/column.rb', line 340

def attributes=(opts)
  opts.each do |setting, value|
    send :"#{setting}=", value
  end
end

#cache_count?Boolean

Returns:

  • (Boolean)


336
337
338
# File 'lib/active_scaffold/data_structures/column.rb', line 336

def cache_count?
  includes.blank? && associated_number? && association&.cache_count?
end

#calculation?Boolean

get whether to run a calculation on this column

Returns:

  • (Boolean)


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

def calculation?
  !(calculate == false || calculate.nil?)
end

this should not only delete any existing link but also prevent column links from being automatically added by later routines



99
100
101
102
# File 'lib/active_scaffold/data_structures/column.rb', line 99

def clear_link
  @link = nil
  @autolink = false
end

#convert_to_native?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/active_scaffold/data_structures/column.rb', line 198

def convert_to_native?
  number? && options[:format] && form_ui != :number
end

#description(record = nil, scope = nil) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/active_scaffold/data_structures/column.rb', line 133

def description(record = nil, scope = nil)
  if @description.respond_to?(:call)
    @description.call(record, self, scope)
  elsif @description
    @description
  else
    I18n.t name, scope: [:activerecord, :description, active_record_class.to_s.underscore.to_sym], default: ''
  end
end

#form_ui=(value) ⇒ Object

value must be a Symbol, or an Array of form_ui and options hash which will be used with form_ui only



225
226
227
228
# File 'lib/active_scaffold/data_structures/column.rb', line 225

def form_ui=(value)
  check_valid_action_ui_params(value)
  @form_ui, @form_ui_options = *value
end

#includes=(value) ⇒ Object



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

def includes=(value)
  @includes =
    case value
    when Array then value
    else value ? [value] : value # not convert nil to [nil]
    end
end

#inplace_edit=(value) ⇒ Object



93
94
95
96
# File 'lib/active_scaffold/data_structures/column.rb', line 93

def inplace_edit=(value)
  clear_link if value
  @inplace_edit = value
end

#label(record = nil, scope = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/active_scaffold/data_structures/column.rb', line 121

def label(record = nil, scope = nil)
  label =
    if @label.respond_to?(:call)
      # sometimes label is called without a record in context (ie, from table
      # headers).  In this case fall back to the default instead of the Proc.
      @label.call(record, self, scope) if record
    elsif @label
      as_(@label)
    end
  label || active_record_class.human_attribute_name(name.to_s)
end


315
316
317
318
319
320
321
322
# File 'lib/active_scaffold/data_structures/column.rb', line 315

def link
  if frozen? && @link.is_a?(Proc)
    ActiveScaffold::Registry.cache(:column_links, cache_key) { @link.call(self).deep_freeze! }
  else
    @link = @link.call(self) if @link.is_a? Proc
    @link
  end
end

#list_uiObject



236
237
238
# File 'lib/active_scaffold/data_structures/column.rb', line 236

def list_ui
  @list_ui || form_ui
end

#list_ui=(value) ⇒ Object

value must be a Symbol, or an Array of list_ui and options hash which will be used with list_ui only



231
232
233
234
# File 'lib/active_scaffold/data_structures/column.rb', line 231

def list_ui=(value)
  check_valid_action_ui_params(value)
  @list_ui, @list_ui_options = *value
end

#list_ui_optionsObject



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

def list_ui_options
  @list_ui ? @list_ui_options : form_ui_options
end

#number?Boolean

Returns:

  • (Boolean)


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

def number?
  @number
end

#number_to_native(value) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/active_scaffold/data_structures/column.rb', line 202

def number_to_native(value)
  return value if value.blank? || !value.is_a?(String)

  native = '.' # native ruby separator
  format = {separator: '', delimiter: ''}.merge! I18n.t('number.format', default: {})
  specific =
    case options[:format]
    when :currency
      I18n.t('number.currency.format', default: nil)
    when :size
      I18n.t('number.human.format', default: nil)
    when :percentage
      I18n.t('number.percentage.format', default: nil)
    end
  format.merge! specific unless specific.nil?
  if format[:separator].blank? || (value.exclude?(format[:separator]) && value.include?(native) && (format[:delimiter] != native || value !~ /\.\d{3}$/))
    value
  else
    value.gsub(/[^0-9\-#{format[:separator]}]/, '').gsub(format[:separator], native)
  end
end

#placeholderObject



117
118
119
# File 'lib/active_scaffold/data_structures/column.rb', line 117

def placeholder
  @placeholder || I18n.t(name, scope: [:activerecord, :placeholder, active_record_class.to_s.underscore.to_sym], default: '')
end

#required?(action = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def required?(action = nil)
  if action && @required
    @required == true || @required.include?(action)
  else
    @required
  end
end

#search_joinsObject

a collection of associations to do left join when this column is included on search



281
282
283
# File 'lib/active_scaffold/data_structures/column.rb', line 281

def search_joins
  @search_joins || includes
end

#search_joins=(value) ⇒ Object



285
286
287
288
289
290
291
# File 'lib/active_scaffold/data_structures/column.rb', line 285

def search_joins=(value)
  @search_joins =
    case value
    when Array then value
    else [value] # automatically convert to an array
    end
end

#search_sqlObject



306
307
308
309
# File 'lib/active_scaffold/data_structures/column.rb', line 306

def search_sql
  initialize_search_sql if @search_sql == true
  @search_sql
end

#search_sql=(value) ⇒ Object

describes how to search on a column

search = true           default, uses intelligent search sql
search = "CONCAT(a, b)" define your own sql for searching. this should be the "left-side" of a WHERE condition. the operator and value will be supplied by ActiveScaffold.
search = [:a, :b]       searches in both fields


297
298
299
300
301
302
303
304
# File 'lib/active_scaffold/data_structures/column.rb', line 297

def search_sql=(value)
  @search_sql =
    if value
      value == true || value.is_a?(Proc) ? value : Array(value)
    else
      value
    end
end

#search_uiObject



264
265
266
# File 'lib/active_scaffold/data_structures/column.rb', line 264

def search_ui
  @search_ui || form_ui || (:select if association && !association.polymorphic?)
end

#search_ui=(value) ⇒ Object

value must be a Symbol, or an Array of search_ui and options hash which will be used with search_ui only



259
260
261
262
# File 'lib/active_scaffold/data_structures/column.rb', line 259

def search_ui=(value)
  check_valid_action_ui_params(value)
  @search_ui, @search_ui_options = *value
end

#search_ui_optionsObject



268
269
270
# File 'lib/active_scaffold/data_structures/column.rb', line 268

def search_ui_options
  @search_ui ? @search_ui_options : form_ui_options
end

#searchable?Boolean

Returns:

  • (Boolean)


311
312
313
# File 'lib/active_scaffold/data_structures/column.rb', line 311

def searchable?
  search_sql.present?
end

associate an action_link with this column



325
326
327
328
329
330
331
332
333
334
# File 'lib/active_scaffold/data_structures/column.rb', line 325

def set_link(action, options = {})
  if action.is_a?(ActiveScaffold::DataStructures::ActionLink) || (action.is_a? Proc)
    @link = action
  else
    options[:label] ||= label
    options[:position] ||= :after unless options.key?(:position)
    options[:type] ||= :member
    @link = ActiveScaffold::DataStructures::ActionLink.new(action, options)
  end
end

#show_blank_record?(associated) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
185
186
187
# File 'lib/active_scaffold/data_structures/column.rb', line 182

def show_blank_record?(associated)
  return false unless @show_blank_record
  return false unless association.klass.authorized_for?(crud_type: :create) && !association.readonly?

  association.collection? || (association.singular? && associated.blank?)
end

#show_uiObject



250
251
252
# File 'lib/active_scaffold/data_structures/column.rb', line 250

def show_ui
  @show_ui || list_ui
end

#show_ui=(value) ⇒ Object

value must be a Symbol, or an Array of show_ui and options hash which will be used with show_ui only



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

def show_ui=(value)
  check_valid_action_ui_params(value)
  @show_ui, @show_ui_options = *value
end

#show_ui_optionsObject



254
255
256
# File 'lib/active_scaffold/data_structures/column.rb', line 254

def show_ui_options
  @show_ui ? @show_ui_options : list_ui_options
end

#sortObject



164
165
166
167
# File 'lib/active_scaffold/data_structures/column.rb', line 164

def sort
  initialize_sort if @sort == true
  @sort
end

#sort=(value) ⇒ Object

sorting on a column can be configured four ways:

sort = true               default, uses intelligent sorting sql default
sort = false              sometimes sorting doesn't make sense
sort = {sql: ""}       define your own sql for sorting. this should be result in a sortable value in SQL. ActiveScaffold will handle the ascending/descending.
sort = {method: ""}    define ruby-side code for sorting. this is SLOW with large recordsets!


155
156
157
158
159
160
161
162
# File 'lib/active_scaffold/data_structures/column.rb', line 155

def sort=(value)
  if value.is_a? Hash
    value.assert_valid_keys(:sql, :method)
    @sort = value
  else
    @sort = value ? true : false # force true or false
  end
end

#sort_by(options) ⇒ Object

a configuration helper for the self.sort property. simply provides a method syntax instead of setter syntax.



174
175
176
# File 'lib/active_scaffold/data_structures/column.rb', line 174

def sort_by(options)
  self.sort = options
end

#sortable?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/active_scaffold/data_structures/column.rb', line 169

def sortable?
  sort != false && !sort.nil?
end

#update_columns=(column_names) ⇒ Object

update dependent columns after value change in form

update_columns = :name
update_columns = [:name, :age]


146
147
148
# File 'lib/active_scaffold/data_structures/column.rb', line 146

def update_columns=(column_names)
  @update_columns = Array(column_names)
end