Class: ItemRelation

Inherits:
ApplicationRecord show all
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

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Class Method Summary collapse

Instance Method Summary collapse

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

#publish_event

Instance Attribute Details

#box1_quantityObject

Returns the value of attribute box1_quantity.



27
28
29
# File 'app/models/item_relation.rb', line 27

def box1_quantity
  @box1_quantity
end

#quantityObject (readonly)



32
# File 'app/models/item_relation.rb', line 32

validates :relation_type, :quantity, presence: true

#relation_typeObject (readonly)



32
# File 'app/models/item_relation.rb', line 32

validates :relation_type, :quantity, presence: true

#target_item_idObject (readonly)



33
# File 'app/models/item_relation.rb', line 33

validates :target_item_id, uniqueness: { scope: [:source_item_id] }

Class Method Details

.accessoriesActiveRecord::Relation<ItemRelation>

A relation of ItemRelations that are accessories. Active Record Scope

Returns:

See Also:



50
# File 'app/models/item_relation.rb', line 50

scope :accessories, -> { where(relation_type: 'Accessory') }

.accessories_or_partsActiveRecord::Relation<ItemRelation>

A relation of ItemRelations that are accessories or parts. Active Record Scope

Returns:

See Also:



58
# File 'app/models/item_relation.rb', line 58

scope :accessories_or_parts, -> { where(relation_type: ['Accessory', 'Replacement Part']) }

.controlsActiveRecord::Relation<ItemRelation>

A relation of ItemRelations that are controls. Active Record Scope

Returns:

See Also:



52
# File 'app/models/item_relation.rb', line 52

scope :controls, -> { where(relation_type: 'Control') }

.kit_componentsActiveRecord::Relation<ItemRelation>

A relation of ItemRelations that are kit components. Active Record Scope

Returns:

See Also:



48
# File 'app/models/item_relation.rb', line 48

scope :kit_components, -> { where(relation_type: 'Kit Component') }

.kit_components_for_specsActiveRecord::Relation<ItemRelation>

A relation of ItemRelations that are kit components for specs. Active Record Scope

Returns:

See Also:



49
# File 'app/models/item_relation.rb', line 49

scope :kit_components_for_specs, -> { kit_components.where(include_in_spec: true) }

.non_kit_componentsActiveRecord::Relation<ItemRelation>

A relation of ItemRelations that are non kit components. Active Record Scope

Returns:

See Also:



47
# File 'app/models/item_relation.rb', line 47

scope :non_kit_components, -> { where.not(relation_type: 'Kit Component') }

.not_accessoriesActiveRecord::Relation<ItemRelation>

A relation of ItemRelations that are not accessories. Active Record Scope

Returns:

See Also:



51
# File 'app/models/item_relation.rb', line 51

scope :not_accessories, -> { where(relation_type: 'Not Accessory') }

.not_accessories_or_partsActiveRecord::Relation<ItemRelation>

A relation of ItemRelations that are not accessories or parts. Active Record Scope

Returns:

See Also:



59
# File 'app/models/item_relation.rb', line 59

scope :not_accessories_or_parts, -> { where(relation_type: ['Not Accessory', 'Not Replacement Part']) }

.not_controlsActiveRecord::Relation<ItemRelation>

A relation of ItemRelations that are not controls. Active Record Scope

Returns:

See Also:



53
# File 'app/models/item_relation.rb', line 53

scope :not_controls, -> { where(relation_type: 'Not Control') }

.not_replacement_partsActiveRecord::Relation<ItemRelation>

A relation of ItemRelations that are not replacement parts. Active Record Scope

Returns:

See Also:



56
# File 'app/models/item_relation.rb', line 56

scope :not_replacement_parts, -> { where(relation_type: 'Not Replacement Part') }

.publicationsActiveRecord::Relation<ItemRelation>

A relation of ItemRelations that are publications. Active Record Scope

Returns:

See Also:



54
# File 'app/models/item_relation.rb', line 54

scope :publications, -> { where(relation_type: 'Publication') }

.relation_type_options_for_selectObject



92
93
94
# File 'app/models/item_relation.rb', line 92

def self.relation_type_options_for_select
  ['Kit Component', 'Publication', 'Accessory', 'Not Accessory', 'Replacement Part', 'Control', 'Not Control', 'Upgrade']
end

.replacement_partsActiveRecord::Relation<ItemRelation>

A relation of ItemRelations that are replacement parts. Active Record Scope

Returns:

See Also:



55
# File 'app/models/item_relation.rb', line 55

scope :replacement_parts, -> { where(relation_type: 'Replacement Part') }

.upgradesActiveRecord::Relation<ItemRelation>

A relation of ItemRelations that are upgrades. Active Record Scope

Returns:

See Also:



57
# File 'app/models/item_relation.rb', line 57

scope :upgrades, -> { where(relation_type: 'Upgrade') }

Instance Method Details

#ask_quantity?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'app/models/item_relation.rb', line 103

def ask_quantity?
  source_item&.is_kit?
end

#box_quantitiesObject



111
112
113
# File 'app/models/item_relation.rb', line 111

def box_quantities
  [box1_quantity, box2_quantity, box3_quantity]
end

#check_box_quantitiesObject



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

Returns:

  • (Boolean)


61
62
63
# File 'app/models/item_relation.rb', line 61

def control?
  relation_type == 'Control'
end

#items_cant_be_the_sameObject



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

Returns:

  • (Boolean)


69
70
71
# File 'app/models/item_relation.rb', line 69

def kit_component?
  relation_type == 'Kit Component'
end

#publication?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/models/item_relation.rb', line 73

def publication?
  relation_type == 'Publication'
end

#relation_type_options_for_selectObject



96
97
98
99
100
101
# File 'app/models/item_relation.rb', line 96

def relation_type_options_for_select
  options = []
  options << 'Kit Component' if source_item.is_kit?
  options += ['Publication', 'Accessory', 'Not Accessory', 'Replacement Part', 'Control', 'Not Control', 'Upgrade']
  options
end

#source_itemItem

, inverse_of: :source_item_relations

Returns:

See Also:



29
# File 'app/models/item_relation.rb', line 29

belongs_to :source_item, class_name: 'Item'

#target_itemItem

, inverse_of: :target_item_relations

Returns:

See Also:



30
# File 'app/models/item_relation.rb', line 30

belongs_to :target_item, class_name: 'Item'

#target_item_is_not_a_kitObject



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

Returns:

  • (Boolean)


65
66
67
# File 'app/models/item_relation.rb', line 65

def upgrade?
  relation_type == 'Upgrade'
end