permissify 0.0.15 → 0.0.16

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.
@@ -1,9 +1,12 @@
1
1
  module SystemFixtures::Abilities
2
+
3
+ # required interface method : app Ability class must implement
4
+ # - use Permissify::AbilityClass builder methods or *ml or whatever : just get @@abilities set
2
5
  def seed
3
6
  SPECIFY_ABILITIES_IN__APP__MODELS__SYSTEM_FIXTURES__ABILITIES
4
- # and remember to trigger in seed process by including in, for example, Ability.seed in your db/seed.rb file
5
7
 
6
- # # organize permissions into categories that correspond to your client's/product team's view of the system
8
+ # can organize permissions into categories that correspond to your client's/product team's view of app.
9
+ # suggest playing with your Ability class and the builder methods in console.
7
10
  # add_category('Tabs', 'Tabs', ['Role'], %w(Admin Dealer Corporate Brand Merchant))
8
11
  # { 'Roles' => 'Admin',
9
12
  # 'Admin Users' => 'Admin',
@@ -13,4 +16,5 @@ module SystemFixtures::Abilities
13
16
  # 'Merchant Users' => 'Merchant Admin',
14
17
  # }.each{ |category, section| add_category(category, section) }
15
18
  end
19
+
16
20
  end
@@ -1,5 +1,5 @@
1
- # TODO : this could be an activerecord, does not need to be.
2
- # - needs to support interface(s) : seed ?also: all, all_for, others?
1
+ # NOTE : this could be an activerecord, does not need to be.
2
+ # - requires implementation of 'seed' interface method : see SystemFixtures::Abilities
3
3
  class Ability
4
4
 
5
5
  class << self
@@ -29,14 +29,14 @@ module SystemFixtures::Roles
29
29
  end
30
30
 
31
31
  def super_user_permissions
32
- @@permissions = Ability.create_permissions_hash
32
+ Ability.create_permissions_hash
33
33
  end
34
34
  def system_admin_permissions
35
- @@permissions = Ability.create_permissions_hash 'roles'
35
+ Ability.create_permissions_hash 'roles'
36
36
  end
37
37
  def dealer_admin_permissions
38
- @@permissions = Ability.create_permissions_hash( [], %w(roles admin))
39
- remove %w(tabs_admin)
38
+ Ability.create_permissions_hash( [], %w(roles admin))
39
+ Ability.remove_permissions %w(tabs_admin)
40
40
  end
41
41
 
42
42
  def corporate_admin_permissions
@@ -44,17 +44,15 @@ module SystemFixtures::Roles
44
44
  end
45
45
  def brand_admin_permissions
46
46
  msa_permissions %w(corporate brand_portal_create brand_portal_update)
47
- remove %w(tabs_corporate)
47
+ Ability.remove_permissions %w(tabs_corporate)
48
48
  end
49
49
  def merchant_admin_permissions
50
50
  msa_permissions %w(corporate brand)
51
- remove %w(tabs_brand tabs_corporate)
51
+ Ability.remove_permissions %w(tabs_brand tabs_corporate)
52
52
  end
53
53
  def msa_permissions(exclude_abilities)
54
54
  no_abilities = exclude_abilities + %w(admin roles dealer)
55
- @@permissions = Ability.create_permissions_hash([], no_abilities)
56
- remove %w(tabs_admin tabs_dealer)
55
+ Ability.create_permissions_hash([], no_abilities)
56
+ Ability.remove_permissions %w(tabs_admin tabs_dealer)
57
57
  end
58
-
59
- def remove(permissions); permissions.each{|permission| @@permissions.delete(permission.to_s)}; @@permissions; end
60
58
  end
@@ -3,38 +3,61 @@ module Permissify
3
3
 
4
4
  @@abilities = []
5
5
 
