restrict 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 288411815db7101a293671bce57958441289d44d
4
- data.tar.gz: 0999aca9b587696b861e0db17c1dfc8736562808
3
+ metadata.gz: 0e119fe0a7f5a3b29548df9ff5d6f6bf77b490ab
4
+ data.tar.gz: 34a0da6663e3113ec9fcd1dce4b1bdde1afc7ae2
5
5
  SHA512:
6
- metadata.gz: ddd2451c5d3ef62411ad1577568af474447007a96d3c97bb524d9425c7f81dba85c98650ca8a6e3ffeeea3eb6fbb8c7c0f690a432dd363aea96cfb6b7d5cfdd0
7
- data.tar.gz: d750c25ef6283f7d160e7046b4edd11873b827bd9249fb7e2cf941255a0a7a839a89d0c7133a244adff61c5c65dc0a6bd207e41aab77339346a5803806af3baf
6
+ metadata.gz: b7d7b6b0e2da3fea9b62fffadd031b464956659a7123d6dbab3c7d66e9fb7a060aa8deef33327af5326ec922985560852a0b38827110fc607c2caee9a89c4c35
7
+ data.tar.gz: 4dce49238f2706ac0930f2d7a59f859cff47cb61d2709422bb9072fe4b916676fb854d33ba25bbe6536afba23596186ab1542487b5227012d5bb5ab6d4b145b4
@@ -1,3 +1,6 @@
1
+ [0.0.6] - 2014-08-25
2
+ * Breaking change: use :unless instead of :allow_if
3
+
1
4
  [0.0.4] - 2014-08-23
2
5
  * Bugfixes
3
6
  * Multiple restrictions for the same action are possible
data/README.md CHANGED
@@ -17,7 +17,7 @@ Works with rails 3 and 4 and all versions every ruby 2.
17
17
  ```ruby
18
18
  class GoodiesController < ApplicationController
19
19
  restrict :take
20
- restrict :delete, allow_if: :goodie_manager?
20
+ restrict :delete, unless: :goodie_manager?
21
21
 
22
22
  def take
23
23
  # Grab a goodie
@@ -47,7 +47,7 @@ What that does:
47
47
  restrict :all_actions
48
48
  ```
49
49
 
50
- This one will apply to all actions on this controller. It takes the `allow_if` option as well.
50
+ This one will apply to all actions on this controller. It takes the `unless` option as well.
51
51
 
52
52
  ### Configuration
53
53
 
@@ -60,7 +60,7 @@ You may set the method that is used to figure out whether a user is signed in or
60
60
 
61
61
  ## Todo Ideas
62
62
 
63
- * restrict :all_actions, except: [:new], allow_if: 'dsfsdf'
63
+ * restrict :all_actions, except: [:new], unless: 'dsfsdf'
64
64
 
65
65
  ## Contributing
66
66
 
@@ -16,8 +16,8 @@ module Restrict
16
16
  def handle_restriction(restriction, controller)
17
17
  validate_signed_in(controller)
18
18
 
19
- if restriction.allow_if
20
- unless controller.__send__(restriction.allow_if)
19
+ if restriction.unless
20
+ unless controller.__send__(restriction.unless)
21
21
  raise Restrict::AccessDenied, reason: restriction
22
22
  end
23
23
  end
@@ -26,7 +26,7 @@ module Restrict
26
26
  def concerning_restrictions(controller)
27
27
  controller.restrictions or return
28
28
  controller.restrictions.select do |restriction|
29
- restriction.concerning?(controller.action_name)
29
+ restriction.applies_to?(controller.action_name)
30
30
  end
31
31
  end
32
32
  end
@@ -14,6 +14,9 @@ module Restrict
14
14
  restrictions << Restrict::Restriction.new(*args)
15
15
  end
16
16
 
17
+ # This could happen in included block as well, but often you need
18
+ # other before filters to happen before you actually check the
19
+ # restrictions, so lets set it where it is used in the code as well.
17
20
  def install_gatekeeper
18
21
  return if @gatekeeper_installed
19
22
  before_filter :invoke_gatekeeper
@@ -1,25 +1,25 @@
1
1
  module Restrict
2
2
  class Restriction
3
- attr_accessor :actions, :allow_if
3
+ attr_accessor :actions, :unless
4
4
 
5
5
  def initialize(*args)
6
6
  options = args.extract_options!
7
- @allow_if = options[:allow_if]
7
+ @unless = options[:unless]
8
8
  @actions = args
9
9
  actions.empty? and raise ArgumentError, "expected actions to restrict, but got #{actions.inspect}"
10
10
  end
11
11
 
12
- def concerning?(action)
13
- concerns_action?(action) || concerns_all?
12
+ def applies_to?(action)
13
+ applies_to_action?(action) || applies_to_all_actions?
14
14
  end
15
15
 
16
16
  private
