Class: ActiveScaffold::DataStructures::Column

Inherits:
Object
  • Object
show all
Includes:
Configurable, ProxyableMethods, OrmChecks
Defined in:
lib/active_scaffold/data_structures/column.rb

Defined Under Namespace

Modules: ProxyableMethods

Constant Summary collapse

NO_PARAMS =
Set.new.freeze
NO_OPTIONS =
{}.freeze
@@associated_limit =
3
@@associated_number =
true
@@show_blank_record =
true
%i[new edit show]
@@association_form_ui =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ProxyableMethods

#<=>, #associated_number?, #attributes=, #cache_count?, #calculation?, #clear_link, #convert_to_native?, #description, #form_ui=, #includes=, #inplace_edit=, #label, #link, #list_ui, #list_ui=, #list_ui_options, #number?, #number_to_native, #placeholder, #required?, #search_joins, #search_joins=, #search_sql, #search_sql=, #search_ui, #search_ui=, #search_ui_options, #searchable?, #set_link, #show_blank_record?, #show_ui, #show_ui=, #show_ui_options, #sort, #sort=, #sort_by, #sortable?, #update_columns=

Methods included from OrmChecks

active_record?, cast, column_type, columns, columns_hash, content_columns, default_value, 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



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/active_scaffold/data_structures/column.rb', line 476

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]
  if @column.nil? && active_record? && active_record_class._default_attributes.key?(name.to_s)
    @column = active_record_class._default_attributes[name.to_s]
  end
  @db_default_value = ActiveScaffold::OrmChecks.default_value active_record_class, name if @column
  @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, :citext, String].include?(column_type)
  @number = false
  setup_defaults_for_column if @column
  @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) }
                                  .compact_blank
  self.required = validators_force_require_on.any?(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

#active_record_classObject (readonly) Also known as: model

Returns the value of attribute active_record_class.



374
375
376
# File 'lib/active_scaffold/data_structures/column.rb', line 374

def active_record_class
  @active_record_class
end

#associationObject (readonly)

the association from the ActiveRecord class



444
445
446
# File 'lib/active_scaffold/data_structures/column.rb', line 444

def association
  @association
end

#cache_keyObject (readonly)

cache key to cache column info



473
474
475
# File 'lib/active_scaffold/data_structures/column.rb', line 473

def cache_key
  @cache_key
end

#columnObject (readonly)

the ConnectionAdapter::*Column object from the ActiveRecord class



441
442
443
# File 'lib/active_scaffold/data_structures/column.rb', line 441

def column
  @column
end

#delegated_associationObject (readonly)

the singular association which this column belongs to



447
448
449
# File 'lib/active_scaffold/data_structures/column.rb', line 447

def delegated_association
  @delegated_association
end

#nameObject (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.



378
379
380
# File 'lib/active_scaffold/data_structures/column.rb', line 378

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object

this is so that array.delete and array.include?, etc., will work by column name



459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/active_scaffold/data_structures/column.rb', line 459

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

#autolink?Boolean

set an action_link to nested list or inline form in this column

Returns:

  • (Boolean)


413
414
415
# File 'lib/active_scaffold/data_structures/column.rb', line 413

def autolink?
  @autolink
end

#cast(value) ⇒ Object



566
567
568
# File 'lib/active_scaffold/data_structures/column.rb', line 566

def cast(value)
  ActiveScaffold::OrmChecks.cast active_record_class, name, value
end

#column_typeObject



562
563
564
# File 'lib/active_scaffold/data_structures/column.rb', line 562

def column_type
  ActiveScaffold::OrmChecks.column_type active_record_class, name
end

#default_for_empty_valueObject



529
530
531
532
533
534
535
536
537
538
539
# File 'lib/active_scaffold/data_structures/column.rb', line 529

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

#default_valueObject



389
390
391
# File 'lib/active_scaffold/data_structures/column.rb', line 389

def default_value
  @default_value || @db_default_value
end

#default_value=(value) ⇒ Object

Raises:

  • (ArgumentError)


393
394
395
396
397
# File 'lib/active_scaffold/data_structures/column.rb', line 393

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

#default_value?Boolean

Returns:

  • (Boolean)


399
400
401
# File 'lib/active_scaffold/data_structures/column.rb', line 399

def default_value?
  defined? @default_value
end

#fieldObject

the table.field name for this column, if applicable



550
551
552
# File 'lib/active_scaffold/data_structures/column.rb', line 550

def field
  @field ||= quoted_field(field_name)
end

#field_nameObject

just the field (not table.field)



523
524
525
526
527
# File 'lib/active_scaffold/data_structures/column.rb', line 523

def field_name
  return nil if virtual?

  @field_name ||= column ? quoted_field_name(column.name) : quoted_field_name(association.foreign_key)
end

#null?Boolean

Returns:

  • (Boolean)


541
542
543
544
545
546
547
# File 'lib/active_scaffold/data_structures/column.rb', line 541

def null?
  if active_record? && !column.is_a?(ActiveModel::Attribute)
    column&.null
  else
    true
  end
end

#optionsObject



406
407
408
409
410
# File 'lib/active_scaffold/data_structures/column.rb', line 406

def options
  return @options || NO_OPTIONS if frozen?

  @options ||= NO_OPTIONS.dup
end

#paramsObject

Any extra parameters this particular column uses. This is for create/update purposes.



383
384
385
386
387
# File 'lib/active_scaffold/data_structures/column.rb', line 383

def params
  return @params || NO_PARAMS if frozen?

  @params ||= NO_PARAMS.dup
end

#quoted_foreign_typeObject



554
555
556
# File 'lib/active_scaffold/data_structures/column.rb', line 554

def quoted_foreign_type
  quoted_field(quoted_field_name(association.foreign_type))
end

#text?Boolean

Returns:

  • (Boolean)


454
455
456
# File 'lib/active_scaffold/data_structures/column.rb', line 454

def text?
  @text
end

#type_for_attributeObject



558
559
560
# File 'lib/active_scaffold/data_structures/column.rb', line 558

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

Returns:

  • (Boolean)


450
451
452
# File 'lib/active_scaffold/data_structures/column.rb', line 450

def virtual?
  column.nil? && association.nil?
end