Class: MultiRoomModel::RoomsQuickReviser
- Inherits:
-
BaseService
- Object
- BaseService
- MultiRoomModel::RoomsQuickReviser
- Defined in:
- app/services/multi_room_model/rooms_quick_reviser.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#initialize(quote_or_order, options = {}) ⇒ RoomsQuickReviser
constructor
A new instance of RoomsQuickReviser.
- #quick_revise(room_configuration_ids: nil) ⇒ Object
Constructor Details
#initialize(quote_or_order, options = {}) ⇒ RoomsQuickReviser
Returns a new instance of RoomsQuickReviser.
5 6 7 8 9 10 11 |
# File 'app/services/multi_room_model/rooms_quick_reviser.rb', line 5 def initialize(quote_or_order, ={}) @quote_or_order = quote_or_order @keep_plan = [:keep_plan] @options = @logger = [:logger] || Rails.logger @errors = [] end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
3 4 5 |
# File 'app/services/multi_room_model/rooms_quick_reviser.rb', line 3 def errors @errors end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
3 4 5 |
# File 'app/services/multi_room_model/rooms_quick_reviser.rb', line 3 def logger @logger end |
Instance Method Details
#quick_revise(room_configuration_ids: nil) ⇒ Object
13 14 15 16 17 18 19 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 |
# File 'app/services/multi_room_model/rooms_quick_reviser.rb', line 13 def quick_revise(room_configuration_ids: nil) @errors = [] unless @quote_or_order.is_a?(Quote) || @quote_or_order.is_a?(Order) @errors << "Itemizable type #{@quote_or_order.class.name} not supported" return false end rc_ids = @quote_or_order.room_configurations.select(&:editing_locked?).map(&:id) if room_configuration_ids.present? rc_ids = rc_ids & room_configuration_ids.map(&:to_i) end total_steps = rc_ids.size * 4 rc_ids.each_with_index do |orig_rc_id, index| current_step = index + 1 orig_rc = RoomConfiguration.find(orig_rc_id) yield(current_step, total_steps, "Quick Revising #{orig_rc.reference_number}") if block_given? RoomConfiguration.transaction do begin current_step += 1 new_rc = orig_rc.create_revision(copy_line_items=true,copy_room_layout=true,copy_installation_plan=@keep_plan) yield(current_step, total_steps, "Revision #{new_rc.reference_number} created, updating #{@quote_or_order.reference_number}") if block_given? @quote_or_order.do_not_detect_shipping = true current_step += 1 yield(current_step, total_steps, "Removing #{orig_rc.reference_number} from #{@quote_or_order.reference_number}") if block_given? @quote_or_order.room_configurations.delete(orig_rc) @quote_or_order.save! @quote_or_order.reload @quote_or_order.do_not_detect_shipping = nil current_step += 1 yield(current_step, total_steps, "Adding #{new_rc.reference_number} to #{@quote_or_order.reference_number}") if block_given? @quote_or_order.room_configurations << new_rc @quote_or_order.save! rescue StandardError => exc @errors << "an unexpected error occured while revising room reference # #{orig_rc.reference_number} or saving quote or order #{exc.to_s}, changes have been rolled back." raise ActiveRecord::Rollback end end end @errors.empty? end |