Class: ActiveScaffold::DataStructures::Column
- Includes:
- Configurable
- Defined in:
- lib/active_scaffold/data_structures/column.rb
Constant Summary collapse
- @@associated_limit =
3
- @@associated_number =
true
- @@show_blank_record =
true
- @@actions_for_association_links =
[:new, :edit, :show]
- @@association_form_ui =
nil
Instance Attribute Summary collapse
-
#actions_for_association_links ⇒ Object
Returns the value of attribute actions_for_association_links.
-
#active_record_class ⇒ Object
readonly
Returns the value of attribute active_record_class.
-
#allow_add_existing ⇒ Object
Whether to enable add_existing for this column.
-
#associated_limit ⇒ Object
Returns the value of attribute associated_limit.
-
#associated_number ⇒ Object
writeonly
Sets the attribute associated_number.
-
#association ⇒ Object
readonly
the association from the ActiveRecord class.
-
#calculate ⇒ Object
define a calculation for the column.
-
#collapsed ⇒ Object
Whether this column set is collapsed by default in contexts where collapsing is supported.
-
#column ⇒ Object
readonly
the ConnectionAdapter::*Column object from the ActiveRecord class.
-
#css_class ⇒ Object
this will be /joined/ to the :name for the td’s class attribute.
- #description ⇒ Object
- #form_ui ⇒ Object
-
#includes ⇒ Object
a collection of associations to pre-load when finding the records on a page.
-
#inplace_edit ⇒ Object
Whether to enable inplace editing for this column.
- #label ⇒ Object
- #list_ui ⇒ Object
-
#name ⇒ Object
this is the name of the getter on the ActiveRecord model.
-
#options ⇒ Object
a place to store dev’s column specific options.
-
#required ⇒ Object
writeonly
whether the field is required or not.
- #search_sql ⇒ Object
- #search_ui ⇒ Object
-
#select_columns ⇒ Object
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 ⇒ Object
Returns the value of attribute send_form_on_update_column.
-
#show_blank_record ⇒ Object
writeonly
Sets the attribute show_blank_record.
-
#stripable ⇒ Object
writeonly
whether the field should be stripped when search values or form values are read for this value.
-
#update_columns ⇒ Object
Returns the value of attribute update_columns.
-
#weight ⇒ Object
to modify the default order of columns.
Instance Method Summary collapse
- #<=>(other_column) ⇒ Object
-
#==(other) ⇒ Object
this is so that array.delete and array.include?, etc., will work by column name.
- #associated_number? ⇒ Boolean
-
#autolink? ⇒ Boolean
set an action_link to nested list or inline form in this column.
- #belongs_to_association? ⇒ Boolean
-
#calculation? ⇒ Boolean
get whether to run a calculation on this column.
-
#clear_link ⇒ Object
this should not only delete any existing link but also prevent column links from being automatically added by later routines.
-
#field_name ⇒ Object
just the field (not table.field).
-
#initialize(name, active_record_class) ⇒ Column
constructor
instantiation is handled internally through the DataStructures::Columns object.
- #link ⇒ Object
-
#params ⇒ Object
Any extra parameters this particular column uses.
- #plural_association? ⇒ Boolean
- #polymorphic_association? ⇒ Boolean
- #readonly_association? ⇒ Boolean
- #required? ⇒ Boolean
- #searchable? ⇒ Boolean
-
#set_link(action, options = {}) ⇒ Object
associate an action_link with this column.
- #show_blank_record?(associated) ⇒ Boolean
- #singular_association? ⇒ Boolean
- #sort ⇒ Object
-
#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 = => “” define your own sql for sorting.
-
#sort_by(options) ⇒ Object
a configuration helper for the self.sort property.
- #sortable? ⇒ Boolean
- #stripable? ⇒ Boolean
- #stripped_value(value) ⇒ Object
- #through_association? ⇒ Boolean
-
#virtual? ⇒ Boolean
an interpreted property.
Methods included from Configurable
Constructor Details
#initialize(name, active_record_class) ⇒ Column
instantiation is handled internally through the DataStructures::Columns object
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 316 317 318 319 320 |
# File 'lib/active_scaffold/data_structures/column.rb', line 289 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 @actions_for_association_links = self.class.actions_for_association_links.clone if @association @options = {:format => :i18n_number} if @column.try(:number?) @form_ui = :checkbox if @column and @column.type == :boolean @form_ui = :textarea if @column and @column.type == :text @stripable = true if @column and @column.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| !val.[:if] && !val.[:unless] && (ActiveModel::Validations::PresenceValidator === val || (ActiveModel::Validations::InclusionValidator === val && !val.[:allow_nil] && !val.[:allow_blank] && !(@form_ui == :checkbox && [[true, false], [false, true]].include?(val.send(:delimiter)))) ) 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
#actions_for_association_links ⇒ Object
Returns the value of attribute actions_for_association_links.
230 231 232 |
# File 'lib/active_scaffold/data_structures/column.rb', line 230 def actions_for_association_links @actions_for_association_links end |
#active_record_class ⇒ Object (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_existing ⇒ Object
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_limit ⇒ Object
Returns the value of attribute associated_limit.
206 207 208 |
# File 'lib/active_scaffold/data_structures/column.rb', line 206 def associated_limit @associated_limit end |
#associated_number=(value) ⇒ Object (writeonly)
Sets the attribute associated_number
211 212 213 |
# File 'lib/active_scaffold/data_structures/column.rb', line 211 def associated_number=(value) @associated_number = value end |
#association ⇒ Object (readonly)
the association from the ActiveRecord class
243 244 245 |
# File 'lib/active_scaffold/data_structures/column.rb', line 243 def association @association end |
#calculate ⇒ Object
define a calculation for the column. anything that ActiveRecord::Calculations::ClassMethods#calculate accepts will do.
150 151 152 |
# File 'lib/active_scaffold/data_structures/column.rb', line 150 def calculate @calculate end |
#collapsed ⇒ Object
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 |
#column ⇒ Object (readonly)
the ConnectionAdapter::*Column object from the ActiveRecord class
240 241 242 |
# File 'lib/active_scaffold/data_structures/column.rb', line 240 def column @column end |
#css_class ⇒ Object
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 |
#description ⇒ Object
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_ui ⇒ Object
101 102 103 |
# File 'lib/active_scaffold/data_structures/column.rb', line 101 def form_ui @form_ui end |
#includes ⇒ Object
a collection of associations to pre-load when finding the records on a page
158 159 160 |
# File 'lib/active_scaffold/data_structures/column.rb', line 158 def includes @includes end |
#inplace_edit ⇒ Object
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 |
#label ⇒ Object
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 |
#list_ui ⇒ Object
106 107 108 |
# File 'lib/active_scaffold/data_structures/column.rb', line 106 def list_ui @list_ui || @form_ui end |
#name ⇒ Object
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 |
#options ⇒ Object
a place to store dev’s column specific options
116 117 118 |
# File 'lib/active_scaffold/data_structures/column.rb', line 116 def @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_sql ⇒ Object
173 174 175 176 |
# File 'lib/active_scaffold/data_structures/column.rb', line 173 def search_sql self.initialize_search_sql if !@search_sql.is_a?(Proc) && @search_sql === true @search_sql end |
#search_ui ⇒ Object
111 112 113 |
# File 'lib/active_scaffold/data_structures/column.rb', line 111 def search_ui @search_ui || @form_ui || (@association && !polymorphic_association? ? :select : nil) end |
#select_columns ⇒ Object
a collection of columns to load when eager loading is disabled, if it’s nil all columns will be loaded
167 168 169 |
# File 'lib/active_scaffold/data_structures/column.rb', line 167 def select_columns @select_columns end |
#send_form_on_update_column ⇒ Object
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
219 220 221 |
# File 'lib/active_scaffold/data_structures/column.rb', line 219 def show_blank_record=(value) @show_blank_record = value end |
#stripable=(value) ⇒ Object (writeonly)
whether the field should be stripped when search values or form values are read for this value
185 186 187 |
# File 'lib/active_scaffold/data_structures/column.rb', line 185 def stripable=(value) @stripable = value end |
#update_columns ⇒ Object
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 |
#weight ⇒ Object
to modify the default order of columns
182 183 184 |
# File 'lib/active_scaffold/data_structures/column.rb', line 182 def weight @weight end |
Instance Method Details
#<=>(other_column) ⇒ Object
329 330 331 332 |
# File 'lib/active_scaffold/data_structures/column.rb', line 329 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
275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/active_scaffold/data_structures/column.rb', line 275 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 |
#associated_number? ⇒ Boolean
212 213 214 |
# File 'lib/active_scaffold/data_structures/column.rb', line 212 def associated_number? @associated_number end |
#autolink? ⇒ Boolean
set an action_link to nested list or inline form in this column
139 140 141 |
# File 'lib/active_scaffold/data_structures/column.rb', line 139 def autolink? @autolink end |
#belongs_to_association? ⇒ Boolean
250 251 252 |
# File 'lib/active_scaffold/data_structures/column.rb', line 250 def belongs_to_association? self.association && self.association.macro == :belongs_to end |
#calculation? ⇒ Boolean
get whether to run a calculation on this column
153 154 155 |
# File 'lib/active_scaffold/data_structures/column.rb', line 153 def calculation? !(@calculate == false or @calculate.nil?) end |
#clear_link ⇒ Object
this should not only delete any existing link but also prevent column links from being automatically added by later routines
144 145 146 147 |
# File 'lib/active_scaffold/data_structures/column.rb', line 144 def clear_link @link = nil @autolink = false end |
#field_name ⇒ Object
just the field (not table.field)
323 324 325 326 327 |
# File 'lib/active_scaffold/data_structures/column.rb', line 323 def field_name return nil if virtual? return column.name if !column.nil? && @active_record_class.respond_to?(:tableless?) && @active_record_class.tableless? column ? @active_record_class.connection.quote_column_name(column.name) : association.foreign_key end |
#link ⇒ Object
121 122 123 124 |
# File 'lib/active_scaffold/data_structures/column.rb', line 121 def link @link = @link.call(self) if @link.is_a? Proc @link end |
#params ⇒ Object
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
247 248 249 |
# File 'lib/active_scaffold/data_structures/column.rb', line 247 def plural_association? self.association and [:has_many, :has_and_belongs_to_many].include? self.association.macro end |
#polymorphic_association? ⇒ Boolean
256 257 258 |
# File 'lib/active_scaffold/data_structures/column.rb', line 256 def polymorphic_association? self.association and self.association..has_key? :polymorphic and self.association.[:polymorphic] end |
#readonly_association? ⇒ Boolean
259 260 261 262 263 264 265 266 267 |
# File 'lib/active_scaffold/data_structures/column.rb', line 259 def readonly_association? if self.association if self.association..has_key? :readonly self.association.[:readonly] else self.through_association? end end end |
#required? ⇒ Boolean
52 53 54 |
# File 'lib/active_scaffold/data_structures/column.rb', line 52 def required? @required end |
#searchable? ⇒ Boolean
177 178 179 |
# File 'lib/active_scaffold/data_structures/column.rb', line 177 def searchable? search_sql != false && search_sql != nil end |
#set_link(action, options = {}) ⇒ Object
associate an action_link with this column
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/active_scaffold/data_structures/column.rb', line 127 def set_link(action, = {}) if action.is_a?(ActiveScaffold::DataStructures::ActionLink) || (action.is_a? Proc) @link = action else [:label] ||= self.label [:position] ||= :after unless .has_key?(:position) [:type] ||= :member @link = ActiveScaffold::DataStructures::ActionLink.new(action, ) end end |
#show_blank_record?(associated) ⇒ Boolean
220 221 222 223 224 225 |
# File 'lib/active_scaffold/data_structures/column.rb', line 220 def show_blank_record?(associated) if @show_blank_record return false unless self.association.klass.(:crud_type => :create) self.plural_association? or (self.singular_association? and associated.empty?) end end |
#singular_association? ⇒ Boolean
244 245 246 |
# File 'lib/active_scaffold/data_structures/column.rb', line 244 def singular_association? self.association and [:has_one, :belongs_to].include? self.association.macro end |
#sort ⇒ Object
83 84 85 86 |
# File 'lib/active_scaffold/data_structures/column.rb', line 83 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!
74 75 76 77 78 79 80 81 |
# File 'lib/active_scaffold/data_structures/column.rb', line 74 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.
93 94 95 |
# File 'lib/active_scaffold/data_structures/column.rb', line 93 def sort_by() self.sort = end |
#sortable? ⇒ Boolean
88 89 90 |
# File 'lib/active_scaffold/data_structures/column.rb', line 88 def sortable? sort != false && !sort.nil? end |
#stripable? ⇒ Boolean
186 187 188 |
# File 'lib/active_scaffold/data_structures/column.rb', line 186 def stripable? @stripable end |
#stripped_value(value) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/active_scaffold/data_structures/column.rb', line 190 def stripped_value(value) if stripable? begin value.strip rescue Rails.logger.error("stripped_value: column #{self.name} is a db text column, but value is nt a string: #{value.inspect}. Please define column[:#{self.name}].stripable = false in config") value end else value end end |
#through_association? ⇒ Boolean
253 254 255 |
# File 'lib/active_scaffold/data_structures/column.rb', line 253 def through_association? self.association and self.association.[:through] end |
#virtual? ⇒ Boolean
an interpreted property. the column is virtual if it isn’t from the active record model or any associated models
270 271 272 |
# File 'lib/active_scaffold/data_structures/column.rb', line 270 def virtual? column.nil? && association.nil? end |