generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Feature/creating calculators constructor #534
Closed
AndriyAndriyovuch
wants to merge
73
commits into
develop
from
feature/creating_calculators_constructor
Closed
Changes from 63 commits
Commits
Show all changes
73 commits
Select commit
Hold shift + click to select a range
a8708fe
initial commit
AndriyAndriyovuch 212ffdc
merge from develop
AndriyAndriyovuch 18486f8
commit before merge to update from in calculator
AndriyAndriyovuch 69c8d5c
merge from develop
AndriyAndriyovuch aa79011
add ability to add product to calculator
AndriyAndriyovuch cf4c354
add draft show page for universal calculator
AndriyAndriyovuch c9d5d87
add draft show page for universal calculator
AndriyAndriyovuch 7cfca42
add default usage to product
AndriyAndriyovuch 7e8bbf1
create draft calculator
AndriyAndriyovuch e7287bb
add draft calculator for all products
AndriyAndriyovuch 456d188
added locales
AndriyAndriyovuch 95acb0c
remove useless relations
AndriyAndriyovuch e4b32b2
Merge branch 'develop' into feature/creating_calculators_constructor
AndriyAndriyovuch 9e36431
fix relations between calculator and product
AndriyAndriyovuch 5e4f0d5
remove before_action
AndriyAndriyovuch 88efc69
remove useless id from path in category/new view
AndriyAndriyovuch 87b11e1
fix selectors after invalid data in form fields
AndriyAndriyovuch 2388ad9
add migration to clean all prices without category
AndriyAndriyovuch fa2f751
added alert if admin wants to delete category with related prices
AndriyAndriyovuch 09b6800
add locale for error message
AndriyAndriyovuch 3704ab9
fix all tests due to new logic, small rubocop fixes
AndriyAndriyovuch 3be2d64
created request tests for account/calculators_controller
AndriyAndriyovuch 12802a6
removed code smells
AndriyAndriyovuch 6cbd864
add test for calculate service
AndriyAndriyovuch 412b789
add addiction test for destroying category which have prices
AndriyAndriyovuch 9dd58bd
create tests for api/v1/calculators
AndriyAndriyovuch e6ee05a
add redirect to all calculators after update
AndriyAndriyovuch 081147b
change image on 'items used'
AndriyAndriyovuch 485a656
add to seeds creating a calculator with related objects
AndriyAndriyovuch fc272ca
Merge branch 'develop' into feature/creating_calculators_constructor
AndriyAndriyovuch 70ae996
rubocop fixes
AndriyAndriyovuch c8c24be
add some extra lines, remove migration for deleting all prices withou…
AndriyAndriyovuch 2dd2787
merged from develop
AndriyAndriyovuch 97e7e72
merged develop
AndriyAndriyovuch fd842e5
merged develop
AndriyAndriyovuch acd0655
fix turbo in delete methods
AndriyAndriyovuch bc14bd4
Fixed devise errors and some tests
AndriyAndriyovuch 5addf7c
refactored category destroy method (add validator)
AndriyAndriyovuch a74832b
rubocop fix
AndriyAndriyovuch e895ef1
fixed minor issues
AndriyAndriyovuch d53eb19
convert calculators templates to erb
iryna-borniak e27c50f
convert categories template to erb
iryna-borniak d41413d
convert products templates to erb
iryna-borniak c234958
convert site settings template to erb
iryna-borniak f5a9b52
merge en locale
iryna-borniak c87ad72
resolve conflicts
iryna-borniak 946704e
fix edit and new calculators templates
iryna-borniak eb34f09
fix products templates
iryna-borniak 296967e
delete slim templates
iryna-borniak 41f7f6e
add translations to locales
iryna-borniak 0e1be29
fix tests
iryna-borniak 40a2ace
refactor calculators controller
iryna-borniak 02977de
fix pop up messages
iryna-borniak 48ba5f8
refactor controllers
iryna-borniak 3c58823
fix seeds
iryna-borniak 043c1d5
Merge branch 'develop' into feature/creating_calculators_constructor
iryna-borniak 04920a5
delete description button
iryna-borniak 50cc0db
refactor test
iryna-borniak e299b8b
refactor calculators controller
iryna-borniak 5dc88bf
refactor service test
iryna-borniak 9a393fc
delete calculator validator, add CalculatorForm with tests
iryna-borniak 64af587
remove diaper calculator validator, add DiaperCalculatorForm
iryna-borniak 5036409
refactor js controller
iryna-borniak 53941e0
remove has-float-label class
iryna-borniak be27d91
delete options from products form
iryna-borniak 45571d9
refactor migration file
iryna-borniak d6c0000
refactor templates, naming
iryna-borniak aa5eff2
refactor tests
iryna-borniak 973f759
refactor template 'show'
iryna-borniak 18fdb98
refactor styles for 'cancel' button
iryna-borniak 9e2a8b8
remove unnecessary seeds
iryna-borniak 49d314d
merge develop
iryna-borniak 0bca812
remove outdated tests
iryna-borniak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
class Api::V1::CalculatorsController < ApplicationController | ||
VALUES = [ | ||
{ name: "bought_diapers", result: 8956 }, | ||
{ name: "money_spent", result: 7841 }, | ||
{ name: "garbage_created", result: 342 } | ||
].freeze | ||
|
||
def compute | ||
render json: { result: VALUES } | ||
def calculate | ||
@calculator_form = CalculatorForm.new(calculator_params) | ||
|
||
if @calculator_form.valid? | ||
result = Calculators::CalculateService.new(product_resource, calculator_params).calculate | ||
|
||
render json: result.to_json, status: :ok | ||
else | ||
render json: { error: @calculator_form.errors.full_messages.join(", ") }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def collection | ||
Calculator.ordered_by_name | ||
end | ||
|
||
def resource | ||
collection.friendly.find(params[:slug]) | ||
end | ||
|
||
def calculator_params | ||
params.permit(:period, :price_id) | ||
end | ||
|
||
def product_resource | ||
Product.find(resource.product_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
import { FetchRequest } from "@rails/request.js"; | ||
import { toastUtils } from "helpers/toast_helper"; | ||
|
||
export default class extends Controller { | ||
static targets = ["period", "priceCategory"]; | ||
static outlets = ["calculationresults"]; | ||
static values = { | ||
url: { | ||
type: String, | ||
default: "en/api/v1/calculators", | ||
}, | ||
}; | ||
|
||
submit(e) { | ||
e.preventDefault(); | ||
|
||
let formData = { | ||
period: this.periodTarget.value, | ||
price_id: this.priceCategoryTarget.value | ||
}; | ||
|
||
const request = new FetchRequest("POST", this.urlValue, { | ||
responseKind: "json", | ||
body: JSON.stringify(formData), | ||
}); | ||
|
||
this.sendRequest(request); | ||
} | ||
|
||
async sendRequest(request) { | ||
const response = await request.perform(); | ||
const result = await response.json; | ||
|
||
if (response.ok) { | ||
this.calculationresultsOutlet.showResults(result); | ||
} else if (response.statusCode == 422) { | ||
toastUtils.showToast(result.error, "error"); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
app/javascript/controllers/calculationresults_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
|
||
export default class extends Controller { | ||
static targets = [ | ||
"moneySpentResult", | ||
"itemsUsedResult", | ||
]; | ||
|
||
showResults(data) { | ||
this.moneySpentResultTarget.innerHTML = data.moneySpent; | ||
this.itemsUsedResultTarget.innerHTML = data.itemsUsed; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,8 @@ class Calculator < ApplicationRecord | |
|
||
has_many :fields, dependent: :destroy | ||
|
||
belongs_to :product | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Посортовано асоціації не гуд |
||
|
||
accepts_nested_attributes_for :fields, allow_destroy: true | ||
|
||
validates :name, presence: true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
class CalculatorForm | ||
include ActiveModel::Model | ||
|
||
attr_accessor :period, :product_category, :price_id | ||
|
||
validates :period, presence: true | ||
validates :price_id, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
class DiaperCalculatorForm | ||
include ActiveModel::Model | ||
|
||
attr_accessor :childs_years, :childs_months | ||
|
||
validate :validate_child_age | ||
|
||
private | ||
|
||
def validate_child_age | ||
if childs_years.blank? && childs_months.blank? | ||
errors.add(:base, I18n.t("calculators.errors.year_and_month_error_msg")) | ||
elsif childs_years.blank? | ||
errors.add(:base, I18n.t("calculators.errors.year_error_msg")) | ||
elsif childs_months.blank? | ||
errors.add(:base, I18n.t("calculators.errors.month_error_msg")) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
class Calculators::CalculateService | ||
attr_accessor :product, :period, :items_used, :money_spent, :params | ||
|
||
DAYS = { | ||
"day" => 1, | ||
"week" => 7, | ||
"month" => 30.5, | ||
"year" => 365 | ||
}.freeze | ||
|
||
def initialize(product, params) | ||
@product = product | ||
@period = DAYS[params[:period]] | ||
@items_used = 0 | ||
@money_spent = 0 | ||
@params = params | ||
end | ||
|
||
def calculate | ||
money_spent = (product.default_usage_per_day * selected_price * period).to_i | ||
items_used = (product.default_usage_per_day * period).to_i | ||
|
||
{ | ||
moneySpent: money_spent, | ||
itemsUsed: items_used | ||
} | ||
end | ||
|
||
private | ||
|
||
def selected_price | ||
product.prices.find(params[:price_id]).sum | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
чому не svg?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
я просто замалював старий малюнок і зверху намалював сміття щоб хоч +- було ясно що показує