third-prestige-rolify 3.3.0.rc5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +17 -0
  4. data/CHANGELOG.rdoc +157 -0
  5. data/Gemfile +21 -0
  6. data/LICENSE +20 -0
  7. data/README.md +220 -0
  8. data/Rakefile +34 -0
  9. data/UPGRADE.rdoc +44 -0
  10. data/gemfiles/Gemfile.rails-3.2 +21 -0
  11. data/gemfiles/Gemfile.rails-4.0 +27 -0
  12. data/lib/generators/active_record/rolify_generator.rb +50 -0
  13. data/lib/generators/active_record/templates/README +8 -0
  14. data/lib/generators/active_record/templates/migration.rb +19 -0
  15. data/lib/generators/mongoid/rolify_generator.rb +51 -0
  16. data/lib/generators/mongoid/templates/README-mongoid +4 -0
  17. data/lib/generators/rolify/rolify_generator.rb +35 -0
  18. data/lib/generators/rolify/templates/README +13 -0
  19. data/lib/generators/rolify/templates/initializer.rb +8 -0
  20. data/lib/generators/rolify/templates/role-active_record.rb +11 -0
  21. data/lib/generators/rolify/templates/role-mongoid.rb +17 -0
  22. data/lib/generators/rolify/user_generator.rb +39 -0
  23. data/lib/rolify.rb +57 -0
  24. data/lib/rolify/adapters/active_record/resource_adapter.rb +26 -0
  25. data/lib/rolify/adapters/active_record/role_adapter.rb +86 -0
  26. data/lib/rolify/adapters/active_record/scopes.rb +27 -0
  27. data/lib/rolify/adapters/base.rb +60 -0
  28. data/lib/rolify/adapters/mongoid/resource_adapter.rb +27 -0
  29. data/lib/rolify/adapters/mongoid/role_adapter.rb +89 -0
  30. data/lib/rolify/adapters/mongoid/scopes.rb +27 -0
  31. data/lib/rolify/configure.rb +56 -0
  32. data/lib/rolify/dynamic.rb +21 -0
  33. data/lib/rolify/finders.rb +40 -0
  34. data/lib/rolify/matchers.rb +13 -0
  35. data/lib/rolify/railtie.rb +20 -0
  36. data/lib/rolify/resource.rb +31 -0
  37. data/lib/rolify/role.rb +85 -0
  38. data/lib/rolify/utils.rb +10 -0
  39. data/lib/rolify/version.rb +3 -0
  40. data/rolify.gemspec +30 -0
  41. data/spec/README.rdoc +24 -0
  42. data/spec/generators/rolify/rolify_activerecord_generator_spec.rb +163 -0
  43. data/spec/generators/rolify/rolify_mongoid_generator_spec.rb +112 -0
  44. data/spec/generators_helper.rb +21 -0
  45. data/spec/rolify/config_spec.rb +191 -0
  46. data/spec/rolify/custom_spec.rb +20 -0
  47. data/spec/rolify/matchers_spec.rb +24 -0
  48. data/spec/rolify/namespace_spec.rb +24 -0
  49. data/spec/rolify/resource_spec.rb +389 -0
  50. data/spec/rolify/resourcifed_and_rolifed_spec.rb +24 -0
  51. data/spec/rolify/role_spec.rb +20 -0
  52. data/spec/rolify/shared_contexts.rb +92 -0
  53. data/spec/rolify/shared_examples/shared_examples_for_add_role.rb +92 -0
  54. data/spec/rolify/shared_examples/shared_examples_for_callbacks.rb +65 -0
  55. data/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +151 -0
  56. data/spec/rolify/shared_examples/shared_examples_for_finders.rb +77 -0
  57. data/spec/rolify/shared_examples/shared_examples_for_has_all_roles.rb +71 -0
  58. data/spec/rolify/shared_examples/shared_examples_for_has_any_role.rb +71 -0
  59. data/spec/rolify/shared_examples/shared_examples_for_has_role.rb +135 -0
  60. data/spec/rolify/shared_examples/shared_examples_for_only_has_role.rb +174 -0
  61. data/spec/rolify/shared_examples/shared_examples_for_remove_role.rb +121 -0
  62. data/spec/rolify/shared_examples/shared_examples_for_roles.rb +102 -0
  63. data/spec/rolify/shared_examples/shared_examples_for_scopes.rb +38 -0
  64. data/spec/spec_helper.rb +30 -0
  65. data/spec/support/adapters/active_record.rb +76 -0
  66. data/spec/support/adapters/mongoid.rb +143 -0
  67. data/spec/support/adapters/mongoid.yml +6 -0
  68. data/spec/support/data.rb +25 -0
  69. data/spec/support/schema.rb +52 -0
  70. metadata +254 -0
