moulin_rouge 0.0.1.alpha → 0.0.1.beta
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/CHANGELOG.md +5 -1
- data/lib/generators/moulin_rouge/install_generator.rb +17 -0
- data/lib/generators/moulin_rouge/permission_generator.rb +18 -0
- data/lib/generators/moulin_rouge/templates/install/config/initializers/moulin_rouge.rb +15 -0
- data/lib/generators/moulin_rouge/templates/permission.rb +19 -0
- data/lib/moulin_rouge/cancan/responder.rb +60 -1
- data/lib/moulin_rouge/version.rb +1 -1
- data/spec/spec_helper.rb +4 -2
- metadata +19 -14
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module MoulinRouge
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < ::Rails::Generators::Base
|
6
|
+
|
7
|
+
source_root File.expand_path('../templates', __FILE__)
|
8
|
+
|
9
|
+
desc "Installs the gem folder structure for the app"
|
10
|
+
|
11
|
+
def generate_folder_structure
|
12
|
+
directory("install")
|
13
|
+
invoke "moulin_rouge:permission admin"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module MoulinRouge
|
4
|
+
module Generators
|
5
|
+
class PermissionGenerator < ::Rails::Generators::Base
|
6
|
+
|
7
|
+
source_root File.expand_path('../templates', __FILE__)
|
8
|
+
|
9
|
+
desc "Creates a new permission file inside the permission folder"
|
10
|
+
|
11
|
+
argument :name
|
12
|
+
|
13
|
+
def creates_a_permission_file
|
14
|
+
copy_file("permission.rb", "app/permissions/#{name}.rb")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
MoulinRouge.configure do |config|
|
2
|
+
# Cache permissions
|
3
|
+
config.cache = Rails.env.production?
|
4
|
+
# The search path for permissions
|
5
|
+
config.path = 'app/permissions/**/*.rb'
|
6
|
+
# The method that will test the permission
|
7
|
+
config.test_method = :is?
|
8
|
+
# The class of the model
|
9
|
+
config.model = User
|
10
|
+
# How you like to call the active user model
|
11
|
+
config.model_instance = :current_user
|
12
|
+
end
|
13
|
+
|
14
|
+
# Creates the Ability class
|
15
|
+
MoulinRouge.run!
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# can :read, Post
|
2
|
+
# can :read, Comment
|
3
|
+
# can :manage, Comment do |comment|
|
4
|
+
# comment.user_id == current_user.id
|
5
|
+
# end
|
6
|
+
#
|
7
|
+
# role :admin do
|
8
|
+
# can :manage, :all
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# group :main do
|
12
|
+
# role :editor do
|
13
|
+
# can :manage, Post
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# role :author do
|
17
|
+
# can :manage, Post, :user_id => current_user.id
|
18
|
+
# end
|
19
|
+
# end
|
@@ -1,7 +1,66 @@
|
|
1
1
|
module MoulinRouge
|
2
2
|
module CanCan
|
3
|
+
# A cleaner implementation of CanCan #load_and_authorize_resource method.
|
4
|
+
# Everytime you use the #respond_with method, send the given resources to check
|
5
|
+
# the authorizations.
|
3
6
|
class Responder # < ActionController::Responder
|
7
|
+
def initialize(controller, resources, options={})
|
8
|
+
super
|
9
|
+
@skip_authorization = options.delete(:skip_authorization)
|
10
|
+
end
|
4
11
|
|
12
|
+
def to_html
|
13
|
+
resources.each do |resource|
|
14
|
+
action = resources.last == resource ? controller.params[:action].to_sym : :show
|
15
|
+
controller.authorize!(action, resource_class_or_instace(resource)) unless skip?(resource)
|
16
|
+
end
|
17
|
+
super # Continue through method chain
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# Returns the resource class or current instance.
|
23
|
+
#
|
24
|
+
# Try to return a valid class for this resource.
|
25
|
+
# It will try 3 strategies, in the following order:
|
26
|
+
# 1. If is an array and is not empty get the class name from first item
|
27
|
+
# 2. If is an empty array, tries to predict the class from variable name,
|
28
|
+
# this strategy relies on Rails naming conventions
|
29
|
+
# 3. Just return the given resource, assuming that already is the class
|
30
|
+
def resource_class_or_instace(resource)
|
31
|
+
if resource.is_a? Array
|
32
|
+
if resource.empty?
|
33
|
+
variable_name(resource).classify.constantize # raises NameError when a match is not found
|
34
|
+
else
|
35
|
+
resource.first.class.name
|
36
|
+
end
|
37
|
+
else
|
38
|
+
resource
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns the variable name that contains that resource
|
43
|
+
def variable_name(resource)
|
44
|
+
name = controller.instance_variables.index { |var| controller.instance_variable_get(var) === resources.first }
|
45
|
+
name = controller.instance_variables[name].to_s.gsub(/@/, '') if name
|
46
|
+
name.to_s
|
47
|
+
end
|
48
|
+
|
49
|
+
# Return true or false if resource is matched to skip
|
50
|
+
# If skip is a symbol skip resource with same name
|
51
|
+
# If skip is a array see if includes the resouce
|
52
|
+
# Otherwise send the param through
|
53
|
+
def skip?(resource)
|
54
|
+
resource_name = variable_name(resource)
|
55
|
+
|
56
|
+
if @skip_authorization.is_a? Symbol
|
57
|
+
@skip_authorization == resource_name.to_sym
|
58
|
+
elsif @skip_authorization.is_a? Array
|
59
|
+
@skip_authorization.include?(resource_name, resource_name.to_sym)
|
60
|
+
else
|
61
|
+
@skip_authorization || false
|
62
|
+
end
|
63
|
+
end
|
5
64
|
end
|
6
65
|
end
|
7
|
-
end
|
66
|
+
end
|
data/lib/moulin_rouge/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -9,8 +9,10 @@ require 'rspec'
|
|
9
9
|
# in spec/support/ and its subdirectories.
|
10
10
|
Dir[File.expand_path("spec/support/**/*.rb")].each {|f| require f}
|
11
11
|
|
12
|
-
# Require all files from the project
|
13
|
-
Dir[File.expand_path("lib/**/*.rb")].each
|
12
|
+
# Require all files from the project, except the generators
|
13
|
+
Dir[File.expand_path("lib/**/*.rb")].each do |f|
|
14
|
+
require f unless f =~ /lib\/generators/
|
15
|
+
end
|
14
16
|
|
15
17
|
def permission_file
|
16
18
|
File.expand_path("spec/fixtures/spec_permission.rb")
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: moulin_rouge
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1.
|
4
|
+
version: 0.0.1.beta
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-03-16 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: cancan
|
16
|
-
requirement: &
|
16
|
+
requirement: &20634180 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *20634180
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rdoc
|
27
|
-
requirement: &
|
27
|
+
requirement: &20633640 !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: *
|
35
|
+
version_requirements: *20633640
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &20633180 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *20633180
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: guard-rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &20632640 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *20632640
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: simplecov
|
60
|
-
requirement: &
|
60
|
+
requirement: &20632060 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,9 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
69
|
-
description: A wrapper to CanCan authorization system,
|
70
|
-
in many ruby files.
|
68
|
+
version_requirements: *20632060
|
69
|
+
description: A wrapper to CanCan authorization system, that lets you declare permissions
|
70
|
+
in many ruby files. Adds new functionalities allowing you do more with less code
|
71
|
+
and DRY.
|
71
72
|
email:
|
72
73
|
- edson.hilios@gmail.com
|
73
74
|
executables: []
|
@@ -78,6 +79,10 @@ files:
|
|
78
79
|
- MIT-LICENSE
|
79
80
|
- README.md
|
80
81
|
- lib/tasks/moulin_rouge_tasks.rake
|
82
|
+
- lib/generators/moulin_rouge/templates/install/config/initializers/moulin_rouge.rb
|
83
|
+
- lib/generators/moulin_rouge/templates/permission.rb
|
84
|
+
- lib/generators/moulin_rouge/permission_generator.rb
|
85
|
+
- lib/generators/moulin_rouge/install_generator.rb
|
81
86
|
- lib/moulin_rouge.rb
|
82
87
|
- lib/moulin_rouge/model_double.rb
|
83
88
|
- lib/moulin_rouge/permission.rb
|
@@ -119,7 +124,7 @@ rubyforge_project:
|
|
119
124
|
rubygems_version: 1.8.10
|
120
125
|
signing_key:
|
121
126
|
specification_version: 3
|
122
|
-
summary: Organize
|
127
|
+
summary: Organize CanCan permissions in many files and DRY.
|
123
128
|
test_files:
|
124
129
|
- spec/fixtures/role.rb
|
125
130
|
- spec/spec_helper.rb
|