Skip to content

Commit d8fb637

Browse files
Slightly more functional code in InvoiceGeneration class
1 parent 8c08418 commit d8fb637

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

rails_application/app/processes/processes/invoice_generation.rb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,19 @@ def add_line(product_id, base_price, price)
109109
end
110110

111111
def remove_line(product_id)
112-
new_lines = lines.dup
113-
index = new_lines.find_index { |line| line.fetch(:product_id) == product_id }
114-
new_lines.delete_at(index)
115-
with(lines: new_lines)
112+
with(lines: lines.dup.tap { |lines| lines.delete_at(index_of_first_line_with(product_id)) })
113+
end
114+
115+
def index_of_first_line_with(product_id)
116+
lines.find_index { |line| line.fetch(:product_id) == product_id }
116117
end
117118

118119
def set_discount(type, amount)
119-
new_discounts = discounts.reject { |d| d.fetch(:type) == type } +
120-
[{ type:, amount: }]
121-
with_discounts_applied(new_discounts)
120+
with_discounts_applied(discounts.reject { |d| d.fetch(:type) == type } + [{ type:, amount: }])
122121
end
123122

124123
def remove_discount(type)
125-
new_discounts = discounts.reject { |d| d.fetch(:type) == type }
126-
with_discounts_applied(new_discounts)
124+
with_discounts_applied(discounts.reject { |d| d.fetch(:type) == type })
127125
end
128126

129127
def mark_placed
@@ -161,8 +159,7 @@ def sub_amounts_total
161159
private
162160

163161
def with_discounts_applied(new_discounts)
164-
new_state = with(discounts: new_discounts)
165-
new_state.apply_discounts_to_lines
162+
with(discounts: new_discounts).apply_discounts_to_lines
166163
end
167164

168165
def subtotal

0 commit comments

Comments
 (0)