From 75585f9012c5b22c13cd12c0d94428407bf26243 Mon Sep 17 00:00:00 2001 From: Dariush Hasanpour Date: Sun, 16 Apr 2017 23:43:49 +0430 Subject: [PATCH] @ lib/acu/helpers/helpers.rb: + ::acu_except --- Gemfile.lock | 6 +++--- README.md | 7 ++++++- lib/acu/helpers/helpers.rb | 4 ++++ lib/acu/version.rb | 2 +- .../spec/controllers/home_controller_spec.rb | 20 ++++++++++++++++++- 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index a4f3291..7bd1c25 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,7 +7,7 @@ GIT PATH remote: . specs: - rails-acu (2.1.0) + rails-acu (2.2.0) rails (~> 5.0.0, >= 5.0.0) GEM @@ -63,8 +63,8 @@ GEM warden (~> 1.2.3) diff-lcs (1.3) erubis (2.7.0) - globalid (0.3.7) - activesupport (>= 4.1.0) + globalid (0.4.0) + activesupport (>= 4.2.0) i18n (0.8.1) jquery-rails (4.3.1) rails-dom-testing (>= 1, < 3) diff --git a/README.md b/README.md index 8139046..81b66ff 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ The method `Acu::Monitor.gaurd` accepts a hashed list of agruments named `by`, p ### Some handy helpers Although you can define a binary allow/deny access rule in the `acu_rules.rb` file but there will be some gray area that neither you can allow _full access_ to the resource nor _no access_.
-for those situations you allow the entities to get access but limits their operations in the action/view/layout with the `acu_is?` and `acu_as` helpers, here is some usage example of them: +For those situations you allow the entities to get access but limits their operations in the action/view/layout with the `acu_is?`, `acu_as` and `acu_except` helpers, here is some usage example of them: ```ruby # return true if the entity `:admin`'s block in `whois :admin` return true, otherwise false @@ -128,6 +128,11 @@ end acu_as [:admin, :client] do puts 'You are either `admin` or `client`' end + +# DO NOT executes the block if current user identified as either `:guest` +acu_except [:guest] do + puts 'Except `:guest`s anyone else can execute this code' +end ``` ### Configurations diff --git a/lib/acu/helpers/helpers.rb b/lib/acu/helpers/helpers.rb index 8f26232..10c8841 100644 --- a/lib/acu/helpers/helpers.rb +++ b/lib/acu/helpers/helpers.rb @@ -6,4 +6,8 @@ def acu_is? symbol def acu_as symbol yield if acu_is? symbol +end + +def acu_except symbol + yield if not acu_is? symbol end \ No newline at end of file diff --git a/lib/acu/version.rb b/lib/acu/version.rb index d95396d..196f506 100644 --- a/lib/acu/version.rb +++ b/lib/acu/version.rb @@ -1,3 +1,3 @@ module Acu - VERSION = '2.1.0' + VERSION = '2.2.0' end diff --git a/spec/dummy/spec/controllers/home_controller_spec.rb b/spec/dummy/spec/controllers/home_controller_spec.rb index a340452..cf3d8e2 100644 --- a/spec/dummy/spec/controllers/home_controller_spec.rb +++ b/spec/dummy/spec/controllers/home_controller_spec.rb @@ -526,7 +526,7 @@ def setup **kwargs expect(acu_is? :everyone).to be true expect(acu_is? :client).to be false end - it "acu_do" do + it "acu_as" do Acu::Rules.define do whois :everyone { true } whois :client { false } @@ -544,6 +544,24 @@ def setup **kwargs expect(acu_is? :everyone).to be true end end + it "acu_except" do + Acu::Rules.define do + whois :everyone { true } + whois :client { false } + end + acu_except :everyone do + # an invalid syntax, this should never run + expect(true).not_to be true + end + acu_except :client do + # a valid syntax + expect(true).to be true + end + # no-one gets through + acu_except [:client, :everyone] do + expect(true).not_to be true + end + end end context 'caching' do it '[Rails.cache]' do