burlesque 2.0.1 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,48 +1,213 @@
1
- Burlesque
2
- =========
1
+ # Burlesque
3
2
 
4
- Crea estructura de roles y grupos para cualquier modelo, considera además las 3 tablas de _join_ que pudiesen darse.
3
+ Create roles and groups structures for any model, also creates the 3 _join_ tables.
5
4
 
6
- Installation
7
- ------------
5
+ ## Installation
8
6
 
9
- Before you can use the generator, add the gem to your project's Gemfile as follows:
7
+ In order to use the generator, add the gem to your project's `Gemfile` as follows:
10
8
 
11
- ```
9
+ ```ruby
12
10
  gem 'burlesque'
13
11
  ```
14
12
 
15
13
  Then install it by running:
16
14
 
17
- ```
15
+ ```bash
18
16
  bundle install
19
17
  ```
20
18
 
21
- After, bootstrap your Rails app, for example:
19
+ Then bootstrap your Rails app:
22
20
 
23
- ```
21
+ ```bash
24
22
  rake burlesque:install:migrations
25
23
  rake db:migrate
26
24
  ```
27
25
 
28
- If you would like to run migrations only from Burlesque engine, you can do it by specifying SCOPE:
26
+ Note that if you want to run **only** the Burlesque migrations, you can use the `SCOPE` option:
29
27
 
30
- ```
28
+ ```bash
31
29
  rake db:migrate SCOPE=burlesque
32
30
  ```
33
31
 
34
- Finally, in model that you need burlesque:
32
+ Finally, enable Burlesque in each model:
33
+
34
+ ```ruby
35
+ class User < ActiveRecord::Base
36
+ include Burlesque::Admin
37
+
38
+ # ...
39
+ end
40
+ ```
41
+
42
+ ## Defining Role's
43
+
44
+ You can define/create all RESTful roles at once with:
45
+
46
+ ```
47
+ Burlesque::Role.for 'user' # String, or
48
+ Burlesque::Role.for :user # Symbol, or
49
+ Burlesque::Role.for User # Constant
50
+ ```
51
+
52
+ This will create the following authorizations:
53
+
54
+ Action | Resource | Description
55
+ :-------|:---------|:-----------
56
+ Read | User | Can see list of all Users or single User details
57
+ Create | User | Can create a new User (:new, :create)
58
+ Update | User | Can Update a User (:edit, :update)
59
+ Destroy | User | Can delete a User
60
+
61
+
62
+ ## Defining Group's
63
+
64
+ Group names must be unique. The best way to define Burlesque groups is:
65
+
66
+ ```
67
+ Burlesque::Group.find_or_create_by_name('Admin')
68
+ ```
69
+
70
+ ## Group's and Role's
35
71
 
72
+ To assign a list of `Burlesque::Role` identifiers to a `Burlesque::Group` just do:
73
+
74
+ ```ruby
75
+ group = Burlesque::Group.find_by_name('Admin')
76
+ group.push_roles [role, another_role] # role and antoher_role are instances of Burlesque::Role
36
77
  ```
37
- include Burlesque::Admin
78
+
79
+ The `push_roles` method ensures the roles are only added once to the group. If you want to assign the groups by yourself, please take care with that. Otherwise, Burlesque will raise an `ActiveRecord::RecordInvalid` exception.
80
+
81
+
82
+ ## Utility functions
83
+
84
+ Assuming you enabled **Burlesque** in your model `User`:
85
+
86
+ ### Questions
87
+
88
+ Ask for groups:
89
+
90
+ ```ruby
91
+ user.group?(group) # group is an instance of Burlesque::Group
92
+ user.group?('Admin') # it also works with groups names
93
+ ```
94
+
95
+ Ask for roles:
96
+
97
+ ```ruby
98
+ user.role?(role) # role is an instance of Burlesque::Role
99
+ user.role?('all#manage') # again, it also works with names
38
100
  ```
39
101
 
102
+ Since an user may have `Burlesque::Role` without necessarily belonging to a `Burlesque::Group`, you may want to know if a given _role_ is provided by the belonging to a `Burlesque::Group`, or not.
40
103
 
104
+ ```ruby
105
+ user.role_in_groups?(role) # role is an instance of Burlesque::Role
106
+ user.role_in_groups?('all#manage')
107
+ ```
41
108
 
42
- CanCan
43
- ------
109
+ ### MassAssignment
44
110
 
