Skip to content

Commit

Permalink
Zer-153 Add price category from UI to controller (#249)
Browse files Browse the repository at this point in the history
* 153 task

* Rspec update

* Update Rspec

* Rspec update

* Rspec update

* Fix rework

* New test for diapers controller, default choose in view

* Update navigation slim file

* Javascript syntax failure resolve

* Remove unnecessary check on js file

* feature test for dropdown product category

* Default value check on controller

* Calculator view conflict resolve

* Conflict resolve fix

* Use rubocop changes

* Tabs in slim and right id using

* feature flag activating in feature test

* Right rspec tests for controller

* Search by product enters between tests

* Rubocop changes

* Id bias in js file

* Removed the extra assignment on js

* Update by Single Responsibility Principe

* Use rubocop changes

* Code cleaning

* Use rubocop changes

* Product price factory testing

* Delete rspec for factory

* Update diaper_calculators_spec.rb

* Update constants with freeze

* Obvious product price factory

* Constants, freezes, traits in diaper_calculator and specs

* Constants conflict resolve

* Move constants to spec/rails_helper.rb

* Move constants to controller

* Use Rubocop change

* Remove findings and split the default test

* One more forgoten split
  • Loading branch information
KostiaFed authored Sep 14, 2022
1 parent 8a59de8 commit a0d1fb5
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 12 deletions.
20 changes: 20 additions & 0 deletions app/controllers/api/v1/diaper_calculators_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class DiaperCalculatorsController < ApplicationController
{ name: 'to_be_used_diapers_amount', result: 0 }
].freeze

LOW = 'LOW'
MEDIUM = 'MEDIUM'
HIGH = 'HIGH'

DIAPER_TITLE = 'diaper'

def create
result = diapers_service_handler(childs_age).calculate!
diapers_be_used = diapers_correct_form(result.to_be_used_diapers_amount)
Expand All @@ -36,6 +42,20 @@ def diapers_service_handler(age)
def childs_age
params[:childs_age].to_i
end

def diaper
@diaper ||= Product.find_by(title: DIAPER_TITLE)
end

def default_price
ProductPrice.find_by(category: MEDIUM, product: diaper)
end

def product_price
selected_price = ProductPrice.find_by(category: params[:price_id],
product: diaper)
selected_price || default_price
end
end
end
end
3 changes: 2 additions & 1 deletion app/javascript/ajax/calculate_result_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ $(document).on('turbolinks:load', function() {
}

const formData = {
childs_age: $("#childs_years").val() * 12 + months
childs_age: $("#childs_years").val() * 12 + months,
price_id: $("#product_category").selectedIndex
}

$.ajax({
Expand Down
15 changes: 9 additions & 6 deletions app/views/calculators/calculator.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
- (0..6).each do |month|
option value = month
= month
- if FeatureFlag.get('feature_budget_category').active?

- if FeatureFlag.get('feature_budget_category').active?
p.form-a
= t '.form_price'
select.custom-select#childs_months [required]
- ([(t('.form.budgetary')), (t('.form.medium')), (t('.form.premium'))]).each do |price|
option value = price
= price

select.custom-select#product_category [required]
option
=t('calculators.calculator.form.budgetary')
option selected=""
=t('calculators.calculator.form.medium')
option
=t('calculators.calculator.form.premium')
= form.text_field :baby_weight, placeholder:t('.form.baby_weight_label'), class:"form-input d-none"
= form.text_field :diapers_per_day, placeholder: "Diapers per day", class:"form-input d-none"
= render_email_receiver_checkbox
Expand Down
6 changes: 3 additions & 3 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@
product_type: hygiene_type
)

budgetary = ProductPrice.create!(
ProductPrice.create!(
price: 4.99,
category: 0,
product: diaper
)

medium = ProductPrice.create!(
ProductPrice.create!(
price: 6.36,
category: 1,
product: diaper
)

premium = ProductPrice.create!(
ProductPrice.create!(
price: 8.21,
category: 2,
product: diaper
Expand Down
58 changes: 58 additions & 0 deletions spec/controllers/diaper_calculators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,67 @@
before do
get :create
end

it 'renders expected result' do
expect(response.body).to eq(expected.to_json)
end
end
end

describe '#product_price' do
context 'when get awaited value' do
it 'first diaper price category returned' do
low = create(:product_price, :LOW)
controller.params[:price_id] = 0
result = controller.send(:product_price)
expect(result).not_to eq(nil)
expect(result).to eq(low)
end

it 'default diaper price category returned' do
medium = create(:product_price, :MEDIUM)

controller.params[:price_id] = 1
result = controller.send(:product_price)
expect(result).not_to eq(nil)
expect(result).to eq(medium)
end

it 'last diaper price category returned' do
high = create(:product_price, :HIGH)

controller.params[:price_id] = 2
result = controller.send(:product_price)
expect(result).not_to eq(nil)
expect(result).to eq(high)
end
end
context 'when get unawaited value' do
it 'get unawaited number' do
medium = create(:product_price, :MEDIUM)

controller.params[:price_id] = -1
result = controller.send(:product_price)
expect(result).not_to eq(nil)
expect(result).to eq(medium)
end
it 'get nil' do
medium = create(:product_price, :MEDIUM)

controller.params[:price_id] = nil
result = controller.send(:product_price)
expect(result).not_to eq(nil)
expect(result).to eq(medium)
end
end
end

describe '#childs_age' do
context 'when default value' do
it 'child`s age selected' do
controller.params[:childs_age] = 1
expect(controller.send(:childs_age)).to eq(1)
end
end
end
end
17 changes: 17 additions & 0 deletions spec/factories/product_price.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FactoryBot.define do
factory :product_price do
association :product, title: "diaper", id: 1
trait :LOW do
category { :LOW }
price { 4.99 }
end
trait :MEDIUM do
category { :MEDIUM }
price { 6.36 }
end
trait :HIGH do
category { :HIGH }
price { 8.21 }
end
end
end
2 changes: 1 addition & 1 deletion spec/factories/product_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

FactoryBot.define do
factory :product_type do
title { 'Title' }
title { 'hygiene' }
end
end
3 changes: 2 additions & 1 deletion spec/factories/products.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

FactoryBot.define do
factory :product do
title { 'Diaper' }
title { 'diaper' }
association :product_type, title: "hygiene", id: 1
end
end
26 changes: 26 additions & 0 deletions spec/features/product_category_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require 'rails_helper'

BUDGETARY_OPTION = 'budgetary'
MEDIUM_OPTION = 'medium'
PREMIUM_OPTION = 'premium'

describe 'product category dropdown list', js: true do
let(:calculator) { create(:calculator) }
before do
FeatureFlag.get('feature_budget_category').activate
visit '/calculator'
find(:select, 'product_category')
has_select?('product_category', with_options: [BUDGETARY_OPTION, MEDIUM_OPTION, PREMIUM_OPTION])
end

it 'default product category' do
expect(page).to have_select('product_category', selected: MEDIUM_OPTION)
end

it 'custom product category selected' do
select(BUDGETARY_OPTION, from: 'product_category')
expect(page).to have_select('product_category', selected: BUDGETARY_OPTION)
end
end
19 changes: 19 additions & 0 deletions spec/models/product_price_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Product, type: :model do
describe 'factory' do
context 'product prices factory not nil' do
it 'low' do
expect(create(:product_price, :LOW)).not_to eq(nil)
end
it 'medium' do
expect(create(:product_price, :MEDIUM)).not_to eq(nil)
end
it 'high' do
expect(create(:product_price, :HIGH)).not_to eq(nil)
end
end
end
end

0 comments on commit a0d1fb5

Please sign in to comment.