6
- def clear; @@abilities = []; end
7
- def all; seed if @@abilities.empty?; @@abilities; end
6
+ def reset
7
+ @@abilities = []
8
+ end
9
+
10
+ def current
11
+ @@abilities
12
+ end
13
+
14
+ def all
15
+ seed if @@abilities.empty?
16
+ current
17
+ end
8
18
 
9
19
  def all_for(applicability_types)
10
20
  applicability_types = [applicability_types] if applicability_types.kind_of?(String)
11
21
  all.select{|a| (a[:applicability] & applicability_types) == applicability_types}
12
22
  end
13
23
 
24
+ def add(key, category, section, action, applicability, number_of_values, position, default_values, admin_expression='', category_allows = :multiple)
25
+ @@abilities << { :key => key, :category => category, :section => section, :action => action,
26
+ :applicability => applicability, :number_of_values => number_of_values, :position => position,
27
+ :default_values => default_values, :administration_expression => admin_expression, :category_allows => category_allows}
28
+ end
29
+
14
30
  def add_category(category, section, applicability=['Role'], actions=%w(View Create Update Delete), category_allows = :multiple)
15
31
  actions = [actions] unless actions.kind_of?(Array)
16
32
  actions.collect do |action|
17
33
  add("#{key_token(category)}_#{key_token(action)}", category, section, action, applicability, 1, actions.index(action)+1, [false], '', category_allows)
18
34
  end
19
35
  end
20
-
36
+
21
37
  def create_permissions_hash(view_only_categories=[], remove_categories=[], applicability_types = 'Role')
22
38
  @@permissions = {}
23
39
  all_for(applicability_types).each{|permission| @@permissions[permission[:key]] = {'0' => '1'}}
24
40
  view_only_categories.each{|category| view_only(category)}
25
- remove_categories.each{|category| remove(category)}
41
+ remove_categories.each{|category| remove_permission(category)}
42
+ @@permissions
43
+ end
44
+
45
+ def remove_permission(key_prefix)
46
+ @@permissions.keys.each{ |key| @@permissions.delete(key) if key.start_with?(key_prefix) }
47
+ @@permissions
48
+ end
49
+
50
+ def current_permissions_hash
26
51
  @@permissions
27
52
  end
28
53
 
29
54
  private
30
- def key_token(token); token.downcase.gsub('-','_').gsub(':','').gsub(' ',' ').gsub(' ','_'); end
31
- def view_only(category); %w(create update delete).each{|action| @@permissions.delete("#{category}_#{action}")}; end
32
- def remove(permission_prefix); @@permissions.keys.each{|key| @@permissions.delete(key) if key.starts_with?(permission_prefix)}; end
33
- def add(key, category, section, action, applicability, number_of_values, position, default_values, admin_expression='', category_allows = :multiple)
34
- @@abilities << { :key => key, :category => category, :section => section, :action => action,
35
- :applicability => applicability, :number_of_values => number_of_values, :position => position,
36
- :default_values => default_values, :administration_expression => admin_expression, :category_allows => category_allows}
55
+ def key_token(token)
56
+ token.downcase.gsub('-','_').gsub(':','').gsub(' ',' ').gsub(' ','_')
57
+ end
58
+
59
+ def view_only(category)
60
+ %w(create update delete).each{|action| @@permissions.delete("#{category}_#{action}")}
37
61
  end
38
-
39
62
  end
40
63
  end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe Permissify::AbilityClass do