45
- Burlesque makes no assumption about how auhtorizations are handled in your application. However it integrates nicely with [CanCan][cancan]; all you have to do is define the `Ability` model as follows:
111
+ To assign a list of groups to an user:
112
+
113
+ ```ruby
114
+ ids = [1, 3, 4] # groups identifiers
115
+ user.group_ids = (ids)
116
+ ```
117
+
118
+ To assign a list of roles to an user:
119
+
120
+ ```ruby
121
+ ids = [6, 7, 9] # roles identifiers
122
+ user.role_ids = (ids)
123
+ ```
124
+
125
+
126
+ ### Search
127
+
128
+ You can search `Burlesque::Role` using defined SCOPE search and combine them any way you need.
129
+
130
+ Scope | Description
131
+ :-------------|:-----------
132
+ action | To search for `Burlesque::Role` that are associated with the same action.
133
+ not_action | To search for `Burlesque::Role` that are not associated with the same action.
134
+ resource | To search for `Burlesque::Role` that are linked to a resource in question
135
+ not_resource | To search for `Burlesque::Role` that are not linked to a resource in question
136
+
137
+ Example:
138
+
139
+ ```ruby
140
+ Burlesque::Role.action('read') # String, or
141
+ Burlesque::Role.action(:read) # Symbol
142
+
143
+ Burlesque::Role.resource('user') # String, or
144
+ Burlesque::Role.resource(:user) # Symbol, or
145
+ Burlesque::Role.resource(User) # Constant
146
+ ```
147
+
148
+ # I18n
149
+
150
+ If you want to translate the roles names, you can use the `translate_name` method:
151
+
152
+ ```ruby
153
+ role = Burlesque::Role.action(:read).resource(:user)
154
+ name = role.translate_name() # where role is an instance of Burlesque::Role to be translated
155
+ # => 'Read User'
156
+ ```
157
+
158
+ The translation is handled by a YML file. If you only have RESTful actions, it may not be necessary to translate the roles by hand, as Burlesque will automagically use the model translations to compose human-friendly strings:
159
+
160
+
161
+ ```ruby
162
+ Burlesque::Role.create(name: 'user#read' ).translate_name() ==> Read User
163
+ Burlesque::Role.create(name: 'user#create' ).translate_name() ==> Create User
164
+ Burlesque::Role.create(name: 'user#update' ).translate_name() ==> Read User
165
+ Burlesque::Role.create(name: 'user#destroy').translate_name() ==> Destroy User
166
+ Burlesque::Role.create(name: 'user#manage' ).translate_name() ==> Manage User
167
+ ```
168
+
169
+ However, you are free to override the Burlesque defaults (e.g. for localization, here in Spanish):
170
+
171
+ ```yaml
172
+ es:
173
+ authorizations:
174
+ read: Leer
175
+ create: Crear
176
+ update: Actualizar
177
+ destroy: Eliminar
178
+ manage: Administrar
179
+ ```
180
+
181
+ ```ruby
182
+ # Provided Spanish is the default language of your app
183
+ Burlesque::Role.create(name: 'user#read' ).translate_name() ==> Leer Usuarios
184
+ Burlesque::Role.create(name: 'user#create' ).translate_name() ==> Crear Usuarios
185
+ Burlesque::Role.create(name: 'user#update' ).translate_name() ==> Actualizar Usuarios
186
+ Burlesque::Role.create(name: 'user#destroy').translate_name() ==> Eliminar Usuarios
187
+ Burlesque::Role.create(name: 'user#manage' ).translate_name() ==> Administrar Usuarios
188
+ ```
189
+
190
+ If what you want is to change the translation of a single `Burlesque::Role`, you can:
191
+ Finally, in case you were not satisfied by the generated translations, you can define your own:
192
+
193
+ ```yaml
194
+ es:
195
+ authorizations:
196
+ user#read: 'Read Awesome Users'
197
+ ```
198
+
199
+ ```ruby
200
+ Burlesque::Role.action(:read).resource(:user).translate_name() ==> Read Awesome Users
201
+ Burlesque::Role.find_by_name('user#read').translate_name() ==> Read Awesome Users
202
+ ```
203
+
204
+ # Contributing
205
+
206
+ TODO
207
+
208
+ # CanCan
209
+
210
+ Burlesque makes no assumption about how auhtorizations are handled in your application. However it integrates nicely with [CanCan][cancan] all you have to do is define the `Ability` model as follows:
46
211
 
47
212
  ```ruby
48
213
  class Ability
@@ -62,7 +227,7 @@ end
62
227
 
63
228
  [cancan]: https://github.com/ryanb/cancan
64
229
 
65
- Todo
66
- ----
230
+
231
+ # Todo
67
232
 
68
233
  Rake task for burlesque admin module inclusion.
@@ -18,10 +18,10 @@ module Burlesque
18
18
 
19
19
  SPLITER = '#'
20
20
 
21
- scope :action, lambda { |action| where('name LIKE ?', "%#{SPLITER}#{action}") }
22
- scope :not_action, lambda { |action| where('name NOT LIKE ?', "%#{SPLITER}#{action}") }
23
- scope :resource, lambda { |model| where('name LIKE ?', "#{model.to_s}#{SPLITER}%") }
24
- scope :not_resource, lambda { |model| where('name NOT LIKE ?', "#{model.to_s}#{SPLITER}%") }
21
+ scope :action, lambda { |action| where('name LIKE ?', "%#{SPLITER}#{action}") }
22
+ scope :not_action, lambda { |action| where('name NOT LIKE ?', "%#{SPLITER}#{action}") }
23
+ scope :resource, lambda { |model| where('name LIKE ?', "#{model.to_s.underscore}#{SPLITER}%") }
24
+ scope :not_resource, lambda { |model| where('name NOT LIKE ?', "#{model.to_s.underscore}#{SPLITER}%") }
25
25
 
26
26
  # Public: Relacion a muchos usuarios
27
27
  #
@@ -44,7 +44,11 @@ module Burlesque
44
44
  translate = I18n.t(name.to_sym, scope: :authorizations)
45
45
  return translate unless translate.include?('translation missing:')
46
46
 
47
- translate = I18n.t(action_sym, scope: :authorizations) + ' ' + resource_class.model_name.human
47
+ begin
48
+ translate = I18n.t(action_sym, scope: :authorizations) + ' ' + resource_class.model_name.human
49
+ rescue
50
+ translate = I18n.t(action_sym, scope: :authorizations) + ' ' + resource_class.to_s
51
+ end
48
52
  end
49
53
 
50
54
  # Public: Entrega el recurso asociado al rol.
@@ -16,6 +16,7 @@
16
16
  attributes:
17
17
  role:
18
18
  name: Nombre
19
+ groups: Grupos
19
20
  created_at: Creado
20
21
  updated_at: Última actualización
21
22
  group:
@@ -1,3 +1,3 @@
1
1
  module Burlesque
2
- VERSION = "2.0.1"
2
+ VERSION = "2.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: burlesque
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-26 00:00:00.000000000 Z
12
+ date: 2013-07-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails