Class: ActsAsTable::RecordModel

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

Overview

ActsAsTable record model (value provider).

Instance Attribute Summary collapse

Belongs to collapse

Has many collapse

Instance Method Summary collapse

Instance Attribute Details

#class_nameString

Returns the ActiveRecord model class name for this ActsAsTable record model.

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/models/acts_as_table/record_model.rb', line 8

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

  self.table_name = ActsAsTable.record_models_table

  acts_as_table_value_provider

  # Returns the ActsAsTable row model for this ActsAsTable record model.
  belongs_to :row_model, **{
    class_name: 'ActsAsTable::RowModel',
    inverse_of: :record_models,
    required: true,
  }

  # Returns the ActsAsTable singular macro associations where this ActsAsTable record model is the source of the association.
  has_many :belongs_tos_as_source, **{
    autosave: true,
    class_name: 'ActsAsTable::BelongsTo',
    dependent: :destroy,
    foreign_key: 'source_record_model_id',
    inverse_of: :source_record_model,
    validate: true,
  }

  # Returns the ActsAsTable singular macro associations where this ActsAsTable record model is the target of the association.
  has_many :belongs_tos_as_target, **{
    autosave: true,
    class_name: 'ActsAsTable::BelongsTo',
    dependent: :destroy,
    foreign_key: 'target_record_model_id',
    inverse_of: :target_record_model,
    validate: true,
  }

  # Returns the ActsAsTable foreign keys for this ActsAsTable record model.
  has_many :foreign_keys, **{
    autosave: true,
    class_name: 'ActsAsTable::ForeignKey',
    dependent: :destroy,
    foreign_key: 'record_model_id',
    inverse_of: :record_model,
    validate: true,
  }

  # Returns the ActsAsTable collection macro associations where this ActsAsTable record model is the source of the association.
  has_many :has_manies_as_source, **{
    autosave: true,
    class_name: 'ActsAsTable::HasMany',
    dependent: :destroy,
    foreign_key: 'source_record_model_id',
    inverse_of: :source_record_model,
    validate: true,
  }

  # Returns the ActsAsTable collection macro associations where this ActsAsTable record model is the target of the association.
  has_many :has_manies_as_target, -> { readonly }, **{
    source: :has_many,
    through: :has_many_targets,
  }

  # Returns the ActsAsTable collection macro association targets for this ActsAsTable record model.
  has_many :has_many_targets, **{
    autosave: true,
    class_name: 'ActsAsTable::HasManyTarget',
    dependent: :destroy,
    foreign_key: 'record_model_id',
    inverse_of: :record_model,
    validate: true,
  }

  # Returns the ActsAsTable attribute accessors for this ActsAsTable record model.
  has_many :lenses, **{
    autosave: true,
    class_name: 'ActsAsTable::Lense',
    dependent: :destroy,
    foreign_key: 'record_model_id',
    inverse_of: :record_model,
    validate: true,
  }

  # Returns the ActsAsTable primary keys for this ActsAsTable record model.
  has_many :primary_keys, **{
    autosave: true,
    class_name: 'ActsAsTable::PrimaryKey',
    dependent: :destroy,
    foreign_key: 'record_model_id',
    inverse_of: :record_model,
    validate: true,
  }

  # Returns the ActsAsTable records that have been provided by this ActsAsTable record model.
  has_many :records, **{
    class_name: 'ActsAsTable::Record',
    dependent: :restrict_with_exception,
    foreign_key: 'record_model_id',
    inverse_of: :record_model,
  }

  # Returns the ActsAsTable row models where this ActsAsTable record model is the root.
  has_many :row_models_as_root, **{
    class_name: 'ActsAsTable::RowModel',
    dependent: :restrict_with_exception,
    foreign_key: 'root_record_model_id',
    inverse_of: :root_record_model,
  }

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

  validate :base_must_be_reachable

  validate :class_name_must_constantize, **{
    if: ::Proc.new { |record_model| record_model.class_name.present? },
  }

  # Get the value for the given record using the given options.
  #
  # @param [ActiveRecord::Base, nil] base
  # @param [Hash<Symbol, Object>] options
  # @option options [Boolean] :default
  # @return [ActsAsTable::ValueProvider::WrappedValue]
  def get_value(base = nil, **options)
    unless base.nil? || self.class_name.eql?(base.class.name)
      raise ::ArgumentError.new("record - invalid class - expected: #{self.class_name.inspect}, found: #{base.inspect}")
    end

    # @return [Hash<ActsAsTable::ValueProvider::InstanceMethods, Object>]
    value_by_value_provider = self.each_acts_as_table_value_provider(nil, except: [:row_model, :row_models_as_root]).inject({}) { |acc, value_provider|
      acc[value_provider] ||= base.nil? ? ActsAsTable.adapter.wrap_value_for(value_provider, base, nil, nil) : ActsAsTable.adapter.get_value_for(value_provider, base, **options)
      acc
    }

    ActsAsTable.adapter.wrap_value_for(self, base, nil, value_by_value_provider)
  end

  # Set the new value for the given record using the given options.
  #
  # @param [ActiveRecord::Base, nil] base
  # @param [Hash<ActsAsTable::ValueProvider::InstanceMethods, Object>] new_value_by_value_provider
  # @param [Hash<Symbol, Object>] options
  # @option options [Boolean] :default
  # @return [ActsAsTable::ValueProvider::WrappedValue]
  def set_value(base = nil, new_value_by_value_provider = {}, **options)
    unless base.nil? || self.class_name.eql?(base.class.name)
      raise ::ArgumentError.new("record - invalid class - expected: #{self.class_name.inspect}, found: #{base.inspect}")
    end

    # @return [Array<Object>]
    value_by_value_provider, changed = *self.each_acts_as_table_value_provider(nil, except: [:row_model, :row_models_as_root]).inject([{}, false]) { |acc, value_provider|
      # @return [Object, nil]
      new_value = new_value_by_value_provider.try(:[], value_provider)

      acc[0][value_provider] ||= base.nil? ? ActsAsTable.adapter.wrap_value_for(value_provider, base, nil, nil) : ActsAsTable.adapter.set_value_for(value_provider, base, new_value, **options)
      acc[1] ||= acc[0][value_provider].changed?
      acc
    }

    ActsAsTable.adapter.wrap_value_for(self, base, nil, value_by_value_provider, changed: changed)
  end

  private

  # @return [void]
  def base_must_be_reachable
    if self.row_model.try(:reachable_record_model?, self) == false
      self.errors.add(:base, :unreachable)
    end

    return
  end

  # @return [void]
  def class_name_must_constantize
    begin
      self.class_name.constantize
    rescue ::NameError
      self.errors.add('class_name', :invalid)
    end

    return
  end
