burlesque 1.0.0 → 2.0.0

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 (85) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +22 -3
  3. data/Rakefile +40 -1
  4. data/app/assets/javascripts/burlesque/application.js +15 -0
  5. data/app/assets/stylesheets/burlesque/application.css +13 -0
  6. data/app/controllers/burlesque/application_controller.rb +4 -0
  7. data/app/helpers/burlesque/application_helper.rb +4 -0
  8. data/app/models/burlesque/admin_group.rb +22 -0
  9. data/app/models/burlesque/admin_role.rb +13 -0
  10. data/app/models/burlesque/group.rb +87 -0
  11. data/app/models/burlesque/role.rb +92 -0
  12. data/app/models/burlesque/role_group.rb +24 -0
  13. data/app/views/layouts/burlesque/application.html.erb +14 -0
  14. data/config/routes.rb +2 -0
  15. data/db/migrate/20130725210208_create_burlesque_groups.rb +9 -0
  16. data/db/migrate/20130725210428_create_burlesque_roles.rb +9 -0
  17. data/db/migrate/20130725214806_create_burlesque_admin_groups.rb +15 -0
  18. data/db/migrate/20130725223204_create_burlesque_role_groups.rb +12 -0
  19. data/db/migrate/20130725223437_create_burlesque_admin_roles.rb +15 -0
  20. data/lib/burlesque.rb +5 -6
  21. data/lib/burlesque/admin.rb +14 -10
  22. data/lib/burlesque/engine.rb +5 -0
  23. data/lib/burlesque/version.rb +1 -1
  24. data/lib/tasks/burlesque_tasks.rake +4 -0
  25. data/test/burlesque_test.rb +7 -0
  26. data/test/dummy/README.rdoc +261 -0
  27. data/test/dummy/Rakefile +7 -0
  28. data/test/dummy/app/assets/javascripts/application.js +15 -0
  29. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  30. data/test/dummy/app/controllers/application_controller.rb +3 -0
  31. data/test/dummy/app/helpers/application_helper.rb +2 -0
  32. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  33. data/test/dummy/config.ru +4 -0
  34. data/test/dummy/config/application.rb +59 -0
  35. data/test/dummy/config/boot.rb +10 -0
  36. data/test/dummy/config/database.yml +25 -0
  37. data/test/dummy/config/environment.rb +5 -0
  38. data/test/dummy/config/environments/development.rb +37 -0
  39. data/test/dummy/config/environments/production.rb +67 -0
  40. data/test/dummy/config/environments/test.rb +37 -0
  41. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  42. data/test/dummy/config/initializers/inflections.rb +15 -0
  43. data/test/dummy/config/initializers/mime_types.rb +5 -0
  44. data/test/dummy/config/initializers/secret_token.rb +7 -0
  45. data/test/dummy/config/initializers/session_store.rb +8 -0
  46. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  47. data/test/dummy/config/locales/en.yml +5 -0
  48. data/test/dummy/config/routes.rb +4 -0
  49. data/test/dummy/public/404.html +26 -0
  50. data/test/dummy/public/422.html +26 -0
  51. data/test/dummy/public/500.html +25 -0
  52. data/test/dummy/public/favicon.ico +0 -0
  53. data/test/dummy/script/rails +6 -0
  54. data/test/fixtures/burlesque/admin_groups.yml +9 -0
  55. data/test/fixtures/burlesque/admin_roles.yml +9 -0
  56. data/test/fixtures/burlesque/groups.yml +7 -0
  57. data/test/fixtures/burlesque/role_groups.yml +9 -0
  58. data/test/fixtures/burlesque/roles.yml +7 -0
  59. data/test/integration/navigation_test.rb +10 -0
  60. data/test/test_helper.rb +15 -0
  61. data/test/unit/burlesque/admin_group_test.rb +9 -0
  62. data/test/unit/burlesque/admin_role_test.rb +9 -0
  63. data/test/unit/burlesque/group_test.rb +9 -0
  64. data/test/unit/burlesque/role_group_test.rb +9 -0
  65. data/test/unit/burlesque/role_test.rb +9 -0
  66. metadata +123 -25
  67. data/.gitignore +0 -7
  68. data/Gemfile +0 -4
  69. data/burlesque.gemspec +0 -24
  70. data/lib/burlesque/admin_group.rb +0 -25
  71. data/lib/burlesque/authorization.rb +0 -13
  72. data/lib/burlesque/group.rb +0 -91
  73. data/lib/burlesque/role.rb +0 -94
  74. data/lib/burlesque/role_group.rb +0 -30
  75. data/lib/generators/burlesque/install_generator.rb +0 -85
  76. data/lib/generators/burlesque/templates/admin_group.rb +0 -3
  77. data/lib/generators/burlesque/templates/authorization.rb +0 -3
  78. data/lib/generators/burlesque/templates/create_admin_groups.rb +0 -13
  79. data/lib/generators/burlesque/templates/create_authorizations.rb +0 -13
  80. data/lib/generators/burlesque/templates/create_groups.rb +0 -9
  81. data/lib/generators/burlesque/templates/create_role_groups.rb +0 -12
  82. data/lib/generators/burlesque/templates/create_roles.rb +0 -9
  83. data/lib/generators/burlesque/templates/group.rb +0 -3
  84. data/lib/generators/burlesque/templates/role.rb +0 -3
  85. data/lib/generators/burlesque/templates/role_group.rb +0 -3
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -9,7 +9,7 @@ Installation
9
9
  Before you can use the generator, add the gem to your project's Gemfile as follows:
