Skip to content

Commit

Permalink
Add voucher type to admin screen
Browse files Browse the repository at this point in the history
Plus specs
  • Loading branch information
rioug committed May 9, 2023
1 parent 28b3163 commit cc65d3a
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/vouchers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def load_enterprise
end

def permitted_resource_params
params.require(:voucher).permit(:code, :amount)
params.require(:voucher).permit(:code, :amount, :voucher_type)
end
end
end
7 changes: 6 additions & 1 deletion app/models/voucher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ class Voucher < ApplicationRecord
validates :voucher_type, inclusion: TYPES

def display_value
Spree::Money.new(amount)
case voucher_type
when FLAT_RATE
Spree::Money.new(amount)
when PERCENTAGE_RATE
I18n.t(:voucher_percentage, amount: amount)
end
end

# Ideally we would use `include CalculatedAdjustments` to be consistent with other adjustments,
Expand Down
6 changes: 5 additions & 1 deletion app/views/admin/vouchers/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
= f.label :code, t('.voucher_code')
.omega.eight.columns
= f.text_area :code, rows: 6, class: 'fullwidth'
.row
.alpha.four.columns
= f.label :voucher_type, t('.voucher_type')
.omega.eight.columns
= f.select :voucher_type, options_for_select(Voucher::TYPES.map { |type| [t(".#{type}"), type] }, @voucher.voucher_type)
.row
.alpha.four.columns
= f.label :amount, t('.voucher_amount')
.omega.eight.columns
= Spree::Money.currency_symbol
= f.text_field :amount, value: @voucher.amount
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,9 @@ en:
save: Save
voucher_code: Voucher Code
voucher_amount: Amount
voucher_type: Voucher Type
flat: Flat
percentage: Percentage (%)

# Admin controllers
controllers:
Expand Down Expand Up @@ -3092,6 +3095,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using
new_payment: "New Payment"
date_completed: "Date Completed"
amount: "Amount"
voucher_percentage: "%{amount} %"
state_names:
ready: Ready
pending: Pending
Expand Down
5 changes: 4 additions & 1 deletion spec/requests/admin/vouchers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@
{
voucher: {
code: code,
amount: amount
amount: amount,
voucher_type: type
}
}
end
let(:code) { "new_code" }
let(:amount) { 15 }
let(:type) { "percentage" }

it "creates a new voucher" do
expect { create_voucher }.to change(Voucher, :count).by(1)

voucher = Voucher.last
expect(voucher.code).to eq(code)
expect(voucher.amount).to eq(amount)
expect(voucher.voucher_type).to eq(type)
end

it "redirects to admin enterprise setting page, voucher panel" do
Expand Down
89 changes: 58 additions & 31 deletions spec/system/admin/vouchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

it 'lists enterprise vouchers' do
# Given an enterprise with vouchers
create(:voucher, enterprise: enterprise, code: voucher_code, amount: amount)
create(:voucher_flat_rate, enterprise: enterprise, code: voucher_code, amount: amount)

# When I go to the enterprise voucher tab
visit edit_admin_enterprise_path(enterprise)
Expand All @@ -35,46 +35,73 @@
expect(page).to have_content amount
end

it 'creates a voucher' do
# Given an enterprise
# When I go to the enterprise voucher tab and click new
visit edit_admin_enterprise_path(enterprise)
describe "adding voucher" do
before do
# Given an enterprise
# When I go to the enterprise voucher tab and click new
visit edit_admin_enterprise_path(enterprise)

click_link 'Vouchers'
within "#vouchers_panel" do
click_link 'Add New'
click_link 'Vouchers'
within "#vouchers_panel" do
click_link 'Add New'
end
end

# And I fill in the fields for a new voucher click save
fill_in 'voucher_code', with: voucher_code
fill_in 'voucher_amount', with: amount
click_button 'Save'
context "with a flat rate voucher" do
it 'creates a voucher' do
# And I fill in the fields for a new voucher click save
fill_in 'voucher_code', with: voucher_code
select "Flat", from: "voucher_voucher_type"
fill_in 'voucher_amount', with: amount
click_button 'Save'

# Then I should get redirect to the entreprise voucher tab and see the created voucher
expect_to_be_redirected_to_enterprise_voucher_tab(page, voucher_code, amount)
expect_voucher_to_be_created(enterprise, voucher_code)
end
end

# Then I should get redirect to the entreprise voucher tab and see the created voucher
expect(page).to have_selector '.success', text: 'Voucher has been successfully created!'
expect(page).to have_content voucher_code
expect(page).to have_content amount
context "with a percentage rate voucher" do
it 'creates a voucher' do
# And I fill in the fields for a new voucher click save
fill_in 'voucher_code', with: voucher_code
select "Percentage (%)", from: "voucher_voucher_type"
fill_in 'voucher_amount', with: amount
click_button 'Save'

# Then I should get redirect to the entreprise voucher tab and see the created voucher
expect_to_be_redirected_to_enterprise_voucher_tab(page, voucher_code, amount)
expect_voucher_to_be_created(enterprise, voucher_code)
end
end

voucher = Voucher.where(enterprise: enterprise, code: voucher_code).first
context 'when entering invalid data' do
it 'shows an error flash message' do
# Given an enterprise
# When I go to the new voucher page
visit new_admin_enterprise_voucher_path(enterprise)

expect(voucher).not_to be(nil)
end
# And I fill in fields with invalid data and click save
click_button 'Save'

context 'when entering invalid data' do
it 'shows an error flash message' do
# Given an enterprise
# When I go to the new voucher page
visit new_admin_enterprise_voucher_path(enterprise)
# Then I should see an error flash message
expect(page).to have_selector '.error', text: "Code can't be blank"

# And I fill in fields with invalid data and click save
click_button 'Save'
vouchers = Voucher.where(enterprise: enterprise)

# Then I should see an error flash message
expect(page).to have_selector '.error', text: "Code can't be blank"
expect(vouchers).to be_empty
end
end
end

vouchers = Voucher.where(enterprise: enterprise)
def expect_to_be_redirected_to_enterprise_voucher_tab(page, voucher_code, amount)
expect(page).to have_selector '.success', text: 'Voucher has been successfully created!'
expect(page).to have_content voucher_code
expect(page).to have_content amount
end

expect(vouchers).to be_empty
end
def expect_voucher_to_be_created(enterprise, voucher_code)
voucher = Voucher.where(enterprise: enterprise, code: voucher_code).first
expect(voucher).not_to be(nil)
end
end

0 comments on commit cc65d3a

Please sign in to comment.