end

Instance Method Details

#belongs_tos_as_sourceActiveRecord::Relation<ActsAsTable::BelongsTo>

Returns the ActsAsTable singular macro associations where this ActsAsTable record model is the source of the association.

Returns:

See Also:



26
27
28
29
30
31
32
33
# File 'app/models/acts_as_table/record_model.rb', line 26

has_many :belongs_tos_as_source, **{
  autosave: true,
  class_name: 'ActsAsTable::BelongsTo',
  dependent: :destroy,
  foreign_key: 'source_record_model_id',
  inverse_of: :source_record_model,
  validate: true,
}

#belongs_tos_as_targetActiveRecord::Relation<ActsAsTable::BelongsTo>

Returns the ActsAsTable singular macro associations where this ActsAsTable record model is the target of the association.

Returns:

See Also:



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

has_many :belongs_tos_as_target, **{
  autosave: true,
  class_name: 'ActsAsTable::BelongsTo',
  dependent: :destroy,
  foreign_key: 'target_record_model_id',
  inverse_of: :target_record_model,
  validate: true,
}

#foreign_keysActiveRecord::Relation<ActsAsTable::ForeignKey>

Returns the ActsAsTable foreign keys for this ActsAsTable record model.

Returns:

See Also:



46
47
48
49
50
51
52
53
# File 'app/models/acts_as_table/record_model.rb', line 46

has_many :foreign_keys, **{
  autosave: true,
  class_name: 'ActsAsTable::ForeignKey',
  dependent: :destroy,
  foreign_key: 'record_model_id',
  inverse_of: :record_model,
  validate: true,
}

#get_value(base = nil, **options) ⇒ ActsAsTable::ValueProvider::WrappedValue

Get the value for the given record using the given options.

Parameters:

  • base (ActiveRecord::Base, nil) (defaults to: nil)
  • options (Hash<Symbol, Object>)

Options Hash (**options):

  • :default (Boolean)

Returns:



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/acts_as_table/record_model.rb', line 133

def get_value(base = nil, **options)
  unless base.nil? || self.class_name.eql?(base.class.name)
    raise ::ArgumentError.new("record - invalid class - expected: #{self.class_name.inspect}, found: #{base.inspect}")
  end

  # @return [Hash<ActsAsTable::ValueProvider::InstanceMethods, Object>]
  value_by_value_provider = self.each_acts_as_table_value_provider(nil, except: [:row_model, :row_models_as_root]).inject({}) { |acc, value_provider|
    acc[value_provider] ||= base.nil? ? ActsAsTable.adapter.wrap_value_for(value_provider, base, nil, nil) : ActsAsTable.adapter.get_value_for(value_provider, base, **options)
    acc
  }

  ActsAsTable.adapter.wrap_value_for(self, base, nil, value_by_value_provider)
end

#has_manies_as_sourceActiveRecord::Relation<ActsAsTable::HasMany>

Returns the ActsAsTable collection macro associations where this ActsAsTable record model is the source of the association.

Returns:

See Also:



56
57
58
59
60
61
62
63
# File 'app/models/acts_as_table/record_model.rb', line 56