4
+ describe 'all' do
5
+ describe 'and interface seed not implemented' do
6
+ it 'should raise NameError' do
7
+ expect{NoSeedAbility.all}.to raise_error(NameError)
8
+ end
9
+ end
10
+
11
+ describe 'and interface seed implemented' do
12
+ it 'should return the correct number of abilities' do
13
+ (a = Ability.all).size.should == 29
14
+ a.first.should == {:default_values=>[false], :applicability=>["Role"], :category=>"Tabs", :administration_expression=>"", :section=>"Tabs", :category_allows=>:multiple, :number_of_values=>1, :key=>"tabs_admin", :position=>1, :action=>"Admin"}
15
+ end
16
+ end
17
+ end
18
+
19
+ describe 'abilities builder method' do
20
+ before(:each) { Ability.reset }
21
+
22
+ describe 'reset' do
23
+ it 'should reset abilities' do
24
+ Ability.current.should == []
25
+ end
26
+ end
27
+
28
+ describe 'add_category' do
29
+ it 'should establish correct ability expression for fully specified category and action' do
30
+ add_category1_section1
31
+ Ability.current.should == [{:section=>"section1", :category=>"category1", :action=>"view", :position=>1, :key=>"category1_view", :applicability=>["Role", "Product"], :category_allows=>:one_or_none, :administration_expression=>"", :number_of_values=>1, :default_values=>[false]}]
32
+ end
33
+
34
+ it 'should establish correct ability expression for category and action with defaulted values' do
35
+ Ability.add_category('category2', 'section2')
36
+ Ability.current.should == [ {:section=>"section2", :category=>"category2", :action=>"View", :position=>1, :key=>"category2_view", :applicability=>["Role"], :category_allows=>:multiple, :administration_expression=>"", :number_of_values=>1, :default_values=>[false]},
37
+ {:section=>"section2", :category=>"category2", :action=>"Create", :position=>2, :key=>"category2_create", :applicability=>["Role"], :category_allows=>:multiple, :administration_expression=>"", :number_of_values=>1, :default_values=>[false]},
38
+ {:section=>"section2", :category=>"category2", :action=>"Update", :position=>3, :key=>"category2_update", :applicability=>["Role"], :category_allows=>:multiple, :administration_expression=>"", :number_of_values=>1, :default_values=>[false]},
39
+ {:section=>"section2", :category=>"category2", :action=>"Delete", :position=>4, :key=>"category2_delete", :applicability=>["Role"], :category_allows=>:multiple, :administration_expression=>"", :number_of_values=>1, :default_values=>[false]} ]
40
+ end
41
+ end
42
+ end
43
+
44
+ # TODO : specs for just Product, and Role and Product, applicabilities
45
+ describe 'permission builder method' do
46
+
47
+ describe 'create_permissions_hash(view_only_categories=[], remove_categories=[], applicability_types = "Role")' do
48
+ before(:each) { Ability.seed; @ability_key_set = Ability.current.collect{|a| a[:key]}.to_set }
49
+
50
+ it 'should express each current ability when invoked with no arguments' do
51
+ @ability_key_set.should == Ability.create_permissions_hash.keys.to_set
52
+ end
53
+
54
+ it 'should exclude respective permissions to create, update and delete when a view only category is specified' do
55
+ expected_keys.should == Ability.create_permissions_hash(['roles']).keys.to_set
56
+ end
57
+
58
+ it 'should exclude permissions for all permissions keys that begin with string in input remove categories arg' do
59
+ expected_keys('roles_view').should == Ability.create_permissions_hash([], ['ro']).keys.to_set
60
+ end
61
+
62
+ def expected_keys(other_excluded_key = nil, excluded_keys = %w(roles_create roles_update roles_delete))
63
+ @ability_key_set - [other_excluded_key] - excluded_keys
64
+ end
65
+ end
66
+
67
+ describe 'remove_permission' do
68
+ before(:each) { Ability.reset }
69
+
70
+ it 'should remove all keys in the permissions hash that start with the input prefix' do
71
+ Ability.reset
72
+ add_category1_section1 %w(view1 view2 bill)
73
+ Ability.create_permissions_hash
74
+ Ability.remove_permission('category1_view')
75
+ Ability.current_permissions_hash.should == {"category1_bill"=>{"0"=>"1"}}
76
+ end
77
+ end
78
+ end
79
+
80
+ def add_category1_section1(actions = 'view')
81
+ Ability.add_category('category1', 'section1', %w(Role Product), actions, :one_or_none)
82
+ end
83
+ end
data/spec/spec_helper.rb CHANGED
@@ -8,6 +8,7 @@ require 'rubygems'
8
8
  require 'bundler/setup'
9
9
 
10
10
  require 'permissify'
11
+ require 'permissify/ability_class'
11
12
 
12
13
  RSpec.configure do |config|
13
14
  config.treat_symbols_as_metadata_keys_with_true_values = true
@@ -17,11 +18,23 @@ end
17
18
 
18
19
  class Ability
19
20
  class << self
20
- include Permissify::Ability
21
+ include Permissify::AbilityClass
21
22
 
22
23
  def seed
23
- # required interface method : app Ability class must implement
24
- # - use Permissify::Abilities helper methods or *ml or whatever : just get @@abilities set
24
+ add_category('Tabs', 'Tabs', ['Role'], %w(Admin Dealer Corporate Brand Merchant))
25
+ { 'Roles' => 'Admin',
26
+ 'Admin Users' => 'Admin',
27
+ 'Dealer Users' => 'Dealer Admin',
28
+ 'Corporate Users' => 'Corporate Admin',
29
+ 'Brand Users' => 'Brand Admin',
30
+ 'Merchant Users' => 'Merchant Admin',
31
+ }.each{ |category, section| add_category(category, section) }
25
32
  end
26
33
  end
27
34
  end
35
+
36
+ class NoSeedAbility
37
+ class << self
38
+ include Permissify::AbilityClass
39
+ end
40
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: permissify
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 63
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 15
10
- version: 0.0.15
9
+ - 16
10
+ version: 0.0.16
11
11
  platform: ruby
12
12
  authors:
13
13
  - Frederick Fix
@@ -108,7 +108,7 @@ files:
108
108
  - lib/permissify/model_class.rb
109
109
  - lib/permissify/roles.rb
110
110
  - lib/permissify.rb
111
- - spec/permissify/ability_spec.rb
111
+ - spec/permissify/ability_class_spec.rb
112
112
  - spec/spec.opts
113
113
  - spec/spec_helper.rb
114
114
  - CHANGELOG.rdoc
@@ -1,42 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Ability do
4
- describe "all" do
5
- it "should return the correct number of abilities" do
6
- Ability.all.size.should == 189
7
- end
8
- end
9
-
10
- # describe "all_for" do
11
- # it "should return the correct number of abilities when invoked with 'Product' applicability type" do
12
- # Ability.all_for('Product').size.should == 50
13
- # end
14
- #
15
- # it "should return the correct number of abilities when invoked with 'Role' applicability type" do
16
- # Ability.all_for('Role').size.should == 179
17
- # end
18
- #
19
- # it "should return the correct number of abilities when invoked with both 'Product' and 'Role' applicability types" do
20
- # Ability.all_for(%w(Product Role)).size.should == 40
21
- # end
22
- #
23
- # it "should do the right set logic" do
24
- # (Ability.all_for('Product') & Ability.all_for('Role')).should == Ability.all_for(%w(Product Role))
25
- # end
26
- # end
27
- #
28
- # describe "create_permissions_hash" do
29
- # it "should return the correct number of entries when full access to all Role permissions" do
30
- # Ability.create_permissions_hash([], [], 'Role').keys.size.should == 179
31
- # end
32
- # it "should return the correct number of entries when full access to all Role permissions except view only to a single category" do
33
- # Ability.create_permissions_hash(['loyalty_id'], [], 'Role').keys.size.should == 176
34
- # end
35
- # it "should return the correct number of entries when full access to all Role permissions except view only to a single category" do
36
- # Ability.create_permissions_hash([], ['loyalty_id'], 'Role').keys.size.should == 175
37
- # end
38
- # it "should yield the expected difference" do
39
- # (Ability.create_permissions_hash(['loyalty_id'], [], 'Role') - Ability.create_permissions_hash([], ['loyalty_id'], 'Role')).should == {"loyalty_id_view"=>{"0"=>"1"}}
40
- # end
41
- # end
42
- end