<%= f.input :en_name, label: "Calculator Name:", class: 'form-control' %>
@@ -35,4 +35,4 @@
<%= t('buttons.cancel') %>
<% end %>
-<% end %>
\ No newline at end of file
+<% end %>
diff --git a/app/views/calculators/show.html.erb b/app/views/calculators/show.html.erb
new file mode 100644
index 000000000..0d9109547
--- /dev/null
+++ b/app/views/calculators/show.html.erb
@@ -0,0 +1,23 @@
+
+
Calculator <%= @calculator.en_name %>
+
+ <%= form_with url: calculate_account_calculator_path(@calculator) do |form| %>
+ <% @calculator.fields.each do |field| %>
+
+
<%= form.label field.var_name, field.en_label %>
+ <% if field.field_type == 'number' %>
+ <%= form.number_field "inputs[#{field.var_name}]", placeholder: field.en_label %>
+ <% else %>
+ <%= form.select "inputs[#{field.var_name}]", options_from_collection_for_select(field.categories, :price, :en_name) %>
+ <% end %>
+
+ <% end %>
+
+ <%= form.submit "Calculate", class: "bg-blue-500 text-white font-semibold px-4 py-2 rounded hover:bg-blue-700" %>
+
+ <% end %>
+
+ <% if @result.present? %>
+
Result: <%= @result %>
+ <% end %>
+
diff --git a/config/routes.rb b/config/routes.rb
index 7de991fc5..f0879ffd9 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -56,14 +56,17 @@
end
resources :diapers_periods
-
+ resources :calculators, param: :slug do
+ member do
+ post :calculate
+ end
+ end
namespace :diapers_periods do
resources :categories, only: [:destroy] do
get :with_periods, on: :collection
get :available, on: :collection
end
end
-
scope module: :calculators do
resources :calculators, only: [], param: :slug do
resources :fields, only: :new
diff --git a/db/migrate/20241107101158_change_calculators.rb b/db/migrate/20241107101158_change_calculators.rb
deleted file mode 100644
index 1798acf11..000000000
--- a/db/migrate/20241107101158_change_calculators.rb
+++ /dev/null
@@ -1,8 +0,0 @@
-class ChangeCalculators < ActiveRecord::Migration[7.2]
- def change
- remove_columns :calculators, :uuid, :name, :preferable
-
- add_column :calculators, :uk_name, :string, null: false
- add_column :calculators, :en_name, :string, null: false
- end
-end
diff --git a/db/migrate/20241107101158_update_calculators.rb b/db/migrate/20241107101158_update_calculators.rb
new file mode 100644
index 000000000..ad7b03f78
--- /dev/null
+++ b/db/migrate/20241107101158_update_calculators.rb
@@ -0,0 +1,20 @@
+class UpdateCalculators < ActiveRecord::Migration[7.2]
+ def up
+ change_table :calculators, bulk: true do |t|
+ t.remove :uuid, :name, :preferable
+ t.string :uk_name, null: false, default: ""
+ t.string :en_name, null: false, default: ""
+ end
+ end
+
+ def down
+ change_table :calculators, bulk: true do |t|
+ t.string :uuid
+ t.string :name
+ t.boolean :preferable, null: false, default: false
+
+ t.remove :uk_name
+ t.remove :en_name
+ end
+ end
+end
diff --git a/db/migrate/20241107134610_create_formulas.rb b/db/migrate/20241107134610_create_formulas.rb
index 0210ce7a1..0178a985d 100644
--- a/db/migrate/20241107134610_create_formulas.rb
+++ b/db/migrate/20241107134610_create_formulas.rb
@@ -1,9 +1,9 @@
class CreateFormulas < ActiveRecord::Migration[7.2]
def change
create_table :formulas do |t|
- t.string :expression, null: false
- t.string :uk_label, null: false
- t.string :en_label, null: false
+ t.string :expression, null: false, default: ""
+ t.string :uk_label, null: false, default: ""
+ t.string :en_label, null: false, default: ""
t.string :uk_unit
t.string :en_unit
t.references :calculator, null: false, foreign_key: true
diff --git a/db/migrate/20241107135318_change_fields.rb b/db/migrate/20241107135318_change_fields.rb
index 286791d10..48854b28b 100644
--- a/db/migrate/20241107135318_change_fields.rb
+++ b/db/migrate/20241107135318_change_fields.rb
@@ -1,10 +1,49 @@
class ChangeFields < ActiveRecord::Migration[7.2]
- def change
- remove_columns :fields, :uuid, :selector, :type, :label, :name, :value, :from, :to, :kind, :unit
+ def up
+ change_table :fields, bulk: true do |t|
+ t.remove :uuid, type: :string
+ t.remove :selector, type: :string
+ t.remove :type, type: :string
+ t.remove :label, type: :string
+ t.remove :name, type: :string
+ t.remove :value, type: :string
+ t.remove :from, type: :integer
+ t.remove :to, type: :integer
+ t.remove :kind, type: :string
+ t.remove :unit, type: :string
+ t.remove :calculator_id, type: :integer
- add_column :fields, :uk_label, :string, null: false
- add_column :fields, :en_label, :string, null: false
- add_column :fields, :var_name, :string, null: false
- add_column :fields, :field_type, :string, null: false
+ t.string :uk_name, null: false, default: ""
+ t.string :en_name, null: false, default: ""
+ t.string :var_name, null: false, default: ""
+ t.string :field_type, null: false, default: ""
+
+ t.references :calculator, foreign_key: true, null: false, default: 0
+ end
+
+ change_column_default :fields, :calculator_id, from: 0, to: nil
+ end
+
+ def down
+ change_table :fields, bulk: true do |t|
+ t.string :uuid
+ t.string :selector
+ t.string :type
+ t.string :label
+ t.string :name
+ t.string :value
+ t.integer :from
+ t.integer :to
+ t.string :kind
+ t.string :unit
+ t.integer :calculator_id
+
+ t.remove :uk_name
+ t.remove :en_name
+ t.remove :var_name
+ t.remove :field_type
+
+ t.remove_references :calculator, foreign_key: true
+ end
end
end
diff --git a/db/migrate/20241107140542_change_categories.rb b/db/migrate/20241107140542_change_categories.rb
index cd7e94b4b..dd0e2677c 100644
--- a/db/migrate/20241107140542_change_categories.rb
+++ b/db/migrate/20241107140542_change_categories.rb
@@ -1,8 +1,23 @@
class ChangeCategories < ActiveRecord::Migration[7.2]
- def change
- remove_columns :categories, :priority, :preferable
- add_column :categories, :price, :float, null: false
+ def up
+ change_table :categories, bulk: true do |t|
+ t.remove :preferable, type: :boolean
- add_reference :categories, :field, null: false, foreign_key: true
+ t.float :price, null: false, default: 0.0
+
+ t.references :field, null: false, foreign_key: true, default: 0
+
+ t.boolean :preferable, null: false, default: false
+ end
+
+ change_column_default :categories, :field_id, nil
+ end
+
+ def down
+ change_table :categories, bulk: true do |t|
+ t.remove :preferable, type: :boolean
+
+ t.remove :pricet.remove_references :field, foreign_key: true
+ end
end
end
diff --git a/db/schema.rb b/db/schema.rb
index e8b05462e..7561ffb1e 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -47,8 +47,8 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug"
- t.string "uk_name", null: false
- t.string "en_name", null: false
+ t.string "uk_name", default: "", null: false
+ t.string "en_name", default: "", null: false
t.index ["slug"], name: "index_calculators_on_slug", unique: true
end
@@ -98,10 +98,10 @@
t.bigint "calculator_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
- t.string "uk_label", null: false
- t.string "en_label", null: false
- t.string "var_name", null: false
- t.string "field_type", null: false
+ t.string "uk_label", default: "", null: false
+ t.string "en_label", default: "", null: false
+ t.string "var_name", default: "", null: false
+ t.string "field_type", default: "", null: false
t.index ["calculator_id"], name: "index_fields_on_calculator_id"
end
@@ -124,8 +124,8 @@
end
create_table "formulas", force: :cascade do |t|
- t.string "expression", null: false
- t.string "uk_label", null: false
+ t.string "expression", default: "", null: false
+ t.string "uk_label", default: "", null: false
t.string "en_label", null: false
t.string "uk_unit"
t.string "en_unit"
@@ -204,9 +204,9 @@
t.string "last_sign_in_ip"
t.string "provider"
t.string "uid"
- t.boolean "blocked", default: false
+ t.boolean "blocked", default: false, null: false
t.integer "role", default: 0
- t.boolean "receive_recomendations", default: false
+ t.boolean "receive_recomendations", default: false, null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
diff --git a/spec/factories/calculators.rb b/spec/factories/calculators.rb
index cad0fdd0d..0e01d1988 100644
--- a/spec/factories/calculators.rb
+++ b/spec/factories/calculators.rb
@@ -20,7 +20,7 @@
#
FactoryBot.define do
factory :calculator do
- name { "Diapers Calculator" }
+ en_name { "Diapers Calculator" }
slug { "diapers" }
end
end
diff --git a/spec/features/account/calculators_spec.rb b/spec/features/account/calculators_spec.rb
index adb78be3b..615a23bfb 100644
--- a/spec/features/account/calculators_spec.rb
+++ b/spec/features/account/calculators_spec.rb
@@ -3,8 +3,8 @@
require "rails_helper"
describe "visit admin page", js: true do
- let!(:diapers_calculator) { create(:calculator, name: "Diapers Calculator", slug: "diapers") }
- let!(:napkin_calculator) { create(:calculator, name: "Napkin Calculator", slug: "napkin") }
+ let!(:diapers_calculator) { create(:calculator, en_name: "Diapers Calculator", slug: "diapers") }
+ let!(:napkin_calculator) { create(:calculator, en_name: "Napkin Calculator", slug: "napkin") }
include_context :authorize_admin
diff --git a/spec/features/show_calculator_spec.rb b/spec/features/show_calculator_spec.rb
new file mode 100644
index 000000000..5fe98128a
--- /dev/null
+++ b/spec/features/show_calculator_spec.rb
@@ -0,0 +1,30 @@
+require "rails_helper"
+
+RSpec.describe CalculatorsController, type: :controller do
+ describe "GET #show" do
+ let(:calculator) { create(:calculator) }
+
+ before do
+ allow(controller).to receive(:resource).and_return(calculator)
+ end
+
+ context "when result parameter is present" do
+ it "assigns @result with the value from params" do
+ get :show, params: { slug: calculator.slug, result: "42", locale: :en }
+ expect(assigns(:result)).to eq("42")
+ end
+ end
+
+ context "when result parameter is not present" do
+ it "assigns @result as nil" do
+ get :show, params: { slug: calculator.slug, locale: :en }
+ expect(assigns(:result)).to be_nil
+ end
+ end
+
+ it "assigns the correct calculator to @calculator" do
+ get :show, params: { slug: calculator.slug, locale: :en }
+ expect(assigns(:calculator)).to eq(calculator)
+ end
+ end
+end