10
10
 
11
11
  ```
12
- gem 'burlesque', :git => 'git@gitlab.acid.cl:burlesque.git'
12
+ gem 'burlesque'
13
13
  ```
14
14
 
15
15
  Then install it by running:
@@ -18,13 +18,27 @@ Then install it by running:
18
18
  bundle install
19
19
  ```
20
20
 
21
- Finally, bootstrap your Rails app, for example:
21
+ After, bootstrap your Rails app, for example:
22
22
 
23
23
  ```
24
- rails generate burlesque:install
24
+ rake burlesque:install:migrations
25
25
  rake db:migrate
26
26
  ```
27
27
 
28
+ If you would like to run migrations only from Burlesque engine, you can do it by specifying SCOPE:
29
+
30
+ ```
31
+ rake db:migrate SCOPE=burlesque
32
+ ```
33
+
34
+ Finally, in model that you need burlesque:
35
+
36
+ ```
37
+ include Burlesque::Admin
38
+ ```
39
+
40
+
41
+
28
42
  CanCan
29
43
  ------
30
44
 
@@ -47,3 +61,8 @@ end
47
61
  ```
48
62
 
49
63
  [cancan]: https://github.com/ryanb/cancan
64
+
65
+ Todo
66
+ ----
67
+
68
+ Rake task for burlesque admin module inclusion.
data/Rakefile CHANGED
@@ -1 +1,40 @@
1
- require "bundler/gem_tasks"
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Burlesque'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ require 'rake/testtask'
31
+
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib'
34
+ t.libs << 'test'
35
+ t.pattern = 'test/**/*_test.rb'
36
+ t.verbose = false
37
+ end
38
+
39
+
40
+ task :default => :test
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,4 @@
1
+ module Burlesque
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Burlesque
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ # Public: Modelo intermedio, permite que un Grupo pueda ser asignado a un 'Administrador',
4
+ # Este administrador puede ser cualquier modelo que incluya el modulo 'Burlesque::Admin'
5
+ #
6
+ module Burlesque
7
+ class AdminGroup < ActiveRecord::Base
8
+ belongs_to :group
9
+ belongs_to :adminable, polymorphic: true
10
+
11
+ validates_uniqueness_of :group_id, scope: [:adminable_id, :adminable_type], message: I18n.t('errors.messages.group_taken')
12
+
13
+ after_create :add_new_roles_to_admin
14
+
15
+ private
16
+ def add_new_roles_to_admin
17
+ group.roles.each do |role|
18
+ adminable.roles << role unless adminable.roles.include? role
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+ # Public: Modelo intermedio, permite que un Role pueda ser asignado a un 'Administrador',
4
+ # Este administrador puede ser cualquier modelo que incluya el modulo 'Burlesque::Admin'
5
+ #
6
+ module Burlesque
7
+ class AdminRole < ActiveRecord::Base
8
+ belongs_to :role
9
+ belongs_to :authorizable, polymorphic: true
10
+
11
+ validates_uniqueness_of :role_id, scope: [:authorizable_id, :authorizable_type], message: I18n.t('errors.messages.role_taken')
12
+ end
13
+ end
@@ -0,0 +1,87 @@
1
+ # encoding: utf-8
2
+
3
+ # Public: Modelo que maneja Grupos
4
+ #
5
+ module Burlesque
6
+ class Group < ActiveRecord::Base
7
+ has_many :role_groups
8
+ has_many :roles, through: :role_groups, dependent: :destroy, after_remove: :remove_roles_from_admin
9
+
10
+ has_many :admin_groups, dependent: :destroy
11
+ # for has_many :admins relations see admins function
12
+
13
+ attr_accessible :name, :role_ids
14
+
15
+ validates :name, presence: true, uniqueness: true
16
+
17
+ # Public: Relacion a muchos usuarios
18
+ #
19
+ # Se usa esta funcion dado que la tabla de administrador es polimorfica.
20
+ #
21
+ # Returns los administradores que tienen el rol en cuestion.
22
+ def admins
23
+ admin_groups.map &:adminable
24
+ end
25
+
26
+ # Public: Indica si el grupo tiene un rol en particular.
27
+ #
28
+ # role - el rol que se quiere consultar, puede ser un Role o Role.name
29
+ #
30
+ # Returns Boolean.
31
+ def role? role
32
+ role_name = role.respond_to?(:name) ? role.name : role
33
+ self.roles.map(&:name).include?(role_name.to_s)
34
+ end
35
+
36
+ # Public: Setea los roles que se indican al grupo.
37
+ # Eliminando los roles que no esten en la lista.
38
+ #
39
+ # ids - id's de los Roles que se desean asignar destructivamente.
40
+ #
41
+ # Returns nothing.
42
+ def role_ids=(ids)
43
+ ids.each do |ri|
44
+ if ri.presence
45
+ role = ::Role.find(ri)
46
+ self.roles << role unless self.roles.include? role
47
+ end
48
+ end
49
+
50
+ to_deletes = []
51
+ role_groups.each do |rg|
52
+ role = rg.role
53
+ to_deletes << role unless ids.include?(role.id.to_s) or ids.include?(role.id)
54
+ end
55
+
56
+ to_deletes.each do |role|
57
+ self.roles.delete(role) if self.roles.include?(role)
58
+ end
59
+ end
60
+
61
+ # Public: Permite agregar un grupo de Roles al Grupo
62
+ # No elimina los roles que no esten en la lista, solo agrega los que no estan.
63
+ #
64
+ # new_roles - el arreglo de roles que se quiere agregar al grupo
65
+ #
66
+ # Returns nothing.
67
+ def push_roles new_roles
68
+ new_roles.each do |role|
69
+ self.roles << role unless self.roles.include? role
70
+ end
71
+ end
72
+
73
+ private
74
+ # Public: Permite que al eliminar un rol de un grupo, tambien se modifiquen los roles de los administradores asociados a ese grupo.
75
+ #
76
+ # role - el rol que se elimino del grupo.
77
+ #
78
+ # Returns nothing.
79
+ def remove_roles_from_admin role
80
+ admins.each do |admin|
81
+ if admin.admin_roles.map(&:role_id).include?(role.id) and not admin.role_in_groups?(role.name)
82
+ admin.admin_roles.find_by_role_id(role.id).destroy
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,92 @@
1
+ # encoding: utf-8
2
+
3
+ # Public: Modulo que maneja Roles.
4
+ # Un Role es un permiso que permite a un usuario a realizar una determinada acción sobre un modelo
5
+ #
6
+ module Burlesque
7
+ class Role < ActiveRecord::Base
8
+ attr_accessible :name
9
+
10
+ has_many :role_groups
11
+ has_many :groups, through: :role_groups, dependent: :destroy
12
+
13
+ has_many :admin_roles, dependent: :destroy
14
+ # for has_many :admins relations see admins function
15
+
16
+ attr_accessible :name
17
+ validates :name, presence: true, uniqueness: true
18
+
19
+ SPLITER = '#'
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}%") }
25
+
26
+ # Public: Relacion a muchos usuarios
27
+ #
28
+ # Se usa esta funcion dado que la tabla de administrador es polimorfica.
29
+ #
30
+ # Returns los administradores que tienen el rol en cuestion.
31
+ def admins
32
+ admin_roles.map &:authorizable
33
+ end
34
+
35
+ # Public: Traduce el nombre de un rol.
36
+ #
37
+ # Primero revisa si existe una traducción para el rol bajo el scope de autorizaciones,
38
+ # luego si el no existe una traducción intenta traducir el nombre del rol usando la
39
+ # traduccion de Burleque, que traduce la acción por defecto e intenta usar las traducciones
40
+ # definidas para cada modelo
41
+ #
42
+ # Returns el nombre del rol ya traducido.
43
+ def translate_name
44
+ translate = I18n.t(name.to_sym, scope: :authorizations)
45
+ return translate unless translate.include?('translation missing:')
46
+
47
+ translate = I18n.t(action_sym, scope: :authorizations) + ' ' + resource_class.model_name.human
48
+ end
49
+
50
+ # Public: Entrega el recurso asociado al rol.
51
+ #
52
+ # Returns Constant.
53
+ def resource_class
54
+ name.split('#', 2).first.pluralize.classify.constantize
55
+ end
56
+
57
+ # Public: Entrega la accion asociada al rol.
58
+ #
59
+ # Returns Symbol.
60
+ def action_sym
61
+ name.split('#', 2).last.to_sym
62
+ end
63
+
64
+ # Public: Entrega la accion y modelo asociado al rol.
65
+ #
66
+ # Returns Symbol.
67
+ def get_action_and_model
68
+ [action_sym, resource_class]
69
+ end
70
+
71
+ # TODO
72
+ # Mejorar el retorno, para saber que paso, ej: si se agregaron los 5 roles o si ya existen
73
+ def self.for model
74
+ if model.class == String
75
+ resource = model.classify.constantize.model_name.underscore
76
+ elsif model.class == Symbol
77
+ resource = model.to_s.classify.constantize.model_name.underscore
78
+ elsif model.class == Class
79
+ resource = model.model_name.underscore
80
+ end
81
+
82
+ if resource
83
+ self.create(name: "#{resource}#{SPLITER}read") unless self.where(name: "#{resource}#{SPLITER}read" ).any?
84
+ self.create(name: "#{resource}#{SPLITER}create") unless self.where(name: "#{resource}#{SPLITER}create" ).any?
85
+ self.create(name: "#{resource}#{SPLITER}update") unless self.where(name: "#{resource}#{SPLITER}update" ).any?
86
+ self.create(name: "#{resource}#{SPLITER}destroy") unless self.where(name: "#{resource}#{SPLITER}destroy").any?
87
+ else
88
+ raise I18n.t('errors.messages.invalid_param', param: model.class)
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ # Public: Modulo intermedio, permite que un Role pueda ser asignado a un Grupo
4
+ #
5
+ module Burlesque
6
+ class RoleGroup < ActiveRecord::Base
7
+ belongs_to :role
8
+ belongs_to :group
9
+
10
+ validates_uniqueness_of :role_id, scope: :group_id, message: I18n.t('errors.messages.role_taken')
11
+
12
+ after_save :add_new_roles_to_admin
13
+
14
+ private
15
+ # Public: Actualiza los roles de los administradores luego de haber actualizado el grupo de roles.
16
+ #
17
+ # Returns nothing.
18
+ def add_new_roles_to_admin
19
+ group.admins.each do |admin|
20
+ admin.roles << self.role unless admin.roles.include?(self.role)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Burlesque</title>
5
+ <%= stylesheet_link_tag "burlesque/application", :media => "all" %>
6
+ <%= javascript_include_tag "burlesque/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,2 @@
1
+ Burlesque::Engine.routes.draw do
2
+ end
@@ -0,0 +1,9 @@
1
+ class CreateBurlesqueGroups < ActiveRecord::Migration
2
+ def change
3
+ create_table :burlesque_groups do |t|
4
+ t.string :name
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class CreateBurlesqueRoles < ActiveRecord::Migration
2
+ def change
3
+ create_table :burlesque_roles do |t|
4
+ t.string :name
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ class CreateBurlesqueAdminGroups < ActiveRecord::Migration
2
+ def change
3
+ create_table :burlesque_admin_groups do |t|
4
+ t.references :group
5
+
6
+ t.integer :adminable_id
7
+ t.string :adminable_type
8
+
9
+ t.timestamps
10
+ end
11
+
12
+ add_index :burlesque_admin_groups, :group_id
13
+ add_index :burlesque_admin_groups, [:adminable_id, :adminable_type], name: 'by_adminable'
14
+ end
15
+ end