Class: ActsAsTable::BelongsTo

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

Overview

ActsAsTable singular macro association.

Instance Attribute Summary collapse

Belongs to collapse

Instance Attribute Details

#macroString

Note:

The macro must be either :belongs_to or :has_one.

Returns the symbolic name for the macro for this ActsAsTable singular macro association.

Returns:

  • (String)


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
# File 'app/models/acts_as_table/belongs_to.rb', line 14

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

  self.table_name = ActsAsTable.belongs_tos_table

  # Returns the ActsAsTable row model for this ActsAsTable singular macro association.
  belongs_to :row_model, **{
    class_name: 'ActsAsTable::RowModel',
    inverse_of: :belongs_tos,
    required: true,
  }

  # Returns the source ActsAsTable record model for this ActsAsTable singular macro association.
  belongs_to :source_record_model, **{
    class_name: 'ActsAsTable::RecordModel',
    inverse_of: :belongs_tos_as_source,
    required: true,
  }

  # Returns the target ActsAsTable record model for this ActsAsTable singular macro association.
  belongs_to :target_record_model, **{
    class_name: 'ActsAsTable::RecordModel',
    inverse_of: :belongs_tos_as_target,
    required: true,
  }

  validates :macro, **{
    inclusion: {
      in: ['belongs_to', 'has_one'],
    },
    presence: true,
  }

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

  validate :macro_and_method_name_must_be_valid_association, **{
    if: ::Proc.new { |belongs_to| belongs_to.macro.present? && belongs_to.method_name.present? },
  }

  private

  # @return [void]
  def macro_and_method_name_must_be_valid_association
    self.source_record_model.try { |record_model| record_model.class_name.constantize rescue nil }.try { |source_klass|
      # @return [ActiveRecord::Reflection::MacroReflection]
      reflection = source_klass.reflect_on_association(self.method_name)

      if reflection.nil?
        self.errors.add('method_name', :required)
      elsif self.macro.eql?(reflection.macro.to_s)
        self.target_record_model.try { |record_model| record_model.class_name.constantize rescue nil }.try { |target_klass|
          unless reflection.klass == target_klass
            self.errors.add('target_record_model_id', :invalid)
          end
        }
      else
        self.errors.add('method_name', :invalid)
      end
    }

    return
  end
end

#method_nameString

Returns the method name for this ActsAsTable singular macro association.

Returns:

  • (String)


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
# File 'app/models/acts_as_table/belongs_to.rb', line 14

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

  self.table_name = ActsAsTable.belongs_tos_table

  # Returns the ActsAsTable row model for this ActsAsTable singular macro association.
  belongs_to :row_model, **{
    class_name: 'ActsAsTable::RowModel',
    inverse_of: :belongs_tos,
    required: true,
  }

  # Returns the source ActsAsTable record model for this ActsAsTable singular macro association.
  belongs_to :source_record_model, **{
    class_name: 'ActsAsTable::RecordModel',
    inverse_of: :belongs_tos_as_source,
    required: true,
  }

  # Returns the target ActsAsTable record model for this ActsAsTable singular macro association.
  belongs_to :target_record_model, **{
    class_name: 'ActsAsTable::RecordModel',
    inverse_of: :belongs_tos_as_target,
    required: true,
  }

  validates :macro, **{
    inclusion: {
      in: ['belongs_to', 'has_one'],
    },
    presence: true,
  }

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

  validate :macro_and_method_name_must_be_valid_association, **{
    if: ::Proc.new { |belongs_to| belongs_to.macro.present? && belongs_to.method_name.present? },
  }

  private

  # @return [void]
  def macro_and_method_name_must_be_valid_association
    self.source_record_model.try { |record_model| record_model.class_name.constantize rescue nil }.try { |source_klass|
      # @return [ActiveRecord::Reflection::MacroReflection]
      reflection = source_klass.reflect_on_association(self.method_name)

      if reflection.nil?
        self.errors.add('method_name', :required)
      elsif self.macro.eql?(reflection.macro.to_s)
        self.target_record_model.try { |record_model| record_model.class_name.constantize rescue nil }.try { |target_klass|
          unless reflection.klass == target_klass
            self.errors.add('target_record_model_id', :invalid)
          end
        }
      else
        self.errors.add('method_name', :invalid)
      end
    }

    return
  end
end

Instance Method Details

#row_modelActsAsTable::RowModel

Returns the ActsAsTable row model for this ActsAsTable singular macro association.



22
23
24
25
26
# File 'app/models/acts_as_table/belongs_to.rb', line 22

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

#source_record_modelActsAsTable::RecordModel

Returns the source ActsAsTable record model for this ActsAsTable singular macro association.



29
30
31
32
33
# File 'app/models/acts_as_table/belongs_to.rb', line 29

belongs_to :source_record_model, **{
  class_name: 'ActsAsTable::RecordModel',
  inverse_of: :belongs_tos_as_source,
  required: true,
}

#target_record_modelActsAsTable::RecordModel

Returns the target ActsAsTable record model for this ActsAsTable singular macro association.



36
37
38
39
40
# File 'app/models/acts_as_table/belongs_to.rb', line 36

belongs_to :target_record_model, **{
  class_name: 'ActsAsTable::RecordModel',
  inverse_of: :belongs_tos_as_target,
  required: true,
}