Class: ItemRelation
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ItemRelation
- Includes:
- Models::Auditable
- 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
Instance Attribute Summary collapse
-
#box1_quantity ⇒ Object
Returns the value of attribute box1_quantity.
- #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
- #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::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 Models::EventPublishable
Instance Attribute Details
#box1_quantity ⇒ Object
Returns the value of attribute box1_quantity.
27 28 29 |
# File 'app/models/item_relation.rb', line 27 def box1_quantity @box1_quantity end |
#quantity ⇒ Object (readonly)
32 |
# File 'app/models/item_relation.rb', line 32 validates :relation_type, :quantity, presence: true |
#relation_type ⇒ Object (readonly)
32 |
# File 'app/models/item_relation.rb', line 32 validates :relation_type, :quantity, presence: true |
#target_item_id ⇒ Object (readonly)
33 |
# File 'app/models/item_relation.rb', line 33 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
50 |
# File 'app/models/item_relation.rb', line 50 scope :accessories, -> { where(relation_type: 'Accessory') } |
.accessories_or_parts ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are accessories or parts. Active Record Scope
58 |
# File 'app/models/item_relation.rb', line 58 scope :accessories_or_parts, -> { where(relation_type: ['Accessory', 'Replacement Part']) } |
.controls ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are controls. Active Record Scope
52 |
# File 'app/models/item_relation.rb', line 52 scope :controls, -> { where(relation_type: 'Control') } |
.kit_components ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are kit components. Active Record Scope
48 |
# File 'app/models/item_relation.rb', line 48 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
49 |
# File 'app/models/item_relation.rb', line 49 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
47 |
# File 'app/models/item_relation.rb', line 47 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
51 |
# File 'app/models/item_relation.rb', line 51 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
59 |
# File 'app/models/item_relation.rb', line 59 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
53 |
# File 'app/models/item_relation.rb', line 53 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
56 |
# File 'app/models/item_relation.rb', line 56 scope :not_replacement_parts, -> { where(relation_type: 'Not Replacement Part') } |
.publications ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are publications. Active Record Scope
54 |
# File 'app/models/item_relation.rb', line 54 scope :publications, -> { where(relation_type: 'Publication') } |
.relation_type_options_for_select ⇒ Object
92 93 94 |
# File 'app/models/item_relation.rb', line 92 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
55 |
# File 'app/models/item_relation.rb', line 55 scope :replacement_parts, -> { where(relation_type: 'Replacement Part') } |
.upgrades ⇒ ActiveRecord::Relation<ItemRelation>
A relation of ItemRelations that are upgrades. Active Record Scope
57 |
# File 'app/models/item_relation.rb', line 57 scope :upgrades, -> { where(relation_type: 'Upgrade') } |
Instance Method Details
#ask_quantity? ⇒ Boolean
103 104 105 |
# File 'app/models/item_relation.rb', line 103 def ask_quantity? source_item&.is_kit? end |
#box_quantities ⇒ Object
111 112 113 |
# File 'app/models/item_relation.rb', line 111 def box_quantities [box1_quantity, box2_quantity, box3_quantity] end |
#check_box_quantities ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'app/models/item_relation.rb', line 115 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
61 62 63 |
# File 'app/models/item_relation.rb', line 61 def control? relation_type == 'Control' end |
#items_cant_be_the_same ⇒ Object
84 85 86 87 88 89 90 |
# File 'app/models/item_relation.rb', line 84 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
69 70 71 |
# File 'app/models/item_relation.rb', line 69 def kit_component? relation_type == 'Kit Component' end |
#publication? ⇒ Boolean
73 74 75 |
# File 'app/models/item_relation.rb', line 73 def publication? relation_type == 'Publication' end |
#relation_type_options_for_select ⇒ Object
96 97 98 99 100 101 |
# File 'app/models/item_relation.rb', line 96 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
29 |
# File 'app/models/item_relation.rb', line 29 belongs_to :source_item, class_name: 'Item' |
#target_item ⇒ Item
, inverse_of: :target_item_relations
30 |
# File 'app/models/item_relation.rb', line 30 belongs_to :target_item, class_name: 'Item' |
#target_item_is_not_a_kit ⇒ Object
77 78 79 80 81 82 |
# File 'app/models/item_relation.rb', line 77 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
65 66 67 |
# File 'app/models/item_relation.rb', line 65 def upgrade? relation_type == 'Upgrade' end |