has_many :has_manies_as_source, **{
  autosave: true,
  class_name: 'ActsAsTable::HasMany',
  dependent: :destroy,
  foreign_key: 'source_record_model_id',
  inverse_of: :source_record_model,
  validate: true,
}

#has_manies_as_targetActiveRecord::Relation<HasManiesAsTarget>

Returns the ActsAsTable collection macro associations where this ActsAsTable record model is the target of the association.

Returns:

  • (ActiveRecord::Relation<HasManiesAsTarget>)

See Also:



66
67
68
69
# File 'app/models/acts_as_table/record_model.rb', line 66

has_many :has_manies_as_target, -> { readonly }, **{
  source: :has_many,
  through: :has_many_targets,
}

#has_many_targetsActiveRecord::Relation<ActsAsTable::HasManyTarget>

Returns the ActsAsTable collection macro association targets for this ActsAsTable record model.

Returns:

See Also:



72
73
74
75
76
77
78
79
# File 'app/models/acts_as_table/record_model.rb', line 72

has_many :has_many_targets, **{
  autosave: true,
  class_name: 'ActsAsTable::HasManyTarget',
  dependent: :destroy,
  foreign_key: 'record_model_id',
  inverse_of: :record_model,
  validate: true,
}

#lensesActiveRecord::Relation<ActsAsTable::Lense>

Returns the ActsAsTable attribute accessors for this ActsAsTable record model.

Returns:

See Also:



82
83
84
85
86
87
88
89
# File 'app/models/acts_as_table/record_model.rb', line 82

has_many :lenses, **{
  autosave: true,
  class_name: 'ActsAsTable::Lense',
  dependent: :destroy,
  foreign_key: 'record_model_id',
  inverse_of: :record_model,
  validate: true,
}

#primary_keysActiveRecord::Relation<ActsAsTable::PrimaryKey>

Returns the ActsAsTable primary keys for this ActsAsTable record model.

Returns:

See Also:



92
93
94
95
96
97
98
99
# File 'app/models/acts_as_table/record_model.rb', line 92

has_many :primary_keys, **{
  autosave: true,
  class_name: 'ActsAsTable::PrimaryKey',
  dependent: :destroy,
  foreign_key: 'record_model_id',
  inverse_of: :record_model,
  validate: true,
}

#recordsActiveRecord::Relation<ActsAsTable::Record>

Returns the ActsAsTable records that have been provided by this ActsAsTable record model.

Returns:

See Also:



102
103
104
105
106
107
# File 'app/models/acts_as_table/record_model.rb', line 102

has_many :records, **{
  class_name: 'ActsAsTable::Record',
  dependent: :restrict_with_exception,
  foreign_key: 'record_model_id',
  inverse_of: :record_model,
}

#row_modelActsAsTable::RowModel

Returns the ActsAsTable row model for this ActsAsTable record model.



19
20
21
22
23
# File 'app/models/acts_as_table/record_model.rb', line 19

belongs_to :row_model, **{
  class_name: 'ActsAsTable::RowModel',
  inverse_of: :record_models,
  required: true,
}

#row_models_as_rootActiveRecord::Relation<ActsAsTable::RowModel>

Returns the ActsAsTable row models where this ActsAsTable record model is the root.

Returns:

See Also:



110
111
112
113
114
115
# File 'app/models/acts_as_table/record_model.rb', line 110

has_many :row_models_as_root, **{
  class_name: 'ActsAsTable::RowModel',
  dependent: :restrict_with_exception,
  foreign_key: 'root_record_model_id',
  inverse_of: :root_record_model,
}

#set_value(base = nil, new_value_by_value_provider = {}, **options) ⇒ ActsAsTable::ValueProvider::WrappedValue

Set the new value for the given record using the given options.

Parameters:

Options Hash (**options):

  • :default (Boolean)

Returns:



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/models/acts_as_table/record_model.rb', line 154

def set_value(base = nil, new_value_by_value_provider = {}, **options)
  unless base.nil? || self.class_name.eql?(base.class.name)
    raise ::ArgumentError.new("record - invalid class - expected: #{self.class_name.inspect}, found: #{base.inspect}")
  end

  # @return [Array<Object>]
  value_by_value_provider, changed = *self.each_acts_as_table_value_provider(nil, except: [:row_model, :row_models_as_root]).inject([{}, false]) { |acc, value_provider|
    # @return [Object, nil]
    new_value = new_value_by_value_provider.try(:[], value_provider)

    acc[0][value_provider] ||= base.nil? ? ActsAsTable.adapter.wrap_value_for(value_provider, base, nil, nil) : ActsAsTable.adapter.set_value_for(value_provider, base, new_value, **options)
    acc[1] ||= acc[0][value_provider].changed?
    acc
  }

  ActsAsTable.adapter.wrap_value_for(self, base, nil, value_by_value_provider, changed: changed)
end