cantango-core 0.1.2 → 0.1.3

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.
Files changed (92) hide show
  1. data/Gemfile +7 -1
  2. data/Gemfile.lock +70 -12
  3. data/README.mdown +16 -14
  4. data/VERSION +1 -1
  5. data/cantango-core.gemspec +41 -51
  6. data/lib/cantango/ability/base/callbacks.rb +37 -0
  7. data/lib/cantango/ability/base.rb +21 -8
  8. data/lib/cantango/ability/cache.rb +1 -1
  9. data/lib/cantango/ability/executor/modes.rb +6 -6
  10. data/lib/cantango/ability/executor/no_cache_mode.rb +2 -6
  11. data/lib/cantango/ability/executor.rb +1 -1
  12. data/lib/cantango/ability/helper/account.rb +6 -10
  13. data/lib/cantango/ability/helper/engine.rb +22 -26
  14. data/lib/cantango/ability/helper/user.rb +10 -14
  15. data/lib/cantango/ability/helper.rb +1 -1
  16. data/lib/cantango/ability.rb +7 -2
  17. data/lib/cantango/adaptor/active_record.rb +6 -0
  18. data/lib/cantango/adaptor/data_mapper.rb +5 -0
  19. data/lib/cantango/adaptor/generic.rb +12 -0
  20. data/lib/cantango/adaptor/mongo.rb +13 -0
  21. data/lib/cantango/adaptor/mongo_mapper.rb +5 -0
  22. data/lib/cantango/adaptor/mongoid.rb +5 -0
  23. data/lib/cantango/adaptor/relational.rb +8 -0
  24. data/lib/cantango/{rules/adaptor.rb → adaptor.rb} +4 -4
  25. data/lib/cantango/{rules → cancan}/rule_class.rb +1 -1
  26. data/lib/cantango/cancan.rb +5 -0
  27. data/lib/cantango/core.rb +5 -13
  28. data/lib/cantango/engine.rb +6 -16
  29. data/lib/cantango/filter/base.rb +1 -1
  30. data/lib/cantango/helpers/debug.rb +31 -1
  31. data/lib/cantango/loader/yaml.rb +2 -11
  32. data/lib/cantango/model/guest.rb +3 -1
  33. data/spec/cantango/ability/base_spec.rb +13 -63
  34. data/spec/cantango/ability/cache/simple_key_spec.rb +29 -0
  35. data/spec/cantango/ability/executor/base_spec.rb +12 -52
  36. data/spec/cantango/ability/executor/custom_spec.rb +51 -0
  37. data/spec/cantango/ability/executor/modes_spec.rb +27 -49
  38. data/spec/cantango/ability/executor/no_cache_mode_spec.rb +29 -0
  39. data/spec/cantango/ability/helper/account_spec.rb +37 -0
  40. data/spec/cantango/ability/helper/engine_spec.rb +36 -0
  41. data/spec/cantango/ability/helper/user_spec.rb +38 -0
  42. data/spec/cantango/adaptor/active_record_spec.rb +38 -0
  43. data/spec/cantango/adaptor/data_mapper_spec.rb +39 -0
  44. data/spec/cantango/{rules/adaptor → adaptor}/mongo_mapper_spec.rb +0 -0
  45. data/spec/cantango/{rules/adaptor → adaptor}/mongoid_spec.rb +0 -0
  46. data/spec/cantango/cancan/rule_spec.rb +5 -0
  47. data/spec/cantango/core_spec.rb +0 -2
  48. data/spec/cantango/engine_spec.rb +51 -0
  49. data/spec/cantango/filter/base_spec.rb +25 -0
  50. data/spec/cantango/helpers/debug_spec.rb +29 -0
  51. data/spec/cantango/loader/yaml_spec.rb +5 -0
  52. data/spec/cantango/model/guest_spec.rb +14 -0
  53. data/spec/cantango/rspec/be_allowed_to_spec.rb +5 -0
  54. data/spec/cantango/scope/ability_spec.rb +33 -0
  55. data/spec/fixtures/models/account.rb +7 -0
  56. data/spec/fixtures/models/admin.rb +2 -0
  57. data/spec/fixtures/models/admin_account.rb +8 -0
  58. data/spec/fixtures/models/items.rb +11 -0
  59. data/spec/fixtures/models/user.rb +20 -0
  60. data/spec/fixtures/models.rb +2 -0
  61. data/spec/spec_helper.rb +4 -0
  62. metadata +60 -67
  63. data/lib/cantango/ability/executor/cache_mode.rb +0 -33
  64. data/lib/cantango/ability/helper/role.rb +0 -21
  65. data/lib/cantango/ability/helper/role_group.rb +0 -21
  66. data/lib/cantango/macros/account.rb +0 -14
  67. data/lib/cantango/macros/user.rb +0 -16
  68. data/lib/cantango/macros.rb +0 -11
  69. data/lib/cantango/rules/adaptor/active_record.rb +0 -10
  70. data/lib/cantango/rules/adaptor/data_mapper.rb +0 -11
  71. data/lib/cantango/rules/adaptor/generic.rb +0 -16
  72. data/lib/cantango/rules/adaptor/mongo.rb +0 -19
  73. data/lib/cantango/rules/adaptor/mongo_mapper.rb +0 -10
  74. data/lib/cantango/rules/adaptor/mongoid.rb +0 -9
  75. data/lib/cantango/rules/adaptor/relational.rb +0 -13
  76. data/lib/cantango/rules/dsl.rb +0 -24
  77. data/lib/cantango/rules/relation.rb +0 -67
  78. data/lib/cantango/rules/scope.rb +0 -24
  79. data/lib/cantango/rules.rb +0 -8
  80. data/spec/cantango/ability/cached_spec.rb +0 -0
  81. data/spec/cantango/ability/executor/base2.rb +0 -75
  82. data/spec/cantango/ability/executor/cache_mode_spec.rb +0 -77
  83. data/spec/cantango/macros/account_spec.rb +0 -0
  84. data/spec/cantango/macros/user_spec.rb +0 -0
  85. data/spec/cantango/rules/adaptor/active_record_spec.rb +0 -0
  86. data/spec/cantango/rules/adaptor/data_mapper_spec.rb +0 -0
  87. data/spec/cantango/rules/adaptor_spec.rb +0 -0
  88. data/spec/cantango/rules/dsl_spec.rb +0 -0
  89. data/spec/cantango/rules/relation_spec.rb +0 -0
  90. data/spec/cantango/rules/rule_class_spec.rb +0 -0
  91. data/spec/cantango/rules/scope_spec.rb +0 -0
  92. data/spec/cantango/rules_spec.rb +0 -55