17
17
 
18
- def concerns_all?
18
+ def applies_to_all_actions?
19
19
  actions.include?(:all_actions)
20
20
  end
21
21
 
22
- def concerns_action?(name)
22
+ def applies_to_action?(name)
23
23
  actions.include?(name.to_sym)
24
24
  end
25
25
  end
@@ -4,12 +4,12 @@ RSpec::Matchers.define :have_restriction_on do |given_action_name|
4
4
  @given_controller = given_controller
5
5
 
6
6
  @restriction = given_controller.restrictions.find do |restriction|
7
- restriction.concerning?(given_action_name)
7
+ restriction.applies_to?(given_action_name)
8
8
  end
9
9
 
10
10
  if @restriction
11
- if @given_allow_if
12
- @restriction.allow_if == @given_allow_if
11
+ if @given_unless
12
+ @restriction.unless == @given_unless
13
13
  else
14
14
  true
15
15
  end
@@ -18,21 +18,21 @@ RSpec::Matchers.define :have_restriction_on do |given_action_name|
18
18
  end
19
19
  end
20
20
 
21
- chain :with_allow_if do |given_allow_if|
22
- @given_allow_if = given_allow_if
21
+ chain :unless do |given_unless|
22
+ @given_unless = given_unless
23
23
  end
24
24
 
25
25
  failure_message do |actual|
26
- if @restriction && @given_allow_if
27
- "Expected restriction to call #{@given_allow_if.inspect}, but calls #{@restriction.allow_if.inspect}"
26
+ if @restriction && @given_unless
27
+ "Expected restriction to call #{@given_unless.inspect}, but calls #{@restriction.unless.inspect}"
28
28
  else
29
29
  "Expected to have restriction on #{@given_action_name}, but was not found in #{@given_controller.restrictions.inspect}"
30
30
  end
31
31
  end
32
32
 
33
33
  failure_message_when_negated do |actual|
34
- if @given_allow_if
35
- "Expected restriction not to call #{@given_allow_if.inspect}, but calls #{@restriction.allow_if.inspect}"
34
+ if @given_unless
35
+ "Expected restriction not to call #{@given_unless.inspect}, but calls #{@restriction.unless.inspect}"
36
36
  else
37
37
  "Expected not to have restriction on #{@given_action_name}, but was found in #{@given_controller.restrictions.inspect}"
38
38
  end
@@ -4,12 +4,12 @@ RSpec::Matchers.define :have_restriction_on do |given_action_name|
4
4
  @given_controller = given_controller
5
5
 
6
6
  @restriction = given_controller.restrictions.find do |restriction|
7
- restriction.concerning?(given_action_name)
7
+ restriction.applies_to?(given_action_name)
8
8
  end
9
9
 
10
10
  if @restriction
11
- if @given_allow_if
12
- @restriction.allow_if == @given_allow_if
11
+ if @given_unless
12
+ @restriction.unless == @given_unless
13
13
  else
14
14
  true
15
15
  end
@@ -18,21 +18,21 @@ RSpec::Matchers.define :have_restriction_on do |given_action_name|
18
18
  end
19
19
  end
20
20
 
21
- chain :with_allow_if do |given_allow_if|
22
- @given_allow_if = given_allow_if
21
+ chain :unless do |given_unless|
22
+ @given_unless = given_unless
23
23
  end
24
24
 
25
25
  failure_message_for_should do |actual|
26
- if @restriction && @given_allow_if
27
- "Expected restriction to call #{@given_allow_if.inspect}, but calls #{@restriction.allow_if.inspect}"
26
+ if @restriction && @given_unless
27
+ "Expected restriction to call #{@given_unless.inspect}, but calls #{@restriction.unless.inspect}"
28
28
  else
29
29
  "Expected to have restriction on #{@given_action_name}, but was not found in #{@given_controller.restrictions.inspect}"
30
30
  end
31
31
  end
32
32
 
33
33
  failure_message_for_should_not do |actual|
34
- if @given_allow_if
35
- "Expected restriction not to call #{@given_allow_if.inspect}, but calls #{@restriction.allow_if.inspect}"
34
+ if @given_unless
35
+ "Expected restriction not to call #{@given_unless.inspect}, but calls #{@restriction.unless.inspect}"
36
36
  else
37
37
  "Expected not to have restriction on #{@given_action_name}, but was found in #{@given_controller.restrictions.inspect}"
38
38
  end
@@ -1,3 +1,3 @@
1
1
  module Restrict
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -34,9 +34,9 @@ describe Restrict::Gatekeeper do
34
34
 
35
35
  context 'with conditional restriction' do
36
36
  before do
37
- controller.class.restrict :action1, allow_if: :missing
38
- controller.class.restrict :action2, allow_if: :falsy
39
- controller.class.restrict :action3, allow_if: :truthy
37
+ controller.class.restrict :action1, unless: :missing
38
+ controller.class.restrict :action2, unless: :falsy
39
+ controller.class.restrict :action3, unless: :truthy
40
40
  end
