class ActiveScaffold::DataStructures::Column

Attributes

active_record_class[R]
allow_add_existing[RW]

Whether to enable add_existing for this column

associated_limit[RW]
associated_number[W]
association[R]

the association from the ActiveRecord class

calculate[RW]

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

collapsed[RW]

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

column[R]

the ConnectionAdapter::*Column object from the ActiveRecord class

css_class[RW]

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.

description[W]

a textual description of the column and its contents. this will be displayed with any associated form input widget, so you may want to consider adding a content example.

form_ui[W]

supported options:

* for association columns
  * :select - displays a simple <select> or a collection of checkboxes to (dis)associate records
includes[R]

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

inplace_edit[R]

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

label[W]

the display-name of the column. this will be used, for instance, as the column title in the table and as the field name in the form. if left alone it will utilize human_attribute_name which includes localization

list_ui[W]
maxlength[R]

maximal length of a string column - obtained from @column

name[RW]

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.

nested[RW]

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

number[W]
options[RW]

a place to store dev’s column specific options

required[W]

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

search_sql[W]

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_ui[W]
select_columns[RW]

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

send_form_on_update_column[RW]
show_blank_record[W]
table[R]

the table name from the ActiveRecord class

update_column[RW]

column to be updated in a form when this column changes

update_columns[R]
weight[RW]

to modify the default order of columns

Public Instance Methods

<=>(other_column) click to toggle source
# 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
associated_number?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 200
def associated_number?
  @associated_number
end
calculation?() click to toggle source

get whether to run a calculation on this column

# File lib/active_scaffold/data_structures/column.rb, line 160
def calculation?
  !(@calculate == false or @calculate.nil?)
end
description() click to toggle source
# 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
field_name() click to toggle source

just the field (not table.field)

# File lib/active_scaffold/data_structures/column.rb, line 310
def field_name
  return nil if virtual?
  column ? name : association[:key]
end
form_ui() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 108
def form_ui
  @form_ui
end
includes=(value) click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 166
def includes=(value)
  @includes = case value
    when Array, Hash then value 
    else [value] # automatically convert to an array
  end
end
inplace_edit=(value) click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 12
def inplace_edit=(value)
  self.clear_link if value
  @inplace_edit = value
end
label() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 32
def label
  as_(@label) || active_record_class.human_attribute_name(name)
end
list_ui() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 113
def list_ui
  @list_ui || @form_ui
end
nested_attribute_name() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 226
def nested_attribute_name
  @nested_attribute_name ||= "#{name}_attributes"
end
number?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 257
def number?
  @number
end
number_to_native(value) click to toggle source
# 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
params() click to toggle source

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

# File lib/active_scaffold/data_structures/column.rb, line 24
def params
  # lazy initialize
  @params ||= Set.new
end
plural_association?() click to toggle source
# 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?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 52
def required?
  @required
end
search_sql() click to toggle source
# 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_ui() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 118
def search_ui
  @search_ui || @form_ui || (@association ? :select : nil)
end
searchable?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 184
def searchable?
  search_sql != false && search_sql != nil
end
show_blank_record?(associated) click to toggle source
# 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?() click to toggle source
# 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
sort() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 90
def sort
  self.initialize_sort if @sort === true
  @sort
end
sort=(value) click to toggle source

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!
# 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) click to toggle source

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

# File lib/active_scaffold/data_structures/column.rb, line 100
def sort_by(options)
  self.sort = options
end
sortable?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 95
def sortable?
  sort != false && !sort.nil?
end
update_columns=(column_names) click to toggle source

update dependent columns after value change in form

update_columns = :name
update_columns = [:name, :age]
# File lib/active_scaffold/data_structures/column.rb, line 61
def update_columns=(column_names)
  @update_columns = Array(column_names)
end
virtual?() click to toggle source

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

# File lib/active_scaffold/data_structures/column.rb, line 252
def virtual?
  column.nil? && association.nil?
end

Protected Instance Methods

estimate_weight() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 375
def estimate_weight
  if singular_association?
    400
  elsif plural_association?
    500
  elsif [:created_at, :updated_at].include?(self.name) 
    600
  elsif [:name, :label, :title].include?(self.name)
    100
  elsif required?
    200
  else
    300
  end
end
field() click to toggle source

the table.field name for this column, if applicable

# File lib/active_scaffold/data_structures/column.rb, line 371
def field
  @field ||= "#{@table}__#{field_name}".to_sym
end
initialize_search_sql() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 357
def initialize_search_sql
  self.search_sql = unless self.virtual?
    if association.nil?
      self.field
    else
      "#{association.associated_class.table_name}__#{association.associated_class.primary_key}".to_sym
    end
  end
end
initialize_sort() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 342
def initialize_sort
  if self.virtual?
    # we don't automatically enable method sorting for virtual columns because it's slow, and we expect fewer complaints this way.
    self.sort = false
  else
    if self.singular_association?
      self.sort = {:method => "#{self.name}.to_s"}
    elsif self.plural_association?
      self.sort = {:method => "#{self.name}.join(',')"}
    else
      self.sort = {:sql => self.field}
    end
  end
end
parse_column_length() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 391
def parse_column_length
  if @column and @column[:db_type] =~ /\((\d+)\)/
    $1.to_i
  end
end