Class: ActiveScaffold::DataStructures::Column

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

#configure, #method_missing

Constructor Details

- (Column) initialize(name, active_record_class)

instantiation is handled internally through the DataStructures::Columns object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/active_scaffold/data_structures/column.rb', line 282

def initialize(name, active_record_class) #:nodoc:
  self.name = name.to_sym
  @column = active_record_class.columns_hash[self.name.to_s]
  @association = active_record_class.reflect_on_association(self.name)
  @autolink = !@association.nil?
  @active_record_class = active_record_class
  @table = active_record_class.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
  
  self.number = @column.try(:number?)
  @options = {:format => :i18n_number} if self.number?
  @form_ui = :checkbox if @column and @column.type == :boolean
  @form_ui = :textarea if @column and @column.type == :text
  @allow_add_existing = true
  @form_ui = self.class.association_form_ui if @association && self.class.association_form_ui
  
  # default all the configurable variables
  self.css_class = ''
  self.required = active_record_class.validators_on(self.name).any? do |val|
    ActiveModel::Validations::PresenceValidator === val or (
      ActiveModel::Validations::InclusionValidator === val and not val.options[:allow_nil] and not val.options[:allow_blank]
    )
  end
  self.sort = true
  self.search_sql = true
  
  @weight = estimate_weight

  self.includes = (association and not polymorphic_association?) ? [association.name] : []
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



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

def actions_for_association_links
  @actions_for_association_links
end

- (Object) active_record_class (readonly)

Returns the value of attribute active_record_class



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

def active_record_class
  @active_record_class
end

- (Object) allow_add_existing

Whether to enable add_existing for this column



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

def allow_add_existing
  @allow_add_existing
end

- (Object) associated_limit

Returns the value of attribute associated_limit



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

def associated_limit
  @associated_limit
end

- (Object) associated_number=(value) (writeonly)

Sets the attribute associated_number

Parameters:

  • value

    the value to set the attribute associated_number to.



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

def associated_number=(value)
  @associated_number = value
end

- (Object) association (readonly)

the association from the ActiveRecord class



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

def association
  @association
end

- (Object) calculate

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



160
161
162
# File 'lib/active_scaffold/data_structures/column.rb', line 160

def calculate
  @calculate
end

- (Object) collapsed

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



18
19
20
# File 'lib/active_scaffold/data_structures/column.rb', line 18

def collapsed
  @collapsed
end

- (Object) column (readonly)

the ConnectionAdapter::*Column object from the ActiveRecord class



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

def column
  @column
end

- (Object) css_class

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.



47
48
49
# File 'lib/active_scaffold/data_structures/column.rb', line 47

def css_class
  @css_class
end

- (Object) description



38
39
40
41
42
43
44
# File 'lib/active_scaffold/data_structures/column.rb', line 38

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

- (Object) form_ui



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

def form_ui
  @form_ui
end

- (Object) includes

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



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

def includes
  @includes
end

- (Object) inplace_edit

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



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

def inplace_edit
  @inplace_edit
end

- (Object) label



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

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

- (Object) list_ui



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

def list_ui
  @list_ui || @form_ui
end

- (Object) name

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.



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

def name
  @name
end

- (Object) number=(value) (writeonly)

Sets the attribute number

Parameters:

  • value

    the value to set the attribute number to.



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

def number=(value)
  @number = value
end

- (Object) options

a place to store dev's column specific options



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

def options
  @options
end

- (Object) required=(value) (writeonly)

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



51
52
53
# File 'lib/active_scaffold/data_structures/column.rb', line 51

def required=(value)
  @required = value
end

- (Object) search_sql



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

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

- (Object) search_ui



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

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

- (Object) select_columns

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



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

def select_columns
  @select_columns
end

- (Object) send_form_on_update_column

Returns the value of attribute send_form_on_update_column



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

def send_form_on_update_column
  @send_form_on_update_column
end

- (Object) show_blank_record=(value) (writeonly)

Sets the attribute show_blank_record

