Skip to content

Commit

Permalink
Merge pull request #993 from ita-social-projects/calculations-local-save
Browse files Browse the repository at this point in the history
Save previous calculations of the user in the storage
  • Loading branch information
loqimean authored Dec 4, 2024
2 parents f934abb + 74953cb commit b20b441
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/controllers/calculators_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def calculate

@results = Calculators::CalculationService.new(@calculator, params[:inputs]).perform

session[:calculation_results] ||= {}
session[:calculation_results][@calculator.slug] = @results

respond_to :turbo_stream
end

Expand Down
10 changes: 7 additions & 3 deletions app/views/calculators/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
<div class="pb-2 input_lable"><%= form.label field.var_name, field.label %></div>
<% if field.kind == 'number' %>
<div class="flex-row rounded flex-item w-100 age_wrapper form_fild">
<%= form.number_field "inputs[#{field.var_name}]",
<%= form.number_field "inputs[#{field.var_name}]",
placeholder: field.label,
class: "required rounded w-100 calculator-field"
%>
</div>
<% else %>
<%= form.select "inputs[#{field.var_name}]",
<%= form.select "inputs[#{field.var_name}]",
options_from_collection_for_select(field.categories, :price, :name),
{},
class: "flex-row rounded flex-item w-100 form_fild calculator-field" %>
Expand All @@ -34,7 +34,11 @@
</div>
</div>

<%= turbo_frame_tag "calc-results" %>
<%= turbo_frame_tag "calc-results" do %>
<% if session.dig(:calculation_results, @calculator.slug).present? %>
<%= render "calculators/partials/calculation_results", results: session.dig(:calculation_results, @calculator.slug).map(&:with_indifferent_access) %>
<% end %>
<% end %>
</div>

<%= render "calculators/partials/show/constructor_calculator_description" %>
13 changes: 13 additions & 0 deletions spec/requests/calculators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,17 @@
end
end
end

describe "POST #calculate" do
let(:calculator) { create(:calculator) }
let(:formula) { build(:formula, expression: "a + 5", calculator: calculator) }
let(:field) { build(:field, var_name: "a", calculator: calculator) }

it "stores the results in the session under the calculator slug" do
post calculate_calculator_path(calculator), params: { calculator: calculator, inputs: { a: 5 }, format: :turbo_stream }

expect(session[:calculation_results]).to have_key(calculator.slug)
expect(session[:calculation_results][calculator.slug]).to eq(assigns(:results))
end
end
end

0 comments on commit b20b441

Please sign in to comment.