Class: ActiveScaffold::DataStructures::Column

Inherits:
Object
  • Object
show all
Includes:
Configurable, OrmChecks
Defined in:
lib/active_scaffold/data_structures/column.rb

Constant Summary

@@associated_limit =
3
@@associated_number =
true
@@show_blank_record =
true
[:new, :edit, :show]
@@association_form_ui =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OrmChecks

#_columns, #_columns_hash, #_content_columns, #_quoted_table_name, #_table_name, #active_record?, #mongoid?, #tableless?

Methods included from Configurable

#configure, #method_missing

Constructor Details

#initialize(name, active_record_class) ⇒ Column

instantiation is handled internally through the DataStructures::Columns object



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/active_scaffold/data_structures/column.rb', line 324

def initialize(name, active_record_class) #:nodoc:
  self.name = name.to_sym
  @active_record_class = active_record_class
  @column = _columns_hash[self.name.to_s]
  @association = active_record_class.reflect_on_association(self.name)
  @autolink = !@association.nil?
  @table = _table_name
  @associated_limit = self.class.associated_limit
  @associated_number = self.class.associated_number
  @show_blank_record = self.class.show_blank_record
  @send_form_on_update_column = self.class.send_form_on_update_column
  @actions_for_association_links = self.class.actions_for_association_links.clone if @association
  @select_columns = default_select_columns

  @text = @column.nil? || [:string, :text].include?(@column.type)
  if @column
    if active_record_class.respond_to?(:defined_enums) && active_record_class.defined_enums[name.to_s]
      @form_ui = :select
      @options = {:options => active_record_class.send(name.to_s.pluralize).keys}
    elsif column_number?
      @number = true
      @form_ui = :number
      @options = {:format => :i18n_number}
    else
      @form_ui = case @column.type
        when :boolean then :checkbox
        when :text then :textarea
      end
    end
  end
  @allow_add_existing = true
  @form_ui = self.class.association_form_ui if @association && self.class.association_form_ui

  if association && !polymorphic_association?
    self.includes = [association.name]
    self.search_joins = includes.clone
  end

  # default all the configurable variables
  self.css_class = ''
  self.required = active_record_class.validators_on(self.name).any? do |val|
    validator_force_required?(val)
  end
  self.sort = true
  self.search_sql = true

  @weight = estimate_weight
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveScaffold::Configurable

Instance Attribute Details

Returns the value of attribute actions_for_association_links



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

def actions_for_association_links
  @actions_for_association_links
end

#active_record_classObject (readonly)

Returns the value of attribute active_record_class



6
7
8
# File 'lib/active_scaffold/data_structures/column.rb', line 6

def active_record_class
  @active_record_class
end

#allow_add_existingObject

Whether to enable add_existing for this column



26
27
28
# File 'lib/active_scaffold/data_structures/column.rb', line 26

def allow_add_existing
  @allow_add_existing
end

#associated_limitObject

Returns the value of attribute associated_limit



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

def associated_limit
  @associated_limit
end

#associated_number=(value) ⇒ Object (writeonly)

Sets the attribute associated_number

Parameters:

  • value

    the value to set the attribute associated_number to.



238
239
240
# File 'lib/active_scaffold/data_structures/column.rb', line 238

def associated_number=(value)
  @associated_number = value
end

#associationObject (readonly)

the association from the ActiveRecord class



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

def association
  @association
end

#calculateObject

define a calculation for the column. anything that ActiveRecord::Calculations::ClassMethods#calculate accepts will do.



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

def calculate
  @calculate
end

#collapsedObject

Whether this column set is collapsed by default in contexts where collapsing is supported



23
24
25
# File 'lib/active_scaffold/data_structures/column.rb', line 23

def collapsed
  @collapsed
end

#columnObject (readonly)

the ConnectionAdapter::*Column object from the ActiveRecord class



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

def column
  @column
end

#css_classObject

this will be /joined/ to the :name for the td's class attribute. useful if you want to style columns on different ActiveScaffolds the same way, but the columns have different names.



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

def css_class
  @css_class
end

#descriptionObject



46
47
48
49
50
51
52
# File 'lib/active_scaffold/data_structures/column.rb', line 46