Parameters:

  • value

    the value to set the attribute show_blank_record to.



210
211
212
# File 'lib/active_scaffold/data_structures/column.rb', line 210

def show_blank_record=(value)
  @show_blank_record = value
end

- (Object) update_columns

Returns the value of attribute update_columns



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

def update_columns
  @update_columns
end

- (Object) weight

to modify the default order of columns



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

def weight
  @weight
end

Instance Method Details

- (Object) <=>(other_column)



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

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

- (Object) ==(other)

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



268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/active_scaffold/data_structures/column.rb', line 268

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

- (Boolean) associated_number?

Returns:

  • (Boolean)


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

def associated_number?
  @associated_number
end

- (Boolean) autolink?

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

Returns:

  • (Boolean)


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

def autolink?
  @autolink
end

- (Boolean) calculation?

get whether to run a calculation on this column

Returns:

  • (Boolean)


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

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

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



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

def clear_link
  @link = nil
  @autolink = false
end

- (Object) field_name

just the field (not table.field)



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

def field_name
  return nil if virtual?
  column ? @active_record_class.connection.quote_column_name(column.name) : association.foreign_key
end


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

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

- (Boolean) number?

Returns:

  • (Boolean)


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

def number?
  @number
end

- (Object) number_to_native(value)



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/active_scaffold/data_structures/column.rb', line 328

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 self.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?
  unless format[:separator].blank? || !value.include?(format[:separator]) && value.include?(native) && (format[:delimiter] != native || value !~ /\.\d{3}$/)
    value.gsub(/[^0-9\-#{format[:separator]}]/, '').gsub(format[:separator], native)
  else
    value
  end
end

- (Object) params

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



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

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

- (Boolean) plural_association?

Returns:

  • (Boolean)


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

def plural_association?
  self.association and [:has_many, :has_and_belongs_to_many].include? self.association.macro
end

- (Boolean) polymorphic_association?

Returns:

  • (Boolean)


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

def polymorphic_association?
  self.association and self.association.options.has_key? :polymorphic and self.association.options[:polymorphic]
end

- (Boolean) readonly_association?

Returns:

  • (Boolean)


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

def readonly_association?
  if self.association
    if self.association.options.has_key? :readonly
      self.association.options[:readonly]
    else
      self.through_association?
    end
  end
end

- (Boolean) required?

Returns:

  • (Boolean)


52
53
54
# File 'lib/active_scaffold/data_structures/column.rb', line 52

def required?
  @required
end

- (Boolean) searchable?

Returns:

  • (Boolean)


187
188
189
# File 'lib/active_scaffold/data_structures/column.rb', line 187

def searchable?
  search_sql != false && search_sql != nil
end

associate an action_link with this column



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

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

- (Boolean) show_blank_record?(associated)

Returns:

  • (Boolean)


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

def show_blank_record?(associated)
  if @show_blank_record
    return false unless self.association.klass.authorized_for?(:crud_type => :create)
    self.plural_association? or (self.singular_association? and associated.blank?)
  end
end

- (Boolean) singular_association?

Returns:

  • (Boolean)


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

def singular_association?
  self.association and [:has_one, :belongs_to].include? self.association.macro
end

- (Object) sort



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

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

- (Object) sort=(value)

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!


84
85
86
87
88
89
90
91
# File 'lib/active_scaffold/data_structures/column.rb', line 84

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

- (Object) sort_by(options)

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



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

def sort_by(options)
  self.sort = options
end

- (Boolean) sortable?

Returns:

  • (Boolean)


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

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

- (Boolean) through_association?

Returns:

  • (Boolean)


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

def through_association?
  self.association and self.association.options[:through]
end

- (Object) update_column=(column_name)

column to be updated in a form when this column changes



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

def update_column=(column_name)
  ActiveSupport::Deprecation.warn "Use update_columns= instead of update_column="
  self.update_columns = column_name
end

- (Boolean) virtual?

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

Returns:

  • (Boolean)


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

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