Class: ActiveScaffold::DataStructures::Column

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

Constant Summary collapse

@@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

#initialize(name, active_record_class) ⇒ Column

instantiation is handled internally through the DataStructures::Columns object



276
277
278
279
280
281
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
# File 'lib/active_scaffold/data_structures/column.rb', line 276

def initialize(name, active_record_class) #:nodoc:
  self.name = name
  @column = active_record_class.db_schema[name]
  @association = active_record_class.association_reflection(name)
  @nested = true if @association and @association[:nested_attributes]
  @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 and (@column[:type] == :integer or @column[:type] == :float or @column[:type] == :decimal))
  @options = {:format => :i18n_number} if self.number?
  @form_ui = :checkbox if @column and @column[:type] == :boolean
  @form_ui = :textarea if @column and @column[:type] == :string and (@column[:db_type] == 'text' or ((mc = @column[:max_chars]) and mc > 255))
  @maxlength = parse_column_length if @column and @column[:type] == :string
  @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 = (@column and @column[:default].nil? and not @column[:allow_null])
  self.sort = true
  self.search_sql = true
  
  @weight = estimate_weight

  self.includes = 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.



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

def actions_for_association_links
  @actions_for_association_links
end

#active_record_classObject (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

#allow_add_existingObject

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

#associated_limitObject

Returns the value of attribute associated_limit.



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

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.



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

def associated_number=(value)
  @associated_number = value
end

#associationObject (readonly)

the association from the ActiveRecord class



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

def association
  @association
end

#calculateObject

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



157
158
159
# File 'lib/active_scaffold/data_structures/column.rb', line 157

def calculate
  @calculate
end

#collapsedObject

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

#columnObject (readonly)

the ConnectionAdapter::*Column object from the ActiveRecord class



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

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.



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

def css_class
  @css_class
end

#descriptionObject



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

#form_uiObject



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

def form_ui
  @form_ui
end

#includesObject

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



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

def includes
  @includes
end

#inplace_editObject

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

#labelObject



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

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

#list_uiObject



113
114
115
# File 'lib/active_scaffold/data_structures/column.rb', line 113

def list_ui
  @list_ui || @form_ui
end

#maxlengthObject (readonly)

maximal length of a string column - obtained from @column



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

def maxlength
  @maxlength
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.



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

def name
  @name
end

#nestedObject

boolean: true if the column is for a nested association, otherwise nil



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

def nested
  @nested
end

#number=(value) ⇒ Object (writeonly)

Sets the attribute number

Parameters:

  • value

    the value to set the attribute number to.



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

def number=(value)
  @number = value
end

#optionsObject

a place to store dev’s column specific options



123
124
125
# File 'lib/active_scaffold/data_structures/column.rb', line 123

def options
  @options
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



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

def required=(value)
  @required = value
end

#search_sqlObject



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

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

#search_uiObject



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

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

#select_columnsObject

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



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

def select_columns
  @select_columns
end

#send_form_on_update_columnObject

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

#show_blank_record=(value) ⇒ Object (writeonly)

Sets the attribute show_blank_record

Parameters:

  • value

    the value to set the attribute show_blank_record to.



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

def show_blank_record=(value)
  @show_blank_record = value
end

#update_columnObject

column to be updated in a form when this column changes



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

def update_column
  @update_column
end

#update_columnsObject

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

#weightObject

to modify the default order of columns



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

def weight
  @weight
end

Instance Method Details

#<=>(other_column) ⇒ Object



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

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

#==(other) ⇒ Object

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



262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/active_scaffold/data_structures/column.rb', line 262

def ==(other) #:nodoc:
  # another column
  if other.respond_to? :name and other.class == self.class
    self.name == other.name
  # 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

#associated_number?Boolean

Returns:

  • (Boolean)


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

def associated_number?
  @associated_number
end

#autolink?Boolean

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

Returns:

  • (Boolean)


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

def autolink?
  @autolink
end

#calculation?Boolean

get whether to run a calculation on this column

Returns:

  • (Boolean)


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

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



151
152
153
154
# File 'lib/active_scaffold/data_structures/column.rb', line 151

def clear_link
  @link = nil
  @autolink = false
end

#field_nameObject

just the field (not table.field)



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

def field_name
  return nil if virtual?
  column ? name : association[:key]
end


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

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

#nested_attribute_nameObject



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

def nested_attribute_name
  @nested_attribute_name ||= "#{name}_attributes"
end

#number?Boolean

Returns:

  • (Boolean)


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

def number?
  @number
end

#number_to_native(value) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/active_scaffold/data_structures/column.rb', line 320

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

#paramsObject

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

#plural_association?Boolean

Returns:

  • (Boolean)


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

def plural_association?
  self.association and [:one_to_many, :many_to_many].include? self.association[:type]
end

#required?Boolean

Returns:

  • (Boolean)


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

def required?
  @required
end

#searchable?Boolean

Returns:

  • (Boolean)


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

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

associate an action_link with this column



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

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

#show_blank_record?(associated) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#singular_association?Boolean

Returns:

  • (Boolean)


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

def singular_association?
  self.association and [:one_to_one, :many_to_one].include? self.association[:type]
end

#sortObject



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

def sort
  self.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!


81
82
83
84
85
86
87
88
# File 'lib/active_scaffold/data_structures/column.rb', line 81

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.



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

def sort_by(options)
  self.sort = options
end

#sortable?Boolean

Returns:

  • (Boolean)


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

def sortable?
  sort != false && !sort.nil?
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)


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

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