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



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
562
563
564
# File 'lib/active_scaffold/data_structures/column.rb', line 517

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.



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

def active_record_class
  @active_record_class
end

#associationObject (readonly)

the association from the ActiveRecord class



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

def association
  @association
end

#cache_keyObject (readonly)

cache key to cache column info



514
515
516
# File 'lib/active_scaffold/data_structures/column.rb', line 514

def cache_key
  @cache_key
end

#columnObject (readonly)

the ConnectionAdapter::*Column object from the ActiveRecord class



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

def column
  @column
end

#delegated_associationObject (readonly)

the singular association which this column belongs to



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

def delegated_association
  @delegated_association
end

#grouped_selectObject



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

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.



419
420
421
# File 'lib/active_scaffold/data_structures/column.rb', line 419

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object

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



500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/active_scaffold/data_structures/column.rb', line 500

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)


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

def autolink?
  @autolink
end

#cast(value) ⇒ Object



624
625
626
# File 'lib/active_scaffold/data_structures/column.rb', line 624

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

#column_typeObject



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

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

#default_for_empty_valueObject



573
574
575
576
577
578
579
580
581
582
583
# File 'lib/active_scaffold/data_structures/column.rb', line 573

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



430
431
432
# File 'lib/active_scaffold/data_structures/column.rb', line 430

def default_value
  @default_value || @db_default_value
end

#default_value=(value) ⇒ Object

Raises:

  • (ArgumentError)


434
435
436
437
438
# File 'lib/active_scaffold/data_structures/column.rb', line 434

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)


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

def default_value?
  defined? @default_value
end

#fieldObject

the table.field name for this column, if applicable



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

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

#field_nameObject

just the field (not table.field)



567
568
569
570
571
# File 'lib/active_scaffold/data_structures/column.rb', line 567

def field_name
  return nil if virtual?

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

#group_byObject



602
603
604
# File 'lib/active_scaffold/data_structures/column.rb', line 602

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

#group_by=(value) ⇒ Object



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

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

#null?Boolean

Returns:

  • (Boolean)


585
586
587
588
589
590
591
# File 'lib/active_scaffold/data_structures/column.rb', line 585

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

#optionsObject



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

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.



424
425
426
427
428
# File 'lib/active_scaffold/data_structures/column.rb', line 424

def params
  return @params || NO_PARAMS if frozen?

  @params ||= NO_PARAMS.dup
end

#quoted_foreign_typeObject



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

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

#text?Boolean

Returns:

  • (Boolean)


495
496
497
# File 'lib/active_scaffold/data_structures/column.rb', line 495

def text?
  @text
end

#type_for_attributeObject



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

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)


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

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