cancan-permits 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.txt CHANGED
@@ -1,3 +1,9 @@
1
+ ## Nov 25, 2010
2
+
3
+ Fixed some generator issues and split generator into:
4
+ * Licenses
5
+ * Permits
6
+
1
7
  ## Nov 24, 2010
2
8
 
3
9
  The generator has now been tested with Rails 3.0.3 and works. Previously there was an issue with missing generator dependencies in the Rakefile.
data/README.markdown CHANGED
@@ -184,31 +184,64 @@ To configure permits to allow localhost to manage objects:
184
184
 
185
185
  Please provide suggestions and feedback on how to improve this :)
186
186
 
187
+ ## Generators
188
+
189
+ The gem comes with the following generators
190
+
191
+ * cancan:permits
192
+ * cancan:licenses
193
+
187
194
  ## Permits Generator
188
195
 
189
196
  Options
190
- * --orm : The ORM to use (active_record, data_mapper, mongoid, mongo_mapper) - creates a Rails initializer
191
- * --initializer : A Rails 3 initializer file for Permits is generated by default. Use --no-initializer option to disable this
192
- * --roles : The roles for which to generate permits ; default Guest (read all) and Admin (manage all)
193
- * --licenses : The licenses to generate; default UserAdmin and Blogging licenses are generated
197
+ * --orm : The ORM to use (active_record, data_mapper, mongoid, mongo_mapper) - creates a Rails initializer
198
+ * --initializer : A Rails 3 initializer file for Permits is generated by default. Use --no-initializer option to disable this
199
+ * --roles : The roles for which to generate permits ; default Guest (read all) and Admin (manage all)
200
+ * --default-permits : By default :guest and :admin permits are generated. Use --no-default-permits option to disable this
201
+
202
+ <code>$ rails g cancan:permits --orm active_record --roles guest author admin</code>
194
203
 
204
+ ### What does the generator generate?
205
+
206
+ To get an understanding of what the generator generates for a Rails 3 application, try to run the spec permit_generator_spec.rb with rspec 2 as follows:
207
+
208
+ <code>$ rspec spec/generators/cancan/permits_generator_spec.rb</code>
209
+
210
+ In the file <code>permits_generator_spec.rb</code> make the following change <code>config.remove_temp_dir = false</code>
211
+ This will prevent the rails /tmp dir from being deleted after the test run, so you can inspect what is generated in the Rails app.
212
+
213
+ ## Licenses Generator
214
+
215
+ Options
216
+ * --licenses : The licenses to generate; default UserAdmin and Blogging licenses are generated
195
217
  * --default-licenses : By default exemplar licenses are generated. Use --no-default-licenses option to disable this
196
- * --default-permits : By default :guest and :admin permits are generated. Use --no-default-permits option to disable this
197
218
 
198
- <code>$ rails g permits --orm active_record --roles guest author admin</code>
219
+ Run examples:
220
+
221
+ Generate default licenses:
222
+
223
+ <code>$ rails g cancan:licenses</code>
224
+
225
+ Genereate specific licenses (no defaults):
226
+
227
+ <code>$ rails g cancan:licenses profile_administration article_editing --no-default-licenses</code>
228
+
229
+ Create both specific and default licenses:
230
+
231
+ <code>$ rails g cancan:licenses profile_administration article_editing</code>
199
232
 
200
233
  ### What does the generator generate?
201
234
 
202
235
  To get an understanding of what the generator generates for a Rails 3 application, try to run the spec permit_generator_spec.rb with rspec 2 as follows:
203
236
 
204
- <code>$ rspec spec/generators/permit_generator_spec.rb</code>
237
+ <code>$ rspec spec/generators/cancan/licenses_generator_spec.rb</code>
205
238
 
206
- In the file <code>permit_generator_spec.rb</code> make the following change <code>config.remove_temp_dir = false</code>
239
+ In the file <code>licenses_generator_spec.rb</code> make the following change <code>config.remove_temp_dir = false</code>
207
240
  This will prevent the rails /tmp dir from being deleted after the test run, so you can inspect what is generated in the Rails app.