@@ -0,0 +1,34 @@
1
+ require 'bundler'
2
+ require 'rspec/core/rake_task'
3
+ require 'coveralls/rake/task'
4
+
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ Coveralls::RakeTask.new
8
+
9
+ RSpec::Core::RakeTask.new(:generators) do |task|
10
+ task.pattern = "spec/generators/**/*_spec.rb"
11
+ end
12
+
13
+ RSpec::Core::RakeTask.new(:rolify) do |task|
14
+ task.pattern = "spec/rolify/**/*_spec.rb"
15
+ end
16
+
17
+ task :default => [ :spec, 'coveralls:push' ]
18
+
19
+ desc "Run all specs"
20
+ task "spec" do
21
+ Rake::Task['generators'].invoke
22
+ return_code1 = $?.exitstatus
23
+ Rake::Task['rolify'].invoke
24
+ return_code2 = $?.exitstatus
25
+ fail if return_code1 != 0 || return_code2 != 0
26
+ end
27
+
28
+ desc "Run specs for all adapters"
29
+ task :spec_all do
30
+ %w[active_record mongoid].each do |model_adapter|
31
+ puts "ADAPTER = #{model_adapter}"
32
+ system "ADAPTER=#{model_adapter} rake"
33
+ end
34
+ end
@@ -0,0 +1,44 @@
1
+ = Upgrade instructions for Rolify library
2
+
3
+ == From a previous rolify installation (1.x or 2.x)
4
+
5
+ The easiest way is to re-run the generator using <tt>rails g Rolify:Role</tt> command, and overwrite the initializer file. Both the migration file and the <tt>role.rb</tt> haven't changed.
6
+ In the <tt>config/initializers/rolify.rb</tt> file, here are the deprecated settings:
7
+ * <tt>role_cname</tt>
8
+ * <tt>user_cname</tt>
9
+ By default, these settings were commented out, so you can safely remove them.
10
+ If you changed them, you can remove them in the initializer file too. <tt>user_cname</tt> is no longer used in 3.x, you don't need it anymore.
11
+ Add your custom <tt>role_cname</tt> setting as argument of the rolify method you use in your User class.
12
+ For instance, if you use <tt>Privilege</tt> as <tt>Role</tt> class, you should add this to your whatever your <tt>User</tt> class is, let's say <tt>Client</tt> for the sake of example:
13
+ class Client < ActiveRecord::Base
14
+ rolify :role_cname => "Privilege"
15
+ end
16
+ If you use the default <tt>Role</tt> class name, you don't have to specify the <tt>:role_cname</tt> argument.
17
+
18
+ If you use <i>dynamic methods</i> (<tt>user.is_admin?</tt> like methods), you should turn it on using <tt>use_dynamic_shortcuts</tt> method starting from rolify 3.0:
19
+ Rolify.configure do |c|
20
+ c.use_dynamic_shortcuts
21
+ end
22
+ The old fashion way still works though, but won't work anymore if the setter method name is changed in a possible future. You've been warned :-)
23
+
24
+ == From a rolify installation 3.x
25
+
26
+ === Dependencies:
27
+
28
+ Starting from 3.3 release, rolify supports Rails 3.2 and newer.
29
+
30
+ Mongoid callbacks are supported if Mongoid 3.1 and newer is installed.
31
+
32
+ === Rails Generators
33
+
34
+ Role model template when using Mongoid has been changed, you should re-run the generator.
35
+
36
+ Rails generator has been renamed to: <tt>rails g rolify</tt> and arguments have been changed:
37
+ * Role class name is now mandatory, User class name remains optional and its default is still <tt>User</tt>
38
+ * ORM optional argument is now <tt>-o</tt> or <tt>--orm=</tt>
39
+ For instance, here is a new rolify generator command example: <tt>rails g rolify Role</tt>
40
+ You can use <tt>rails g rolify --help</tt> to see all available options.
41
+
42
+ === Testing
43
+
44
+ Starting from rolify 3.3, to run the specs you should run: <tt>rake spec</tt> or simply <tt>rake</tt>.
@@ -0,0 +1,21 @@
1
+ source "https://rubygems.org"
2
+
3
+ case ENV["ADAPTER"]
4
+ when nil, "active_record"
5
+ group :test do
6
+ gem "activerecord-jdbcsqlite3-adapter", ">= 1.3.0.rc", :platform => "jruby"
7
+ gem "sqlite3", :platform => "ruby"
8
+ end
9
+ gem "activerecord", "~> 3.2.0", :require => "active_record"
10
+ when "mongoid"
11
+ gem "mongoid", "~> 3.1"
12
+ gem "bson_ext", :platform => "ruby"
13
+ else
14
+ raise "Unknown model adapter: #{ENV["ADAPTER"]}"
15
+ end
16
+
17
+ group :test do
18
+ gem 'coveralls', :require => false
19
+ end
20
+
21
+ gemspec :path => '../'
@@ -0,0 +1,27 @@
1
+ source "https://rubygems.org"
2
+
3
+ case ENV["ADAPTER"]
4
+ when nil, "active_record"
5
+ gem "activerecord", "~> 4.0.0.rc", :require => "active_record"
6
+ when "mongoid"
7
+ gem "mongoid", :git => "git://github.com/mongoid/mongoid.git"
8
+ gem "bson_ext", :platform => "ruby"
9
+ else
10
+ raise "Unknown model adapter: #{ENV["ADAPTER"]}"
11
+ end
12
+
13
+ group :test do
14
+ gem "rails", "~> 4.0.0.rc"
15
+ gem "ammeter"
16
+ gem "rake"
17
+ gem "rspec"
18
+ gem "rspec-rails"
19
+ gem "fuubar"
20
+ gem "bundler"
21
+
22
+ gem "activerecord-jdbcsqlite3-adapter", "~> 1.3.0.beta", :platform => "jruby"
23
+ gem "sqlite3", :platform => "ruby"
24
+ gem "activerecord", "~> 4.0.0.rc", :require => "active_record"
25
+ gem "mongoid", :git => "git://github.com/mongoid/mongoid.git"
26
+ gem 'coveralls', :require => false
27
+ end
@@ -0,0 +1,50 @@
1
+ require 'rails/generators/active_record'
2
+ require 'active_support/core_ext'
3
+
4
+ module ActiveRecord
5
+ module Generators
6
+ class RolifyGenerator < ActiveRecord::Generators::Base
7
+ source_root File.expand_path("../templates", __FILE__)
8
+
9
+ argument :user_cname, :type => :string, :default => "User", :banner => "User"
10
+
11
+ def generate_model
12
+ invoke "active_record:model", [ name ], :migration => false
13
+ end
14
+
15
+ def inject_role_class
16
+ inject_into_class(model_path, class_name, model_content)
17
+ end
18
+
19
+ def copy_rolify_migration
20
+ migration_template "migration.rb", "db/migrate/rolify_create_#{table_name}"
21
+ end
22
+
23
+ def join_table
24
+ user_cname.constantize.table_name + "_" + table_name
25
+ end
26
+
27
+ def user_reference
28
+ user_cname.demodulize.underscore
29
+ end
30
+
31
+ def role_reference
32
+ class_name.demodulize.underscore
33
+ end
34
+
35
+ def model_path
36
+ File.join("app", "models", "#{file_path}.rb")
37
+ end
38
+
39
+ def model_content
40
+ content = <<RUBY
41
+ has_and_belongs_to_many :%{user_cname}, :join_table => :%{join_table}
42
+ belongs_to :resource, :polymorphic => true
43
+
44
+ scopify
45
+ RUBY
46
+ content % { :user_cname => user_cname.constantize.table_name, :join_table => "#{user_cname.constantize.table_name}_#{table_name}"}
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,8 @@
1
+ Now, you just have to run the migration using rake command:
2
+
3
+ rake db:migrate
4
+
5
+ and you will be able to add the resourcify method inside all models you want
6
+ scoped by a role.
7
+
8
+ ===============================================================================
@@ -0,0 +1,19 @@
1
+ class RolifyCreate<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ create_table(:<%= table_name %>) do |t|
4
+ t.string :name
5
+ t.references :resource, :polymorphic => true
6
+
7
+ t.timestamps
8
+ end
9
+
10
+ create_table(:<%= join_table %>, :id => false) do |t|
11
+ t.references :<%= user_reference %>
12
+ t.references :<%= role_reference %>
13
+ end
14
+
15
+ add_index(:<%= table_name %>, :name)
16
+ add_index(:<%= table_name %>, [ :name, :resource_type, :resource_id ])
17
+ add_index(:<%= join_table %>, [ :<%= user_reference %>_id, :<%= role_reference %>_id ])
18
+ end
19
+ end
@@ -0,0 +1,51 @@
1
+ require 'rails/generators/mongoid_generator'
2
+ require 'active_support/core_ext'
3
+
4
+ module Mongoid
5
+ module Generators
6
+ class RolifyGenerator < Rails::Generators::NamedBase
7
+ source_root File.expand_path("../templates", __FILE__)
8
+
9
+ argument :user_cname, :type => :string, :default => "User", :banner => "User"
10
+
11
+ def generate_model
12
+ invoke "mongoid:model", [ name ]
13
+ end
14
+
15
+ def inject_role_class
16
+ inject_into_file(model_path, model_contents, :after => "include Mongoid::Document\n")
17
+ end
18
+
19
+ def user_reference
20
+ user_cname.demodulize.underscore
21
+ end
22
+
23
+ def role_reference
24
+ class_name.demodulize.underscore
25
+ end
26
+
27
+ def model_path
28
+ File.join("app", "models", "#{file_path}.rb")
29
+ end
30
+
31
+ def model_contents
32
+ content = <<RUBY
33
+ has_and_belongs_to_many :%{user_cname}
34
+ belongs_to :resource, :polymorphic => true
35
+
36
+ field :name, :type => String
37
+
38
+ index({
39
+ :name => 1,
40
+ :resource_type => 1,
41
+ :resource_id => 1
42
+ },
43
+ { :unique => true})
44
+
45
+ scopify
46
+ RUBY
47
+ content % { :user_cname => user_cname.constantize.collection_name }
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,4 @@
1
+ Now, you are able to add the resourcify method inside all models you want
2
+ scoped by a role.
3
+
4
+ ===============================================================================
@@ -0,0 +1,35 @@
1
+ module Rolify
2
+ module Generators
3
+ class RolifyGenerator < Rails::Generators::NamedBase
4
+ Rails::Generators::ResourceHelpers
5
+
6
+ source_root File.expand_path('../templates', __FILE__)
7
+ argument :user_cname, :type => :string, :default => "User"
8
+
9
+ namespace :rolify
10
+ hook_for :orm, :required => true
11
+
12
+ desc "Generates a model with the given NAME and a migration file."
13
+
14
+ def self.start(args, config)
15
+ user_cname = args.size > 1 ? args[1] : "User"
16
+ args.insert(1, user_cname) # 0 being the view name
17
+ super
18
+ end
19
+
20
+ def inject_user_class
21
+ invoke "rolify:user", [ user_cname, class_name ], :orm => options.orm
22
+ end
23
+
24
+ def copy_initializer_file
25
+ template "initializer.rb", "config/initializers/rolify.rb"
26
+ end
27
+
28
+ def show_readme
29
+ if behavior == :invoke
30
+ readme "README"
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ ===============================================================================
2
+
3
+ An initializer file has been created here: config/initializers/rolify.rb, you
4
+ can change rolify settings to match your needs.
5
+ Defaults values are commented out.
6
+
7
+ A Role class has been created in app/models (with the name you gave as
8
+ argument otherwise the default is role.rb), you can add your own business logic
9
+ inside.
10
+
11
+ Inside your User class (or the name you gave as argument otherwise the default
12
+ is user.rb), rolify method has been inserted to provide rolify methods.
13
+
@@ -0,0 +1,8 @@
1
+ Rolify.configure<%= "(\"#{class_name.camelize.to_s}\")" if class_name != "Role" %> do |config|
2
+ # By default ORM adapter is ActiveRecord. uncomment to use mongoid
3
+ <%= "# " if options.orm == :active_record || !options.orm %>config.use_mongoid
4
+
5
+ # Dynamic shortcuts for User class (user.is_admin? like methods). Default is: false
6
+ # Enable this feature _after_ running rake db:migrate as it relies on the roles table
7
+ # config.use_dynamic_shortcuts
8
+ end
@@ -0,0 +1,11 @@
1
+ class <%= role_cname.camelize %> < ActiveRecord::Base
2
+ <% if need_table_prefix?(role_cname) %>
3
+ def self.table_name_prefix
4
+ <%= table_prefix(role_cname) %>_
5
+ end
6
+ <% end %>
7
+ has_and_belongs_to_many :<%= user_cname.tableize %>, :join_table => :<%= "#{table_name(user_cname, true)}_#{table_name(role_cname, true)}" %>
8
+ belongs_to :resource, :polymorphic => true
9
+
10
+ scopify
11
+ end
@@ -0,0 +1,17 @@
1
+ class <%= role_cname.camelize %>
2
+ include Mongoid::Document
3
+
4
+ has_and_belongs_to_many :<%= user_cname.tableize %>
5
+ belongs_to :resource, :polymorphic => true
6
+
7
+ field :name, :type => String
8
+
9
+ index({
10
+ :name => 1,
11
+ :resource_type => 1,
12
+ :resource_id => 1
13
+ },
14
+ { :unique => true})
15
+
16
+ scopify
17
+ end
@@ -0,0 +1,39 @@
1
+ require 'rails/generators/migration'
2
+ require 'active_support/core_ext'
3
+
4
+ module Rolify
5
+ module Generators
6
+ class UserGenerator < Rails::Generators::NamedBase
7
+ argument :role_cname, :type => :string, :default => "Role"
8
+ class_option :orm, :type => :string, :default => "active_record"
9
+
10
+ desc "Inject rolify method in the User class."
11
+
12
+ def inject_user_content
13
+ inject_into_file(model_path, :after => inject_rolify_method) do
14
+ " rolify#{role_association}\n"
15
+ end
16
+ end
17
+
18
+ def inject_rolify_method
19
+ if options.orm == :active_record
20
+ /class #{class_name.camelize}\n|class #{class_name.camelize} .*\n|class #{class_name.demodulize.camelize}\n|class #{class_name.demodulize.camelize} .*\n/
21
+ else
22
+ /include Mongoid::Document\n|include Mongoid::Document .*\n/
23
+ end
24
+ end
25
+
26
+ def model_path
27
+ File.join("app", "models", "#{file_path}.rb")
28
+ end
29
+
30
+ def role_association
31
+ if role_cname != "Role"
32
+ " :role_cname => '#{role_cname.camelize}'"
33
+ else
34
+ ""
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,57 @@
1
+ require 'rolify/railtie' if defined?(Rails)
2
+ require 'rolify/utils'
3
+ require 'rolify/role'
4
+ require 'rolify/configure'
5
+ require 'rolify/dynamic'
6
+ require 'rolify/resource'
7
+ require 'rolify/adapters/base'
8
+
9
+ module Rolify
10
+ extend Configure
11
+
12
+ attr_accessor :role_cname, :adapter, :role_join_table_name, :role_table_name
13
+
14
+ def rolify(options = {})
15
+ include Role
16
+ extend Dynamic if Rolify.dynamic_shortcuts
17
+
18
+ options.reverse_merge!({:role_cname => 'Role'})
19
+ self.role_cname = options[:role_cname]
20
+ self.role_table_name = self.role_cname.tableize.gsub(/\//, "_")
21
+
22
+ default_join_table = "#{self.to_s.tableize.gsub(/\//, "_")}_#{self.role_table_name}"
23
+ options.reverse_merge!({:role_join_table_name => default_join_table})
24
+ self.role_join_table_name = options[:role_join_table_name]
25
+
26
+ rolify_options = { :class_name => options[:role_cname].camelize }
27
+ rolify_options.merge!({ :join_table => self.role_join_table_name }) if Rolify.orm == "active_record"
28
+ rolify_options.merge!(options.reject{ |k,v| ![ :before_add, :after_add, :before_remove, :after_remove ].include? k.to_sym })
29
+
30
+ has_and_belongs_to_many :roles, rolify_options
31
+
32
+ self.adapter = Rolify::Adapter::Base.create("role_adapter", self.role_cname, self.name)
33
+ load_dynamic_methods if Rolify.dynamic_shortcuts
34
+ end
35
+
36
+ def resourcify(association_name = :roles, options = {})
37
+ include Resource
38
+
39
+ options.reverse_merge!({ :role_cname => 'Role', :dependent => :destroy })
40
+ resourcify_options = { :class_name => options[:role_cname].camelize, :as => :resource, :dependent => options[:dependent] }
41
+ self.role_cname = options[:role_cname]
42
+ self.role_table_name = self.role_cname.tableize.gsub(/\//, "_")
43
+
44
+ has_many association_name, resourcify_options
45
+
46
+ self.adapter = Rolify::Adapter::Base.create("resource_adapter", self.role_cname, self.name)
47
+ end
48
+
49
+ def scopify
50
+ require "rolify/adapters/#{Rolify.orm}/scopes.rb"
51
+ extend Rolify::Adapter::Scopes
52
+ end
53
+
54
+ def role_class
55
+ self.role_cname.constantize
56
+ end
57
+ end