saucy 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  class InvitationsController < ApplicationController
2
2
  before_filter :authorize_admin, :except => [:show, :update]
3
+ before_filter :ensure_account_within_users_limit, :only => [:new, :create]
3
4
  skip_before_filter :authenticate, :only => [:show, :update]
4
5
  layout Saucy::Layouts.to_proc
5
6
 
@@ -53,4 +54,8 @@ class InvitationsController < ApplicationController
53
54
  yield
54
55
  end
55
56
  end
57
+
58
+ def ensure_account_within_users_limit
59
+ ensure_account_within_limit("users")
60
+ end
56
61
  end
@@ -15,7 +15,31 @@ class Limit < ActiveRecord::Base
15
15
  where(:name => name).first
16
16
  end
17
17
 
18
+ def self.within?(limit_name, account)
19
+ if account.plan.limit(limit_name)
20
+ account.plan.limit(limit_name).within?(account)
21
+ else
22
+ true
23
+ end
24
+ end
25
+
26
+ def self.can_add_one?(limit_name, account)
27
+ if account.plan.limit(limit_name)
28
+ account.plan.limit(limit_name).can_add_one?(account)
29
+ else
30
+ true
31
+ end
32
+ end
33
+
18
34
  def allowed?
19
35
  value != 0
20
36
  end
37
+
38
+ def within?(account)
39
+ account.send(:"#{name}_count") <= value
40
+ end
41
+
42
+ def can_add_one?(account)
43
+ (account.send(:"#{name}_count") + 1) <= value
44
+ end
21
45
  end
@@ -47,3 +47,15 @@ Feature: Manage Projects
47
47
  And I should see "Project 2" within "ul.projects"
48
48
  But I should not see "Project 3" within "ul.projects"
49
49
  And I should not see "Project 4" within "ul.projects"
50
+
51
+ Scenario: Create new project when at the account limit
52
+ Given the following limit exists for the "Test" account:
53
+ | name | value |
54
+ | projects | 1 |
55
+ And the following project exists:
56
+ | account | name |
57
+ | name: Test | Project 1 |
58
+ When I go to the projects page for the "Test" account
59
+ And I follow "New Project"
60
+ Then I should be on the projects page for the "Test" account
61
+ And I should see "at your limit"
@@ -105,3 +105,11 @@ Feature: Managing users
105
105
  Then I should see "User removed"
106
106
  When I go to the memberships page for the "Test" account
107
107
  Then I should not see "Frank"
108
+
109
+ Scenario: Invite new user when at the account limit
110
+ Given the following limit exists for the "Test" account:
111
+ | name | value |
112
+ | users | 1 |
113
+ When I follow "Invite user"
114
+ Then I should be on the memberships page for the "Test" account
115
+ And I should see "at your limit"
@@ -9,3 +9,11 @@ Given /^the "([^"]*)" account was created (\d+) days ago$/ do |account_name, day
9
9
  account.save!
10
10
  end
11
11
  end
12
+
13
+ Given /^the following limit exists for the "([^"]*)" account:$/ do |account_name, table|
14
+ Account.find_by_name!(account_name).tap do |account|
15
+ table.hashes.each do |limit|
16
+ account.plan.limits.create!(limit)
17
+ end
18
+ end
19
+ end
@@ -56,6 +56,12 @@ module Saucy
56
56
  flash[:alert] = t("saucy.errors.#{failure}.#{role}")
57
57
  redirect_to path
58
58
  end
59
+
60
+ def ensure_account_within_limit(limit_name)
61
+ if !Limit.can_add_one?(limit_name, current_account)
62
+ redirect_to :back, :alert => t("saucy.errors.limited", :default => "You are at your limit of %{name} for your current plan.", :name => limit_name)
63
+ end
64
+ end
59
65
  end
60
66
  end
61
67
  end
@@ -6,6 +6,7 @@ module Saucy
6
6
  before_filter :authorize_member, :only => :show
7
7
  before_filter :authorize_admin, :except => [:show]
8
8
  before_filter :ensure_active_account, :only => [:show, :destroy, :index]
9
+ before_filter :ensure_account_within_projects_limit, :only => [:new, :create]
9
10
  layout Saucy::Layouts.to_proc
10
11
  end
11
12
 
@@ -56,6 +57,10 @@ module Saucy
56
57
  def current_project
57
58
  @project ||= ::Project.find_by_keyword!(params[:id])
58
59
  end
60
+
61
+ def ensure_account_within_projects_limit
62
+ ensure_account_within_limit("projects")
63
+ end
59
64
  end
60
65
  end
61
66
  end
@@ -33,3 +33,33 @@ describe Limit, "various kinds" do
33
33
  ssl.allowed?.should_not be
34
34
  end
35
35
  end
36
+
37
+ describe Limit, "with account and limits" do
38
+ subject { Factory(:limit, :name => "users", :value => 1) }
39
+
40
+ before do
41
+ @account = Factory(:account, :plan => subject.plan)
42
+ end
43
+
44
+ it "indicates whether the account is below the limit" do
45
+ subject.within?(@account)
46
+ end
47
+
48
+ it "can query whether the given account is below the specified limit" do
49
+ Limit.within?("users", @account)
50
+ end
51
+
52
+ it "indicates whether the specified account can add one" do
53
+ Limit.can_add_one?("users", @account).should be
54
+
55
+ Factory(:membership, :account => @account)
56
+ Factory(:project, :account => @account)
57
+
58
+ Limit.can_add_one?("users", @account).should_not be
59
+ end
60
+
61
+ it "returns true if there is no limit" do
62
+ Limit.can_add_one?("nomatch", @account).should be
63
+ Limit.within?("nomatch", @account).should be
64
+ end
65
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: saucy
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 1
10
- version: 0.3.1
9
+ - 2
10
+ version: 0.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - thoughtbot, inc.