@@ -1,68 +1,46 @@
1
- require 'rspec'
2
- require 'cantango'
3
- # require 'simple_roles'
1
+ require 'spec_helper'
4
2
  require 'fixtures/models'
5
- require 'cantango/rspec'
6
3
 
7
- def config_folder
8
- File.dirname(__FILE__)+ "/../fixtures/config/"
9
- end
10
-
11
- CanTango.configure do |config|
12
- config.clear!
13
- config.permits.enabled_types = [:role]
14
- end
15
- CanTango.debug!
16
-
17
- class User
18
- tango_user
19
- include_and_extend SimpleRoles
20
- end
21
-
22
- class AdminRolePermit < CanTango::Permit::Role
23
- def initialize ability
24
- super
25
- end
26
-
27
- protected
28
-
29
- def static_rules
30
- can :read, Article
4
+ module CanTango::Ability::Executor
5
+ class CacheMode
6
+ def permit_rules
7
+ can :read, Post
8
+ end
31
9
  end
10
+ end
32
11
 
33
- module Cached
12
+ module CanTango::Ability::Executor
13
+ class NoCacheMode
34
14
  def permit_rules
35
- can :edit, Article
36
- can :delete, Article
15
+ can :write, Post
37
16
  end
38
17
  end
39
18
  end
40
19
 
41
20
  describe CanTango::Ability::Executor::Modes do
42
- context 'non-cached only' do
43
- before do
44
- CanTango.configure.ability.mode = :no_cache
45
-
46
- @user = User.new 'admin', 'admin@mail.ru', :role => 'admin'
47
- @abil = CanTango::AbilityExecutor.new @user
48
- end
49
-
50
- subject { CanTango::AbilityExecutor.new @user }
51
-
52
- describe 'config no_cache' do
53
- specify { CanTango.configure.ability.modes.should == [:no_cache] }
54
- end
21
+ before do
22
+ @user = User.new 'admin', 'admin@mail.ru'
23
+ end
55
24
 
