simple_roles 0.0.5

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 (123) hide show
  1. data/.rspec +1 -0
  2. data/Gemfile +22 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +81 -0
  5. data/Rakefile +53 -0
  6. data/VERSION +1 -0
  7. data/app/assets/images/simple_roles/.gitkeep +0 -0
  8. data/app/assets/javascripts/simple_roles/.gitkeep +0 -0
  9. data/app/assets/stylesheets/simple_roles/.gitkeep +0 -0
  10. data/app/controllers/.gitkeep +0 -0
  11. data/app/helpers/.gitkeep +0 -0
  12. data/app/mailers/.gitkeep +0 -0
  13. data/app/models/.gitkeep +0 -0
  14. data/app/models/role.rb +4 -0
  15. data/app/models/user_role.rb +4 -0
  16. data/app/views/.gitkeep +0 -0
  17. data/config/routes.rb +2 -0
  18. data/db/migrate/001_create_user_roles.rb +15 -0
  19. data/db/migrate/002_create_roles.rb +22 -0
  20. data/lib/simple_roles/base.rb +111 -0
  21. data/lib/simple_roles/configuration.rb +33 -0
  22. data/lib/simple_roles/engine.rb +4 -0
  23. data/lib/simple_roles/macros.rb +13 -0
  24. data/lib/simple_roles/roles_array.rb +63 -0
  25. data/lib/simple_roles/version.rb +3 -0
  26. data/lib/simple_roles.rb +19 -0
  27. data/lib/tasks/simple_roles_tasks.rake +4 -0
  28. data/script/rails +6 -0
  29. data/simple_roles.gemspec +196 -0
  30. data/spec/dummy/Rakefile +7 -0
  31. data/spec/dummy/app/assets/javascripts/application.js +9 -0
  32. data/spec/dummy/app/assets/javascripts/default.js +2 -0
  33. data/spec/dummy/app/assets/javascripts/jquery.js +16 -0
  34. data/spec/dummy/app/assets/javascripts/jquery_ujs.js +169 -0
  35. data/spec/dummy/app/assets/javascripts/posts.js +2 -0
  36. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  37. data/spec/dummy/app/assets/stylesheets/default.css +4 -0
  38. data/spec/dummy/app/assets/stylesheets/posts.css +4 -0
  39. data/spec/dummy/app/assets/stylesheets/scaffold.css +56 -0
  40. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  41. data/spec/dummy/app/controllers/default_controller.rb +5 -0
  42. data/spec/dummy/app/controllers/posts_controller.rb +83 -0
  43. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  44. data/spec/dummy/app/helpers/default_helper.rb +2 -0
  45. data/spec/dummy/app/helpers/posts_helper.rb +2 -0
  46. data/spec/dummy/app/mailers/.gitkeep +0 -0
  47. data/spec/dummy/app/models/.gitkeep +0 -0
  48. data/spec/dummy/app/models/post.rb +2 -0
  49. data/spec/dummy/app/models/user.rb +15 -0
  50. data/spec/dummy/app/views/default/index.html.erb +4 -0
  51. data/spec/dummy/app/views/devise/confirmations/new.html.erb +12 -0
  52. data/spec/dummy/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  53. data/spec/dummy/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  54. data/spec/dummy/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  55. data/spec/dummy/app/views/devise/passwords/edit.html.erb +16 -0
  56. data/spec/dummy/app/views/devise/passwords/new.html.erb +12 -0
  57. data/spec/dummy/app/views/devise/registrations/edit.html.erb +25 -0
  58. data/spec/dummy/app/views/devise/registrations/new.html.erb +18 -0
  59. data/spec/dummy/app/views/devise/sessions/new.html.erb +17 -0
  60. data/spec/dummy/app/views/devise/shared/_links.erb +25 -0
  61. data/spec/dummy/app/views/devise/unlocks/new.html.erb +12 -0
  62. data/spec/dummy/app/views/layouts/application.html.erb +15 -0
  63. data/spec/dummy/app/views/posts/_form.html.erb +17 -0
  64. data/spec/dummy/app/views/posts/edit.html.erb +6 -0
  65. data/spec/dummy/app/views/posts/index.html.erb +21 -0
  66. data/spec/dummy/app/views/posts/new.html.erb +5 -0
  67. data/spec/dummy/app/views/posts/show.html.erb +5 -0
  68. data/spec/dummy/config/application.rb +51 -0
  69. data/spec/dummy/config/boot.rb +10 -0
  70. data/spec/dummy/config/database.yml +26 -0
  71. data/spec/dummy/config/database_mysql.yml +29 -0
  72. data/spec/dummy/config/database_pgsql.yml +35 -0
  73. data/spec/dummy/config/environment.rb +5 -0
  74. data/spec/dummy/config/environments/development.rb +24 -0
  75. data/spec/dummy/config/environments/production.rb +52 -0
  76. data/spec/dummy/config/environments/test.rb +39 -0
  77. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  78. data/spec/dummy/config/initializers/devise.rb +204 -0
  79. data/spec/dummy/config/initializers/inflections.rb +10 -0
  80. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  81. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  82. data/spec/dummy/config/initializers/session_store.rb +8 -0
  83. data/spec/dummy/config/initializers/simple_roles.rb +3 -0
  84. data/spec/dummy/config/initializers/wrap_parameters.rb +12 -0
  85. data/spec/dummy/config/locales/devise.en.yml +53 -0
  86. data/spec/dummy/config/locales/en.yml +5 -0
  87. data/spec/dummy/config/routes.rb +13 -0
  88. data/spec/dummy/config.ru +4 -0
  89. data/spec/dummy/db/migrate/010_devise_create_users.rb +29 -0
  90. data/spec/dummy/db/migrate/20110925210726_create_user_roles.rb +15 -0
  91. data/spec/dummy/db/migrate/20110925210727_create_roles.rb +22 -0
  92. data/spec/dummy/db/schema.rb +51 -0
  93. data/spec/dummy/db/seeds.rb +14 -0
  94. data/spec/dummy/log/.gitkeep +0 -0
  95. data/spec/dummy/public/404.html +26 -0
  96. data/spec/dummy/public/422.html +26 -0
  97. data/spec/dummy/public/500.html +26 -0
  98. data/spec/dummy/public/favicon.ico +0 -0
  99. data/spec/dummy/script/rails +6 -0
  100. data/spec/dummy/test/fixtures/posts.yml +11 -0
  101. data/spec/dummy/test/fixtures/users.yml +11 -0
  102. data/spec/dummy/test/functional/default_controller_test.rb +9 -0
  103. data/spec/dummy/test/functional/posts_controller_test.rb +49 -0
  104. data/spec/dummy/test/unit/helpers/default_helper_test.rb +4 -0
  105. data/spec/dummy/test/unit/helpers/posts_helper_test.rb +4 -0
  106. data/spec/dummy/test/unit/post_test.rb +7 -0
  107. data/spec/dummy/test/unit/user_test.rb +7 -0
  108. data/spec/dummy_spec_helper.rb +41 -0
  109. data/spec/integration/main_spec.rb +7 -0
  110. data/spec/integration/messages_spec.rb +62 -0
  111. data/spec/integration/requests/main_spec.rb +21 -0
  112. data/spec/simple_roles/base_spec.rb +191 -0
  113. data/spec/simple_roles/macros_spec.rb +25 -0
  114. data/spec/spec_helper.rb +52 -0
  115. data/spec/support/aliases.rb +0 -0
  116. data/spec/support/controller_macros.rb +26 -0
  117. data/spec/support/database.yml +6 -0
  118. data/spec/support/factories.rb +22 -0
  119. data/spec/support/fixtures/models/.gitkeep +0 -0
  120. data/spec/support/fixtures/models/user.rb +3 -0
  121. data/spec/support/migrations/010_create_users.rb +14 -0
  122. data/spec/support/rspec_helpers.rb +22 -0
  123. metadata +320 -0
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format nested
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in simple_roles.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+
7
+ gem 'require_all'
8
+ gem 'sugar-high'
9
+ gem 'sweetloader'
10
+
11
+ group :development, :test do
12
+ gem 'activerecord'
13
+ gem 'rake-kit'
14
+ gem 'devise'
15
+ gem 'mysql2'
16
+ gem 'jeweler'
17
+ gem 'cutter'
18
+ gem 'rails'
19
+ gem 'rspec-rails'
20
+ gem 'capybara'
21
+ gem 'factory_girl'
22
+ end
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 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 ADDED
@@ -0,0 +1,81 @@
1
+ # SimpleRoles
2
+
3
+ SimpleRoles is a Rails Engine providing simple Role System for any Rails 3 app. It was created as demo role-system to accompany [CanTango gem](https://github.com/kristianmandrup/cantango) initial installiation and usage. Intended to be very easy to setup & use.
4
+
5
+ It even seems to be good for being used as a real role system inspite of (or due to ;)) its almost maximum simplicity.
6
+
7
+ If you are looking for a real serious roles system solution try [Troles](https://github.com/kristianmandrup/troles) gem created by [Kristian Mandrup](https://github.com/kristianmandrup)
8
+
9
+ ## Installiation
10
+
11
+ ### Prerequisites
12
+
13
+ SimpleRoles only assumes you have User model
14
+
15
+ ### Not a Gem yet
16
+
17
+ include in Gemfile:
18
+
19
+ ```ruby
20
+ gem 'simple_roles', :git => "git://github.com/stanislaw/simple_roles.git"
21
+ bundle update
22
+ ```
23
+
24
+ ### Set up valid roles you're gonna have in your app
25
+
26
+ Create file simple_roles.rb in config/initializers and write there:
27
+
28
+ ```ruby
29
+ # config/initializers/simple_roles.rb
30
+ SimpleRoles.configure do |config|
31
+ config.valid_roles = [:user, :admin, :editor]
32
+ end
33
+ ```
34
+
35
+ ### Copy and migrate SimpleRoles migrations by following rake task:
36
+
37
+ ```ruby
38
+ rake simple_roles_engine:install:migrations
39
+ rake db:migrate
40
+ ```
41
+
42
+ Note! Migrations are based on roles you set up as valid (see previous step). If you do not create initializer with valid_roles, then valid_roles will be set up to defaults: :user and :admin.
43
+
44
+ ### And finally include 'simple_roles' macros in your User model:
45
+
46
+ ```ruby
47
+ class User
48
+ simple_roles
49
+ end
50
+ ```
51
+
52
+ ## Usage
53
+
54
+ ```ruby
55
+ user.roles => #<RolesArray: {}>
56
+
57
+ user.roles = :admin
58
+ user.roles # => #<RolesArray: {:admin}>
59
+ user.roles_list # => #<RolesArray: {:admin}>
60
+ user.admin? # => true
61
+ user.is_admin? # => true
62
+
63
+ user.roles << :user
64
+ user.roles # => #<RolesArray: {:admin, :user}>
65
+ user.is_user? # => true
66
+
67
+ user.add_role :editor
68
+ user.roles # => #<RolesArray: #{:admin, :user, :editor}>
69
+
70
+ user.remove_role :user
71
+ user.roles # => #<RolesArray: {:admin, :editor}>
72
+ user.has_role?(:admin) # => true
73
+ user.has_any_role?(:admin, :blip) # => true
74
+ user.has_role?(:blogger) # => false
75
+ ```
76
+
77
+ ## Todo:
78
+
79
+ - Write role groups part
80
+ - Provide some more config options
81
+ - More and better tests
data/Rakefile ADDED
@@ -0,0 +1,53 @@
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
+ require 'jeweler'
16
+ Jeweler::Tasks.new do |gem|
17
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
18
+ gem.name = "simple_roles"
19
+ gem.homepage = "http://github.com/stanislaw/simple_roles"
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{Rails Engine providing Role System for Rails apps}
22
+ gem.description = %Q{Simple Role System for Rails Apps}
23
+ gem.email = "s.pankevich@gmail.com"
24
+ gem.authors = ["stanislaw"]
25
+ # dependencies defined in Gemfile
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ RDoc::Task.new(:rdoc) do |rdoc|
30
+ rdoc.rdoc_dir = 'rdoc'
31
+ rdoc.title = 'SimpleRoles'
32
+ rdoc.options << '--line-numbers'
33
+ rdoc.rdoc_files.include('README.rdoc')
34
+ rdoc.rdoc_files.include('lib/**/*.rb')
35
+ end
36
+
37
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
38
+ load 'rails/tasks/engine.rake'
39
+
40
+
41
+ Bundler::GemHelper.install_tasks
42
+
43
+ require 'rake/testtask'
44
+
45
+ Rake::TestTask.new(:test) do |t|
46
+ t.libs << 'lib'
47
+ t.libs << 'test'
48
+ t.pattern = 'test/**/*_test.rb'
49
+ t.verbose = false
50
+ end
51
+
52
+
53
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.5
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,4 @@
1
+ class Role < ActiveRecord::Base
2
+ has_many :user_roles
3
+ has_many :users, :through => :user_roles
4
+ end
@@ -0,0 +1,4 @@
1
+ class UserRole < ActiveRecord::Base
2
+ belongs_to :user
3
+ belongs_to :role
4
+ end
File without changes
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,15 @@
1
+ class CreateUserRoles < ActiveRecord::Migration
2
+ def up
3
+ create_table :user_roles do |t|
4
+ t.integer :user_id
5
+ t.integer :role_id
6
+
7
+ t.timestamps
8
+ end
9
+ add_index :user_roles, [:user_id, :role_id]
10
+ end
11
+
12
+ def down
13
+ drop_table :user_roles
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ class CreateRoles < ActiveRecord::Migration
2
+ def up
3
+ create_table :roles do |t|
4
+ t.string :name
5
+ t.timestamps
6
+ end
7
+
8
+ create_roles
9
+ end
10
+
11
+ def down
12
+ drop_table :roles
13
+ end
14
+
15
+
16
+ def create_roles
17
+ SimpleRoles::Configuration.valid_roles.each do |role|
18
+ Role.create(:name => role.to_s)
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,111 @@
1
+ module SimpleRoles
2
+ module Base
3
+
4
+ class << self
5
+ def included base
6
+ base.class_eval %{
7
+ has_many :user_roles
8
+ has_many :db_roles, :through => :user_roles, :class_name => 'Role', :source => :role
9
+ }
10
+
11
+ base.send :include, SimpleRoles::Base::InstanceMethods
12
+ SimpleRoles::Configuration.user_models << base
13
+ base.register_roles_methods
14
+ end
15
+ end
16
+
17
+ module ClassMethods
18
+ def register_roles_methods
19
+ valid_roles.each do |role|
20
+ class_eval %{
21
+ def self.#{role}s
22
+ Role.find_by_name("#{role}").users
23
+ end
24
+
25
+ def self.#{role}s_ids
26
+ Role.find_by_name("#{role}").user_ids
27
+ end
28
+ }
29
+
30
+ define_method :"#{role}?" do
31
+ roles.include?(:"#{role}")
32
+ end
33
+
34
+ alias_method :"is_#{role}?", :"#{role}?"
35
+ end
36
+ end
37
+
38
+ def valid_roles
39
+ SimpleRoles::Configuration.valid_roles
40
+ end
41
+ end
42
+
43
+ module InstanceMethods
44
+
45
+ def self.included base
46
+ base.send :extend, ClassMethods
47
+ end
48
+
49
+ def mass_assignment_authorizer *args
50
+ super.empty? ? super : (super + [:roles])
51
+ end
52
+
53
+ def roles
54
+ roles_array
55
+ end
56
+
57
+ alias_method :roles_list, :roles
58
+
59
+ def has_roles? *rolez
60
+ rolez.flatten!
61
+ rolez.each do |role|
62
+ return false if !roles.include? role
63
+ end
64
+ true
65
+ end
66
+
67
+ alias_method :has_role?, :has_roles?
68
+
69
+ def has_any_role? *rolez
70
+ rolez.flatten!
71
+ rolez.each do |role|
72
+ return true if roles.include? role
73
+ end
74
+ false
75
+ end
76
+
77
+ def add_roles *rolez
78
+ roles_array.add *rolez
79
+ end
80
+
81
+ alias_method :add_role, :add_roles
82
+
83
+ def remove_roles *rolez
84
+ roles_array.remove *rolez
85
+ end
86
+
87
+ alias_method :remove_role, :remove_roles
88
+
89
+ def roles= *rolez
90
+ roles_array.roles = *rolez
91
+ end
92
+
93
+ # TODO: implement
94
+ #
95
+ def role_groups_list
96
+ []
97
+ end
98
+
99
+ private
100
+
101
+ def roles_array
102
+ @roles_array ||= SimpleRoles::RolesArray.new self
103
+ end
104
+
105
+ end
106
+
107
+ end
108
+ end
109
+
110
+
111
+
@@ -0,0 +1,33 @@
1
+ require 'singleton'
2
+ module SimpleRoles
3
+ module Configuration
4
+
5
+ extend self
6
+
7
+ attr_accessor :valid_roles, :user_models
8
+
9
+ def user_models
10
+ @user_models ||= []
11
+ end
12
+
13
+ def valid_roles= vr
14
+ raise "There should be an array of valid roles" if !vr.kind_of?(Array)
15
+ @valid_roles = vr
16
+ distribute_methods
17
+ end
18
+
19
+ def valid_roles
20
+ @valid_roles || default_roles
21
+ end
22
+
23
+ def default_roles
24
+ [:user, :admin]
25
+ end
26
+
27
+ def distribute_methods
28
+ user_models.each do |um|
29
+ um.register_roles_methods
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,4 @@
1
+ module SimpleRoles
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ module SimpleRoles
2
+ module Macros
3
+ def self.included(base)
4
+ end
5
+
6
+ def simple_roles &block
7
+ yield SimpleRoles::Configuration if block
8
+ include SimpleRoles::Base
9
+ end
10
+ end
11
+ end
12
+
13
+ Module.send :include, SimpleRoles::Macros
@@ -0,0 +1,63 @@
1
+ module SimpleRoles
2
+ class RolesArray < Array
3
+
4
+ attr_reader :base
5
+
6
+ def initialize *args
7
+ @base = args.delete(args.first)
8
+ super
9
+ synchronize
10
+ end
11
+
12
+ def synchronize
13
+ replace real_roles
14
+ end
15
+
16
+ def real_roles_db
17
+ base.db_roles
18
+ end
19
+
20
+ def real_roles
21
+ real_roles_db.map(&:name).map(&:to_sym)
22
+ end
23
+
24
+ def roles= *rolez
25
+ rolez.to_symbols!.flatten!
26
+ raise "Not a valid role!" if (rolez.to_a - SimpleRoles::Configuration.valid_roles).size > 0
27
+
28
+ base.db_roles = rolez.map do |rolle|
29
+ begin
30
+ Role.find_by_name!(rolle.to_s)
31
+ rescue
32
+ raise "Couldn't find Role for #{rolle}. Maybe you need to re-run migrations?"
33
+ end
34
+ end
35
+
36
+ synchronize
37
+ end
38
+
39
+ def << *rolez
40
+ rolez.flatten!
41
+ self.roles = self.to_a + rolez
42
+ end
43
+
44
+ alias_method :add, :<<
45
+
46
+ def remove *rolez
47
+ rolez.flatten!
48
+ self.roles = self.to_a - rolez
49
+ end
50
+
51
+ def clear!
52
+ real_roles_db.clear
53
+ base.save!
54
+ clear
55
+ end
56
+
57
+ private
58
+
59
+ def clear
60
+ super
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleRoles
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ require "sweetloader"
2
+ require "require_all"
3
+ require "sugar-high/array"
4
+ require "sugar-high/dsl"
5
+
6
+ require "simple_roles/engine" if defined?(Rails)
7
+ require "simple_roles/macros"
8
+
9
+ require_all File.expand_path("../../app", __FILE__)
10
+
11
+ module SimpleRoles
12
+ autoload_modules :Base, :Configuration, :RolesArray
13
+
14
+ class << self
15
+ def configure &block
16
+ yield SimpleRoles::Configuration
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :simple_roles do
3
+ # # Task goes here
4
+ # end
data/script/rails ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ #!/usr/bin/env ruby
3
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
4
+
5
+ ENGINE_PATH = File.expand_path('../..', __FILE__)
6
+ load File.expand_path('../../spec/dummy/script/rails', __FILE__)