diff --git a/app/controllers/account/calculators_controller.rb b/app/controllers/account/calculators_controller.rb index e8e3e28e4..9013ebda1 100644 --- a/app/controllers/account/calculators_controller.rb +++ b/app/controllers/account/calculators_controller.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class Account::CalculatorsController < Account::BaseController - before_action :calculator, only: [:edit, :update, :destroy] + before_action :resource, only: [:edit, :update, :destroy] load_and_authorize_resource def index @@ -58,8 +58,8 @@ def collection Calculator.ordered_by_name end - def calculator - @calculator = Calculator.friendly.find(params[:slug]) + def resource + Calculator.find(params[:slug]) end def collect_fields_for_form @@ -77,11 +77,10 @@ def collect_fields_for_kind(kind) def calculator_params params.require(:calculator).permit( - :name, :id, :slug, :preferable, - fields_attributes: [ - :id, :selector, :label, :name, :value, :unit, :from, :to, :type, :kind, - :_destroy - ] + :id, :en_name, :uk_name, + formulas_attributes: [:id, :expression, :en_label, :uk_label, :calculator_id, :_destroy], + fields_attributes: [:id, :en_label, :uk_label, :var_name, :field_type, :_destroy, + categories_attributes: [:id, :en_name, :price, :_destroy]] ) end diff --git a/app/controllers/calculators_controller.rb b/app/controllers/calculators_controller.rb index 0051ba41a..eb8801ddf 100644 --- a/app/controllers/calculators_controller.rb +++ b/app/controllers/calculators_controller.rb @@ -14,6 +14,7 @@ def index def show @calculator = resource + @result = params[:result] end def calculate @@ -45,6 +46,6 @@ def collection end def resource - collection.friendly.find(params[:slug]) + collection.find(params[:slug]) end end diff --git a/app/models/calculator.rb b/app/models/calculator.rb index d8999935d..b3b293760 100644 --- a/app/models/calculator.rb +++ b/app/models/calculator.rb @@ -17,11 +17,15 @@ # class Calculator < ApplicationRecord + include Translatable + has_many :fields, dependent: :destroy has_many :formulas, dependent: :destroy accepts_nested_attributes_for :fields, reject_if: :all_blank, allow_destroy: true accepts_nested_attributes_for :formulas, reject_if: :all_blank, allow_destroy: true + translates :name + scope :ordered_by_name, -> { order(:en_name) } def self.ransackable_attributes(auth_object = nil) diff --git a/app/models/field.rb b/app/models/field.rb index 738c17f64..d6956056b 100644 --- a/app/models/field.rb +++ b/app/models/field.rb @@ -26,6 +26,7 @@ # class Field < ApplicationRecord belongs_to :calculator + has_many :categories, dependent: :destroy accepts_nested_attributes_for :categories, reject_if: :all_blank, allow_destroy: true end diff --git a/app/views/account/calculators/index.html.erb b/app/views/account/calculators/index.html.erb index 922eb9f02..a608c4533 100644 --- a/app/views/account/calculators/index.html.erb +++ b/app/views/account/calculators/index.html.erb @@ -29,7 +29,7 @@ <% @calculators.each do |calculator| %> - "> + <%= calculator.id %> <%= calculator.name %> <%= calculator.slug %> diff --git a/app/views/account/calculators/partials/_form.html.erb b/app/views/account/calculators/partials/_form.html.erb index dfcc45faf..d5f924164 100644 --- a/app/views/account/calculators/partials/_form.html.erb +++ b/app/views/account/calculators/partials/_form.html.erb @@ -1,4 +1,4 @@ -<%= simple_form_for(@calculator) do |f| %> +<%= simple_form_for(@calculator, url: account_calculators_path) do |f| %>
<%= 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