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



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
521
522
523
524
525
526
527
# File 'lib/active_scaffold/data_structures/column.rb', line 482

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

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



380
381
382
# File 'lib/active_scaffold/data_structures/column.rb', line 380

def active_record_class
  @active_record_class
end

#associationObject (readonly)

the association from the ActiveRecord class



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

def association
  @association
end

#cache_keyObject (readonly)

cache key to cache column info



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

def cache_key
  @cache_key
end

#columnObject (readonly)

the ConnectionAdapter::*Column object from the ActiveRecord class



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

def column
  @column
end

#delegated_associationObject (readonly)

the singular association which this column belongs to



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

def delegated_association
  @delegated_association
end

#grouped_selectObject



571
572
573
# File 'lib/active_scaffold/data_structures/column.rb', line 571

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.



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

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object

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



465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/active_scaffold/data_structures/column.rb', line 465

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)


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

def autolink?
  @autolink
end

#cast(value) ⇒ Object



587
588
589
# File 'lib/active_scaffold/data_structures/column.rb', line 587

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

#column_typeObject



583
584
585
# File 'lib/active_scaffold/data_structures/column.rb', line 583

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

#default_for_empty_valueObject



536
537
538
539
540
541
542
543
544
545
546
# File 'lib/active_scaffold/data_structures/column.rb', line 536

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



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

def default_value
  @default_value || @db_default_value
end

#default_value=(value) ⇒ Object

Raises:

  • (ArgumentError)


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

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)


405
406
407
# File 'lib/active_scaffold/data_structures/column.rb', line 405

def default_value?
  defined? @default_value
end

#fieldObject

the table.field name for this column, if applicable



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

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

#field_nameObject

just the field (not table.field)



530
531
532
533
534
# File 'lib/active_scaffold/data_structures/column.rb', line 530

def field_name
  return nil if virtual?

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

#group_byObject



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

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

#group_by=(value) ⇒ Object



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

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

#null?Boolean

Returns:

  • (Boolean)


548
549
550
551
552
553
554
# File 'lib/active_scaffold/data_structures/column.rb', line 548

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

#optionsObject



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

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.



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

def params
  return @params || NO_PARAMS if frozen?

  @params ||= NO_PARAMS.dup
end

#quoted_foreign_typeObject



575
576
577
# File 'lib/active_scaffold/data_structures/column.rb', line 575

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

#text?Boolean

Returns:

  • (Boolean)


460
461
462
# File 'lib/active_scaffold/data_structures/column.rb', line 460

def text?
  @text
end

#type_for_attributeObject



579
580
581
# File 'lib/active_scaffold/data_structures/column.rb', line 579

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)


456
457
458
# File 'lib/active_scaffold/data_structures/column.rb', line 456

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