Class: ActiveScaffold::DataStructures::Column
- Includes:
- Configurable, OrmChecks
- Defined in:
- lib/active_scaffold/data_structures/column.rb
Constant Summary collapse
- NO_PARAMS =
Set.new.freeze
- NO_OPTIONS =
{}.freeze
- @@associated_limit =
3
- @@associated_number =
true
- @@show_blank_record =
true
- @@actions_for_association_links =
%i[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
(also: #model)
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.
-
#association_join_text ⇒ Object
what string to use to join records from plural associations.
-
#cache_key ⇒ Object
readonly
cache key to cache column info.
-
#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.
-
#delegated_association ⇒ Object
readonly
the singular association which this column belongs to.
- #description ⇒ Object
-
#form_ui ⇒ Object
supported options: * for association columns * :select - displays a simple <select> or a collection of checkboxes to (dis)associate records.
-
#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
:table to refresh list true or :row to refresh row.
- #label ⇒ Object
- #list_ui ⇒ Object
-
#name ⇒ Object
readonly
this is the name of the getter on the ActiveRecord model.
-
#number ⇒ Object
writeonly
Sets the attribute number.
- #options ⇒ Object
- #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.
- #show_ui ⇒ Object
-
#update_columns ⇒ Object
Returns the value of attribute update_columns.
-
#weight ⇒ Object
to modify the default order of columns.
Instance Method Summary collapse
- #<=>(other) ⇒ 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.
- #column_type ⇒ Object
- #convert_to_native? ⇒ Boolean
- #default_for_empty_value ⇒ Object
-
#field ⇒ Object
the table.field name for this column, if applicable.
-
#field_name ⇒ Object
just the field (not table.field).
-
#initialize(name, active_record_class, delegated_association = nil) ⇒ 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.
- #required?(action = nil) ⇒ 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
- #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
- #text? ⇒ Boolean
- #type_for_attribute ⇒ Object
-
#virtual? ⇒ Boolean
an interpreted property.
Methods included from OrmChecks
active_record?, column_type, columns, columns_hash, content_columns, mongoid?, quoted_table_name, reflect_on_all_associations, table_name, tableless?, type_for_attribute
Methods included from Configurable
#configure, #method_missing, #respond_to_missing?
Constructor Details
#initialize(name, active_record_class, delegated_association = nil) ⇒ Column
instantiation is handled internally through the DataStructures::Columns object
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 362 363 364 365 366 367 368 |
# File 'lib/active_scaffold/data_structures/column.rb', line 313 def initialize(name, active_record_class, delegated_association = nil) #:nodoc: @name = name.to_sym @active_record_class = active_record_class @column = _columns_hash[name.to_s] @delegated_association = delegated_association @cache_key = [@active_record_class.name, name].compact.map(&:to_s).join('#') setup_association_info @link = nil @autolink = association.present? @table = _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.dup if association @select_columns = default_select_columns @text = @column.nil? || [:string, :text, String].include?(column_type) @number = false if @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 :checkbox when :text then :textarea end end end @allow_add_existing = true @form_ui = self.class.association_form_ui if @association && self.class.association_form_ui self.includes = [association.name] if association&.allow_join? if delegated_association self.includes = includes ? [delegated_association.name => includes] : [delegated_association.name] end # default all the configurable variables self.css_class = '' validators_force_require_on = active_record_class.validators_on(name) .map { |val| validator_force_required?(val) } .select(&:present?) self.required = validators_force_require_on.any? { |opt| opt == true } || validators_force_require_on.reject { |opt| opt == true }.flatten.presence self.sort = true self.search_sql = true @weight = estimate_weight 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.
263 264 265 |
# File 'lib/active_scaffold/data_structures/column.rb', line 263 def actions_for_association_links @actions_for_association_links end |
#active_record_class ⇒ Object (readonly) Also known as: model
Returns the value of attribute active_record_class.
8 9 10 |
# File 'lib/active_scaffold/data_structures/column.rb', line 8 def active_record_class @active_record_class end |
#allow_add_existing ⇒ Object
Whether to enable add_existing for this column
29 30 31 |
# File 'lib/active_scaffold/data_structures/column.rb', line 29 def allow_add_existing @allow_add_existing end |
#associated_limit ⇒ Object
Returns the value of attribute associated_limit.
237 238 239 |
# File 'lib/active_scaffold/data_structures/column.rb', line 237 def associated_limit @associated_limit end |
#associated_number=(value) ⇒ Object (writeonly)
Sets the attribute associated_number
242 243 244 |
# File 'lib/active_scaffold/data_structures/column.rb', line 242 def associated_number=(value) @associated_number = value end |
#association ⇒ Object (readonly)
the association from the ActiveRecord class
276 277 278 |
# File 'lib/active_scaffold/data_structures/column.rb', line 276 def association @association end |
#association_join_text ⇒ Object
what string to use to join records from plural associations
248 249 250 |
# File 'lib/active_scaffold/data_structures/column.rb', line 248 def association_join_text @association_join_text end |
#cache_key ⇒ Object (readonly)
cache key to cache column info
310 311 312 |
# File 'lib/active_scaffold/data_structures/column.rb', line 310 def cache_key @cache_key end |
#calculate ⇒ Object
define a calculation for the column. anything that ActiveRecord::Calculations::ClassMethods#calculate accepts will do.
176 177 178 |
# File 'lib/active_scaffold/data_structures/column.rb', line 176 def calculate @calculate end |
#collapsed ⇒ Object
Whether this column set is collapsed by default in contexts where collapsing is supported
26 27 28 |
# File 'lib/active_scaffold/data_structures/column.rb', line 26 def collapsed @collapsed end |
#column ⇒ Object (readonly)
the ConnectionAdapter::*Column object from the ActiveRecord class
273 274 275 |
# File 'lib/active_scaffold/data_structures/column.rb', line 273 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.
64 65 66 |
# File 'lib/active_scaffold/data_structures/column.rb', line 64 def css_class @css_class end |
#delegated_association ⇒ Object (readonly)
the singular association which this column belongs to
279 280 281 |
# File 'lib/active_scaffold/data_structures/column.rb', line 279 def delegated_association @delegated_association end |
#description ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/active_scaffold/data_structures/column.rb', line 49 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
supported options:
* for association columns
* :select - displays a simple <select> or a collection of checkboxes to (dis)associate records
121 122 123 |
# File 'lib/active_scaffold/data_structures/column.rb', line 121 def form_ui @form_ui end |
#includes ⇒ Object
a collection of associations to pre-load when finding the records on a page
184 185 186 |
# File 'lib/active_scaffold/data_structures/column.rb', line 184 def includes @includes end |
#inplace_edit ⇒ Object
Whether to enable inplace editing for this column. Currently works for text columns, in the List.
15 16 17 |
# File 'lib/active_scaffold/data_structures/column.rb', line 15 def inplace_edit @inplace_edit end |
#inplace_edit_update ⇒ Object
:table to refresh list true or :row to refresh row
23 24 25 |
# File 'lib/active_scaffold/data_structures/column.rb', line 23 def inplace_edit_update @inplace_edit_update end |
#label ⇒ Object
43 44 45 |
# File 'lib/active_scaffold/data_structures/column.rb', line 43 def label as_(@label) || active_record_class.human_attribute_name(name.to_s) end |
#list_ui ⇒ Object
125 126 127 |
# File 'lib/active_scaffold/data_structures/column.rb', line 125 def list_ui @list_ui || form_ui end |
#name ⇒ Object (readonly)
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.
12 13 14 |
# File 'lib/active_scaffold/data_structures/column.rb', line 12 def name @name end |
#number=(value) ⇒ Object (writeonly)
Sets the attribute number
286 287 288 |
# File 'lib/active_scaffold/data_structures/column.rb', line 286 def number=(value) @number = value end |
#options ⇒ Object
141 142 143 144 |
# File 'lib/active_scaffold/data_structures/column.rb', line 141 def return @options || NO_OPTIONS if frozen? @options ||= NO_OPTIONS.dup end |
#placeholder ⇒ Object
59 60 61 |
# File 'lib/active_scaffold/data_structures/column.rb', line 59 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
68 69 70 |
# File 'lib/active_scaffold/data_structures/column.rb', line 68 def required=(value) @required = value end |
#search_ui ⇒ Object
135 136 137 |
# File 'lib/active_scaffold/data_structures/column.rb', line 135 def search_ui @search_ui || @form_ui || (:select if association && !association.polymorphic?) 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
207 208 209 |
# File 'lib/active_scaffold/data_structures/column.rb', line 207 def select_associated_columns @select_associated_columns end |
#select_columns ⇒ Object
What columns load from main table
32 33 34 |
# File 'lib/active_scaffold/data_structures/column.rb', line 32 def select_columns @select_columns end |
#send_form_on_update_column ⇒ Object
Returns the value of attribute send_form_on_update_column.
88 89 90 |
# File 'lib/active_scaffold/data_structures/column.rb', line 88 def send_form_on_update_column @send_form_on_update_column end |
#show_blank_record=(value) ⇒ Object (writeonly)
Sets the attribute show_blank_record
253 254 255 |
# File 'lib/active_scaffold/data_structures/column.rb', line 253 def show_blank_record=(value) @show_blank_record = value end |
#show_ui ⇒ Object
130 131 132 |
# File 'lib/active_scaffold/data_structures/column.rb', line 130 def show_ui @show_ui || list_ui end |
#update_columns ⇒ Object
Returns the value of attribute update_columns.
77 78 79 |
# File 'lib/active_scaffold/data_structures/column.rb', line 77 def update_columns @update_columns end |
#weight ⇒ Object
to modify the default order of columns
232 233 234 |
# File 'lib/active_scaffold/data_structures/column.rb', line 232 def weight @weight end |
Instance Method Details
#<=>(other) ⇒ Object
376 377 378 379 |
# File 'lib/active_scaffold/data_structures/column.rb', line 376 def <=>(other) order_weight = weight <=> other.weight order_weight.nonzero? ? order_weight : name.to_s <=> other.name.to_s end |
#==(other) ⇒ Object
this is so that array.delete and array.include?, etc., will work by column name
296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/active_scaffold/data_structures/column.rb', line 296 def ==(other) #:nodoc: # another column if other.respond_to?(:name) && other.class == self.class name == other.name.to_sym elsif other.is_a? Symbol name == other elsif other.is_a? String name.to_s == other # avoid creating new symbols else # unknown eql? other end end |
#associated_number? ⇒ Boolean
243 244 245 |
# File 'lib/active_scaffold/data_structures/column.rb', line 243 def associated_number? @associated_number end |
#autolink? ⇒ Boolean
set an action_link to nested list or inline form in this column
165 166 167 |
# File 'lib/active_scaffold/data_structures/column.rb', line 165 def autolink? @autolink end |
#calculation? ⇒ Boolean
get whether to run a calculation on this column
179 180 181 |
# File 'lib/active_scaffold/data_structures/column.rb', line 179 def calculation? !(@calculate == false || @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
170 171 172 173 |
# File 'lib/active_scaffold/data_structures/column.rb', line 170 def clear_link @link = nil @autolink = false end |
#column_type ⇒ Object
419 420 421 |
# File 'lib/active_scaffold/data_structures/column.rb', line 419 def column_type ActiveScaffold::OrmChecks.column_type active_record_class, name end |
#convert_to_native? ⇒ Boolean
381 382 383 |
# File 'lib/active_scaffold/data_structures/column.rb', line 381 def convert_to_native? number? && [:format] && form_ui != :number end |
#default_for_empty_value ⇒ Object
406 407 408 |
# File 'lib/active_scaffold/data_structures/column.rb', line 406 def default_for_empty_value (column.null ? nil : column.default) if column end |
#field ⇒ Object
the table.field name for this column, if applicable
411 412 413 |
# File 'lib/active_scaffold/data_structures/column.rb', line 411 def field @field ||= quoted_field(field_name) end |
#field_name ⇒ Object
just the field (not table.field)
371 372 373 374 |
# File 'lib/active_scaffold/data_structures/column.rb', line 371 def field_name return nil if virtual? @field_name ||= column ? quoted_field_name(column.name) : association.foreign_key end |
#link ⇒ Object
146 147 148 149 150 |
# File 'lib/active_scaffold/data_structures/column.rb', line 146 def link return @link.call(self) if frozen? && @link.is_a?(Proc) @link = @link.call(self) if @link.is_a? Proc @link end |
#number? ⇒ Boolean
287 288 289 |
# File 'lib/active_scaffold/data_structures/column.rb', line 287 def number? @number end |
#number_to_native(value) ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/active_scaffold/data_structures/column.rb', line 385 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 [: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 ⇒ Object
Any extra parameters this particular column uses. This is for create/update purposes.
35 36 37 38 |
# File 'lib/active_scaffold/data_structures/column.rb', line 35 def params return @params || NO_PARAMS if frozen? @params ||= NO_PARAMS.dup end |
#required?(action = nil) ⇒ Boolean
69 70 71 72 73 74 75 |
# File 'lib/active_scaffold/data_structures/column.rb', line 69 def required?(action = nil) if action && @required @required == true || @required.include?(action) else @required end end |
#search_joins ⇒ Object
a collection of associations to do left join when this column is included on search
194 195 196 |
# File 'lib/active_scaffold/data_structures/column.rb', line 194 def search_joins @search_joins || @includes end |
#search_joins=(value) ⇒ Object
198 199 200 201 202 203 204 |
# File 'lib/active_scaffold/data_structures/column.rb', line 198 def search_joins=(value) @search_joins = case value when Array then value else [value] # automatically convert to an array end end |
#search_sql ⇒ Object
222 223 224 225 |
# File 'lib/active_scaffold/data_structures/column.rb', line 222 def search_sql 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
213 214 215 216 217 218 219 220 |
# File 'lib/active_scaffold/data_structures/column.rb', line 213 def search_sql=(value) @search_sql = if value value == true || value.is_a?(Proc) ? value : Array(value) else value end end |
#searchable? ⇒ Boolean
227 228 229 |
# File 'lib/active_scaffold/data_structures/column.rb', line 227 def searchable? search_sql.present? end |
#set_link(action, options = {}) ⇒ Object
associate an action_link with this column
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/active_scaffold/data_structures/column.rb', line 153 def set_link(action, = {}) if action.is_a?(ActiveScaffold::DataStructures::ActionLink) || (action.is_a? Proc) @link = action else [:label] ||= label [:position] ||= :after unless .key?(:position) [:type] ||= :member @link = ActiveScaffold::DataStructures::ActionLink.new(action, ) end end |
#show_blank_record?(associated) ⇒ Boolean
254 255 256 257 258 |
# File 'lib/active_scaffold/data_structures/column.rb', line 254 def show_blank_record?(associated) return false unless @show_blank_record return false unless association.klass.(:crud_type => :create) && !association.readonly? association.collection? || (association.singular? && associated.blank?) end |
#sort ⇒ Object
104 105 106 107 |
# File 'lib/active_scaffold/data_structures/column.rb', line 104 def sort 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!
95 96 97 98 99 100 101 102 |
# File 'lib/active_scaffold/data_structures/column.rb', line 95 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.
114 115 116 |
# File 'lib/active_scaffold/data_structures/column.rb', line 114 def sort_by() self.sort = end |
#sortable? ⇒ Boolean
109 110 111 |
# File 'lib/active_scaffold/data_structures/column.rb', line 109 def sortable? sort != false && !sort.nil? end |
#text? ⇒ Boolean
291 292 293 |
# File 'lib/active_scaffold/data_structures/column.rb', line 291 def text? @text end |
#type_for_attribute ⇒ Object
415 416 417 |
# File 'lib/active_scaffold/data_structures/column.rb', line 415 def type_for_attribute ActiveScaffold::OrmChecks.type_for_attribute active_record_class, name end |
#virtual? ⇒ Boolean
an interpreted property. the column is virtual if it isn’t from the active record model or any associated models
282 283 284 |
# File 'lib/active_scaffold/data_structures/column.rb', line 282 def virtual? column.nil? && association.nil? end |