role_me 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
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.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ = RoleMe
2
+
3
+ Adds simple role handling to model.
4
+
5
+ == How to use
6
+
7
+ Supose you have an User model:
8
+
9
+ class User < ActiveRecord::Base
10
+ include RoleMe::HasRoles
11
+ has_roles
12
+ end
13
+
14
+ Then, you have:
15
+
16
+ user = User.first
17
+ user.has_role! :admin # adds the admin role to the model
18
+ user.has_role? :admin # checks if the model has the admin role
19
+
20
+ == Installation
21
+
22
+ Add to your `Gemfile`:
23
+
24
+ gem 'role_me'
25
+
26
+ And use the role_me:role_migration generator to create the role migration, if you are using the gem's role model.
27
+
28
+ rails generator role_me:role_migration
29
+
30
+
31
+ == Custom Role Model
32
+
33
+ If you want to use a custom model for Roles, please make sure that:
34
+
35
+ * It has a `with_name(role_name)` scope. This must filter the roles by their names.
36
+ * It responds to `find_or_create_by_name(role_name)`. Implement this if you are not using AR.
37
+
38
+ === has_roles Options
39
+
40
+ * :role_class_name
41
+
42
+ "RoleMe::Role" by default. You can use you own role model, just pass it through here.
43
+
44
+ * :role_join_table
45
+
46
+ This is the name of the join table between the roled and the role model. By default "role_me_roles_join".
47
+
data/Rakefile ADDED
@@ -0,0 +1,42 @@
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 = 'RoleMe'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+ require 'metric_fu'
37
+ MetricFu::Configuration.run do |config|
38
+ config.rcov[:test_files] = ['spec/**/*_spec.rb']
39
+ config.rcov[:rcov_opts] << "-Ispec" # Needed to find spec_helper
40
+ end
41
+
42
+ task :default => :test
@@ -0,0 +1,7 @@
1
+ # Role Model
2
+ #
3
+ class RoleMe::Role < ActiveRecord::Base
4
+
5
+ scope :with_name, lambda { |role_name| where(:name => role_name).limit(1) }
6
+
7
+ end
@@ -0,0 +1,5 @@
1
+ module RoleMe
2
+ def self.table_name_prefix
3
+ 'role_me_'
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate role_migration Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,27 @@
1
+ module RoleMe
2
+ class RoleMigrationGenerator < Rails::Generators::Base
3
+ include Rails::Generators::Migration
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def create_role_migration
7
+ migration_template "create_role_me_roles.rb", File.join("db", "migrate", "create_role_me_roles.rb")
8
+ sleep(1)
9
+ end
10
+
11
+ def create_role_join_table_migration
12
+ migration_template "create_role_me_roles_join.rb", File.join("db", "migrate", "create_role_me_roles_join.rb")
13
+ sleep(1)
14
+ end
15
+
16
+ private
17
+
18
+ def self.next_migration_number(dirname) #:nodoc:
19
+ if ActiveRecord::Base.timestamped_migrations
20
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
21
+ else
22
+ "%.3d" % (current_migration_number(dirname) + 1)
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ class CreateRoleMeRoles < ActiveRecord::Migration
2
+ def change
3
+ create_table :role_me_roles do |t|
4
+ t.string :name
5
+ t.timestamps
6
+ end
7
+ add_index :role_me_roles, [:name], :unique => true
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ class CreateRoleMeRolesJoin < ActiveRecord::Migration
2
+ def change
3
+ create_table :role_me_roles_join, :id => false do |t|
4
+ t.integer :roled_id
5
+ t.integer :role_id
6
+ t.timestamps
7
+ end
8
+ add_index :role_me_roles_join, [:roled_id, :role_id], :unique => true, :name => "roled_roles"
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ module RoleMe
2
+ class Engine < ::Rails::Engine
3
+
4
+ end
5
+ end
@@ -0,0 +1,105 @@
1
+ module RoleMe
2
+ module HasRoles
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ # Adds roles to the model. options:
7
+ #
8
+ # * :role_class_name => "RoleMe::Role"
9
+ #
10
+ # Sets the default model that holds roles.
11
+ # Default: "RoleMe::Role"
12
+ #
13
+ # * :role_join_table => "role_me_roles_join"
14
+ #
15
+ # Table to hold the has_and_belongs_to_many keys
16
+ # Default : "role_me_roles_join"
17
+ #
18
+ # Examples:
19
+ #
20
+ # * has_role
21
+ # * has_role :role_class_name => "Role"
22
+ #
23
+ #
24
+ # If you are using a custom model for Role, please note:
25
+ #
26
+ # * Your model must respond to a scope `with_name` that selects
27
+ # a role by its name.
28
+ #
29
+ # * Your model must respond to `find_or_create_by_name`. If you are note using
30
+ # AR, you must implement this.
31
+ #
32
+ # The join table must have these columns:
33
+ # `roled_id` -> to store your "user" id
34
+ # `role_id` -> to store your "role" id
35
+ #
36
+ #
37
+ def has_roles(options = {})
38
+ return if self.included_modules.include?(RoleMe::HasRolesMethods)
39
+
40
+ cattr_accessor :role_me_options
41
+
42
+ options.reverse_merge!({
43
+ :role_class_name => "::RoleMe::Role",
44
+ :role_join_table => "role_me_roles_join"
45
+ })
46
+
47
+ self.role_me_options = options
48
+
49
+ self.send(:include, RoleMe::HasRolesMethods)
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ # Module included when you call the has_role in a model
56
+ #
57
+ module HasRolesMethods
58
+ extend ActiveSupport::Concern
59
+
60
+ included do
61
+
62
+ has_and_belongs_to_many :roles,
63
+ :class_name => self.role_me_options[:role_class_name],
64
+ :join_table => self.role_me_options[:role_join_table],
65
+ :foreign_key => "roled_id",
66
+ :association_foreign_key => "role_id"
67
+
68
+ end
69
+
70
+ module InstanceMethods
71
+
72
+ # Adds the role to the model
73
+ #
74
+ # Example:
75
+ #
76
+ # user.has_role! :admin
77
+ # -> will add the admin role to user
78
+ #
79
+ def has_role!(role_name)
80
+ return if has_role?(role_name)
81
+ self.roles << find_or_create_role_by_name(role_name.to_s)
82
+ end
83
+
84
+ # Check if the model has the role
85
+ #
86
+ # Example:
87
+ #
88
+ # user.has_role? :admin
89
+ # -> will return true if the user has the admin role
90
+ #
91
+ def has_role?(role_name)
92
+ not self.roles.with_name(role_name.to_s).empty?
93
+ end
94
+
95
+ private
96
+
97
+ # Checks if the role exists or create a new one
98
+ # and returns it
99
+ def find_or_create_role_by_name(role_name)
100
+ self.class.role_me_options[:role_class_name].constantize.find_or_create_by_name(role_name)
101
+ end
102
+
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,3 @@
1
+ module RoleMe
2
+ VERSION = "1.0.0"
3
+ end
data/lib/role_me.rb ADDED
@@ -0,0 +1,7 @@
1
+ module RoleMe
2
+
3
+ autoload 'HasRoles', 'role_me/has_roles'
4
+
5
+ end
6
+
7
+ require 'role_me/engine'
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :role_me do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: role_me
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tiago Scolari
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-12 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &2156007680 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2156007680
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3
27
+ requirement: &2156005720 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2156005720
36
+ description: An easy way to add roles to your model.
37
+ email:
38
+ - tscolari@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - app/models/role_me/role.rb
44
+ - app/models/role_me.rb
45
+ - lib/generators/role_me/role_migration/role_migration_generator.rb
46
+ - lib/generators/role_me/role_migration/templates/create_role_me_roles.rb
47
+ - lib/generators/role_me/role_migration/templates/create_role_me_roles_join.rb
48
+ - lib/generators/role_me/role_migration/USAGE
49
+ - lib/role_me/engine.rb
50
+ - lib/role_me/has_roles.rb
51
+ - lib/role_me/version.rb
52
+ - lib/role_me.rb
53
+ - lib/tasks/role_me_tasks.rake
54
+ - MIT-LICENSE
55
+ - Rakefile
56
+ - README.rdoc
57
+ homepage: https://github.com/tscolari/role_me
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ segments:
70
+ - 0
71
+ hash: -564280631143811076
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ segments:
79
+ - 0
80
+ hash: -564280631143811076
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.6
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Simple Role Handling to Models
87
+ test_files: []