canard 0.2.0 → 0.2.1

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.
data/TODO CHANGED
@@ -2,4 +2,6 @@
2
2
  * Test the ability class.
3
3
  * Test the generators.
4
4
  * Test the railtie
5
- * Expand the tests to produce all the standard abilities: index,show,read,new,create,edit,update,destroy.
5
+ * Expand the tests to produce all the standard abilities: index,show,read,new,create,edit,update,destroy.
6
+ * Take the Factory references out of the generated tests.
7
+ * Add some rdocs.
data/canard.gemspec CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.version = Canard::VERSION
8
8
  s.authors = ["James McCarthy"]
9
9
  s.email = ["james2mccarthy@gmail.com"]
10
- s.homepage = ""
10
+ s.homepage = "https://github.com/james2m/canard"
11
11
  s.summary = %q{Adds RoleModel roles to CanCan.}
12
12
  s.description = %q{Wraps CanCan and RoleModel up with a generator and makes role based authorisation really easy in Rails 3.x.}
13
13
 
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
21
21
  s.add_development_dependency "minitest", "~> 2"
22
22
  s.add_development_dependency "sqlite3"
23
23
  s.add_development_dependency "activerecord"
24
+ s.add_runtime_dependency 'activesupport'
24
25
  s.add_runtime_dependency "cancan"
25
26
  s.add_runtime_dependency "role_model"
26
27
  end
data/lib/ability.rb CHANGED
@@ -31,7 +31,7 @@ class Ability
31
31
  end
32
32
 
33
33
  def ability_definitions
34
- Canard.ability_definitions
34
+ Canard::Abilities.definitions
35
35
  end
36
36
 
37
37
  def load_abilities(role)
data/lib/canard.rb CHANGED
@@ -1,12 +1,10 @@
1
1
  require 'cancan'
2
2
  require 'role_model'
3
- require "canard/version"
3
+ require 'canard/abilities'
4
+ require 'canard/version'
5
+ require 'canard/user_model'
4
6
  require "canard/find_abilities"
5
7
  require "ability"
6
8
 
7
- module Canard
8
- autoload :UserModel, 'canard/user_model'
9
- end
10
-
11
9
  require 'canard/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
12
10
 
@@ -0,0 +1,22 @@
1
+ module Canard
2
+ class Abilities
3
+
4
+ @definitions = {}
5
+ @definition_paths = ['app/abilities']
6
+
7
+ class << self
8
+
9
+ attr_accessor :definition_paths
10
+
11
+ attr_reader :definitions
12
+
13
+ def for(name, &block)
14
+ raise ArgumentError.new('No block of ability definitions given') unless block_given?
15
+ @definitions[name.to_sym] = block
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -7,13 +7,14 @@ module Canard
7
7
  attr_accessor :abilities_path
8
8
 
9
9
  def ability_definitions
10
- @ability_definitions ||= {}
10
+ Abilities.definitions
11
11
  end
12
12
 
13
13
  def abilities_for(role, &block)
14
+ ::ActiveSupport::Deprecation.warn("abilities_for is deprecated and will be removed from Canard 0.4.0. Use Canard::Abilities.for and move the definitions to app/abilities.")
14
15
  ability_definitions[role] = block
15
16
  end
16
-
17
+
17
18
  end
18
19
 
19
20
  def self.find_abilities #:nodoc:
@@ -25,7 +26,14 @@ module Canard
25
26
  self.class_eval File.read(file)
26
27
  end
27
28
  end
29
+
30
+ Abilities.definition_paths.each do |path|
31
+ Dir[File.join(Rails.root, path, '**', '*.rb')].sort.each do |file|
32
+ load file
33
+ end
34
+ end
28
35
 
29
36
  end
30
37
 
38
+
31
39
  end
