class ActiveScaffold::DataStructures::Column
Constants
- NO_OPTIONS
- NO_PARAMS
Attributes
the association from the ActiveRecord
class
cache key to cache column info
the ConnectionAdapter::*Column object from the ActiveRecord
class
the singular association which this column belongs to
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.
the table name from the ActiveRecord
class
Public Instance Methods
Source
# File lib/active_scaffold/data_structures/column.rb, line 419 def autolink? @autolink end
set an action_link to nested list or inline form in this column
Source
# File lib/active_scaffold/data_structures/column.rb, line 587 def cast(value) ActiveScaffold::OrmChecks.cast active_record_class, name, value end
Source
# File lib/active_scaffold/data_structures/column.rb, line 583 def column_type ActiveScaffold::OrmChecks.column_type active_record_class, name end
Source
# File lib/active_scaffold/data_structures/column.rb, line 536 def default_for_empty_value return nil unless column if column.is_a?(ActiveModel::Attribute) column.value elsif active_record? && null? nil else @db_default_value end end
Source
# File lib/active_scaffold/data_structures/column.rb, line 395 def default_value @default_value || @db_default_value end
Source
# File lib/active_scaffold/data_structures/column.rb, line 399 def default_value=(value) raise ArgumentError, "Can't set default value for non-DB columns (virtual columns or associations)" unless column @default_value = value end
Source
# File lib/active_scaffold/data_structures/column.rb, line 405 def default_value? defined? @default_value end
Source
# File lib/active_scaffold/data_structures/column.rb, line 557 def field @field ||= quoted_field(field_name) end
the table.field name for this column, if applicable
Source
# File lib/active_scaffold/data_structures/column.rb, line 530 def field_name return nil if virtual? @field_name ||= column ? quoted_field_name(column.name) : quoted_field_name(association.foreign_key) end
just the field (not table.field)
Source
# File lib/active_scaffold/data_structures/column.rb, line 565 def group_by @group_by || select_columns || [field] end
Source
# File lib/active_scaffold/data_structures/column.rb, line 561 def group_by=(value) @group_by = value ? Array(value) : nil end
Source
# File lib/active_scaffold/data_structures/column.rb, line 571 def grouped_select Arel.sql(@grouped_select&.to_s || field) end
Source
# File lib/active_scaffold/data_structures/column.rb, line 548 def null? if active_record? && !column.is_a?(ActiveModel::Attribute) column&.null else true end end
Source
# File lib/active_scaffold/data_structures/column.rb, line 412 def options return @options || NO_OPTIONS if frozen? @options ||= NO_OPTIONS.dup end
Source
# File lib/active_scaffold/data_structures/column.rb, line 389 def params return @params || NO_PARAMS if frozen? @params ||= NO_PARAMS.dup end
Any extra parameters this particular column uses. This is for create/update purposes.
Source
# File lib/active_scaffold/data_structures/column.rb, line 575 def quoted_foreign_type quoted_field(quoted_field_name(association.foreign_type)) end
Source
# File lib/active_scaffold/data_structures/column.rb, line 579 def type_for_attribute ActiveScaffold::OrmChecks.type_for_attribute active_record_class, name end
Source
# File lib/active_scaffold/data_structures/column.rb, line 456 def virtual? column.nil? && association.nil? end
an interpreted property. the column is virtual if it isn’t from the active record model or any associated models
Protected Instance Methods
Source
# File lib/active_scaffold/data_structures/column.rb, line 707 def check_valid_action_ui_params(value) return true if valid_action_ui_params?(value) raise ArgumentError, 'value must be a Symbol, or an array of Symbol and Hash' end
Source
# File lib/active_scaffold/data_structures/column.rb, line 668 def column_number? if active_record? %i[float decimal integer].include? column_type elsif mongoid? @column.type < Numeric end end
Source
# File lib/active_scaffold/data_structures/column.rb, line 647 def default_select_columns if association.nil? && column [field] elsif association&.polymorphic? [field, quoted_field(quoted_field_name(association.foreign_type))] elsif association if association.belongs_to? [field] else columns = [] if _columns_hash[count_column = "#{association.name}_count"] columns << quoted_field(quoted_field_name(count_column)) end if association.through_reflection&.belongs_to? columns << quoted_field(quoted_field_name(association.through_reflection.foreign_key)) end columns end end end
Source
# File lib/active_scaffold/data_structures/column.rb, line 691 def estimate_weight if association&.singular? 400 elsif association&.collection? 500 elsif %i[created_at updated_at].include?(name) 600 elsif %i[name label title].include?(name) 100 elsif required? 200 else 300 end end
Source
# File lib/active_scaffold/data_structures/column.rb, line 642 def inclusion_validator_for_checkbox?(val) @form_ui == :checkbox && [[true, false], [false, true]].include?(val.options[:with] || val.options[:within] || val.options[:in]) end
Source
# File lib/active_scaffold/data_structures/column.rb, line 684 def quoted_field(name) active_record? ? [_quoted_table_name, name].compact.join('.') : name end
Source
# File lib/active_scaffold/data_structures/column.rb, line 676 def quoted_field_name(column_name) if active_record? @active_record_class.connection.quote_column_name(column_name) else column_name.to_s end end
Source
# File lib/active_scaffold/data_structures/column.rb, line 610 def setup_association_info assoc = active_record_class.reflect_on_association(name) @association = if assoc if active_record? Association::ActiveRecord.new(assoc) elsif mongoid? Association::Mongoid.new(assoc) end elsif defined?(ActiveMongoid) && model < ActiveMongoid::Associations assoc = active_record_class.reflect_on_am_association(name) Association::ActiveMongoid.new(assoc) if assoc end end
Source
# File lib/active_scaffold/data_structures/column.rb, line 593 def setup_defaults_for_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.map(&:to_sym)} elsif column_number? @number = true @form_ui = :number @options = {format: :i18n_number} else @form_ui = case column_type when :boolean then null? ? :boolean : :checkbox when :text then :textarea end end end
Source
# File lib/active_scaffold/data_structures/column.rb, line 713 def valid_action_ui_params?(value) if value.is_a?(Array) value.size <= 2 && value[0].is_a?(Symbol) && (value[1].nil? || value[1].is_a?(Hash)) else value.nil? || value.is_a?(Symbol) end end
Source
# File lib/active_scaffold/data_structures/column.rb, line 625 def validator_force_required?(val) return false if val.options[:if] || val.options[:unless] case val when ActiveModel::Validations::PresenceValidator validator_required_on(val) when ActiveModel::Validations::InclusionValidator if !val.options[:allow_nil] && !val.options[:allow_blank] && !inclusion_validator_for_checkbox?(val) validator_required_on(val) end end end
Source
# File lib/active_scaffold/data_structures/column.rb, line 638 def validator_required_on(val) val.options[:on] ? Array(val.options[:on]) : true end