Class: ActsAsTable::Record
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ActsAsTable::Record
- Defined in:
- app/models/acts_as_table/record.rb
Overview
ActsAsTable record.
Instance Attribute Summary collapse
-
#position ⇒ Integer
Returns the position of this ActsAsTable record.
-
#record_errors_count ⇒ Integer
readonly
Returns the number of ActsAsTable record errors for this ActsAsTable record.
-
#record_model_changed ⇒ Boolean
Returns
true
if the ActsAsTable record model changed the record for this ActsAsTable record. -
#values_count ⇒ Integer
readonly
Returns the number of ActsAsTable values for this ActsAsTable record.
Belongs to collapse
-
#base ⇒ ActiveRecord::Base?
Returns the record for this ActsAsTable record or
nil
. -
#record_model ⇒ ActsAsTable::RecordModel
Returns the ActsAsTable record model for this ActsAsTable record.
-
#table ⇒ ActsAsTable::Table
Returns the ActsAsTable table for this ActsAsTable record.
Has many collapse
-
#record_errors ⇒ ActiveRecord::Relation<ActsAsTable::RecordError>
Returns the ActsAsTable record errors for this ActsAsTable record.
-
#values ⇒ ActiveRecord::Relation<ActsAsTable::Value>
Returns the ActsAsTable values for this ActsAsTable record.
Instance Attribute Details
#position ⇒ Integer
Returns the position of this ActsAsTable record.
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 |
# File 'app/models/acts_as_table/record.rb', line 20 class Record < ::ActiveRecord::Base # @!parse # include ActsAsTable::ValueProvider # include ActsAsTable::ValueProviderAssociationMethods self.table_name = ActsAsTable.records_table # Returns the record for this ActsAsTable record or `nil`. # # @return [ActiveRecord::Base, nil] belongs_to :base, **{ polymorphic: true, required: false, } # Returns the ActsAsTable record model for this ActsAsTable record. belongs_to :record_model, **{ class_name: 'ActsAsTable::RecordModel', inverse_of: :records, required: true, } # Returns the ActsAsTable table for this ActsAsTable record. belongs_to :table, **{ class_name: 'ActsAsTable::Table', counter_cache: 'records_count', inverse_of: :records, required: true, } # Returns the ActsAsTable record errors for this ActsAsTable record. has_many :record_errors, -> { order(attribute_name: :asc, message: :asc) }, **{ autosave: true, class_name: 'ActsAsTable::RecordError', dependent: :destroy, foreign_key: 'record_id', inverse_of: :record, validate: true, } # Returns the ActsAsTable values for this ActsAsTable record. has_many :values, -> { order(position: :asc) }, **{ autosave: true, class_name: 'ActsAsTable::Value', dependent: :destroy, foreign_key: 'record_id', inverse_of: :record, validate: true, } validates :position, **{ numericality: { greater_than_or_equal_to: 1, only_integer: true, }, presence: true, uniqueness: { scope: ['table_id', 'record_model_id'], }, } # validates :record_model_changed, **{} validate :base_must_be_valid_class, :record_model_must_belong_to_row_model_for_table, **{ if: ::Proc.new { |record| record.record_model.present? }, } private # @return [void] def base_must_be_valid_class self.base_type.try { |base_type| unless base_type.nil? || base_type.eql?(self.record_model.class_name) self.errors.add('base_type', :invalid) end } end # @return [void] def record_model_must_belong_to_row_model_for_table self.table.try(:row_model).try(:record_models).try { |record_models| unless record_models.include?(self.record_model) self.errors.add('record_model_id', :invalid) end } return end end |
#record_errors_count ⇒ Integer (readonly)
Returns the number of ActsAsTable record errors for this ActsAsTable record.
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 |
# File 'app/models/acts_as_table/record.rb', line 20 class Record < ::ActiveRecord::Base # @!parse # include ActsAsTable::ValueProvider # include ActsAsTable::ValueProviderAssociationMethods self.table_name = ActsAsTable.records_table # Returns the record for this ActsAsTable record or `nil`. # # @return [ActiveRecord::Base, nil] belongs_to :base, **{ polymorphic: true, required: false, } # Returns the ActsAsTable record model for this ActsAsTable record. belongs_to :record_model, **{ class_name: 'ActsAsTable::RecordModel', inverse_of: :records, required: true, } # Returns the ActsAsTable table for this ActsAsTable record. belongs_to :table, **{ class_name: 'ActsAsTable::Table', counter_cache: 'records_count', inverse_of: :records, required: true, } # Returns the ActsAsTable record errors for this ActsAsTable record. has_many :record_errors, -> { order(attribute_name: :asc, message: :asc) }, **{ autosave: true, class_name: 'ActsAsTable::RecordError', dependent: :destroy, foreign_key: 'record_id', inverse_of: :record, validate: true, } # Returns the ActsAsTable values for this ActsAsTable record. has_many :values, -> { order(position: :asc) }, **{ autosave: true, class_name: 'ActsAsTable::Value', dependent: :destroy, foreign_key: 'record_id', inverse_of: :record, validate: true, } validates :position, **{ numericality: { greater_than_or_equal_to: 1, only_integer: true, }, presence: true, uniqueness: { scope: ['table_id', 'record_model_id'], }, } # validates :record_model_changed, **{} validate :base_must_be_valid_class, :record_model_must_belong_to_row_model_for_table, **{ if: ::Proc.new { |record| record.record_model.present? }, } private # @return [void] def base_must_be_valid_class self.base_type.try { |base_type| unless base_type.nil? || base_type.eql?(self.record_model.class_name) self.errors.add('base_type', :invalid) end } end # @return [void] def record_model_must_belong_to_row_model_for_table self.table.try(:row_model).try(:record_models).try { |record_models| unless record_models.include?(self.record_model) self.errors.add('record_model_id', :invalid) end } return end end |
#record_model_changed ⇒ Boolean
Returns true
if the ActsAsTable record model changed the record for this ActsAsTable record. Otherwise, returns false
.
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 |
# File 'app/models/acts_as_table/record.rb', line 20 class Record < ::ActiveRecord::Base # @!parse # include ActsAsTable::ValueProvider # include ActsAsTable::ValueProviderAssociationMethods self.table_name = ActsAsTable.records_table # Returns the record for this ActsAsTable record or `nil`. # # @return [ActiveRecord::Base, nil] belongs_to :base, **{ polymorphic: true, required: false, } # Returns the ActsAsTable record model for this ActsAsTable record. belongs_to :record_model, **{ class_name: 'ActsAsTable::RecordModel', inverse_of: :records, required: true, } # Returns the ActsAsTable table for this ActsAsTable record. belongs_to :table, **{ class_name: 'ActsAsTable::Table', counter_cache: 'records_count', inverse_of: :records, required: true, } # Returns the ActsAsTable record errors for this ActsAsTable record. has_many :record_errors, -> { order(attribute_name: :asc, message: :asc) }, **{ autosave: true, class_name: 'ActsAsTable::RecordError', dependent: :destroy, foreign_key: 'record_id', inverse_of: :record, validate: true, } # Returns the ActsAsTable values for this ActsAsTable record. has_many :values, -> { order(position: :asc) }, **{ autosave: true, class_name: 'ActsAsTable::Value', dependent: :destroy, foreign_key: 'record_id', inverse_of: :record, validate: true, } validates :position, **{ numericality: { greater_than_or_equal_to: 1, only_integer: true, }, presence: true, uniqueness: { scope: ['table_id', 'record_model_id'], }, } # validates :record_model_changed, **{} validate :base_must_be_valid_class, :record_model_must_belong_to_row_model_for_table, **{ if: ::Proc.new { |record| record.record_model.present? }, } private # @return [void] def base_must_be_valid_class self.base_type.try { |base_type| unless base_type.nil? || base_type.eql?(self.record_model.class_name) self.errors.add('base_type', :invalid) end } end # @return [void] def record_model_must_belong_to_row_model_for_table self.table.try(:row_model).try(:record_models).try { |record_models| unless record_models.include?(self.record_model) self.errors.add('record_model_id', :invalid) end } return end end |
#values_count ⇒ Integer (readonly)
Returns the number of ActsAsTable values for this ActsAsTable record.
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 |
# File 'app/models/acts_as_table/record.rb', line 20 class Record < ::ActiveRecord::Base # @!parse # include ActsAsTable::ValueProvider # include ActsAsTable::ValueProviderAssociationMethods self.table_name = ActsAsTable.records_table # Returns the record for this ActsAsTable record or `nil`. # # @return [ActiveRecord::Base, nil] belongs_to :base, **{ polymorphic: true, required: false, } # Returns the ActsAsTable record model for this ActsAsTable record. belongs_to :record_model, **{ class_name: 'ActsAsTable::RecordModel', inverse_of: :records, required: true, } # Returns the ActsAsTable table for this ActsAsTable record. belongs_to :table, **{ class_name: 'ActsAsTable::Table', counter_cache: 'records_count', inverse_of: :records, required: true, } # Returns the ActsAsTable record errors for this ActsAsTable record. has_many :record_errors, -> { order(attribute_name: :asc, message: :asc) }, **{ autosave: true, class_name: 'ActsAsTable::RecordError', dependent: :destroy, foreign_key: 'record_id', inverse_of: :record, validate: true, } # Returns the ActsAsTable values for this ActsAsTable record. has_many :values, -> { order(position: :asc) }, **{ autosave: true, class_name: 'ActsAsTable::Value', dependent: :destroy, foreign_key: 'record_id', inverse_of: :record, validate: true, } validates :position, **{ numericality: { greater_than_or_equal_to: 1, only_integer: true, }, presence: true, uniqueness: { scope: ['table_id', 'record_model_id'], }, } # validates :record_model_changed, **{} validate :base_must_be_valid_class, :record_model_must_belong_to_row_model_for_table, **{ if: ::Proc.new { |record| record.record_model.present? }, } private # @return [void] def base_must_be_valid_class self.base_type.try { |base_type| unless base_type.nil? || base_type.eql?(self.record_model.class_name) self.errors.add('base_type', :invalid) end } end # @return [void] def record_model_must_belong_to_row_model_for_table self.table.try(:row_model).try(:record_models).try { |record_models| unless record_models.include?(self.record_model) self.errors.add('record_model_id', :invalid) end } return end end |
Instance Method Details
#base ⇒ ActiveRecord::Base?
Returns the record for this ActsAsTable record or nil
.
30 31 32 33 |
# File 'app/models/acts_as_table/record.rb', line 30 belongs_to :base, **{ polymorphic: true, required: false, } |
#record_errors ⇒ ActiveRecord::Relation<ActsAsTable::RecordError>
Returns the ActsAsTable record errors for this ActsAsTable record.
51 52 53 54 55 56 57 58 |
# File 'app/models/acts_as_table/record.rb', line 51 has_many :record_errors, -> { order(attribute_name: :asc, message: :asc) }, **{ autosave: true, class_name: 'ActsAsTable::RecordError', dependent: :destroy, foreign_key: 'record_id', inverse_of: :record, validate: true, } |
#record_model ⇒ ActsAsTable::RecordModel
Returns the ActsAsTable record model for this ActsAsTable record.
36 37 38 39 40 |
# File 'app/models/acts_as_table/record.rb', line 36 belongs_to :record_model, **{ class_name: 'ActsAsTable::RecordModel', inverse_of: :records, required: true, } |
#table ⇒ ActsAsTable::Table
Returns the ActsAsTable table for this ActsAsTable record.
43 44 45 46 47 48 |
# File 'app/models/acts_as_table/record.rb', line 43 belongs_to :table, **{ class_name: 'ActsAsTable::Table', counter_cache: 'records_count', inverse_of: :records, required: true, } |
#values ⇒ ActiveRecord::Relation<ActsAsTable::Value>
Returns the ActsAsTable values for this ActsAsTable record.
61 62 63 64 65 66 67 68 |
# File 'app/models/acts_as_table/record.rb', line 61 has_many :values, -> { order(position: :asc) }, **{ autosave: true, class_name: 'ActsAsTable::Value', dependent: :destroy, foreign_key: 'record_id', inverse_of: :record, validate: true, } |