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
-
#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.
-
#inplace_edit_update ⇒ Object
Returns the value of attribute inplace_edit_update.
- #label ⇒ Object
-
#list_method ⇒ Object
to cache method to get value in list.
-
#name ⇒ Object
this is the name of the getter on the ActiveRecord model.
-
#number ⇒ Object
writeonly
Sets the attribute number.
-
#numerical_constraints ⇒ Object
cache constraints for numeric columns (get in ActiveScaffold::Helpers::FormColumnHelpers::numerical_constraints_for_column).
-
#options ⇒ Object
a place to store dev’s column specific options.
- #placeholder ⇒ Object
-
#required ⇒ Object
writeonly
whether the field is required or not.
- #search_ui ⇒ Object
-
#select_associated_columns ⇒ Object
a collection of columns to load when eager loading is disabled, if it’s nil all columns will be loaded.
-
#select_columns ⇒ Object
What columns load from main table.
-
#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.
-
#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.
-
#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 ⇒ Object
the table.field name for this column, if applicable.
-
#field_name ⇒ Object
just the field (not table.field).
- #form_ui ⇒ Object
-
#form_ui=(value) ⇒ Object
supported options: * for association columns * :select - displays a simple <select> or a collection of checkboxes to (dis)associate records.
-
#initialize(name, active_record_class) ⇒ Column
constructor
instantiation is handled internally through the DataStructures::Columns object.
- #link ⇒ Object
- #list_ui ⇒ Object
- #list_ui=(value) ⇒ Object
- #number? ⇒ Boolean
- #number_to_native(value) ⇒ Object
-
#params ⇒ Object
Any extra parameters this particular column uses.
- #plural_association? ⇒ Boolean
- #polymorphic_association? ⇒ Boolean
- #readonly_association? ⇒ Boolean
- #required? ⇒ Boolean
-
#search_joins ⇒ Object
a collection of associations to do left join when this column is included on search.
- #search_joins=(value) ⇒ Object
- #search_sql ⇒ Object
-
#search_sql=(value) ⇒ Object
describes how to search on a column search = true default, uses intelligent search sql search = “CONCAT(a, b)” define your own sql for searching.
- #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
- #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
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/active_scaffold/data_structures/column.rb', line 305 def initialize(name, active_record_class) #:nodoc: self.name = name.to_sym @tableless = active_record_class < ActiveScaffold::Tableless @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 @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 @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 self.association.macro == :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(:macro) == :belongs_to columns << quoted_field(@active_record_class.connection.quote_column_name(@association.through_reflection.foreign_key)) end columns end end self.number = @column.try(:number?) = {:format => :i18n_number} if self.number? @form_ui = :checkbox if @column and @column.type == :boolean @form_ui = :textarea if @column and @column.type == :text @form_ui = :number if @column and self.number? @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]) ) end self.sort = true self.search_sql = true @weight = estimate_weight if association && !polymorphic_association? self.includes = [association.name] self.search_joins = self.includes.clone end 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.
244 245 246 |
# File 'lib/active_scaffold/data_structures/column.rb', line 244 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
23 24 25 |
# File 'lib/active_scaffold/data_structures/column.rb', line 23 def allow_add_existing @allow_add_existing end |
#associated_limit ⇒ Object
Returns the value of attribute associated_limit.
220 221 222 |
# File 'lib/active_scaffold/data_structures/column.rb', line 220 def associated_limit @associated_limit end |
#associated_number=(value) ⇒ Object (writeonly)
Sets the attribute associated_number
225 226 227 |
# File 'lib/active_scaffold/data_structures/column.rb', line 225 def associated_number=(value) @associated_number = value end |
#association ⇒ Object (readonly)
the association from the ActiveRecord class
257 258 259 |
# File 'lib/active_scaffold/data_structures/column.rb', line 257 def association @association end |
#calculate ⇒ Object
define a calculation for the column. anything that ActiveRecord::Calculations::ClassMethods#calculate accepts will do.
168 169 170 |
# File 'lib/active_scaffold/data_structures/column.rb', line 168 def calculate @calculate end |
#collapsed ⇒ Object
Whether this column set is collapsed by default in contexts where collapsing is supported
20 21 22 |
# File 'lib/active_scaffold/data_structures/column.rb', line 20 def collapsed @collapsed end |
#column ⇒ Object (readonly)
the ConnectionAdapter::*Column object from the ActiveRecord class
254 255 256 |
# File 'lib/active_scaffold/data_structures/column.rb', line 254 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.
58 59 60 |
# File 'lib/active_scaffold/data_structures/column.rb', line 58 def css_class @css_class end |
#description ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/active_scaffold/data_structures/column.rb', line 43 def description if @description @description else I18n.t name, :scope => [:activerecord, :description, active_record_class.to_s.underscore.to_sym], :default => '' end end |
#includes ⇒ Object
a collection of associations to pre-load when finding the records on a page
176 177 178 |
# File 'lib/active_scaffold/data_structures/column.rb', line 176 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 |
#inplace_edit_update ⇒ Object
Returns the value of attribute inplace_edit_update.
17 18 19 |
# File 'lib/active_scaffold/data_structures/column.rb', line 17 def inplace_edit_update @inplace_edit_update end |
#label ⇒ Object
37 38 39 |
# File 'lib/active_scaffold/data_structures/column.rb', line 37 def label as_(@label) || active_record_class.human_attribute_name(name.to_s) end |
#list_method ⇒ Object
to cache method to get value in list
395 396 397 |
# File 'lib/active_scaffold/data_structures/column.rb', line 395 def list_method @list_method 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 |
#number=(value) ⇒ Object (writeonly)
Sets the attribute number
285 286 287 |
# File 'lib/active_scaffold/data_structures/column.rb', line 285 def number=(value) @number = value end |
#numerical_constraints ⇒ Object
cache constraints for numeric columns (get in ActiveScaffold::Helpers::FormColumnHelpers::numerical_constraints_for_column)
398 399 400 |
# File 'lib/active_scaffold/data_structures/column.rb', line 398 def numerical_constraints @numerical_constraints end |
#options ⇒ Object
a place to store dev’s column specific options
134 135 136 |
# File 'lib/active_scaffold/data_structures/column.rb', line 134 def end |
#placeholder ⇒ Object
53 54 55 |
# File 'lib/active_scaffold/data_structures/column.rb', line 53 def placeholder @placeholder || I18n.t(name, :scope => [:activerecord, :placeholder, active_record_class.to_s.underscore.to_sym], :default => '') 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
62 63 64 |
# File 'lib/active_scaffold/data_structures/column.rb', line 62 def required=(value) @required = value end |
#search_ui ⇒ Object
129 130 131 |
# File 'lib/active_scaffold/data_structures/column.rb', line 129 def search_ui @search_ui || @form_ui || (@association && !polymorphic_association? ? :select : nil) end |
#select_associated_columns ⇒ Object
a collection of columns to load when eager loading is disabled, if it’s nil all columns will be loaded
197 198 199 |
# File 'lib/active_scaffold/data_structures/column.rb', line 197 def select_associated_columns @select_associated_columns end |
#select_columns ⇒ Object
What columns load from main table
26 27 28 |
# File 'lib/active_scaffold/data_structures/column.rb', line 26 def select_columns @select_columns end |
#send_form_on_update_column ⇒ Object
Returns the value of attribute send_form_on_update_column.
78 79 80 |
# File 'lib/active_scaffold/data_structures/column.rb', line 78 def send_form_on_update_column @send_form_on_update_column end |
#show_blank_record=(value) ⇒ Object (writeonly)
Sets the attribute show_blank_record
233 234 235 |
# File 'lib/active_scaffold/data_structures/column.rb', line 233 def show_blank_record=(value) @show_blank_record = value end |
#update_columns ⇒ Object
Returns the value of attribute update_columns.
67 68 69 |
# File 'lib/active_scaffold/data_structures/column.rb', line 67 def update_columns @update_columns end |
#weight ⇒ Object
to modify the default order of columns
215 216 217 |
# File 'lib/active_scaffold/data_structures/column.rb', line 215 def weight @weight end |
Instance Method Details
#<=>(other_column) ⇒ Object
369 370 371 372 |
# File 'lib/active_scaffold/data_structures/column.rb', line 369 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
291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/active_scaffold/data_structures/column.rb', line 291 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
226 227 228 |
# File 'lib/active_scaffold/data_structures/column.rb', line 226 def associated_number? @associated_number end |
#autolink? ⇒ Boolean
set an action_link to nested list or inline form in this column
157 158 159 |
# File 'lib/active_scaffold/data_structures/column.rb', line 157 def autolink? @autolink end |
#calculation? ⇒ Boolean
get whether to run a calculation on this column
171 172 173 |
# File 'lib/active_scaffold/data_structures/column.rb', line 171 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
162 163 164 165 |
# File 'lib/active_scaffold/data_structures/column.rb', line 162 def clear_link @link = nil @autolink = false end |
#field ⇒ Object
the table.field name for this column, if applicable
401 402 403 |
# File 'lib/active_scaffold/data_structures/column.rb', line 401 def field @field ||= quoted_field(field_name) end |
#field_name ⇒ Object
just the field (not table.field)
364 365 366 367 |
# File 'lib/active_scaffold/data_structures/column.rb', line 364 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 ⇒ Object
115 116 117 |
# File 'lib/active_scaffold/data_structures/column.rb', line 115 def form_ui @form_ui end |
#form_ui=(value) ⇒ Object
supported options:
* for association columns
* :select - displays a simple <select> or a collection of checkboxes to (dis)associate records
111 112 113 114 |
# File 'lib/active_scaffold/data_structures/column.rb', line 111 def form_ui=(value) self.list_method = nil if @list_ui.nil? && value != @form_ui @form_ui = value end |
#link ⇒ Object
139 140 141 142 |
# File 'lib/active_scaffold/data_structures/column.rb', line 139 def link @link = @link.call(self) if @link.is_a? Proc @link end |
#list_ui ⇒ Object
124 125 126 |
# File 'lib/active_scaffold/data_structures/column.rb', line 124 def list_ui @list_ui || @form_ui end |
#list_ui=(value) ⇒ Object
119 120 121 122 |
# 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? ⇒ Boolean
286 287 288 |
# File 'lib/active_scaffold/data_structures/column.rb', line 286 def number? @number end |
#number_to_native(value) ⇒ Object
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/active_scaffold/data_structures/column.rb', line 374 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.[: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 ⇒ Object
Any extra parameters this particular column uses. This is for create/update purposes.
29 30 31 32 |
# File 'lib/active_scaffold/data_structures/column.rb', line 29 def params # lazy initialize @params ||= Set.new end |
#plural_association? ⇒ Boolean
261 262 263 |
# File 'lib/active_scaffold/data_structures/column.rb', line 261 def plural_association? self.association and [:has_many, :has_and_belongs_to_many].include? self.association.macro end |
#polymorphic_association? ⇒ Boolean
267 268 269 |
# File 'lib/active_scaffold/data_structures/column.rb', line 267 def polymorphic_association? self.association and self.association..has_key? :polymorphic and self.association.[:polymorphic] end |
#readonly_association? ⇒ Boolean
270 271 272 273 274 275 276 277 278 |
# File 'lib/active_scaffold/data_structures/column.rb', line 270 def readonly_association? if self.association if self.association..has_key? :readonly self.association.[:readonly] else self.through_association? end end end |
#required? ⇒ Boolean
63 64 65 |
# File 'lib/active_scaffold/data_structures/column.rb', line 63 def required? @required end |
#search_joins ⇒ Object
a collection of associations to do left join when this column is included on search
185 186 187 |
# File 'lib/active_scaffold/data_structures/column.rb', line 185 def search_joins @search_joins || @includes end |
#search_joins=(value) ⇒ Object
189 190 191 192 193 194 |
# File 'lib/active_scaffold/data_structures/column.rb', line 189 def search_joins=(value) @search_joins = case value when Array then value else [value] # automatically convert to an array end end |
#search_sql ⇒ Object
206 207 208 209 |
# File 'lib/active_scaffold/data_structures/column.rb', line 206 def search_sql self.initialize_search_sql if @search_sql === true @search_sql end |
#search_sql=(value) ⇒ Object
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
203 204 205 |
# File 'lib/active_scaffold/data_structures/column.rb', line 203 def search_sql=(value) @search_sql = (value == true || value.is_a?(Proc)) ? value : Array(value) end |
#searchable? ⇒ Boolean
210 211 212 |
# File 'lib/active_scaffold/data_structures/column.rb', line 210 def searchable? search_sql != false && search_sql != nil end |
#set_link(action, options = {}) ⇒ Object
associate an action_link with this column
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/active_scaffold/data_structures/column.rb', line 145 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
234 235 236 237 238 239 |
# File 'lib/active_scaffold/data_structures/column.rb', line 234 def show_blank_record?(associated) if @show_blank_record return false unless self.association.klass.(:crud_type => :create) and not self.association.[:readonly] self.plural_association? or (self.singular_association? and associated.blank?) end end |
#singular_association? ⇒ Boolean
258 259 260 |
# File 'lib/active_scaffold/data_structures/column.rb', line 258 def singular_association? self.association and [:has_one, :belongs_to].include? self.association.macro end |
#sort ⇒ Object
94 95 96 97 |
# File 'lib/active_scaffold/data_structures/column.rb', line 94 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!
85 86 87 88 89 90 91 92 |
# File 'lib/active_scaffold/data_structures/column.rb', line 85 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.
104 105 106 |
# File 'lib/active_scaffold/data_structures/column.rb', line 104 def sort_by() self.sort = end |
#sortable? ⇒ Boolean
99 100 101 |
# File 'lib/active_scaffold/data_structures/column.rb', line 99 def sortable? sort != false && !sort.nil? end |
#through_association? ⇒ Boolean
264 265 266 |
# File 'lib/active_scaffold/data_structures/column.rb', line 264 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
281 282 283 |
# File 'lib/active_scaffold/data_structures/column.rb', line 281 def virtual? column.nil? && association.nil? end |