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, #sort_joins, #sort_joins=, #sortable?, #subform_includes=, #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



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/active_scaffold/data_structures/column.rb', line 514

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
  @disable_on_update_column = true
  @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
  self.subform_includes = true if association

  # 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
  self.logical_search = [name] unless virtual? || association || tableless?

  @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.



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

def active_record_class
  @active_record_class
end

#associationObject (readonly)

the association from the ActiveRecord class



482
483
484
# File 'lib/active_scaffold/data_structures/column.rb', line 482

def association
  @association
end

#cache_keyObject (readonly)

cache key to cache column info



511
512
513
# File 'lib/active_scaffold/data_structures/column.rb', line 511

def cache_key
  @cache_key
end

#columnObject (readonly)

the ConnectionAdapter::*Column object from the ActiveRecord class



479
480
481
# File 'lib/active_scaffold/data_structures/column.rb', line 479

def column
  @column
end

#delegated_associationObject (readonly)

the singular association which this column belongs to



485
486
487
# File 'lib/active_scaffold/data_structures/column.rb', line 485

def delegated_association
  @delegated_association
end

#grouped_selectObject



605
606
607
# File 'lib/active_scaffold/data_structures/column.rb', line 605

def grouped_select
  Arel.sql(@grouped_select&.to_s || field)
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.



416
417
418
# File 'lib/active_scaffold/data_structures/column.rb', line 416

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object

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



497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/active_scaffold/data_structures/column.rb', line 497

def ==(other) # :nodoc:
  # another column
  if other.respond_to?(:name) && (other.class == self.class || other.class == ProxyColumn)
    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)


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

def autolink?
  @autolink
end

#cast(value) ⇒ Object



621
622
623
# File 'lib/active_scaffold/data_structures/column.rb', line 621

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

#column_typeObject



617
618
619
# File 'lib/active_scaffold/data_structures/column.rb', line 617

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

#default_for_empty_valueObject



570
571
572
573
574
575
576
577
578
579
580
# File 'lib/active_scaffold/data_structures/column.rb', line 570

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



427
428
429
# File 'lib/active_scaffold/data_structures/column.rb', line 427

def default_value
  @default_value || @db_default_value
end

#default_value=(value) ⇒ Object

Raises:

  • (ArgumentError)


431
432
433
434
435
# File 'lib/active_scaffold/data_structures/column.rb', line 431

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)


437
438
439
# File 'lib/active_scaffold/data_structures/column.rb', line 437

def default_value?
  defined? @default_value
end

#fieldObject

the table.field name for this column, if applicable



591
592
593
# File 'lib/active_scaffold/data_structures/column.rb', line 591

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

#field_nameObject

just the field (not table.field)



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

def field_name
  return nil if virtual?

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

#group_byObject



599
600
601
# File 'lib/active_scaffold/data_structures/column.rb', line 599

def group_by
  @group_by || select_columns || [field]
end

#group_by=(value) ⇒ Object



595
596
597
# File 'lib/active_scaffold/data_structures/column.rb', line 595

def group_by=(value)
  @group_by = value ? Array(value) : nil
end

#null?Boolean

Returns:

  • (Boolean)


582
583
584
585
586
587
588
# File 'lib/active_scaffold/data_structures/column.rb', line 582

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

#optionsObject



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

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.



421
422
423
424
425
# File 'lib/active_scaffold/data_structures/column.rb', line 421

def params
  return @params || NO_PARAMS if frozen?

  @params ||= NO_PARAMS.dup
end

#quoted_foreign_typeObject



609
610
611
# File 'lib/active_scaffold/data_structures/column.rb', line 609

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

#text?Boolean

Returns:

  • (Boolean)


492
493
494
# File 'lib/active_scaffold/data_structures/column.rb', line 492

def text?
  @text
end

#type_for_attributeObject



613
614
615
# File 'lib/active_scaffold/data_structures/column.rb', line 613

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)


488
489
490
# File 'lib/active_scaffold/data_structures/column.rb', line 488

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