56
- describe 'engines_on?' do
57
- specify { subject.engines_on?.should be_true }
25
+ context 'Set execution modes via :modes option'
26
+ subject do
27
+ modes_executor = CanTango::Ability::Executor::Modes.new @user
28
+ modes_executor.modes << :no_cache
29
+ modes_executor
58
30
  end
59
31
 
60
- its(:cached_rules) { should be_empty }
32
+ its(:modes) { should include(:no_cache) }
61
33
  its(:non_cached_rules) { should_not be_empty }
62
34
 
63
35
  describe 'rules contain only non-cached rules' do
64
- specify { subject.rules.size.should == @abil.non_cached_rules.size }
65
- specify { subject.rules.size.should == 2 }
36
+ specify { subject.rules.size.should == subject.non_cached_rules.size }
37
+ specify { subject.rules.size.should == 1 }
66
38
  end
67
39
  end
40
+
41
+ context 'Set execution modes via :modes option'
42
+ subject { @CanTango::Ability::Executor::Modes.new @user, :modes => [:no_cache] }
43
+
44
+ its(:modes) { should include(:no_cache) }
45
+ end
68
46
  end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ require 'fixtures/models'
3
+
4
+ module CanTango::Ability::Executor
5
+ class NoCacheMode
6
+ def permit_rules
7
+ can :read, Post
8
+ end
9
+ end
10
+ end
11
+
12
+ describe CanTango::Ability::Executor::NoCacheMode do
13
+ context 'no_cached mode only' do
14
+ before do
15
+ @user = User.new 'admin', 'admin@mail.ru'
16
+ @ability = CanTango::Ability::Base.new @user
17
+ end
18
+
19
+ subject { CanTango::Ability::Executor::NoCacheMode.new @ability }
20
+
21
+ its(:non_cached_rules) { should be_empty }
22
+
23
+ describe 'rules contain only non-cached rules' do
24
+ specify { subject.rules.size.should == @abil.cached_rules.size }
25
+ specify { subject.rules.size.should == 1 }
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ module CanTango::Ability
4
+ class Base
5
+ include CanTango::Ability::Helper::Account
6
+
7
+ def permit_rules
8
+ can :edit, Project
9
+ end
10
+ end
11
+ end
12
+
13
+ class MyAccount
14
+ end
15
+
16
+ class User
17
+ attr_accessor :account
18
+ end
19
+
20
+ describe CanTango::Ability::Helper::Account do
21
+ before do
22
+ @user = User.new
23
+ @account = MyAccount.new
24
+ @user.account = @account
25
+ end
26
+
27
+ subject do
28
+ CanTango::Ability::Base.new @user
29
+ end
30
+
31
+ context 'Ability should have' do
32
+ describe '#account method' do
33
+ specify { subject.account.should == @account }
34
+ end
35
+ end
36
+ end
37
+
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ module CanTango::Ability
4
+ class Base
5
+ include CanTango::Ability::Helper::Engine
6
+
7
+ def permit_rules
8
+ can :edit, Project
9
+ end
10
+ end
11
+ end
12
+
13
+ class MyAccount
14
+ end
15
+
16
+ class User
17
+ attr_accessor :account
18
+ end
19
+
20
+ describe CanTango::Ability::Helper::Engine do
21
+ before do
22
+ @user = User.new
23
+ end
24
+
25
+ subject do
26
+ CanTango::Ability::Base.new @user
27
+ end
28
+
29
+ context 'Ability should have' do
30
+ [:engines, :each_engine, :execute_engines!].each do |method_name|
31
+ describe "#{method_name} method" do
32
+ specify { subject.should respond_to(method_name) }
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ class MyAccount
4
+ end
5
+
6
+ class User
7
+ attr_accessor :account
8
+ end
9
+
10
+ module CanTango::Ability
11
+ class Base
12
+ include CanTango::Ability::Helper::User
13
+
14
+ def permit_rules
15
+ can :edit, Project
16
+ end
17
+ end
18
+ end
19
+
20
+ describe CanTango::Ability::Helper::User do
21
+ before do
22
+ @user = User.new
23
+ end
24
+
25
+ subject do
26
+ CanTango::Ability::Base.new @user
27
+ end
28
+
29
+ context 'Ability should have' do
30
+ describe '#user method' do
31
+ specify { subject.user.should == @user }
32
+ end
33
+
34
+ describe '#user_key_field' do
35
+ specify { subject.should respond_to(:user_key_field) }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ # This is used to wrap the CanCan ORM adapters, that enable convenient
2
+ # 'simple' queries using hashes similar to 'metawhere' gem
3
+ require 'spec_helper'
4
+
5
+ class Poster < ActiveRecord::Base
6
+ end
7
+
8
+ module CanTango::Ability
9
+ class Base
10
+ def permit_rules
11
+ can :read, Poster, :owner => user
12
+ end
13
+ end
14
+ end
15
+
16
+
17
+ describe CanTango::Adaptor::ActiveRecord do
18
+ before do
19
+ @user = User.new 'admin', 'admin@mail.ru'
20
+ @stranger = User.new 'stranger', 'stranger@mail.ru'
21
+
22
+ @poster = Poster.create :owner => @user
23
+ @poster.owner = @user
24
+ end
25
+
26
+ context 'subject is owner of poster' do
27
+ subject { CanTango::Ability::Executor::Base.new @user }
28
+
29
+ specify { subject.should be_allowed_to(:read, @poster) }
30
+ end
31
+
32
+ context 'subject is NOT owner of poster' do
33
+ subject { CanTango::Ability::Executor::Base.new @stranger }
34
+
35
+ specify { subject.should_not be_allowed_to(:read, @poster) }
36
+ end
37
+ end
38
+
@@ -0,0 +1,39 @@
1
+ # This is used to wrap the CanCan ORM adapters, that enable convenient
2
+ # 'simple' queries using hashes similar to 'metawhere' gem
3
+ require 'spec_helper'
4
+
5
+ class Chair
6
+ include DataMapper::Resource
7
+ end
8
+
9
+ module CanTango::Ability
10
+ class Base
11
+ def permit_rules
12
+ can :read, Chair, :owner => user
13
+ end
14
+ end
15
+ end
16
+
17
+
18
+ describe CanTango::Adaptor::DataMapper do
19
+ before do
20
+ @user = User.new 'admin', 'admin@mail.ru'
21
+ @stranger = User.new 'stranger', 'stranger@mail.ru'
22
+
23
+ @chair = Chair.create :owner => @user
24
+ @chair.owner = @user
25
+ end
26
+
27
+ context 'subject is owner of poster' do
28
+ subject { CanTango::Ability::Executor::Base.new @user }
29
+
30
+ specify { subject.should be_allowed_to(:read, @chair) }
31
+ end
32
+
33
+ context 'subject is NOT owner of poster' do
34
+ subject { CanTango::Ability::Executor::Base.new @stranger }
35
+
36
+ specify { subject.should_not be_allowed_to(:read, @chair) }
37
+ end
38
+ end
39
+
@@ -0,0 +1,5 @@
1
+ describe CanCan::RuleClass do
2
+ subject { CanCan::RuleClass.new }
3
+
4
+ its(:block) { should be_nil }
5
+ end
@@ -1,7 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- # require 'cantango/core'
4
-
5
3
  describe 'cantango/core' do