208
241
 
209
242
  # TODO ?
210
243
 
211
- The Permits generator should attempt to discover which roles are currently defined as available to the system (Generic Roles API, User#roles etc.) and generate permits for those roles. Any roles specified in the --roles option should be merged with the roles available in the app.
244
+ The Permits generator should attempt to attempt to uncover which roles are currently defined as available to the system (Generic Roles API, User#roles etc.) and generate permits for those roles. Any roles specified in the --roles option should be merged with the roles available in the app.
212
245
 
213
246
  ## Note on Patches/Pull Requests
214
247
 
data/Rakefile CHANGED
@@ -7,7 +7,7 @@ begin
7
7
  gem.email = "kmandrup@gmail.com"
8
8
  gem.homepage = "http://github.com/kristianmandrup/cancan-permits"
9
9
  gem.authors = ["Kristian Mandrup"]
10
- gem.add_development_dependency "rspec", ">= 2.0.0"
10
+ gem.add_development_dependency "rspec", ">= 2.0.1"
11
11
  gem.add_development_dependency 'code-spec', "~> 0.2.5"
12
12
  gem.add_development_dependency 'rails-app-spec', "~> 0.3.0"
13
13
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cancan-permits}
8
- s.version = "0.3.1"
8
+ s.version = "0.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
@@ -41,10 +41,12 @@ Gem::Specification.new do |s|
41
41
  "lib/cancan-permits/rspec/matchers/have_license.rb",
42
42
  "lib/cancan-permits/rspec/matchers/have_license_class.rb",
43
43
  "lib/cancan-permits/rspec/matchers/have_license_file.rb",
44
- "lib/generators/permits/permits_generator.rb",
45
- "lib/generators/permits/templates/blogging_license.rb",
46
- "lib/generators/permits/templates/permit.rb",
47
- "lib/generators/permits/templates/user_admin_license.rb",
44
+ "lib/generators/cancan/licenses/licenses_generator.rb",
45
+ "lib/generators/cancan/licenses/templates/blogging_license.rb",
46
+ "lib/generators/cancan/licenses/templates/license.rb",
47
+ "lib/generators/cancan/licenses/templates/user_admin_license.rb",
48
+ "lib/generators/cancan/permits/permits_generator.rb",
49
+ "lib/generators/cancan/permits/templates/permit.rb",
48
50
  "spec/active_record/db/database.yml",
49
51
  "spec/active_record/migrations/001_create_user.rb",
50
52
  "spec/active_record/migrations/002_create_comment.rb",
@@ -64,7 +66,8 @@ Gem::Specification.new do |s|
64
66
  "spec/fixtures/permits/guest_permit.rb",
65
67
  "spec/fixtures/permits/super_admin_permit.rb",
66
68
  "spec/fixtures/permits/system_permit.rb",
67
- "spec/generators/permit_generator_spec.rb",
69
+ "spec/generators/cancan/licenses_generator_spec.rb",
70
+ "spec/generators/cancan/permits_generator_spec.rb",
68
71
  "spec/generic/api/basic/config.rb",
69
72
  "spec/generic/api/basic/xgroup.rb",
70
73
  "spec/generic/api/basic/xgroup_orm.rb",
@@ -114,7 +117,8 @@ Gem::Specification.new do |s|
114
117
  "spec/fixtures/permits/guest_permit.rb",
115
118
  "spec/fixtures/permits/super_admin_permit.rb",
116
119
  "spec/fixtures/permits/system_permit.rb",
117
- "spec/generators/permit_generator_spec.rb",
120
+ "spec/generators/cancan/licenses_generator_spec.rb",
121
+ "spec/generators/cancan/permits_generator_spec.rb",
118
122
  "spec/generic/api/basic/config.rb",
119
123
  "spec/generic/api/basic/xgroup.rb",
120
124
  "spec/generic/api/basic/xgroup_orm.rb",
@@ -145,7 +149,7 @@ Gem::Specification.new do |s|
145
149
  s.specification_version = 3
146
150
 
147
151
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
148
- s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
152
+ s.add_development_dependency(%q<rspec>, [">= 2.0.1"])
149
153
  s.add_development_dependency(%q<code-spec>, ["~> 0.2.5"])
150
154
  s.add_development_dependency(%q<rails-app-spec>, ["~> 0.3.0"])
151
155
  s.add_runtime_dependency(%q<cancan>, ["~> 1.4.1"])
@@ -155,7 +159,7 @@ Gem::Specification.new do |s|
155
159
  s.add_runtime_dependency(%q<activesupport>, [">= 3.0.1"])
156
160
  s.add_runtime_dependency(%q<logging_assist>, [">= 0.1.6"])
157
161
  else
158
- s.add_dependency(%q<rspec>, [">= 2.0.0"])
162
+ s.add_dependency(%q<rspec>, [">= 2.0.1"])
159
163
  s.add_dependency(%q<code-spec>, ["~> 0.2.5"])
160
164
  s.add_dependency(%q<rails-app-spec>, ["~> 0.3.0"])
161
165
  s.add_dependency(%q<cancan>, ["~> 1.4.1"])
@@ -166,7 +170,7 @@ Gem::Specification.new do |s|
166
170
  s.add_dependency(%q<logging_assist>, [">= 0.1.6"])
167
171
  end
168
172
  else
169
- s.add_dependency(%q<rspec>, [">= 2.0.0"])
173
+ s.add_dependency(%q<rspec>, [">= 2.0.1"])
170
174
  s.add_dependency(%q<code-spec>, ["~> 0.2.5"])
171
175
  s.add_dependency(%q<rails-app-spec>, ["~> 0.3.0"])
172
176
  s.add_dependency(%q<cancan>, ["~> 1.4.1"])
@@ -12,8 +12,12 @@ module RSpec::RubyContentMatchers
12
12
  @name = name
13
13
  end
14
14
 
15
+ def license_dir
16
+ File.join(app_dir, 'licenses')
17
+ end
18
+
15
19
  def license_file name
16
- File.join(app_dir, 'licenses', "#{name}_license.rb")
20
+ File.join(license_dir, "#{name}_license.rb")
17
21
  end
18
22
 
19
23
  def matches? obj, &block
@@ -21,11 +25,58 @@ module RSpec::RubyContentMatchers
21
25
  found = File.file? file_name
22
26
  yield File.read(file_name) if block && found
23
27
  found
24
- end
28
+ end
29
+
30
+ def failure_message
31
+ "No license file found for #{name} in #{license_dir} as expected"
32
+ end
33
+
34
+ def negative_failure_message
35
+ "License file #{name} found in #{license_dir} but was not expected"
36
+ end
37
+
25
38
  end
26
39
 
27
40
  def have_license_file name
28
41
  HaveLicenseFile.new name
29
42
  end
43
+
44
+ class HaveLicenseFiles
45
+ include ::Rails3::Assist::Artifact::Directory
46
+ include ::Rails3::Assist::Directory
47
+
48
+ attr_reader :names
49
+
50
+ def initialize *names
51
+ @names = names.flatten
52
+ end
53
+
54
+ def license_dir
55
+ File.join(app_dir, 'licenses')
56
+ end
57
+
58
+ def license_file name
59
+ File.join(license_dir, "#{name}_license.rb")
60
+ end
61
+
62
+ def matches? obj, &block
63
+ names.flatten.each do |name|
64
+ return false if !File.file? license_file(name)
65
+ end
66
+ true
67
+ end
68
+
69
+ def failure_message
70
+ "License files #{names} not found in #{license_dir} as expected"
71
+ end
72
+
73
+ def negative_failure_message
74
+ "License files #{names} found in #{license_dir} but was not expected"
75
+ end
76
+ end
77
+
78
+ def have_license_files *names
79
+ HaveLicenseFiles.new names
80
+ end
30
81
  end
31
82
  end
@@ -0,0 +1,58 @@
1
+ require 'rails/generators/base'
2
+ require 'sugar-high/array'
3
+ require 'active_support/inflector'
4
+ require 'rails3_artifactor'
5
+ require 'logging_assist'
6
+
7
+ module Cancan
8
+ module Generators
9
+ class LicensesGenerator < Rails::Generators::Base
10
+ desc "Creates Licenses for use with CanCan and Permits"
11
+
12
+ argument :licenses, :type => :array, :default => [], :desc => "Licenses"
13
+ class_option :default_licenses, :type => :boolean, :default => true, :desc => "Create default exemplar licenses"
14
+
15
+ source_root File.dirname(__FILE__) + '/templates'
16
+
17
+ def main_flow
18
+ if default_licenses?
19
+ default_licenses.each{|license| default_license license }
20
+ end
21
+
22
+ licenses.each do |license|
23
+ template_license(license) # if !skip_license?(license)
24
+ end
25
+ end
26
+
27
+ protected
28
+
29
+ include Rails3::Assist::BasicLogger
30
+ extend Rails3::Assist::UseMacro
31
+
32
+ use_helpers :app, :file, :special
33
+
34
+ attr_accessor :license_name
35
+
36
+ def skip_license? license
37
+ default_licenses? && default_licenses.include?(license.to_sym)
38
+ end
39
+
40
+ def default_licenses
41
+ [:user_admin, :blogging]
42
+ end
43
+
44
+ def default_licenses?
45
+ options[:default_licenses]
46
+ end
47
+
48
+ def template_license name
49
+ self.license_name = name
50
+ template "license.rb", "app/licenses/#{name}_license.rb"
51
+ end
52
+
53
+ def default_license name
54
+ template "#{name}_license.rb", "app/licenses/#{name}_license.rb"
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,8 @@
1
+ class <%= license_name.to_s.camelize %>License < License::Base
2
+ def initialize name
3
+ super
4
+ end
5
+
6
+ def enforce!
7
+ end
8
+ end
@@ -0,0 +1,127 @@
1
+ require 'rails/generators/base'
2
+ require 'sugar-high/array'
3
+ require 'active_support/inflector'
4
+ require 'rails3_artifactor'
5
+ require 'logging_assist'
6
+
7
+ module Cancan
8
+ module Generators
9
+ class PermitsGenerator < Rails::Generators::Base
10
+ desc "Creates a Permit for each role in 'app/permits' and ensures that the permit folder is added to Rails load path."
11
+
12
+ argument :roles, :type => :array, :default => [], :desc => "Roles to create permits for"
13
+
14
+ # ORM to use
15
+ class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
16
+ class_option :initializer, :type => :boolean, :default => true, :desc => "Create Permits initializer"
17
+
18
+ class_option :default_permits, :type => :boolean, :default => true, :desc => "Create default permits for guest and admin roles"
19
+
20
+ source_root File.dirname(__FILE__) + '/templates'
21
+
22
+ def main_flow
23
+ default_roles.each do |role|
24
+ template_permit role
25
+ end
26
+
27
+ template_permit :any, :any_permit
28
+ template_permit :system, :barebones_permit
29
+
30
+ permit_logic = base_logic
31
+ get_roles.each do |role|
32
+ template_permit(role) if !skip_permit?(role)
33
+ end
34
+
35
+ permits_initializer if permits_initializer?
36
+ end
37
+
38
+ protected
39
+
40
+ include Rails3::Assist::BasicLogger
41
+ extend Rails3::Assist::UseMacro
42
+
43
+ use_helpers :app, :file, :special
44
+
45
+ attr_accessor :permit_name, :permit_logic
46
+
47
+ def default_roles
48
+ [:guest, :admin]
49
+ end
50
+
51
+ def permits_initializer?
52
+ options[:initializer]
53
+ end
54
+
55
+ def skip_permit? permit
56
+ default_permits? && default_roles.include?(permit.to_sym)
57
+ end
58
+
59
+
60
+ # TODO: merge with any registered roles in application
61
+ def get_roles
62
+ roles.uniq.to_symbols
63
+ end
64
+
65
+ def default_permits?
66
+ options[:default_permits]
67
+ end
68
+
69
+ def orm
70
+ options[:orm]
71
+ end
72
+
73
+ def permits_initializer
74
+ create_initializer :permits do
75
+ "Permits::Ability.orm = :#{orm}"
76
+ end
77
+ end
78
+
79
+ def template_permit name, template_name=nil
80
+ permit_logic = send "#{name}_logic" if [:admin, :system, :any].include?(name)
81
+ self.permit_name = name
82
+
83
+ template "permit.rb", "app/permits/#{name}_permit.rb"
84
+ end
85
+
86
+ def any_logic
87
+ ""
88
+ end
89
+
90
+ def system_logic
91
+ %{
92
+ # allow to manage all and return :break to
93
+ # abort calling any other permissions
94
+
95
+ request = options[:request]
96
+ if request && request.host.localhost? && localhost_manager?
97
+ can(:manage, :all)
98
+ return :break
99
+ end
100
+ }
101
+ end
102
+
103
+ def base_logic
104
+ %{
105
+ return if !role_match? user
106
+
107
+ # can :create, Comment
108
+ # owns(user, Comment)
109
+ }
110
+ end
111
+
112
+ def admin_logic
113
+ %{
114
+ return if !role_match? user
115
+ can :manage, :all
116
+ }
117
+ end
118
+
119
+ def guest_logic
120
+ %{
121
+ return if !role_match? user
122
+ can :read, :all
123
+ }
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+ require 'generator-spec'
3
+
4
+ require_generator :cancan => :licenses
5
+
6
+ RSpec::Generator.configure do |config|
7
+ config.debug = true
8
+ config.remove_temp_dir = true #false
9
+ config.default_rails_root(__FILE__)
10
+ config.lib = File.dirname(__FILE__) + '/../lib'
11
+ config.logger = :stdout # :file
12
+ end
13
+
14
+
15
+ describe 'Licenses generator' do
16
+ use_helpers :controller, :special, :file
17
+
18
+ setup_generator :licenses do
19
+ tests Cancan::Generators::LicensesGenerator
20
+ end
21
+
22
+ describe "Licenses: Profile Administration and Article Editing" do
23
+ before :each do
24
+ @generator = with_generator do |g|
25
+ g.run_generator "profile_administration article_editing --no-default-licenses".args
26
+ end
27
+ end
28
+
29
+ it "should have created license files" do
30
+ @generator.should have_license_files :profile_administration, :article_editing
31
+ @generator.should_not have_license_files :blogging, :user_admin
32
+ end
33
+ end
34
+
35
+ describe "Licenses: Profile Administration and Article Editing" do
36
+ before :each do
37
+ @generator = with_generator do |g|
38
+ g.run_generator "profile_administration article_editing".args
39
+ end
40
+ end
41
+
42
+ it "should have created license files" do
43
+ @generator.should have_license_files :profile_administration, :article_editing, :blogging, :user_admin
44
+ end
45
+
46
+ it "should have created license file :profile_administration with the right class" do
47
+ @generator.should have_license_file :profile_administration do |license|
48
+ license.should have_license_class :profile_administration
49
+ end
50
+ end
51
+
52
+ it "should have created license file :article_editing with the right class" do
53
+ @generator.should have_license_file :article_editing do |license|
54
+ license.should have_license_class :article_editing
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
  require 'generator-spec'
3
3
 
4
- require_generator :permits
4
+ require_generator :cancan => :permits
5
5
 
6
6
  RSpec::Generator.configure do |config|
7
7
  config.debug = true
@@ -16,14 +16,13 @@ describe 'Permits generator' do
16
16
  use_helpers :controller, :special, :file
17
17
 
18
18
  setup_generator :permits do
19
- tests PermitsGenerator
19
+ tests Cancan::Generators::PermitsGenerator
20
20
  end
21
21
 
22
22
  describe 'result of running generator with default profile' do
23
23
  before :each do
24
24
  @generator = with_generator do |g|
25
- arguments = "--orm mongoid".args
26
- g.run_generator arguments
25
+ g.run_generator "--orm mongoid".args
27
26
  end
28
27
  end
29
28
 
@@ -42,7 +41,7 @@ describe 'Permits generator' do
42
41
  context "Registered roles :editor, :admin" do
43
42
  before :each do
44
43
  @generator = with_generator do |g|
45
- g.run_generator "--roles admin editor".args
44
+ g.run_generator "admin editor".args
46
45
  end
47
46
  end
48
47
 
@@ -55,16 +54,6 @@ describe 'Permits generator' do
55
54
  editor_permit.should_not have_licenses :user_admin, :blogging
56
55
  end
57
56
  end
58
-
59
- it "should have created the License file with the :user_admin and :blogging licenses used by the :editor permit" do
60
- @generator.should have_license_file :user_admin do |license_file|
61
- license_file.should have_license_class :user_admin
62
- end
63
-
64
- @generator.should have_license_file :blogging do |license_file|
65
- license_file.should have_license_class :blogging
66
- end
67
- end
68
57
  end #ctx
69
58
  end
70
59
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 1
9
- version: 0.3.1
8
+ - 2
9
+ version: 0.3.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -28,8 +28,8 @@ dependencies:
28
28
  segments:
29
29
  - 2
30
30
  - 0
31
- - 0
32
- version: 2.0.0
31
+ - 1
32
+ version: 2.0.1
33
33
  type: :development
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
@@ -186,10 +186,12 @@ files:
186
186
  - lib/cancan-permits/rspec/matchers/have_license.rb
187
187
  - lib/cancan-permits/rspec/matchers/have_license_class.rb
188
188
  - lib/cancan-permits/rspec/matchers/have_license_file.rb
189
- - lib/generators/permits/permits_generator.rb
190
- - lib/generators/permits/templates/blogging_license.rb
191
- - lib/generators/permits/templates/permit.rb
192
- - lib/generators/permits/templates/user_admin_license.rb
189
+ - lib/generators/cancan/licenses/licenses_generator.rb
190
+ - lib/generators/cancan/licenses/templates/blogging_license.rb
191
+ - lib/generators/cancan/licenses/templates/license.rb
192
+ - lib/generators/cancan/licenses/templates/user_admin_license.rb
193
+ - lib/generators/cancan/permits/permits_generator.rb
194
+ - lib/generators/cancan/permits/templates/permit.rb
193
195
  - spec/active_record/db/database.yml
194
196
  - spec/active_record/migrations/001_create_user.rb
195
197
  - spec/active_record/migrations/002_create_comment.rb
@@ -209,7 +211,8 @@ files:
209
211
  - spec/fixtures/permits/guest_permit.rb
210
212
  - spec/fixtures/permits/super_admin_permit.rb
211
213
  - spec/fixtures/permits/system_permit.rb
212
- - spec/generators/permit_generator_spec.rb
214
+ - spec/generators/cancan/licenses_generator_spec.rb
215
+ - spec/generators/cancan/permits_generator_spec.rb
213
216
  - spec/generic/api/basic/config.rb
214
217
  - spec/generic/api/basic/xgroup.rb
215
218
  - spec/generic/api/basic/xgroup_orm.rb
@@ -285,7 +288,8 @@ test_files:
285
288
  - spec/fixtures/permits/guest_permit.rb
286
289
  - spec/fixtures/permits/super_admin_permit.rb
287
290
  - spec/fixtures/permits/system_permit.rb
288
- - spec/generators/permit_generator_spec.rb
291
+ - spec/generators/cancan/licenses_generator_spec.rb
292
+ - spec/generators/cancan/permits_generator_spec.rb
289
293
  - spec/generic/api/basic/config.rb
290
294
  - spec/generic/api/basic/xgroup.rb
291
295
  - spec/generic/api/basic/xgroup_orm.rb
@@ -1,150 +0,0 @@
1
- require 'rails/generators/base'
2
- require 'sugar-high/array'
3
- require 'active_support/inflector'
4
- require 'rails3_artifactor'
5
- require 'logging_assist'
6
-
7
- class PermitsGenerator < Rails::Generators::Base
8
- desc "Creates a Permit for each role in 'app/permits' and ensures that the permit folder is added to Rails load path."
9
-
10
- class_option :roles, :type => :array, :default => [], :desc => "Roles to create permits for"
11
- class_option :licenses, :type => :array, :default => [], :desc => "Licenses"
12
-
13
- # ORM to use
14
- class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
15
- class_option :initializer, :type => :boolean, :default => true, :desc => "Create Permits initializer"
16
-
17
- class_option :default_permits, :type => :boolean, :default => true, :desc => "Create default permits for guest and admin roles"
18
- class_option :default_licenses, :type => :boolean, :default => true, :desc => "Create default exemplar licenses"
19
-
20
- source_root File.dirname(__FILE__) + '/templates'
21
-
22
- def main_flow
23
- default_roles.each do |role|
24
- template_permit role
25
- end
26
-
27
- template_permit :any, :any_permit
28
- template_permit :system, :barebones_permit
29
-
30
- permit_logic = base_logic
31
- roles.each do |role|
32
- template_permit(role) if !skip_permit?(role)
33
- end
34
-
35
- if default_licenses?
36
- template_license :user_admin
37
- template_license :blogging
38
- end
39
-
40
- licenses.each do |license|
41
- template_license(license) if !skip_license?(license)
42
- end
43
-
44
- permits_initializer if permits_initializer?
45
- end
46
-
47
- protected
48
-
49
- include Rails3::Assist::BasicLogger
50
- extend Rails3::Assist::UseMacro
51
-
52
- use_helpers :app, :file, :special
53
-
54
- attr_accessor :permit_name, :permit_logic
55
-
56
- def default_roles
57
- [:guest, :admin]
58
- end
59
-
60
- def permits_initializer?
61
- options[:initializer]
62
- end
63
-
64
- def skip_license? license
65
- default_licenses? && default_licenses.include?(license.to_sym)
66
- end
67
-
68
- def skip_permit? permit
69
- default_permits? && default_roles.include?(permit.to_sym)
70
- end
71
-
72
-
73
- # TODO: merge with any registered roles in application
74
- def roles
75
- options[:roles].uniq.to_symbols
76
- end
77
-
78
- def default_licenses?
79
- options[:default_licenses]
80
- end
81
-
82
- def default_permits?
83
- options[:default_permits]
84
- end
85
-
86
- def licenses
87
- options[:licenses]
88
- end
89
-
90
- def orm
91
- options[:orm]
92
- end
93
-
94
- def permits_initializer
95
- create_initializer :permits do
96
- "Permits::Ability.orm = :#{orm}"
97
- end
98
- end
99
-
100
- def template_license name
101
- template "#{name}_license.rb", "app/licenses/#{name}_license.rb"
102
- end
103
-
104
- def template_permit name, template_name=nil
105
- permit_logic = send "#{name}_logic" if [:admin, :system, :any].include?(name)
106
- self.permit_name = name
107
-
108
- template "permit.rb", "app/permits/#{name}_permit.rb"
109
- end
110
-
111
- def any_logic
112
- ""
113
- end
114
-
115
- def system_logic
116
- %{
117
- # allow to manage all and return :break to
118
- # abort calling any other permissions
119
-
120
- request = options[:request]
121
- if request && request.host.localhost? && localhost_manager?
122
- can(:manage, :all)
123
- return :break
124
- end
125
- }
126
- end
127
-
128
- def base_logic
129
- %{
130
- return if !role_match? user
131
-
132
- # can :create, Comment
133
- # owns(user, Comment)
134
- }
135
- end
136
-
137
- def admin_logic
138
- %{
139
- return if !role_match? user
140
- can :manage, :all
141
- }
142
- end
143
-
144
- def guest_logic
145
- %{
146
- return if !role_match? user
147
- can :read, :all
148
- }
149
- end
150
- end