Class: ActsAsTable::PrimaryKey

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/acts_as_table/primary_key.rb

Overview

ActsAsTable primary key (value provider).

Instance Attribute Summary collapse

Belongs to collapse

Has many collapse

Instance Attribute Details

#method_nameString

Returns the method name for this ActsAsTable primary key (a Ruby on Rails model column name).

Returns:

  • (String)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/acts_as_table/primary_key.rb', line 8

class PrimaryKey < ::ActiveRecord::Base
  # @!parse
  #   include ActsAsTable::ValueProvider
  #   include ActsAsTable::ValueProvider::InstanceMethods
  #   include ActsAsTable::ValueProviderAssociationMethods

  self.table_name = ActsAsTable.primary_keys_table

  acts_as_table_value_provider

  # Returns the ActsAsTable column model for this ActsAsTable primary key or `nil`.
  #
  # @return [ActsAsTable::ColumnModel, nil]
  belongs_to :column_model, **{
    class_name: 'ActsAsTable::ColumnModel',
    inverse_of: :primary_keys,
    required: false,
  }

  # Returns the ActsAsTable record model for this ActsAsTable primary key.
  belongs_to :record_model, **{
    class_name: 'ActsAsTable::RecordModel',
    inverse_of: :primary_keys,
    required: true,
  }

  # Returns the ActsAsTable values that have been provided by this ActsAsTable primary key.
  has_many :values, **{
    as: :value_provider,
    class_name: 'ActsAsTable::Value',
    dependent: :restrict_with_exception,
    foreign_key: 'value_provider_id',
    foreign_type: 'value_provider_type',
    inverse_of: :value_provider,
  }

  validates :method_name, **{
    presence: true,
  }

  validates :record_model_id, **{
    uniqueness: true,
  }

  validate :record_model_class_must_respond_to_method_name, **{
    if: ::Proc.new { |primary_key| primary_key.method_name.present? },
  }

  protected

  # @param [ActiveRecord::Base, nil] base
  # @return [Object, nil]
  def _get_value(base = nil)
    if base.nil?
      nil
    else
      base.send(:"#{self.method_name}")
    end
  end

  # @param [ActiveRecord::Base, nil] base
  # @param [Object, nil] new_value
  # @return [Array<Object>]
  def _set_value(base = nil, new_value = nil)
    if base.nil?
      [
        nil,
        false,
      ]
    else
      # @return [Object, nil]
      orig_value = _get_value(base)

      [
        orig_value,
        false,
      ]
    end
  end

  private

  # @param [Class] klass
  # @param [String] column_name
  # @return [Boolean]
  def _column?(klass, column_name)
    klass.column_names.include?(column_name.to_s)
  end

  # @return [void]
  def record_model_class_must_respond_to_method_name
    self.record_model.try { |record_model| record_model.class_name.constantize rescue nil }.try { |klass|
      unless _column?(klass, self.method_name)
        self.errors.add('method_name', :required)
      end
    }

    return
  end
end

#record_model_idObject (readonly)



48
49
50
# File 'app/models/acts_as_table/primary_key.rb', line 48

validates :record_model_id, **{
  uniqueness: true,
}

Instance Method Details

#column_modelActsAsTable::ColumnModel?

Returns the ActsAsTable column model for this ActsAsTable primary key or nil.

Returns:



21
22
23
24
25
# File 'app/models/acts_as_table/primary_key.rb', line 21

belongs_to :column_model, **{
  class_name: 'ActsAsTable::ColumnModel',
  inverse_of: :primary_keys,
  required: false,
}

#record_modelActsAsTable::RecordModel

Returns the ActsAsTable record model for this ActsAsTable primary key.



28
29
30
31
32
# File 'app/models/acts_as_table/primary_key.rb', line 28

belongs_to :record_model, **{
  class_name: 'ActsAsTable::RecordModel',
  inverse_of: :primary_keys,
  required: true,
}

#valuesActiveRecord::Relation<ActsAsTable::Value>

Returns the ActsAsTable values that have been provided by this ActsAsTable primary key.

Returns:

See Also:



35
36
37
38
39
40
41
42
# File 'app/models/acts_as_table/primary_key.rb', line 35

has_many :values, **{
  as: :value_provider,
  class_name: 'ActsAsTable::Value',
  dependent: :restrict_with_exception,
  foreign_key: 'value_provider_id',
  foreign_type: 'value_provider_type',
  inverse_of: :value_provider,
}