6
4
  specify do
7
5
  lambda { CanTango }.should_not raise_error
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+ require 'fixtures/models'
3
+
4
+ describe CanTango::Engine do
5
+ before do
6
+ @user = User.new 'admin', 'admin@mail.ru'
7
+ @ability = CanTango::Ability::Base.new @user
8
+ end
9
+
10
+ context '#initilize with ability' do
11
+ subject { CanTango::Engine.new @ability }
12
+
13
+ specify { subject.should be_a CanTango::Engine }
14
+
15
+ specify do
16
+ lambda { subject.execute! }.should raise_error
17
+ end
18
+
19
+ specify do
20
+ lambda { subject.engine_name }.should raise_error
21
+ end
22
+
23
+ specify do
24
+ subject.modes.should be_empty
25
+ end
26
+
27
+ specify do
28
+ subject.valid_mode?(:no_cache).should be_false
29
+ end
30
+ end
31
+
32
+ context '#initilize with ability and :no_cache mode' do
33
+ subject { CanTango::Engine.new @ability, :modes => [:no_cache] }
34
+
35
+ specify do
36
+ subject.modes.should_not be_empty
37
+ end
38
+
39
+ specify do
40
+ subject.modes.should include(:no_cache)
41
+ end
42
+
43
+ specify do
44
+ subject.valid_mode?(:no_cache).should be_true
45
+ end
46
+
47
+ specify do
48
+ subject.valid_mode?(:cache).should be_false
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ class MyFilter < CanTango::Filter::Base
4
+ def initialize item, list
5
+ super
6
+ end
7
+ end
8
+
9
+ describe CanTango::Filter::Base do
10
+ context "a and list of a,b,c" do
11
+ before do
12
+ @list = ['a', 'b', 'c']
13
+ end
14
+ subject { MyFilter.new 'a', @list }
15
+
16
+ its(:include_list) { should == @list.map(&:to_sym) }
17
+ its(:item) { should == :a }
18
+
19
+ its(:valid?) { should be_true }
20
+ its(:in_include_list?) { should be_true }
21
+
22
+ its(:not_only?) { should be_false }
23
+ its(:excluded?) { should be_false }
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ class Paper
4
+ include CanTango::Helpers::Debug
5
+ end
6
+
7
+ module Writer
8
+ def self.global_buffer
9
+ @global_buffer ||= ""
10
+ end
11
+ end
12
+
13
+ describe CanTango::Helpers::Debug do
14
+ before do
15
+ @paper = Paper.new
16
+ @paper.debug_writer = lambda {|m| Writer.global_buffer << m }
17
+ @paper.do_debug = true
18
+ end
19
+
20
+ subject { @paper }
21
+
22
+ describe 'should write to writer' do
23
+ before do
24
+ @paper.debug 'hello'
25
+ end
26
+
27
+ specify { Writer.global_buffer.should == 'hello'}
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe CanTango::Loader::Yaml do
4
+ pending 'Nothing much to spec here!'
5
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ class Guest
4
+ include CanTango::Model::Guest
5
+ end
6
+
7
+ describe CanTango::Model::Guest do
8
+ subject { Guest.new }
9
+
10
+ its(:email) { should == 'guest@example.com' }
11
+
12
+ specify { subject.has_role?(:guest).should be_true }
13
+ specify { subject.has_role?(:user).should be_false }
14
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'RSpec::Matchers be_allowed_to' do
4
+ pending 'It should work!'
5
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ require 'fixtures/models'
3
+
4
+ module CanTango::Ability
5
+ class Base
6
+ include CanTango::Ability::Helper::Account
7
+
8
+ def permit_rules
9
+ can :read, Project
10
+ cannot :edit, Project
11
+ end
12
+ end
13
+ end
14
+
15
+ describe CanTango::Scope::Ability do
16
+ before do
17
+ @user = User.new 'krisy', 'kris@gmail.com'
18
+ @ability = CanTango::Ability::Base.new @user
19
+
20
+ @project = Project.new
21
+ end
22
+
23
+ subject { CanTango::Scope::Ability.new @ability }
24
+
25
+ specify do
26
+ subject.can?(:read, @project).should be_true
27
+ end
28
+
29
+ specify do
30
+ subject.cannot?(:edit, @project).should be_true
31
+ subject.can?(:edit, @project).should be_false
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ class Account
2
+ attr_accessor :user
3
+
4
+ def initialize user, options = {}
5
+ @user = user
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ class Admin < User
2
+ end
@@ -0,0 +1,8 @@
1
+ class AdminAccount
2
+ attr_accessor :user
3
+
4
+ def initialize user
5
+ @user = user
6
+ end
7
+ end
8
+
@@ -0,0 +1,11 @@
1
+ class Post
2
+ end
3
+
4
+ class Article
5
+ end
6
+
7
+ class Comment
8
+ end
9
+
10
+ class Project
11
+ end
@@ -0,0 +1,20 @@
1
+ class User
2
+ attr_accessor :name, :account, :email
3
+
4
+ def initialize name, email = nil, options = {}
5
+ @name = name
6
+ @email = email
7
+ set_option_vars options
8
+ end
9
+
10
+ def set_option_vars options = {}
11
+ options.each_pair do |name, value|
12
+ var = :"@#{name}"
13
+ self.instance_variable_set(var, value)
14
+ end
15
+ end
16
+
17
+ def email
18
+ @email ||= 'default@gmail.com'
19
+ end
20
+ end
@@ -0,0 +1,2 @@
1
+ require 'require_all'
2
+ require_all File.dirname(__FILE__) + '/models'
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  require 'rspec'
2
+ require 'cantango/rspec'
2
3
  require 'cantango/core'
3
4
 
5
+ class Project
6
+ end
7
+
4
8
  # Requires supporting files with custom matchers and macros, etc,
5
9
  # in ./support/ and its subdirectories.
6
10
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}