-
Notifications
You must be signed in to change notification settings - Fork 40
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
Allow users to opt-out of overdrafting #87
Draft
YtvwlD
wants to merge
4
commits into
chaosdorf:main
Choose a base branch
from
YtvwlD:no-overdraft
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a88f0cc
:sparkles: Allow users to opt-out of overdrafting
YtvwlD b45a43b
:white_check_mark: Add tests for disabling overdrafting
YtvwlD 954e0a5
:recycle: Just fade out alerts created by flash
YtvwlD e00a593
:sparkles: Hide some drinks when overdrawing is disabled
YtvwlD 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
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 |
---|---|---|
|
@@ -12,6 +12,11 @@ def index | |
def show | ||
@user = User.find(params[:id]) | ||
@drinks = Drink.order(active: :desc).order_by_name_asc | ||
@drinks_missing = false | ||
unless @user.can_overdraw | ||
@drinks = @drinks.where(price: ([email protected])) | ||
@drinks_missing = Drink.where.not(price: ([email protected])).exists? | ||
end | ||
# show.html.haml | ||
end | ||
|
||
|
@@ -130,7 +135,7 @@ def buy_drink | |
end | ||
|
||
def user_params | ||
params.require(:user).permit(:name, :email, :balance, :active, :audit, :redirect) | ||
params.require(:user).permit(:name, :email, :balance, :active, :audit, :redirect, :can_overdraw) | ||
end | ||
|
||
def warn_user_if_audit | ||
|
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,4 +1,4 @@ | ||
- flash.each do |level, message| | ||
.alert(class="alert-#{level}") | ||
.flash.alert(class="alert-#{level}") | ||
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. same as above |
||
%strong= "#{level}!" | ||
= message |
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,10 @@ | ||
class AddCanOverdrawToUsers < ActiveRecord::Migration[5.2] | ||
def up | ||
add_column :users, :can_overdraw, :boolean, default: true | ||
User.all.each { |user| user.can_overdraw = true } | ||
end | ||
|
||
def down | ||
remove_column :users, :can_overdraw | ||
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ one: | |
name: A | ||
email: [email protected] | ||
balance: 100 | ||
can_overdraw: false | ||
|
||
two: | ||
name: B | ||
|
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 |
---|---|---|
|
@@ -6,12 +6,32 @@ class UserTest < ActiveSupport::TestCase | |
assert_not user.save, "Saved the user without a name" | ||
end | ||
|
||
test "should not save without overdraft if balance is below zero" do | ||
user = User.new | ||
user.can_overdraw = false | ||
user.balance = -1 | ||
assert_not user.save, "Saved the user with overdrafting disabled and balance below zero" | ||
end | ||
|
||
test "should save" do | ||
user = User.new | ||
user.name = "test" | ||
assert user.save, "Failed to save the user with a name" | ||
end | ||
|
||
test "should not modify if overdrafting is disabled and balance is below zero" do | ||
user = users(:one) | ||
user.balance = -1 | ||
assert_not user.save, "Modified the user with overdrafting disabled and balance below zero" | ||
end | ||
|
||
test "should modify if overdrafting is enabled and balance is below zero" do | ||
user = users(:one) | ||
user.balance = -1 | ||
user.can_overdraw = true | ||
assert user.save, "Failed to modify the user with overdrafting enabled and balance below zero" | ||
end | ||
|
||
test "should deposit" do | ||
assert users(:one).deposit(rand(500)), "Failed to deposit money" | ||
end | ||
|
@@ -25,17 +45,25 @@ class UserTest < ActiveSupport::TestCase | |
end | ||
|
||
test "should pay" do | ||
assert users(:one).payment(rand(500)), "Failed to pay" | ||
assert users(:one).payment(rand(99)), "Failed to pay" | ||
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. why the change to |
||
end | ||
|
||
test "payment should decrease balance" do | ||
user = users(:one) | ||
amount = rand(500) | ||
amount = rand(99) | ||
balance_was = user.balance | ||
user.payment amount | ||
assert_equal balance_was - amount, user.balance, "Payment didn't decrease the balance" | ||
end | ||
|
||
test "payment should not lead to a negative balance if overdrafting is disabled" do | ||
user = users(:one) | ||
amount = 200 | ||
assert_raises ActiveRecord::RecordInvalid do | ||
user.payment amount | ||
end | ||
end | ||
|
||
test "should buy a drink" do | ||
assert users(:one).buy(drinks(:one)), "Failed to buy" | ||
end | ||
|
@@ -48,6 +76,15 @@ class UserTest < ActiveSupport::TestCase | |
assert_equal balance_was - drink.price, user.balance, "Buying didn't decrease the balance" | ||
end | ||
|
||
test "buying should not lead to a negative balance if overdrafting is disabled" do | ||
user = users(:one) | ||
user.balance = 1 | ||
drink = drinks(:one) | ||
assert_raises ActiveRecord::RecordInvalid do | ||
user.buy drink | ||
end | ||
end | ||
|
||
test "should have attributes" do | ||
u = users(:one) | ||
assert_respond_to u, :id, "id missing" | ||
|
@@ -59,5 +96,6 @@ class UserTest < ActiveSupport::TestCase | |
assert_respond_to u, :active, "active missing" | ||
assert_respond_to u, :audit, "audit missing" | ||
assert_respond_to u, :redirect, "redirect missing" | ||
assert_respond_to u, :can_overdraw, "can_overdraw missing" | ||
end | ||
end |
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.
Is this related to #88? General UI changes shouldn't happen in this PR.