41
41
 
42
42
  it 'raises on missing method' do
@@ -66,7 +66,7 @@ describe Restrict::Gatekeeper do
66
66
  context 'with multiple restrictions' do
67
67
  before do
68
68
  controller.class.restrict :all_actions
69
- controller.class.restrict :edit, allow_if: :falsy
69
+ controller.class.restrict :edit, unless: :falsy
70
70
  end
71
71
 
72
72
  it 'denies access if any restriction fails' do
@@ -6,7 +6,7 @@ describe Restrict::Rails::Controller do
6
6
 
7
7
  before do
8
8
  controller.class.restrict :index
9
- controller.class.restrict :show, allow_if: :access_allowed?
9
+ controller.class.restrict :show, unless: :access_allowed?
10
10
  end
11
11
 
12
12
  describe '#restrict' do
@@ -15,7 +15,7 @@ describe Restrict::Rails::Controller do
15
15
  end
16
16
 
17
17
  it 'builds and adds a conditional restriction' do
18
- expect(controller).to have_restriction_on(:show).with_allow_if(:access_allowed?)
18
+ expect(controller).to have_restriction_on(:show).unless(:access_allowed?)
19
19
  end
20
20
  end
21
21
 
@@ -13,23 +13,23 @@ describe Restrict::Restriction do
13
13
  end
14
14
  end
15
15
 
16
- describe '#concerning?' do
16
+ describe '#applies_to?' do
17
17
  it 'returns true if the given action is contained' do
18
- expect(restriction).to be_concerning(:show)
18
+ expect(restriction).to be_applies_to(:show)
19
19
  end
20
20
 
21
21
  it 'returns true if the given name is a string' do
22
- expect(restriction).to be_concerning('show')
22
+ expect(restriction).to be_applies_to('show')
23
23
  end
24
24
 
25
25
  it 'returns false if the given action name is not contained' do
26
- expect(restriction).not_to be_concerning(:index)
26
+ expect(restriction).not_to be_applies_to(:index)
27
27
  end
28
28
 
29
29
  it 'returns true if it concerns :all_actions' do
30
30
  restriction = Restrict::Restriction.new(:all_actions)
31
- expect(restriction).to be_concerning(:foo)
32
- expect(restriction).to be_concerning(:bar)
31
+ expect(restriction).to be_applies_to(:foo)
32
+ expect(restriction).to be_applies_to(:bar)
33
33
  end
34
34
  end
35
35
  end
@@ -3,12 +3,6 @@ require 'spec_helper'
3
3
  describe 'have_restriction_on' do
4
4
  let(:controller) { ExampleController.new }
5
5
 
6
- # before do
7
- # controller.class.restrict :index
8
- # controller.class.restrict :show, allow_if: :access_allowed?
9
- # end
10
-
11
-
12
6
  context 'without restrictions' do
13
7
  it 'matcher fails' do
14
8
  expect {
@@ -42,17 +36,17 @@ describe 'have_restriction_on' do
42
36
 
43
37
  it 'matcher conditional chain fails' do
44
38
  expect {
45
- expect(controller).to have_restriction_on(:show).with_allow_if(:something)
39
+ expect(controller).to have_restriction_on(:show).unless(:something)
46
40
  }.to raise_error RSpec::Expectations::ExpectationNotMetError
47
41
  end
48
42
 
49
43
  it 'negated matcher with conditional chain passes' do
50
- expect(controller).not_to have_restriction_on(:show).with_allow_if(:something)
44
+ expect(controller).not_to have_restriction_on(:show).unless(:something)
51
45
  end
52
46
  end
53
47
 
54
48
  context 'with conditional restriction' do
55
- before { controller.class.restrict :show, allow_if: :something }
49
+ before { controller.class.restrict :show, unless: :something }
56
50
 
57
51
  it 'matcher passes' do
58
52
  expect(controller).to have_restriction_on(:show)
@@ -71,12 +65,12 @@ describe 'have_restriction_on' do
71
65
  end
72
66
 
73
67
  it 'matcher conditional chain passes' do
74
- expect(controller).to have_restriction_on(:show).with_allow_if(:something)
68
+ expect(controller).to have_restriction_on(:show).unless(:something)
75
69
  end
76
70
 
77
71
  it 'negated matcher with conditional chain passes' do
78
72
  expect {
79
- expect(controller).not_to have_restriction_on(:show).with_allow_if(:something)
73
+ expect(controller).not_to have_restriction_on(:show).unless(:something)
80
74
  }.to raise_error RSpec::Expectations::ExpectationNotMetError
81
75
  end
82
76
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restrict
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johannes Opper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-24 00:00:00.000000000 Z
11
+ date: 2014-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails