Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

Commit

Permalink
Make subjects an array instead of a hash
Browse files Browse the repository at this point in the history
Adds ruby 2.5.0 to travis test
  • Loading branch information
xjunior committed Mar 29, 2019
1 parent 07f9ce1 commit 7bd9372
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 24 deletions.
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.5.0
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ sudo: false
language: ruby
rvm:
- 2.2.2
before_install: gem install bundler -v 1.12.1
- 2.5.0
before_install: gem install bundler -v 1.17.3
deploy:
provider: rubygems
api_key:
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* DSL validate Consent state
2 changes: 1 addition & 1 deletion consent.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|

spec.add_development_dependency 'activesupport', '~> 3.2.22'
spec.add_development_dependency 'cancancan', '~> 1.15.0'
spec.add_development_dependency 'bundler', '~> 1.12'
spec.add_development_dependency 'bundler', '~> 1.17.3'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
end
16 changes: 14 additions & 2 deletions lib/consent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ def self.default_views
end

def self.subjects
@subjects ||= {}
@subjects ||= []
end

def self.find_subjects(subject_key)
@subjects.find_all do |subject|
subject.key.eql?(subject_key)
end
end

def self.find_view(subject_key, action_key, view_key)
Consent.find_subject(subject_key)
.map{|subject| subject.view(action_key, view_key)}
.first
end

def self.load_subjects!(paths)
Expand All @@ -26,7 +38,7 @@ def self.load_subjects!(paths)

def self.define(key, label, options = {}, &block)
defaults = options.fetch(:defaults, {})
subjects[key] = Subject.new(key, label).tap do |subject|
subjects << Subject.new(key, label).tap do |subject|
DSL.build(subject, defaults, &block)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/consent/permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(permissions)
end

def each(&block)
Consent.subjects.values.each do |subject|
Consent.subjects.each do |subject|
subject.actions.map do |action|
map_permission subject, action
end.compact.each(&block)
Expand Down
4 changes: 2 additions & 2 deletions spec/nitro/consent/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
let(:ability) { Consent::Ability.new(permissions, user) }

it 'it authorizes symbol permissions' do
permissions[:features] = { beta: '1' }
permissions[:beta] = { lol_til_death: '1' }

expect(ability).to be_able_to(:beta, :features)
expect(ability).to be_able_to(:lol_til_death, :beta)
end

it 'it authorizes model permissions' do
Expand Down
34 changes: 22 additions & 12 deletions spec/nitro/consent/permissions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
expect(permission.view_key).to be :view1
end

it 'maps a permissions hash to consent subjects with subjects split in different definitions' do
permissions_hash = { beta: { lol_til_death: true, request_frustration: true } }

subjects = Consent.permissions(permissions_hash).map(&:subject_key)
actions = Consent.permissions(permissions_hash).map(&:action_key)

expect(subjects).to include :beta
expect(actions).to include :lol_til_death, :request_frustration
end

it 'maps string view keys' do
permissions_hash = { 'some_model' => { 'action1' => 'view1' } }

Expand All @@ -22,53 +32,53 @@
end

it 'maps symbol subjects' do
permissions_hash = { features: { beta: true } }
permissions_hash = { beta: { lol_til_death: true } }

permission = Consent.permissions(permissions_hash).to_a.last

expect(permission.subject_key).to be :features
expect(permission.action_key).to be :beta
expect(permission.subject_key).to be :beta
expect(permission.action_key).to be :lol_til_death
expect(permission.view_key).to be nil
end

it 'empty view means no permission' do
permissions_hash = { features: { beta: '' } }
permissions_hash = { beta: { lol_til_death: '' } }

permissions = Consent.permissions(permissions_hash)

expect(permissions.map(&:subject_key)).to_not include(:features)
expect(permissions.map(&:subject_key)).to_not include(:beta)
end

it '0 view means no permission' do
permissions_hash = { features: { beta: 0 } }
permissions_hash = { beta: { lol_til_death: 0 } }

permissions = Consent.permissions(permissions_hash)

expect(permissions.map(&:subject_key)).to_not include(:features)
expect(permissions.map(&:subject_key)).to_not include(:beta)
end

it '"0" view means no permission' do
permissions_hash = { features: { beta: '0' } }
permissions_hash = { beta: { lol_til_death: '0' } }

permissions = Consent.permissions(permissions_hash)

expect(permissions.map(&:subject_key)).to_not include(:features)
expect(permissions.map(&:subject_key)).to_not include(:beta)
end

it 'unexisting view means no permission' do
permissions_hash = { features: { beta: :something_funky } }
permissions_hash = { beta: { lol_til_death: :something_funky } }

permissions = Consent.permissions(permissions_hash)

expect(permissions.map(&:subject_key)).to_not include(:features)
expect(permissions.map(&:subject_key)).to_not include(:beta)
end

it 'does not include permissions not given that do not default' do
permissions_hash = {}

permissions = Consent.permissions(permissions_hash)

expect(permissions.map(&:subject_key)).to_not include(:features)
expect(permissions.map(&:subject_key)).to_not include(:beta)
end

it 'is the default view when no permission' do
Expand Down
17 changes: 14 additions & 3 deletions spec/nitro/consent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
it 'creates a new subject with the given key and label' do
Consent.define(:lol_key, 'My Label') {}

expect(Consent.subjects[:lol_key].label).to eql 'My Label'
expect(Consent.subjects[:lol_key].key).to eql :lol_key
expect(Consent.subjects.last.label).to eql 'My Label'
expect(Consent.subjects.last.key).to eql :lol_key
end

it 'yields a in dsl context' do
Expand All @@ -16,7 +16,7 @@
end

expect(build_context).to be_a(Consent::DSL)
expect(build_context.subject).to be Consent.subjects[:lol_key]
expect(build_context.subject).to be Consent.subjects.last
end

it 'yields a in dsl context with defaults' do
Expand All @@ -28,5 +28,16 @@

Consent.define :lol_key, 'My Label', defaults: defaults, &block
end

it 'allows a subject to have multiple action definitions' do
Consent.define(:lol_key, 'LOL at work') {}
Consent.define(:lol_key, 'LOL at home') {}

keys = Consent.subjects.map(&:key)
labels = Consent.subjects.map(&:label)

expect(labels).to include 'LOL at work', 'LOL at home'
expect(keys).to include :lol_key
end
end
end
8 changes: 6 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
action :destroy, 'Destroy', views: [:lol, :self], default_view: :future
end

Consent.define :features, 'My Label' do
action :beta, 'Beta feature'
Consent.define :beta, 'LOL Department (Beta)' do
action :lol_til_death, 'LOL Until you die'
end

Consent.define :beta, 'Frustration Department (Beta)' do
action :request_frustration, 'Request Frustration'
end

0 comments on commit 7bd9372

Please sign in to comment.