def description
  if @description
    @description
  else
    I18n.t name, :scope => [:activerecord, :description, active_record_class.to_s.underscore.to_sym], :default => ''
  end
end

#form_uiObject

Returns the value of attribute form_ui



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

def form_ui
  @form_ui
end

#includesObject

a collection of associations to pre-load when finding the records on a page



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

def includes
  @includes
end

#inplace_editObject

Whether to enable inplace editing for this column. Currently works for text columns, in the List.



12
13
14
# File 'lib/active_scaffold/data_structures/column.rb', line 12

def inplace_edit
  @inplace_edit
end

#inplace_edit_updateObject

:table to refresh list true or :row to refresh row



20
21
22
# File 'lib/active_scaffold/data_structures/column.rb', line 20

def inplace_edit_update
  @inplace_edit_update
end

#labelObject



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

def label
  as_(@label) || active_record_class.human_attribute_name(name.to_s)
end

#list_methodObject

to cache method to get value in list



413
414
415
# File 'lib/active_scaffold/data_structures/column.rb', line 413

def list_method
  @list_method
end

#nameObject

this is the name of the getter on the ActiveRecord model. it is the only absolutely required attribute … all others will be inferred from this name.



9
10
11
# File 'lib/active_scaffold/data_structures/column.rb', line 9

def name
  @name
end

#number=(value) ⇒ Object (writeonly)

Sets the attribute number

Parameters:

  • value

    the value to set the attribute number to.



300
301
302
# File 'lib/active_scaffold/data_structures/column.rb', line 300

def number=(value)
  @number = value
end

#numerical_constraintsObject

cache constraints for numeric columns (get in ActiveScaffold::Helpers::FormColumnHelpers::numerical_constraints_for_column)



416
417
418
# File 'lib/active_scaffold/data_structures/column.rb', line 416

def numerical_constraints
  @numerical_constraints
end

#optionsObject

a place to store dev's column specific options



140
141
142
# File 'lib/active_scaffold/data_structures/column.rb', line 140

def options
  @options
end

#placeholderObject



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

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

#required=(value) ⇒ Object (writeonly)

whether the field is required or not. used on the form for visually indicating the fact to the user. TODO: move into predicate



65
66
67
# File 'lib/active_scaffold/data_structures/column.rb', line 65

def required=(value)
  @required = value
end

#search_uiObject



135
136
137
# File 'lib/active_scaffold/data_structures/column.rb', line 135

def search_ui
  @search_ui || @form_ui || (@association && !polymorphic_association? ? :select : nil)
end

#select_associated_columnsObject

a collection of columns to load when eager loading is disabled, if it's nil all columns will be loaded



203
204
205
# File 'lib/active_scaffold/data_structures/column.rb', line 203

def select_associated_columns
  @select_associated_columns
end

#select_columnsObject

What columns load from main table



29
30
31
# File 'lib/active_scaffold/data_structures/column.rb', line 29

def select_columns
  @select_columns
end

#send_form_on_update_columnObject

Returns the value of attribute send_form_on_update_column



81
82
83
# File 'lib/active_scaffold/data_structures/column.rb', line 81

def send_form_on_update_column
  @send_form_on_update_column
end

#show_blank_record=(value) ⇒ Object (writeonly)

Sets the attribute show_blank_record

Parameters:

  • value

    the value to set the attribute show_blank_record to.



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

def show_blank_record=(value)
  @show_blank_record = value
end

#show_uiObject



130
131
132
# File 'lib/active_scaffold/data_structures/column.rb', line 130

def show_ui
  @show_ui || list_ui
end

#update_columnsObject

Returns the value of attribute update_columns



70
71
72
# File 'lib/active_scaffold/data_structures/column.rb', line 70

def update_columns
  @update_columns
end

#weightObject

to modify the default order of columns



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

def weight
  @weight
end

Instance Method Details

#<=>(other) ⇒ Object



387
388
389
390
# File 'lib/active_scaffold/data_structures/column.rb', line 387

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

#==(other) ⇒ Object

this is so that array.delete and array.include?, etc., will work by column name



310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/active_scaffold/data_structures/column.rb', line 310

