Skip to content

Commit fe614df

Browse files
committed
Prevent from creating cards when the limit has been exceeded
1 parent 1203a05 commit fe614df

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

app/controllers/cards_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class CardsController < ApplicationController
44
before_action :set_board, only: %i[ create ]
55
before_action :set_card, only: %i[ show edit update destroy ]
66
before_action :ensure_permission_to_administer_card, only: %i[ destroy ]
7+
before_action :ensure_can_create_cards, only: %i[ create ]
78

89
def index
910
set_page_and_extract_portion_from @filter.cards
@@ -42,6 +43,10 @@ def ensure_permission_to_administer_card
4243
head :forbidden unless Current.user.can_administer_card?(@card)
4344
end
4445

46+
def ensure_can_create_cards
47+
head :forbidden if Current.account.card_limit_exceeded?
48+
end
49+
4550
def card_params
4651
params.expect(card: [ :status, :title, :description, :image, tag_ids: [] ])
4752
end

test/controllers/cards_controller_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ class CardsControllerTest < ActionDispatch::IntegrationTest
3535
assert_redirected_to draft
3636
end
3737

38+
test "cannot create cards when card limit exceeded" do
39+
logout_and_sign_in_as :mike
40+
accounts(:initech).update_column(:cards_count, 1001)
41+
42+
assert_no_difference -> { Card.count } do
43+
post board_cards_path(boards(:miltons_wish_list), script_name: accounts(:initech).slug)
44+
end
45+
46+
assert_response :forbidden
47+
end
48+
3849
test "show" do
3950
get card_path(cards(:logo))
4051
assert_response :success

test/fixtures/accesses.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ private_kevin:
2121
account: 37s_uuid
2222
board: private_uuid
2323
user: kevin_uuid
24+
25+
miltons_wish_list_mike:
26+
id: <%= ActiveRecord::FixtureSet.identify("miltons_wish_list_mike", :uuid) %>
27+
account: initech_uuid
28+
board: miltons_wish_list_uuid
29+
user: mike_uuid

0 commit comments

Comments
 (0)