@@ -1,3 +1,3 @@
1
1
  module Canard
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+ require 'canard'
3
+
4
+ describe 'Canard::Abilities' do
5
+
6
+ subject { Canard::Abilities }
7
+
8
+ describe "ability_paths" do
9
+
10
+ it "defaults to app/abilities" do
11
+ subject.definition_paths.must_include 'app/abilities'
12
+ end
13
+
14
+ it "appends paths" do
15
+ subject.definition_paths << 'other_abilities'
16
+ subject.definition_paths.must_include 'other_abilities'
17
+ end
18
+
19
+ it "can be overwritten" do
20
+ subject.definition_paths = ['other_abilities']
21
+
22
+ subject.definition_paths.must_equal ['other_abilities']
23
+ end
24
+
25
+ end
26
+
27
+ describe "define" do
28
+
29
+ it "adds the block to the definitions" do
30
+ block = lambda { puts 'some block' }
31
+
32
+ subject.for(:definition, &block)
33
+
34
+ assert_equal block, subject.definitions[:definition]
35
+ end
36
+
37
+ it "normalises the key to a symbol" do
38
+ subject.for('definition') { puts 'a block' }
39
+
40
+ subject.definitions.keys.must_include :definition
41
+ end
42
+
43
+ it "rasises ArgumentError if no block is provided" do
44
+ proc { subject.for(:definition) }.must_raise ArgumentError
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,9 @@
1
+ Canard::Abilities.for(:moderator) do
2
+
3
+ can :manage, [Post]
4
+
5
+ cannot :destroy, User do |u|
6
+ (user == u)
7
+ end
8
+
9
+ end
data/test/canard_test.rb CHANGED
@@ -1,21 +1,8 @@
1
1
  require 'test_helper'
2
- require_relative '../lib/canard.rb'
2
+ require 'canard'
3
3
 
4
4
  describe Canard do
5
-
6
- before do
7
- class User < ActiveRecord::Base
8
- end
9
-
10
- Canard.abilities_path = File.expand_path('../abilities', __FILE__)
11
- end
12
-
13
- # Sanity test
14
- it "must be an user" do
15
- user = User.new
16
- user.must_be_instance_of User
17
- end
18
-
5
+
19
6
  describe "abilities_path" do
20
7
 
21
8
  it "should be mutable" do
@@ -36,15 +23,5 @@ describe Canard do
36
23
  end
37
24
 
38
25
  end
39
-
40
- describe "find_abilities" do
41
26
 
42
- it "should load the abilities into ability_definitions" do
43
- Canard.find_abilities
44
-
45
- Canard.ability_definitions.keys.must_include :admin
46
- end
47
-
48
- end
49
-
50
27
  end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+ require 'active_support/testing/deprecation'
3
+
4
+ describe Canard do
5
+
6
+ include ActiveSupport::Testing::Deprecation
7
+
8
+ before do
9
+ # Stop the deprecation warnings coming to stderr for these tests.
10
+ ActiveSupport::Deprecation.behavior = :notify
11
+
12
+ Canard.abilities_path = File.expand_path('../abilities', __FILE__)
13
+ end
14
+
15
+ describe "find_abilities" do
16
+
17
+ it "loads the abilities into ability_definitions" do
18
+ Canard.find_abilities
19
+
20
+ Canard.ability_definitions.keys.must_include :admin
21
+ end
22
+
23
+ it "finds the abilities with the new syntax" do
24
+ Canard.find_abilities
25
+
26
+ Canard.ability_definitions.keys.must_include :moderator
27
+ end
28
+
29
+ it "reloads existing abilities" do
30
+ Canard.find_abilities
31
+ Canard::Abilities.send(:instance_variable_set, '@definitions', {})
32
+ Canard.find_abilities
33
+
34
+ Canard.ability_definitions.keys.must_include :moderator
35
+ Canard.ability_definitions.keys.must_include :admin
36
+ end
37
+
38
+ end
39
+
40
+ describe "abilities_for" do
41
+
42
+ it "raises a deprecation warning" do
43
+ assert_deprecated do
44
+ Canard.abilities_for(:this) { puts 'that' }
45
+ end
46
+ end
47
+
48
+ end
49
+ end
data/test/test_helper.rb CHANGED
@@ -2,6 +2,18 @@ require 'rubygems'
2
2
  gem 'minitest'
3
3
  require 'active_record'
4
4
  require 'minitest/autorun'
5
+ require 'pathname'
6
+
7
+ module Rails
8
+
9
+ module VERSION
10
+ MAJOR = 0
11
+ end
12
+
13
+ def self.root
14
+ Pathname.new(File.expand_path('..', __FILE__))
15
+ end
16
+ end
5
17
 
6
18
  module MiniTestWithHooks
7
19
  class Unit < MiniTest::Unit
@@ -55,4 +67,21 @@ module MiniTestWithActiveRecord
55
67
  end
56
68
  end
57
69
 
