Skip to content

Commit

Permalink
Merge pull request #51 from Exelord/quick-fix
Browse files Browse the repository at this point in the history
Quick fix for v2.0
  • Loading branch information
Exelord committed Sep 10, 2016
2 parents c2355bb + d9208e5 commit 3053828
Show file tree
Hide file tree
Showing 74 changed files with 58 additions and 21 deletions.
12 changes: 6 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
.bundle/
log/*.log
pkg/
spec/dummy/log/*.log
spec/dummy/db/*.sqlite3
spec/dummy/db/*.sqlite3-journal
spec/dummy/log/*.log
spec/dummy/tmp/
spec/dummy/.sass-cache
dummy/log/*.log
dummy/db/*.sqlite3
dummy/db/*.sqlite3-journal
dummy/log/*.log
dummy/tmp/
dummy/.sass-cache
.DS_Store
coverage/
.lock-*
Expand Down
6 changes: 3 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
AllCops:
TargetRubyVersion: 2.3
Exclude:
- 'spec/dummy/db/seeds.rb'
- 'spec/dummy/db/migrate/**'
- 'spec/dummy/db/schema.rb'
- 'dummy/db/seeds.rb'
- 'dummy/db/migrate/**'
- 'dummy/db/schema.rb'
- 'lib/generators/templates/**/*'
Documentation:
Enabled: false
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions dummy/spec
2 changes: 1 addition & 1 deletion lib/monarchy/acts_as_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def assign_parent(force = false)

if parentize
was_changed = changes["#{parentize}_id"] || changes["#{parentize}_type"]
Monarchy::Validators.resource(send(parentize), true)
Monarchy::Validators.resource(send(parentize), true, false)
self.parent = send(parentize) if was_changed || force
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/monarchy/acts_as_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def acts_as_user

module InstanceMethods
def roles_for(resource, inheritence = true)
Monarchy::Validators.resource(resource)
Monarchy::Validators.resource(resource, false, false)
accessible_roles_for(resource, inheritence)
end

Expand Down Expand Up @@ -51,6 +51,7 @@ def revoke_role!(role_name, resource)
private

def accessible_roles_for(resource, inheritnce)
return Monarchy.role_class.none unless resource.hierarchy
accessible_roles = if inheritnce
resource_and_inheritence_roles(resource)
else
Expand Down
6 changes: 6 additions & 0 deletions lib/monarchy/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,11 @@ def message
'Role can NOT be nil!'
end
end

class ResourceNotPersist < Error
def message
'Resource has to persisted!'
end
end
end
end
12 changes: 9 additions & 3 deletions lib/monarchy/validators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ def role_names(*role_names)
roles
end

def resource(resource, allow_nil = false)
def resource(resource, allow_nil = false, persistance = true)
raise Monarchy::Exceptions::ResourceIsNil if !resource && !allow_nil

check_model_class(resource, 'ModelNotResource') do
model = check_model_class(resource, 'ModelNotResource') do
resource.class.try(:acting_as_resource)
end

persistance && resource && !resource.persisted? ? raise(Monarchy::Exceptions::ResourceNotPersist) : model
end

def user(user, allow_nil = false)
Expand All @@ -54,7 +56,11 @@ def role(role, allow_nil = false)
private

def check_model_class(model, exception_class)
yield ? model : (raise "Monarchy::Exceptions::#{exception_class}".constantize, model if model)
if yield
model
else
raise "Monarchy::Exceptions::#{exception_class}".constantize, model if model
end
end

def model_is_class(model, klass, exception_class)
Expand Down
Binary file removed spec/dummy/db/test.sqlite3
Binary file not shown.
1 change: 0 additions & 1 deletion spec/dummy/spec

This file was deleted.

5 changes: 5 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
subject { user.roles_for('oko') }
it { is_expected_block.to raise_exception(Monarchy::Exceptions::ModelNotResource) }
end

context 'when model is not persist' do
subject { user.roles_for(build(:memo)) }
it { is_expected.to match_array([]) }
end
end

describe '#grant' do
Expand Down
24 changes: 24 additions & 0 deletions spec/monarchy/validators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@

describe '.resource' do
context 'allow nil' do
context 'persistance' do
context 'turn on by default' do
subject { described_class.resource(nil, true) }
it { is_expected_block.not_to raise_exception(Monarchy::Exceptions::ResourceNotPersist) }
end

context 'turn off by default' do
subject { described_class.resource(nil, true, false) }
it { is_expected_block.not_to raise_exception(Monarchy::Exceptions::ResourceNotPersist) }
end
end

context 'model is nil' do
subject { described_class.resource(nil, true) }
it { is_expected_block.not_to raise_exception(Monarchy::Exceptions::ResourceIsNil) }
Expand All @@ -101,6 +113,18 @@
end

context 'not allow nil' do
context 'persistance' do
context 'turn on by default' do
subject { described_class.resource(build(:memo)) }
it { is_expected_block.to raise_exception(Monarchy::Exceptions::ResourceNotPersist) }
end

context 'turn off by default' do
subject { described_class.resource(build(:memo), false, false) }
it { is_expected_block.not_to raise_exception(Monarchy::Exceptions::ResourceNotPersist) }
end
end

context 'model is nil' do
subject { described_class.resource(nil) }
it { is_expected_block.to raise_exception(Monarchy::Exceptions::ResourceIsNil) }
Expand Down
2 changes: 1 addition & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../dummy/config/environment', __FILE__)
require File.expand_path('../../dummy/config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'monarchy'
Expand Down
5 changes: 0 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
require 'codeclimate-test-reporter'
require 'support/helpers/is_expected_block'

SimpleCov.start do
add_group 'lib', '/lib'
add_filter '/spec/'
end

CodeClimate::TestReporter.start

# This file was generated by the `rails generate rspec:install` command. Conventionally, all
Expand Down

0 comments on commit 3053828

Please sign in to comment.