Class: ElementPoleAssignment
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ElementPoleAssignment
- Includes:
- Models::Auditable
- Defined in:
- app/models/element_pole_assignment.rb
Overview
== Schema Information
Table name: element_pole_assignments
Database name: primary
id :bigint not null, primary key
heating_element_number :integer not null
notes :string
pole_number :integer not null
relay_number :integer not null
created_at :datetime not null
updated_at :datetime not null
creator_id :integer
heating_element_item_id :integer not null
relay_item_id :integer not null
room_configuration_id :integer not null
updater_id :integer
Indexes
idx_unique_element_pole_assignment (relay_item_id,heating_element_item_id,relay_number,pole_number,heating_element_number) UNIQUE
index_element_pole_assignments_on_heating_element_item_id (heating_element_item_id)
Foreign Keys
fk_rails_... (heating_element_item_id => items.id)
fk_rails_... (relay_item_id => items.id)
fk_rails_... (room_configuration_id => room_configurations.id)
Constant Summary
Constants included from Models::Auditable
Models::Auditable::ALWAYS_IGNORED
Constants included from Models::Schedulable
Models::Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
-
#heating_element_item_id ⇒ Object
readonly
Validates heating element item id.
-
#heating_element_number ⇒ Object
readonly
Validates relay number, pole number, heating element number.
-
#pole_number ⇒ Object
readonly
Validates relay number, pole number, heating element number.
-
#relay_number ⇒ Object
readonly
Validates relay number, pole number, heating element number.
Belongs to collapse
-
#heating_element_item ⇒ Item
The heating element item this record belongs to.
-
#relay_item ⇒ Item
The relay item this record belongs to.
-
#room_configuration ⇒ RoomConfiguration
The room configuration this record belongs to.
Methods included from Models::Auditable
Class Method Summary collapse
-
.order_default ⇒ ActiveRecord::Relation<ElementPoleAssignment>
A relation of ElementPoleAssignments that are order default.
-
.ordered ⇒ ActiveRecord::Relation<ElementPoleAssignment>
A relation of ElementPoleAssignments that are ordered.
Instance Method Summary collapse
-
#heating_element_combo ⇒ Object
Heating element combo.
-
#heating_element_combo=(val) ⇒ Object
Sets the heating element combo.
-
#heating_element_combo_select ⇒ Object
Heating element combo select.
-
#heating_elements_grouped ⇒ Object
Heating elements grouped.
-
#next ⇒ Object
Next.
-
#previous ⇒ Object
broadcasts_to ->(element_pole_assignment) { [element_pole_assignment.room_configuration_id, "element_pole_assignments"] }, partial: "/crm/room_configurations/element_pole_assignments/element_pole_assignment", inserts_by: :append.
-
#relay_combo_select ⇒ Object
Relay combo select.
-
#relay_grouped ⇒ Object
Relay grouped.
-
#relay_identifier ⇒ Object
Relay identifier.
-
#relay_pole_combo ⇒ Object
Relay pole combo.
-
#relay_pole_combo=(val) ⇒ Object
Sets the relay pole combo.
-
#to_partial_path ⇒ Object
Partial lives under
app/views/crm/element_pole_assignments/, not the conventionalapp/views/element_pole_assignments/.
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::Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Attribute Details
#heating_element_item_id ⇒ Object (readonly)
Validates heating element item id.
Validations:
- Uniqueness ({ scope: %i[room_configuration_id relay_item_id relay_number pole_number heating_element_number], message: "cannot be assigned to the same pole multiple times" })
53 |
# File 'app/models/element_pole_assignment.rb', line 53 validates :heating_element_item_id, uniqueness: { scope: %i[room_configuration_id relay_item_id relay_number pole_number heating_element_number], message: "cannot be assigned to the same pole multiple times" } |
#heating_element_number ⇒ Object (readonly)
Validates relay number, pole number, heating element number.
Validations:
- Presence
- Numericality ({ only_integer: true, greater_than: 0 })
51 |
# File 'app/models/element_pole_assignment.rb', line 51 validates :relay_number, :pole_number, :heating_element_number, presence: true, numericality: { only_integer: true, greater_than: 0 } |
#pole_number ⇒ Object (readonly)
Validates relay number, pole number, heating element number.
Validations:
- Presence
- Numericality ({ only_integer: true, greater_than: 0 })
51 |
# File 'app/models/element_pole_assignment.rb', line 51 validates :relay_number, :pole_number, :heating_element_number, presence: true, numericality: { only_integer: true, greater_than: 0 } |
#relay_number ⇒ Object (readonly)
Validates relay number, pole number, heating element number.
Validations:
- Presence
- Numericality ({ only_integer: true, greater_than: 0 })
51 |
# File 'app/models/element_pole_assignment.rb', line 51 validates :relay_number, :pole_number, :heating_element_number, presence: true, numericality: { only_integer: true, greater_than: 0 } |
Class Method Details
.order_default ⇒ ActiveRecord::Relation<ElementPoleAssignment>
A relation of ElementPoleAssignments that are order default. Active Record Scope
57 |
# File 'app/models/element_pole_assignment.rb', line 57 scope :order_default, -> { order(:room_configuration_id, :heating_element_number, :pole_number, :relay_number) } |
.ordered ⇒ ActiveRecord::Relation<ElementPoleAssignment>
A relation of ElementPoleAssignments that are ordered. Active Record Scope
58 |
# File 'app/models/element_pole_assignment.rb', line 58 scope :ordered, -> { order_default } |
Instance Method Details
#heating_element_combo ⇒ Object
Heating element combo.
91 92 93 |
# File 'app/models/element_pole_assignment.rb', line 91 def heating_element_combo "#{heating_element_item_id}|#{heating_element_number}" end |
#heating_element_combo=(val) ⇒ Object
Sets the heating element combo.
86 87 88 |
# File 'app/models/element_pole_assignment.rb', line 86 def heating_element_combo=(val) self.heating_element_item_id, self.heating_element_number = val.presence&.split('|') end |
#heating_element_combo_select ⇒ Object
Heating element combo select.
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'app/models/element_pole_assignment.rb', line 118 def heating_element_combo_select # Create an incremental label counter = [] index = 0 heating_elements_grouped.each do |item, line_items| (1..line_items.sum(&:quantity)).each do |_counter| index += 1 << ["#{item.sku} # #{index}", "#{item.id}|#{index}"] end end end |
#heating_element_item ⇒ Item
Returns the heating element item this record belongs to.
48 |
# File 'app/models/element_pole_assignment.rb', line 48 belongs_to :heating_element_item, class_name: 'Item' |
#heating_elements_grouped ⇒ Object
Heating elements grouped.
132 133 134 |
# File 'app/models/element_pole_assignment.rb', line 132 def heating_elements_grouped room_configuration.line_items.heating_elements.sort_for_plan.joins(:item).group_by(&:item) end |
#next ⇒ Object
Next.
79 80 81 |
# File 'app/models/element_pole_assignment.rb', line 79 def next room_configuration.element_pole_assignments.order_default_at(self).next end |
#previous ⇒ Object
broadcasts_to ->(element_pole_assignment) { [element_pole_assignment.room_configuration_id, "element_pole_assignments"] },
partial: "/crm/room_configurations/element_pole_assignments/element_pole_assignment",
inserts_by: :append
74 75 76 |
# File 'app/models/element_pole_assignment.rb', line 74 def previous room_configuration.element_pole_assignments.order_default_at(self).previous end |
#relay_combo_select ⇒ Object
Relay combo select.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'app/models/element_pole_assignment.rb', line 142 def relay_combo_select = [] relay_grouped.each do |item, line_items| index = 0 (1..line_items.sum(&:quantity)).each do |_counter| index += 1 (1..item.num_poles).to_a.each do |pole_number| label = "#{item.sku} # #{index} - Pole #{pole_number}" if (assignments = room_configuration.element_pole_assignments.select { |epa| epa.relay_item_id == item.id && epa.relay_number == index && epa.pole_number == pole_number }).present? label += " [Assigned to Element #{assignments.map(&:heating_element_number).join(',')}]" end value = "#{item.id}|#{index}|#{pole_number}" << [label, value] end end end end |
#relay_grouped ⇒ Object
Relay grouped.
137 138 139 |
# File 'app/models/element_pole_assignment.rb', line 137 def relay_grouped room_configuration.line_items.relay_panels.joins(:item).order(Item[:sku]).group_by(&:item) end |
#relay_identifier ⇒ Object
Relay identifier.
110 111 112 113 114 115 |
# File 'app/models/element_pole_assignment.rb', line 110 def relay_identifier return unless relay_item_id && relay_number && pole_number # "#{relay_item.sku} ##{relay_number}" "#{relay_item.num_poles} PL ##{relay_number}" end |
#relay_item ⇒ Item
Returns the relay item this record belongs to.
46 |
# File 'app/models/element_pole_assignment.rb', line 46 belongs_to :relay_item, class_name: 'Item' |
#relay_pole_combo ⇒ Object
Relay pole combo.
103 104 105 106 107 |
# File 'app/models/element_pole_assignment.rb', line 103 def relay_pole_combo return unless relay_item_id && relay_number && pole_number "#{relay_item_id}|#{relay_number}|#{pole_number}" end |
#relay_pole_combo=(val) ⇒ Object
Sets the relay pole combo.
98 99 100 |
# File 'app/models/element_pole_assignment.rb', line 98 def relay_pole_combo=(val) self.relay_item_id, self.relay_number, self.pole_number = val.presence&.split('|') end |
#room_configuration ⇒ RoomConfiguration
Returns the room configuration this record belongs to.
44 |
# File 'app/models/element_pole_assignment.rb', line 44 belongs_to :room_configuration |
#to_partial_path ⇒ Object
Partial lives under app/views/crm/element_pole_assignments/, not the
conventional app/views/element_pole_assignments/. Override so
render @element_pole_assignments and the turbo_stream broadcasts in
crm/element_pole_assignments/{create,update}.turbo_stream.erb resolve
correctly from any controller, not just Crm::ElementPoleAssignmentsController.
39 40 41 |
# File 'app/models/element_pole_assignment.rb', line 39 def to_partial_path 'crm/element_pole_assignments/element_pole_assignment' end |