58
- MiniTest::Unit.runner = MiniTestWithActiveRecord::Unit.new
70
+ MiniTestWithActiveRecord::Unit::TestCase.add_teardown_hook do
71
+ Object.send(:remove_const, 'Canard') if Object.const_defined?('Canard')
72
+ GC.start
73
+ end
74
+
75
+ MiniTestWithActiveRecord::Unit::TestCase.add_setup_hook do
76
+ [ 'canard/abilities.rb',
77
+ 'canard/user_model.rb',
78
+ "canard/find_abilities.rb"
79
+ ].each do |file|
80
+ file_path = File.join(File.expand_path('../../lib', __FILE__), file)
81
+ load file_path
82
+ end
83
+
84
+ end
85
+
86
+ MiniTest::Unit.runner = MiniTestWithActiveRecord::Unit.new
87
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: canard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-05 00:00:00.000000000Z
12
+ date: 2012-03-12 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &2160840240 !ruby/object:Gem::Requirement
16
+ requirement: &2157922760 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '2'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2160840240
24
+ version_requirements: *2157922760
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sqlite3
27
- requirement: &2160839820 !ruby/object:Gem::Requirement
27
+ requirement: &2157922340 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2160839820
35
+ version_requirements: *2157922340
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activerecord
38
- requirement: &2160839360 !ruby/object:Gem::Requirement
38
+ requirement: &2157921880 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,21 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2160839360
46
+ version_requirements: *2157921880
47
+ - !ruby/object:Gem::Dependency
48
+ name: activesupport
49
+ requirement: &2157921460 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *2157921460
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: cancan
49
- requirement: &2160838940 !ruby/object:Gem::Requirement
60
+ requirement: &2157913300 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,10 +65,10 @@ dependencies:
54
65
  version: '0'
55
66
  type: :runtime
56
67
  prerelease: false
57
- version_requirements: *2160838940
68
+ version_requirements: *2157913300
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: role_model
60
- requirement: &2160838520 !ruby/object:Gem::Requirement
71
+ requirement: &2157912440 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ! '>='
@@ -65,7 +76,7 @@ dependencies:
65
76
  version: '0'
66
77
  type: :runtime
67
78
  prerelease: false
68
- version_requirements: *2160838520
79
+ version_requirements: *2157912440
69
80
  description: Wraps CanCan and RoleModel up with a generator and makes role based authorisation
70
81
  really easy in Rails 3.x.
71
82
  email:
@@ -83,6 +94,7 @@ files:
83
94
  - canard.gemspec
84
95
  - lib/ability.rb
85
96
  - lib/canard.rb
97
+ - lib/canard/abilities.rb
86
98
  - lib/canard/find_abilities.rb
87
99
  - lib/canard/railtie.rb
88
100
  - lib/canard/user_model.rb
@@ -94,13 +106,16 @@ files:
94
106
  - lib/generators/rspec/ability/ability_generator.rb
95
107
  - lib/generators/rspec/ability/templates/abilities_spec.rb.erb
96
108
  - test/abilities/admins.rb
109
+ - test/abilities_test.rb
110
+ - test/app/abilities/moderators.rb
97
111
  - test/canard_test.rb
112
+ - test/find_abilities_test.rb
98
113
  - test/models/user.rb
99
114
  - test/models/user_without_role.rb
100
115
  - test/models/user_without_role_mask.rb
101
116
  - test/test_helper.rb
102
117
  - test/user_model_test.rb
103
- homepage: ''
118
+ homepage: https://github.com/james2m/canard
104
119
  licenses: []
105
120
  post_install_message:
106
121
  rdoc_options: []
@@ -120,13 +135,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
135
  version: '0'
121
136
  requirements: []
122
137
  rubyforge_project: canard
123
- rubygems_version: 1.8.6
138
+ rubygems_version: 1.8.12
124
139
  signing_key:
125
140
  specification_version: 3
126
141
  summary: Adds RoleModel roles to CanCan.
127
142
  test_files:
128
143
  - test/abilities/admins.rb
144
+ - test/abilities_test.rb
145
+ - test/app/abilities/moderators.rb
129
146
  - test/canard_test.rb
147
+ - test/find_abilities_test.rb
130
148
  - test/models/user.rb
131
149
  - test/models/user_without_role.rb
132
150
  - test/models/user_without_role_mask.rb