Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show calculator #946

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions app/controllers/account/calculators_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
end

def calculator
@calculator = Calculator.friendly.find(params[:slug])
@calculator = Calculator.find(params[:slug])

Check warning on line 62 in app/controllers/account/calculators_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/account/calculators_controller.rb#L62

Added line #L62 was not covered by tests
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тестів нема + юзай ресурс

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не дуже розумію, що це за результат з парамсів?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SEO-friendly урла залишена з попердньої реалізації.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

що це ще таке?? ну Наталя, як ми юзаємо ресурс?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

я і не казав забирати слаг

end

def collect_fields_for_form
Expand All @@ -77,11 +77,10 @@

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

Expand Down
3 changes: 2 additions & 1 deletion app/controllers/calculators_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

def show
@calculator = resource
@result = params[:result]

Check warning on line 17 in app/controllers/calculators_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/calculators_controller.rb#L17

Added line #L17 was not covered by tests
end

def calculate
Expand Down Expand Up @@ -45,6 +46,6 @@
end

def resource
collection.friendly.find(params[:slug])
collection.find(params[:slug])

Check warning on line 49 in app/controllers/calculators_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/calculators_controller.rb#L49

Added line #L49 was not covered by tests
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а чому шукає слаг, але скоуп френдлі видаляється?

end
end
1 change: 1 addition & 0 deletions app/models/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions app/views/account/calculators/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
</thead>
<tbody class="admin-table-links">
<% @calculators.each do |calculator| %>
<tr class="<%= calculator.preferable? ? "table-success" : "text-inherit" %>">
<tr >
<td><%= calculator.id %></td>
<td><%= calculator.name %></td>
<td><%= calculator.en_name %></td>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<td><%= calculator.en_name %></td>
<td><%= calculator.name %></td>

<td><%= calculator.slug %></td>
<td class="text-center"><%= link_to icon('fa-solid', 'eye'), account_calculator_path(slug: calculator) %></td>
<td class="text-center"><%= link_to icon('fa-solid', 'edit'), edit_account_calculator_path(slug: calculator) %></td>
Expand Down
4 changes: 2 additions & 2 deletions app/views/account/calculators/partials/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= simple_form_for(@calculator) do |f| %>
<%= simple_form_for(@calculator, url: account_calculators_path) do |f| %>
<div class="form-group row">
<div class="my-auto col-12 has-float-label">
<%= f.input :en_name, label: "Calculator Name:", class: 'form-control' %>
Expand Down Expand Up @@ -35,4 +35,4 @@
<span><%= t('buttons.cancel') %></span>
<% end %>
</div>
<% end %>
<% end %>
23 changes: 23 additions & 0 deletions app/views/calculators/show.html.erb
loqimean marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div class="container place-items-center">
<h1 class="text-4xl font-bold">Calculator <%= @calculator.en_name %></h1><br>

<%= form_with url: calculate_account_calculator_path(slug: @calculator.slug || @calculator.id) do |form| %>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<%= form_with url: calculate_account_calculator_path(slug: @calculator.slug || @calculator.id) do |form| %>
<%= form_with url: calculate_account_calculator_path(@calculator) do |form| %>

<% @calculator.fields.each do |field| %>
<div class="space-y-4 form-inputs">
<h1><%= form.label field.var_name, field.en_label %></h1>
<% 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 %>
</div><br>
<% end %>
<div class="mt-6 form-actions">
<%= form.submit "Calculate", class: "bg-blue-500 text-white font-semibold px-4 py-2 rounded hover:bg-blue-700" %>
</div>
<% end %>

<% if @result.present? %>
<p>Result: <%= @result %></p>
<% end %>
</div>
7 changes: 5 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions db/migrate/20241107101158_change_calculators.rb

This file was deleted.

20 changes: 20 additions & 0 deletions db/migrate/20241107101158_update_calculators.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions db/migrate/20241107134610_create_formulas.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
51 changes: 45 additions & 6 deletions db/migrate/20241107135318_change_fields.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 19 additions & 4 deletions db/migrate/20241107140542_change_categories.rb
Original file line number Diff line number Diff line change
@@ -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
20 changes: 10 additions & 10 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
Loading