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.
-
#number ⇒ Object
writeonly
Sets the attribute number.
-
#options ⇒ Object
a place to store dev’s column specific options.
-
#required ⇒ Object
writeonly
whether the field is required or not.
- #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.
-
#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_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
- #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_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
-
#update_column=(column_name) ⇒ Object
column to be updated in a form when this column changes.
-
#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
285 286 287 288 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 285 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 self.number = @column.try(:number?) @options = {:format => :i18n_number} if self.number? @form_ui = :checkbox if @column and @column.type == :boolean @form_ui = :textarea if @column and @column.type == :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| ActiveModel::Validations::PresenceValidator === val or ( ActiveModel::Validations::InclusionValidator === val and not val.[:allow_nil] and not val.[:allow_blank] ) end self.sort = true self.search_sql = true @weight = estimate_weight self.clear_link if self.polymorphic_association? 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.
224 225 226 |
# File 'lib/active_scaffold/data_structures/column.rb', line 224 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.
200 201 202 |
# File 'lib/active_scaffold/data_structures/column.rb', line 200 def associated_limit @associated_limit end |
#associated_number=(value) ⇒ Object (writeonly)
Sets the attribute associated_number
205 206 207 |
# File 'lib/active_scaffold/data_structures/column.rb', line 205 def associated_number=(value) @associated_number = value end |
#association ⇒ Object (readonly)
the association from the ActiveRecord class
237 238 239 |
# File 'lib/active_scaffold/data_structures/column.rb', line 237 def association @association end |
#calculate ⇒ Object
define a calculation for the column. anything that ActiveRecord::Calculations::ClassMethods#calculate accepts will do.
160 161 162 |
# File 'lib/active_scaffold/data_structures/column.rb', line 160 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
234 235 236 |
# File 'lib/active_scaffold/data_structures/column.rb', line 234 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
111 112 113 |
# File 'lib/active_scaffold/data_structures/column.rb', line 111 def form_ui @form_ui end |
#includes ⇒ Object
a collection of associations to pre-load when finding the records on a page
168 169 170 |
# File 'lib/active_scaffold/data_structures/column.rb', line 168 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
116 117 118 |
# File 'lib/active_scaffold/data_structures/column.rb', line 116 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 |
#number=(value) ⇒ Object (writeonly)
Sets the attribute number
265 266 267 |
# File 'lib/active_scaffold/data_structures/column.rb', line 265 def number=(value) @number = value end |
#options ⇒ Object
a place to store dev’s column specific options
126 127 128 |
# File 'lib/active_scaffold/data_structures/column.rb', line 126 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_ui ⇒ Object
121 122 123 |
# File 'lib/active_scaffold/data_structures/column.rb', line 121 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
177 178 179 |
# File 'lib/active_scaffold/data_structures/column.rb', line 177 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
213 214 215 |
# File 'lib/active_scaffold/data_structures/column.rb', line 213 def show_blank_record=(value) @show_blank_record = 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
195 196 197 |
# File 'lib/active_scaffold/data_structures/column.rb', line 195 def weight @weight end |
Instance Method Details
#<=>(other_column) ⇒ Object
328 329 330 331 |
# File 'lib/active_scaffold/data_structures/column.rb', line 328 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
271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/active_scaffold/data_structures/column.rb', line 271 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
206 207 208 |
# File 'lib/active_scaffold/data_structures/column.rb', line 206 def associated_number? @associated_number end |
#autolink? ⇒ Boolean
set an action_link to nested list or inline form in this column
149 150 151 |
# File 'lib/active_scaffold/data_structures/column.rb', line 149 def autolink? @autolink end |
#calculation? ⇒ Boolean
get whether to run a calculation on this column
163 164 165 |
# File 'lib/active_scaffold/data_structures/column.rb', line 163 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
154 155 156 157 |
# File 'lib/active_scaffold/data_structures/column.rb', line 154 def clear_link @link = nil @autolink = false end |
#field_name ⇒ Object
just the field (not table.field)
323 324 325 326 |
# File 'lib/active_scaffold/data_structures/column.rb', line 323 def field_name return nil if virtual? @field_name ||= column ? @active_record_class.connection.quote_column_name(column.name) : association.foreign_key end |
#link ⇒ Object
131 132 133 134 |
# File 'lib/active_scaffold/data_structures/column.rb', line 131 def link @link = @link.call(self) if @link.is_a? Proc @link end |
#number? ⇒ Boolean
266 267 268 |
# File 'lib/active_scaffold/data_structures/column.rb', line 266 def number? @number end |
#number_to_native(value) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/active_scaffold/data_structures/column.rb', line 333 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.
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
241 242 243 |
# File 'lib/active_scaffold/data_structures/column.rb', line 241 def plural_association? self.association and [:has_many, :has_and_belongs_to_many].include? self.association.macro end |
#polymorphic_association? ⇒ Boolean
247 248 249 |
# File 'lib/active_scaffold/data_structures/column.rb', line 247 def polymorphic_association? self.association and self.association..has_key? :polymorphic and self.association.[:polymorphic] end |
#readonly_association? ⇒ Boolean
250 251 252 253 254 255 256 257 258 |
# File 'lib/active_scaffold/data_structures/column.rb', line 250 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 |
#search_sql ⇒ Object
186 187 188 189 |
# File 'lib/active_scaffold/data_structures/column.rb', line 186 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
183 184 185 |
# File 'lib/active_scaffold/data_structures/column.rb', line 183 def search_sql=(value) @search_sql = (value == true || value.is_a?(Proc)) ? value : Array(value) end |
#searchable? ⇒ Boolean
190 191 192 |
# File 'lib/active_scaffold/data_structures/column.rb', line 190 def searchable? search_sql != false && search_sql != nil end |
#set_link(action, options = {}) ⇒ Object
associate an action_link with this column
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/active_scaffold/data_structures/column.rb', line 137 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
214 215 216 217 218 219 |
# File 'lib/active_scaffold/data_structures/column.rb', line 214 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.blank?) end end |
#singular_association? ⇒ Boolean
238 239 240 |
# File 'lib/active_scaffold/data_structures/column.rb', line 238 def singular_association? self.association and [:has_one, :belongs_to].include? self.association.macro end |
#sort ⇒ Object
93 94 95 96 |
# File 'lib/active_scaffold/data_structures/column.rb', line 93 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!
84 85 86 87 88 89 90 91 |
# File 'lib/active_scaffold/data_structures/column.rb', line 84 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.
103 104 105 |
# File 'lib/active_scaffold/data_structures/column.rb', line 103 def sort_by() self.sort = end |
#sortable? ⇒ Boolean
98 99 100 |
# File 'lib/active_scaffold/data_structures/column.rb', line 98 def sortable? sort != false && !sort.nil? end |
#through_association? ⇒ Boolean
244 245 246 |
# File 'lib/active_scaffold/data_structures/column.rb', line 244 def through_association? self.association and self.association.[:through] end |
#update_column=(column_name) ⇒ Object
column to be updated in a form when this column changes
70 71 72 73 |
# File 'lib/active_scaffold/data_structures/column.rb', line 70 def update_column=(column_name) ActiveSupport::Deprecation.warn "Use update_columns= instead of update_column=" self.update_columns = column_name end |
#virtual? ⇒ Boolean
an interpreted property. the column is virtual if it isn’t from the active record model or any associated models
261 262 263 |
# File 'lib/active_scaffold/data_structures/column.rb', line 261 def virtual? column.nil? && association.nil? end |