Class: ItemRelation
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ItemRelation
- Includes:
- Models::Auditable, Models::EventPublishable
- Defined in:
- app/models/item_relation.rb
Overview
== Schema Information
Table name: item_relations
Database name: primary
id :integer not null, primary key
box2_quantity :integer default(0), not null
box3_quantity :integer default(0), not null
include_in_spec :boolean default(FALSE), not null
note :string(255)
quantity :decimal(6, 2) default(1.0), not null
relation_type :string(255) not null
created_at :datetime
updated_at :datetime
source_item_id :integer not null
target_item_id :integer not null
Indexes
index_item_relations_on_source_item_id_and_relation_type (source_item_id,relation_type)
index_item_relations_on_target_item_id_and_relation_type (target_item_id,relation_type)
item_relations_unique (source_item_id,target_item_id,relation_type) UNIQUE
Constant Summary
Constants included from Models::Auditable
Models::Auditable::ALWAYS_IGNORED
Constants included from Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
- #quantity ⇒ Object readonly
- #relation_type ⇒ Object readonly
- #target_item_id ⇒ Object readonly
Belongs to collapse
-
#source_item ⇒ Item
, inverse_of: :source_item_relations.
-
#target_item ⇒ Item
, inverse_of: :target_item_relations.
Methods included from Models::Auditable
Class Method Summary collapse
-
.accessories ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are accessories.
-
.accessories_or_parts ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are accessories or parts.
-
.controls ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are controls.
-
.kit_components ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are kit components.
-
.kit_components_for_specs ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are kit components for specs.
-
.non_kit_components ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are non kit components.
-
.not_accessories ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are not accessories.
-
.not_accessories_or_parts ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are not accessories or parts.
-
.not_controls ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are not controls.
-
.not_replacement_parts ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are not replacement parts.
-
.publications ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are publications.
- .relation_type_options_for_select ⇒ Object
-
.replacement_parts ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are replacement parts.
-
.upgrades ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are upgrades.
Instance Method Summary collapse
- #ask_quantity? ⇒ Boolean
- #box1_quantity ⇒ Object
- #box_quantities ⇒ Object
- #check_box_quantities ⇒ Object
- #control? ⇒ Boolean
- #items_cant_be_the_same ⇒ Object
- #kit_component? ⇒ Boolean
- #publication? ⇒ Boolean
- #relation_type_options_for_select ⇒ Object
- #target_item_is_not_a_kit ⇒ Object
- #upgrade? ⇒ Boolean
Methods included from Models::EventPublishable
Methods included from Models::Auditable
#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
Methods included from Schedulable
Methods included from Models::AfterCommittable
Instance Attribute Details
#quantity ⇒ Object (readonly)
33 |
# File 'app/models/item_relation.rb', line 33 validates :relation_type, :quantity, presence: true |
#relation_type ⇒ Object (readonly)
33 |
# File 'app/models/item_relation.rb', line 33 validates :relation_type, :quantity, presence: true |
#target_item_id ⇒ Object (readonly)
34 |
# File 'app/models/item_relation.rb', line 34 validates :target_item_id, uniqueness: { scope: [:source_item_id] } |
Class Method Details
.accessories ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are accessories. Active Record Scope
63 |
# File 'app/models/item_relation.rb', line 63 scope :accessories, -> { where(relation_type: 'Accessory') } |
.accessories_or_parts ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are accessories or parts. Active Record Scope
71 |
# File 'app/models/item_relation.rb', line 71 scope :accessories_or_parts, -> { where(relation_type: ['Accessory', 'Replacement Part']) } |
.controls ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are controls. Active Record Scope
65 |
# File 'app/models/item_relation.rb', line 65 scope :controls, -> { where(relation_type: 'Control') } |
.kit_components ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are kit components. Active Record Scope
61 |
# File 'app/models/item_relation.rb', line 61 scope :kit_components, -> { where(relation_type: 'Kit Component') } |
.kit_components_for_specs ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are kit components for specs. Active Record Scope
62 |
# File 'app/models/item_relation.rb', line 62 scope :kit_components_for_specs, -> { kit_components.where(include_in_spec: true) } |
.non_kit_components ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are non kit components. Active Record Scope
60 |
# File 'app/models/item_relation.rb', line 60 scope :non_kit_components, -> { where.not(relation_type: 'Kit Component') } |
.not_accessories ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are not accessories. Active Record Scope
64 |
# File 'app/models/item_relation.rb', line 64 scope :not_accessories, -> { where(relation_type: 'Not Accessory') } |
.not_accessories_or_parts ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are not accessories or parts. Active Record Scope
72 |
# File 'app/models/item_relation.rb', line 72 scope :not_accessories_or_parts, -> { where(relation_type: ['Not Accessory', 'Not Replacement Part']) } |
.not_controls ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are not controls. Active Record Scope
66 |
# File 'app/models/item_relation.rb', line 66 scope :not_controls, -> { where(relation_type: 'Not Control') } |
.not_replacement_parts ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are not replacement parts. Active Record Scope
69 |
# File 'app/models/item_relation.rb', line 69 scope :not_replacement_parts, -> { where(relation_type: 'Not Replacement Part') } |
.publications ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are publications. Active Record Scope
67 |
# File 'app/models/item_relation.rb', line 67 scope :publications, -> { where(relation_type: 'Publication') } |
.relation_type_options_for_select ⇒ Object
105 106 107 |
# File 'app/models/item_relation.rb', line 105 def self. ['Kit Component', 'Publication', 'Accessory', 'Not Accessory', 'Replacement Part', 'Control', 'Not Control', 'Upgrade'] end |
.replacement_parts ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are replacement parts. Active Record Scope
68 |
# File 'app/models/item_relation.rb', line 68 scope :replacement_parts, -> { where(relation_type: 'Replacement Part') } |
.upgrades ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are upgrades. Active Record Scope
70 |
# File 'app/models/item_relation.rb', line 70 scope :upgrades, -> { where(relation_type: 'Upgrade') } |
Instance Method Details
#ask_quantity? ⇒ Boolean
116 117 118 |
# File 'app/models/item_relation.rb', line 116 def ask_quantity? source_item&.is_kit? end |
#box1_quantity ⇒ Object
120 121 122 |
# File 'app/models/item_relation.rb', line 120 def box1_quantity (quantity - box2_quantity - box3_quantity).to_i end |
#box_quantities ⇒ Object
124 125 126 |
# File 'app/models/item_relation.rb', line 124 def box_quantities [box1_quantity, box2_quantity, box3_quantity] end |
#check_box_quantities ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/models/item_relation.rb', line 128 def check_box_quantities return unless relation_type == 'Kit Component' errors.add(:box2_quantity, 'cannot be less than zero') if box2_quantity.negative? errors.add(:box3_quantity, 'cannot be less than zero') if box3_quantity.negative? errors.add(:box2_quantity, 'cannot be more than the kit component quantity') if box2_quantity > quantity errors.add(:box3_quantity, 'cannot be more than the kit component quantity') if box3_quantity > quantity errors.add(:base, 'sum of box 2 and box 3 quantities cannot be more than the kit component quantity') if (box2_quantity + box3_quantity) > quantity # This can cause cyclical problem was you add more than one component at time it doesn't catch up CB # Needs a little more testing, try adding 2 kit components # source_item.boxes.each_with_index do |(_box_length, _box_width, _box_height, box_weight), i| # next if i.zero? # skip box 1 because we need to be able to save one at a time # errors.add(:base, "weight of box #{i + 1}: #{box_weight} lbs seems to be less than the base weight of the kit component quantity: #{target_item.base_weight * box_quantities[i]}") if (target_item.base_weight * box_quantities[i]) > box_weight # end end |
#control? ⇒ Boolean
74 75 76 |
# File 'app/models/item_relation.rb', line 74 def control? relation_type == 'Control' end |
#items_cant_be_the_same ⇒ Object
97 98 99 100 101 102 103 |
# File 'app/models/item_relation.rb', line 97 def items_cant_be_the_same return unless kit_component? return unless target_item == source_item errors.add(:target_item_id, "#{target_item.sku} can't be a kit component of itself") end |
#kit_component? ⇒ Boolean
82 83 84 |
# File 'app/models/item_relation.rb', line 82 def kit_component? relation_type == 'Kit Component' end |
#publication? ⇒ Boolean
86 87 88 |
# File 'app/models/item_relation.rb', line 86 def publication? relation_type == 'Publication' end |
#relation_type_options_for_select ⇒ Object
109 110 111 112 113 114 |
# File 'app/models/item_relation.rb', line 109 def = [] << 'Kit Component' if source_item.is_kit? += ['Publication', 'Accessory', 'Not Accessory', 'Replacement Part', 'Control', 'Not Control', 'Upgrade'] end |
#source_item ⇒ Item
, inverse_of: :source_item_relations
30 |
# File 'app/models/item_relation.rb', line 30 belongs_to :source_item, class_name: 'Item' |
#target_item ⇒ Item
, inverse_of: :target_item_relations
31 |
# File 'app/models/item_relation.rb', line 31 belongs_to :target_item, class_name: 'Item' |
#target_item_is_not_a_kit ⇒ Object
90 91 92 93 94 95 |
# File 'app/models/item_relation.rb', line 90 def target_item_is_not_a_kit return unless kit_component? return unless target_item.is_kit? errors.add(:target_item_id, "#{target_item.sku} cannot be another kit") end |
#upgrade? ⇒ Boolean
78 79 80 |
# File 'app/models/item_relation.rb', line 78 def upgrade? relation_type == 'Upgrade' end |