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[R]
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.

inplace_edit_update[RW]

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

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_method[RW]

to cache method to get value in list

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.

number[W]
numerical_constraints[RW]

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

options[RW]

a place to store dev's column specific options

placeholder[W]

A placeholder text, to be used inside blank text fields to describe, what should be typed in

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

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

select_columns[RW]

What columns load from main table

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

the table name from the ActiveRecord class

update_columns[R]
weight[RW]

to modify the default order of columns

Public Instance Methods

<=>(other) click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 379
def <=>(other)
  order_weight = weight <=> other.weight
  order_weight != 0 ? order_weight : name.to_s <=> other.name.to_s
end
associated_number?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 238
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 176
def calculation?
  !(@calculate == false || @calculate.nil?)
end
description() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 45
def description
  if @description
    @description
  else
    I18n.t name, :scope => [:activerecord, :description, active_record_class.to_s.underscore.to_sym], :default => ''
  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 411
def field
  @field ||= quoted_field(field_name)
end
field_name() click to toggle source

just the field (not table.field)

# File lib/active_scaffold/data_structures/column.rb, line 374
def field_name
  return nil if virtual?
  @field_name ||= column ? @active_record_class.connection.quote_column_name(column.name) : association.foreign_key
end
form_ui=(value) click to toggle source

supported options:

* for association columns
  * :select - displays a simple <select> or a collection of checkboxes to (dis)associate records
# File lib/active_scaffold/data_structures/column.rb, line 113
def form_ui=(value)
  self.list_method = nil if @list_ui.nil? && value != @form_ui
  @form_ui = value
end
includes=(value) click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 182
def includes=(value)
  @includes = case value
    when Array then value
    else value ? [value] : value # not convert nil to [nil]
  end
end
inplace_edit=(value) click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 12
def inplace_edit=(value)
  clear_link if value
  @inplace_edit = value
end
label() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 39
def label
  as_(@label) || active_record_class.human_attribute_name(name.to_s)
end
list_ui() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 124
def list_ui
  @list_ui || form_ui
end
list_ui=(value) click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 119
def list_ui=(value)
  self.list_method = nil if value != @list_ui
  @list_ui = value
end
number?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 300
def number?
  @number
end
number_to_native(value) click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 384
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
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 31
def params
  # lazy initialize
  @params ||= Set.new
end
placeholder() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 55
def placeholder
  @placeholder || I18n.t(name, :scope => [:activerecord, :placeholder, active_record_class.to_s.underscore.to_sym], :default => '')
end
plural_association?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 273
def plural_association?
  association && association.collection?
end
polymorphic_association?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 281
def polymorphic_association?
  association && association.options[:polymorphic]
end
readonly_association?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 285
def readonly_association?
  return false unless association
  if association.options.key? :readonly
    association.options[:readonly]
  else
    self.through_association?
  end
end
required?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 65
def required?
  @required
end
search_joins() click to toggle source

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

# File lib/active_scaffold/data_structures/column.rb, line 190
def search_joins
  @search_joins || @includes
end
search_joins=(value) click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 194
def search_joins=(value)
  @search_joins = case value
    when Array then value
    else [value] # automatically convert to an array
  end
end
search_sql() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 217
def search_sql
  initialize_search_sql if @search_sql == true
  @search_sql
end
search_sql=(value) click to toggle source

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
# File lib/active_scaffold/data_structures/column.rb, line 208
def search_sql=(value)
  @search_sql =
    if value
      (value == true || value.is_a?(Proc)) ? value : Array(value)
    else
      value
    end
end
search_ui() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 134
def search_ui
  @search_ui || @form_ui || (@association && !polymorphic_association? ? :select : nil)
end
searchable?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 222
def searchable?
  search_sql.present?
end
show_blank_record?(associated) click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 246
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
show_ui() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 129
def show_ui
  @show_ui || list_ui
end
singular_association?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 269
def singular_association?
  association && !association.collection?
end
sort() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 96
def sort
  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 87
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 106
def sort_by(options)
  self.sort = options
end
sortable?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 101
def sortable?
  sort != false && !sort.nil?
end
text?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 304
def text?
  @text
end
through_association?() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 277
def through_association?
  association && association.options[:through]
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 74
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 295
def virtual?
  column.nil? && association.nil?
end

Protected Instance Methods

default_select_columns() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 432
def default_select_columns
  if association.nil? && column
    [field]
  elsif polymorphic_association?
    [field, quoted_field(@active_record_class.connection.quote_column_name(association.foreign_type))]
  elsif association
    if association.belongs_to?
      [field]
    else
      columns = []
      if active_record_class.columns_hash[count_column = "#{association.name}_count"]
        columns << quoted_field(@active_record_class.connection.quote_column_name(count_column))
      end
      if association.through_reflection.try(:belongs_to?)
        columns << quoted_field(@active_record_class.connection.quote_column_name(association.through_reflection.foreign_key))
      end
      columns
    end
  end
end
estimate_weight() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 484
def estimate_weight
  if singular_association?
    400
  elsif plural_association?
    500
  elsif [:created_at, :updated_at].include?(name)
    600
  elsif [:name, :label, :title].include?(name)
    100
  elsif required?
    200
  else
    300
  end
end
inclusion_validator_for_checkbox?(val) click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 428
def inclusion_validator_for_checkbox?(val)
  @form_ui == :checkbox && [[true, false], [false, true]].include?(val.options[:with] || val.options[:within])
end
initialize_search_sql() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 470
def initialize_search_sql
  self.search_sql =
    unless self.virtual?
      if association.nil?
        field.to_s unless @tableless
      elsif !self.polymorphic_association?
        [association.klass.quoted_table_name, association.klass.quoted_primary_key].join('.') unless association.klass < ActiveScaffold::Tableless
      end
    end
end
initialize_sort() click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 457
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 column && !@tableless
      self.sort = {:sql => field}
    else
      self.sort = false
    end
  end
end
quoted_field(name) click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 453
def quoted_field(name)
  [@active_record_class.quoted_table_name, name].join('.')
end
validator_force_required?(val) click to toggle source
# File lib/active_scaffold/data_structures/column.rb, line 417
def validator_force_required?(val)
  return false if val.options[:if] || val.options[:unless]
  case val
  when ActiveModel::Validations::PresenceValidator
    true
  when ActiveModel::Validations::InclusionValidator
    !val.options[:allow_nil] && !val.options[:allow_blank] &&
      !inclusion_validator_for_checkbox?(val)
  end
end