Skip to content

don't allow transfers of 0 hours and 0 minutes #421

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

Merged
merged 3 commits into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions app/assets/javascripts/give_time.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ jQuery.validator.addMethod "either-hours-minutes-informed", ((value, element) ->
giveTimeReadyFn = () ->
config =
submitHandler: (form) ->
$(" #transfer_amount ").val($(" #transfer_hours ").val() * 3600 + $(" #transfer_minutes ").val() * 60)
form.submit()
amount = $("#transfer_hours").val() * 3600 + $("#transfer_minutes").val() * 60
$("#transfer_amount").val(amount)

if amount > 0
form.submit()
else
$(form).find('.form-actions .error').removeClass('invisible').show()

$( "#new_transfer" ).validate(config)

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/transfers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def create
if persister.save
redirect_to redirect_target
else
flash[:error] = transfer.errors.full_messages.to_sentence
redirect_to :back, alert: transfer.errors.full_messages.to_sentence
end
end

Expand Down
2 changes: 2 additions & 0 deletions app/models/movement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Movement < ActiveRecord::Base
where(created_at: month.beginning_of_month..month.end_of_month)
}

validates :amount, numericality: { other_than: 0 }

after_create do
account.update_balance
end
Expand Down
9 changes: 1 addition & 8 deletions app/models/transfer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Transfer < ActiveRecord::Base
has_many :movements, dependent: :destroy
has_many :events, dependent: :destroy

validates :amount, numericality: { greater_than: 0 }
validate :different_source_and_destination

after_create :make_movements
Expand All @@ -26,14 +27,6 @@ def make_movements
movements.create(account: Account.find(destination_id), amount: amount.to_i)
end

def movement_from
movements.detect {|m| m.amount < 0 }
end

def movement_to
movements.detect {|m| m.amount > 0 }
end

def source_id
source.respond_to?(:id) ? source.id : source
end
Expand Down
3 changes: 2 additions & 1 deletion app/views/transfers/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
</div>

<div class="form-actions">
<%= f.button :submit, class: "btn btn-default", style: "margin-bottom: 20px;" %>
<%= f.button :submit, class: "btn btn-default" %>
<label class="error invisible"><%= t ".error_amount" %></label>
</div>
<% end %>

Expand Down
2 changes: 2 additions & 0 deletions config/locales/ca.yml
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ ca:
minute:
one: "%{count} minut"
other: "%{count} minuts"
new:
error_amount:
users:
edit:
edit_user: Canviar usuari
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ en:
minute:
one: "%{count} minute"
other: "%{count} minutes"
new:
error_amount: Time must be greater than 0
users:
edit:
edit_user: Update user
Expand Down
2 changes: 2 additions & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ es:
minute:
one: "%{count} minuto"
other: "%{count} minutos"
new:
error_amount:
users:
edit:
edit_user: Cambiar usuario
Expand Down
2 changes: 2 additions & 0 deletions config/locales/eu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ eu:
minute:
one: "%{count}minutu"
other: "%{count} minutu"
new:
error_amount:
users:
edit:
edit_user: Erabiltzailea aldatu
Expand Down
2 changes: 2 additions & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ pt-BR:
minute:
one: "%{count} minuto"
other: "%{count} minutos"
new:
error_amount:
users:
edit:
edit_user: Trocar usuário
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20181004200104_add_amount_constraint_to_movements.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddAmountConstraintToMovements < ActiveRecord::Migration
def change
# Destroy movements (and parent transfer) with amount equal to 0
Movement.includes(:transfer).where(amount: 0).find_each do |movement|
movement.transfer&.destroy
end

execute 'ALTER TABLE movements ADD CONSTRAINT non_zero_amount CHECK(amount != 0)'
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180924164456) do
ActiveRecord::Schema.define(version: 20181004200104) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down
17 changes: 17 additions & 0 deletions spec/controllers/transfers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,22 @@
end
end
end

context 'with invalid params' do
let(:user) { member_giver.user }
let(:referer) { "/transfers/new?destination_account_id=#{member_taker.account.id}" }

before do
request.env["HTTP_REFERER"] = referer
end

it 'does not create any Transfer and redirects to :back if the amount is 0' do
expect {
post(:create, transfer: { amount: 0, destination: member_taker.account.id })
}.not_to change(Transfer, :count)

expect(response).to redirect_to(referer)
end
end
end
end