def ==(other) #:nodoc:
  # another column
  if other.respond_to?(:name) && other.class == self.class
    name == other.name.to_sym
  # a string or symbol
  elsif other.respond_to? :to_sym
    name == other.to_sym rescue false # catch "interning empty string"
  # unknown
  else
    self.eql? other
  end
end

#associated_number?Boolean

Returns:

  • (Boolean)


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

def associated_number?
  @associated_number
end

#autolink?Boolean

set an action_link to nested list or inline form in this column

Returns:

  • (Boolean)


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

def autolink?
  @autolink
end

#calculation?Boolean

get whether to run a calculation on this column

Returns:

  • (Boolean)


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

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



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

def clear_link
  @link = nil
  @autolink = false
end

#fieldObject

the table.field name for this column, if applicable



419
420
421
# File 'lib/active_scaffold/data_structures/column.rb', line 419

def field
  @field ||= quoted_field(field_name)
end

#field_nameObject

just the field (not table.field)



374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/active_scaffold/data_structures/column.rb', line 374

def field_name
  return nil if virtual?
  @field_name ||= if column
    if active_record?
      @active_record_class.connection.quote_column_name(column.name)
    else
      column.name.to_s
    end
  else
    association.foreign_key
  end
end


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

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

#list_uiObject



125
126
127
# File 'lib/active_scaffold/data_structures/column.rb', line 125

def list_ui
  @list_ui || form_ui
end

#list_ui=(value) ⇒ Object



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

def list_ui=(value)
  self.list_method = nil if value != @list_ui
  @list_ui = value
end

#number?Boolean

Returns:

  • (Boolean)


301
302
303
# File 'lib/active_scaffold/data_structures/column.rb', line 301

def number?
  @number
end

#number_to_native(value) ⇒ Object



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/active_scaffold/data_structures/column.rb', line 392

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.include?(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

#paramsObject

Any extra parameters this particular column uses. This is for create/update purposes.



32
33
34
35
# File 'lib/active_scaffold/data_structures/column.rb', line 32

def params
  # lazy initialize
  @params ||= Set.new
end

#plural_association?Boolean

Returns:

  • (Boolean)


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

def plural_association?
  association && association.collection?
end

#polymorphic_association?Boolean

Returns:

  • (Boolean)


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

def polymorphic_association?
  association && association.options[:polymorphic]
end

#readonly_association?Boolean

Returns:

  • (Boolean)


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

def readonly_association?
  return false unless association
  if association.options.key? :readonly
    association.options[:readonly]
  else
    self.through_association?
  end
end

#required?Boolean

Returns:

  • (Boolean)


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

def required?
  @required
end

#search_joinsObject

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



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

def search_joins
  @search_joins || @includes
end

#search_joins=(value) ⇒ Object



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

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

#search_sqlObject



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

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


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

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

#searchable?Boolean

Returns:

  • (Boolean)


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

def searchable?
  search_sql.present?
end

associate an action_link with this column



151
152
153
154
155
156
157
158
159
160
# File 'lib/active_scaffold/data_structures/column.rb', line 151

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)


247
248
249
250
251
# File 'lib/active_scaffold/data_structures/column.rb', line 247

def show_blank_record?(associated)
  return false unless @show_blank_record
  return false unless association.klass.authorized_for?(:crud_type => :create) && !association.options[:readonly]
  self.plural_association? || (self.singular_association? && associated.blank?)
end

#singular_association?Boolean

Returns:

  • (Boolean)


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

def singular_association?
  association && !association.collection?
end

#sortObject



97
98
99
100
# File 'lib/active_scaffold/data_structures/column.rb', line 97

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!


88
89
90
91
92
93
94
95
# File 'lib/active_scaffold/data_structures/column.rb', line 88

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.



107
108
109
# File 'lib/active_scaffold/data_structures/column.rb', line 107

def sort_by(options)
  self.sort = options
end

#sortable?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/active_scaffold/data_structures/column.rb', line 102

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

#text?Boolean

Returns:

  • (Boolean)


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

def text?
  @text
end

#through_association?Boolean

Returns:

  • (Boolean)


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

def through_association?
  association && association.options[:through]
end

#virtual?Boolean

an interpreted property. the column is virtual if it isn't from the active record model or any associated models

Returns:

  • (Boolean)


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

def virtual?
  column.nil? && association.nil?
end