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



222
223
224
225
# File 'lib/active_scaffold/data_structures/column.rb', line 222

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)


207
208
209
# File 'lib/active_scaffold/data_structures/column.rb', line 207

def associated_number?
  @associated_number
end

#attributes=(opts) ⇒ Object



377
378
379
380
381
# File 'lib/active_scaffold/data_structures/column.rb', line 377

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

#cache_count?Boolean

Returns:

  • (Boolean)


373
374
375
# File 'lib/active_scaffold/data_structures/column.rb', line 373

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

#calculation?Boolean

get whether to run a calculation on this column

Returns:

  • (Boolean)


121
122
123
# File 'lib/active_scaffold/data_structures/column.rb', line 121

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



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

def clear_link
  @link = nil
  @autolink = false
end

#convert_to_native?Boolean

Returns:

  • (Boolean)


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

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

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



149
150
151
152
153
154
155
156
157
# File 'lib/active_scaffold/data_structures/column.rb', line 149

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



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

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

#includes=(value) ⇒ Object



301
302
303
304
305
306
307
# File 'lib/active_scaffold/data_structures/column.rb', line 301

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



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

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

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



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/active_scaffold/data_structures/column.rb', line 137

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


352
353
354
355
356
357
358
359
# File 'lib/active_scaffold/data_structures/column.rb', line 352

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



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

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



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

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

#list_ui_optionsObject



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

def list_ui_options
  @list_ui ? @list_ui_options : form_ui_options
end

#number?Boolean

Returns:

  • (Boolean)


218
219
220
# File 'lib/active_scaffold/data_structures/column.rb', line 218

def number?
  @number
end

#number_to_native(value) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/active_scaffold/data_structures/column.rb', line 231

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



133
134
135
# File 'lib/active_scaffold/data_structures/column.rb', line 133

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)


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

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



318
319
320
# File 'lib/active_scaffold/data_structures/column.rb', line 318

def search_joins
  @search_joins || includes
end

#search_joins=(value) ⇒ Object



322
323
324
325
326
327
328
# File 'lib/active_scaffold/data_structures/column.rb', line 322

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

#search_sqlObject



343
344
345
346
# File 'lib/active_scaffold/data_structures/column.rb', line 343

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


334
335
336
337
338
339
340
341
# File 'lib/active_scaffold/data_structures/column.rb', line 334

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

#search_uiObject



293
294
295
# File 'lib/active_scaffold/data_structures/column.rb', line 293

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



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

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

#search_ui_optionsObject



297
298
299
# File 'lib/active_scaffold/data_structures/column.rb', line 297

def search_ui_options
  @search_ui ? @search_ui_options : form_ui_options
end

#searchable?Boolean

Returns:

  • (Boolean)


348
349
350
# File 'lib/active_scaffold/data_structures/column.rb', line 348

def searchable?
  search_sql.present? || (logical_search.present? && ActiveScaffold::Finder::LOGICAL_COMPARATORS.present?)
end

associate an action_link with this column



362
363
364
365
366
367
368
369
370
371
# File 'lib/active_scaffold/data_structures/column.rb', line 362

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)


211
212
213
214
215
216
# File 'lib/active_scaffold/data_structures/column.rb', line 211

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



279
280
281
# File 'lib/active_scaffold/data_structures/column.rb', line 279

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



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

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

#show_ui_optionsObject



283
284
285
# File 'lib/active_scaffold/data_structures/column.rb', line 283

def show_ui_options
  @show_ui ? @show_ui_options : list_ui_options
end

#sortObject



180
181
182
183
# File 'lib/active_scaffold/data_structures/column.rb', line 180

def sort
  initialize_sort if @sort == true
  @sort if @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!


171
172
173
174
175
176
177
178
# File 'lib/active_scaffold/data_structures/column.rb', line 171

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.



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

def sort_by(options)
  self.sort = options
end

#sort_joinsObject

a collection of associations to do left join when the list is sorted by this column



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

def sort_joins
  @sort_joins || includes
end

#sort_joins=(value) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/active_scaffold/data_structures/column.rb', line 199

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

#sortable?Boolean

Returns:

  • (Boolean)


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

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

#subform_includes=(value) ⇒ Object



309
310
311
312
313
314
315
# File 'lib/active_scaffold/data_structures/column.rb', line 309

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

#update_columns=(column_names) ⇒ Object

update dependent columns after value change in form

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


162
163
164
# File 'lib/active_scaffold/data_structures/column.rb', line 162

def update_columns=(column_names)
  @update_columns = column_names.is_a?(Array) ? column_names : [column_names]
end