global_roles 0.0.2

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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Y2VkYjFjZmU0MjAxNzc0MmJlYjJmYmJmYmQ0OThkOGRiNDZjYjFjNw==
5
+ data.tar.gz: !binary |-
6
+ N2I5ZDVmNWM1ZGI5ZTBkNmE3ODlkNDUzMjc4ZDk0ZDcxMDEyMjYxZQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ODJkYjNhMmU1NTM5Nzk0ZjVjNDI5NTM1MTIxMTdkZjhjM2EzNGYwYTA0ZmZh
10
+ YjE4ZTFiMzVhNjc0NGZkZGI4NjUwYjI1NTcxOWJlNTgwNjE3NTVhNzY1YzI0
11
+ OGM2ZGUzMTdmNzg0OTM2M2EyOGY3MzI2OGZhMTg2YTY4Yzk3ODk=
12
+ data.tar.gz: !binary |-
13
+ MjAyZDEwYTNkN2VlZjQzNTJkODZlMGNmOGRmMGIwNzcwNDc2ZDliYzMwMDFk
14
+ NTQ2NjIzNmVjOTc5OGZmOGQ3ZmI0YjEyYmI3ZTg5ZmEwZmVjNDQ1NDJkYjhk
15
+ NWU4ZTg1MTI5NTIwMTA1MzU0MTBkYjQzNTQzNzU4YTQyOGM3NzI=
data/MIT-LICENSE ADDED
@@ -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 ADDED
@@ -0,0 +1,94 @@
1
+ # GlobalRoles
2
+
3
+ _**Disclaimer #1:** My english is awful, so feel free to send me
4
+ corrections of this text._
5
+
6
+ _**Disclaimer #2:** If you like this project, please
7
+ [![endorse](https://api.coderwall.com/gvino/endorsecount.png)](https://coderwall.com/gvino)
8
+ me on coderwall._
9
+
10
+ This is a simple gem to add global role to any model in your
11
+ Ruby on Rails application.
12
+
13
+ ## Usage
14
+
15
+ For example if you have model `User` and want to add global
16
+ role to it, you should do following things:
17
+
18
+ $ rails generate global_roles:install User
19
+ $ rake db:migrate
20
+
21
+ If you want to specify your own roles list, you should provide it
22
+ after model name, e.g.
23
+
24
+ $ rails generate global_roles:install User role1 role2
25
+
26
+ If you want to specify role that will be default for model you
27
+ should mark it as default in roles list:
28
+
29
+ $ rails generate global_roles:install User role1 role2:default
30
+
31
+ **Be careful! If you don't specify default role, it will be first
32
+ role in list.**
33
+
34
+ After this you will see something like this in your model:
35
+
36
+ ```ruby
37
+ class User < ActiveRecord::Base
38
+ ROLES = [:regular, :admin, :moderator]
39
+ # or, if you provide roles list
40
+ # ROLES = [:role1, :role2, ...]
41
+ setup_global_roles!
42
+
43
+ <your old code here>
44
+ end
45
+ ```
46
+
47
+ Constant `ROLES` contains list of roles that you will be able to set
48
+ to your model. You can add new roles to the and of this array but
49
+ not between existing elements since there are any models with roles
50
+ in your database otherwise you won't like results.
51
+
52
+ If you pass optional argument `default: <role_name>` to
53
+ `setup_global_roles!` it will set `<role_name>` as default global
54
+ role for new instances of your model.
55
+
56
+ You can check model's role anywhere in your application using query
57
+ methods `global_<role_name>?`, for example:
58
+
59
+ ```ruby
60
+ if @user.global_admin?
61
+ <some actions>
62
+ end
63
+ ```
64
+
65
+ If you want to set role to model, you can do it two ways. You may
66
+ pass role name
67
+
68
+ ```ruby
69
+ @user.global_role = :admin
70
+ ```
71
+
72
+ or role index:
73
+
74
+ ```ruby
75
+ @user.global_role = 2 # set User::ROLES[2] role
76
+ ```
77
+
78
+ If you need to collect all models with particular role, you should
79
+ use `Model.with_global_role` method:
80
+
81
+ ```ruby
82
+ User.with_global_role(:moderator) # will return list of moderators
83
+ ```
84
+
85
+ ## TODO
86
+
87
+ - [x] Add validation to model
88
+ - [x] Make `with_global_role` method a scope
89
+ - [x] Add possibility to set default role through generator
90
+ - [ ] **Make specs readable**
91
+
92
+ ## License
93
+
94
+ This code is distributed under terms of MIT-LICENSE
data/Rakefile ADDED
@@ -0,0 +1,43 @@
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
+ require 'rspec/core/rake_task'
8
+ begin
9
+ require 'rdoc/task'
10
+ rescue LoadError
11
+ require 'rdoc/rdoc'
12
+ require 'rake/rdoctask'
13
+ RDoc::Task = Rake::RDocTask
14
+ end
15
+
16
+ RDoc::Task.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'GlobalRoles'
19
+ rdoc.options << '--line-numbers'
20
+ rdoc.rdoc_files.include('README.rdoc')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ Bundler::GemHelper.install_tasks
25
+
26
+ RSpec::Core::RakeTask.new(:generators) do |task|
27
+ task.pattern = "spec/generators/**/*_spec.rb"
28
+ end
29
+
30
+ RSpec::Core::RakeTask.new(:global_roles) do |task|
31
+ task.pattern = "spec/global_roles/**/*_spec.rb"
32
+ end
33
+
34
+ task :default => :spec
35
+
36
+ desc "Run all specs"
37
+ task "spec" do
38
+ Rake::Task['generators'].invoke
39
+ return_code1 = $?.exitstatus
40
+ Rake::Task['global_roles'].invoke
41
+ return_code2 = $?.exitstatus
42
+ fail if return_code1 != 0 || return_code2 != 0
43
+ end
@@ -0,0 +1,23 @@
1
+ require 'rails/generators/active_record'
2
+ require 'active_support/core_ext'
3
+
4
+ module ActiveRecord
5
+ module Generators
6
+ class InstallGenerator < ActiveRecord::Generators::Base
7
+ source_root File.expand_path("../templates", __FILE__)
8
+
9
+ def copy_global_roles_migration
10
+ migration_template "migration.rb", "db/migrate/add_global_role_to_#{table_name}"
11
+ end
12
+
13
+ def model_path
14
+ File.join("app", "models", "#{file_path}.rb")
15
+ end
16
+
17
+ def default_value
18
+ args.first == -1 ? 0 : args.first
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,6 @@
1
+ class AddGlobalRoleTo<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ add_column :<%= table_name %>, :global_role, :integer, :null => :false, :default => <%= default_value %>
4
+ add_index :<%= table_name %>, :global_role
5
+ end
6
+ end
@@ -0,0 +1,63 @@
1
+ require 'rails/generators/migration'
2
+ require 'active_support/core_ext'
3
+
4
+ module GlobalRoles
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::NamedBase
7
+ Rails::Generators::ResourceHelpers
8
+
9
+ DEFAULT_ROLES = %w(regular admin moderator)
10
+
11
+ source_root File.expand_path('../templates', __FILE__)
12
+ argument :roles, :type => :array, :default => DEFAULT_ROLES,
13
+ :banner => "role1[:default] role2[:default] role3[:default] ..."
14
+
15
+ desc "Inject roles list and `set_global_roles` method in the specified class"
16
+
17
+ def inject_user_class
18
+ inject_into_file(model_path, :after => inject_global_roles_method) do
19
+ <<RUBY
20
+
21
+ # Global roles
22
+ ROLES = #{roles_list}
23
+ setup_global_roles!#{default_role}
24
+
25
+ RUBY
26
+ end
27
+ end
28
+
29
+ def setup_migration
30
+ invoke "active_record:install", [ name, fetch_default ]
31
+ end
32
+
33
+ protected
34
+
35
+ def inject_global_roles_method
36
+ Regexp.union(
37
+ /class #{class_name.camelize}\n/,
38
+ /class #{class_name.camelize} .*\n/,
39
+ /class #{class_name.demodulize.camelize}\n/,
40
+ /class #{class_name.demodulize.camelize} .*\n/
41
+ )
42
+ end
43
+
44
+ def model_path
45
+ File.join("app", "models", "#{file_path}.rb")
46
+ end
47
+
48
+ def roles_list
49
+ roles.map{ |r| r.split(':').first.to_sym }
50
+ end
51
+
52
+ def fetch_default
53
+ d = roles.map {|r| r.split(':') }.select {|r| r[1] == 'default'}
54
+ d.empty? ? -1 : roles_list.index(d[0].first.to_sym)
55
+ end
56
+
57
+ def default_role
58
+ a = fetch_default
59
+ (a == -1) ? '' : " default: :#{roles_list[a]}"
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,56 @@
1
+ module GlobalRoles
2
+ module Methods
3
+ extend ::ActiveSupport::Concern
4
+
5
+ def global_role
6
+ @global_role ||= self.class::ROLES[self.global_role_id]
7
+ end
8
+
9
+ def global_role_id
10
+ self.read_attribute :global_role
11
+ end
12
+
13
+ def reload_global_role
14
+ @global_role = nil
15
+ end
16
+
17
+ def global_role=(name)
18
+ write_attribute(:global_role, self.class::global_role_id_for(name))
19
+ end
20
+
21
+ def global_role_id=(id)
22
+ unless id.is_a? Fixnum
23
+ raise ArgumentError, "Expected a Fixnum, but got \#{id.inspect}"
24
+ end
25
+ id = self.class::global_role_id_for(id)
26
+ @global_role = nil
27
+ write_attribute(:global_role, id)
28
+ end
29
+
30
+ module ClassMethods
31
+ def values_for_global_role
32
+ self::ROLES
33
+ end
34
+
35
+ def global_role_id_for(r)
36
+ if !valid_role?(r)
37
+ raise ArgumentError, "Unsupported value for `global_role': #{r.inspect}"
38
+ end
39
+ (r.is_a? Integer) ? r : self::ROLES.index(r)
40
+ end
41
+
42
+ private
43
+ def valid_role?(r)
44
+ if (r.is_a? Integer)
45
+ if !(0...self::ROLES.size).include?(r)
46
+ return false
47
+ end
48
+ else
49
+ return self::ROLES.include?(r.respond_to?(:to_sym) ? r.to_sym : r)
50
+ end
51
+ true
52
+ end
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module GlobalRoles
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,42 @@
1
+ require 'active_record'
2
+ require 'active_support'
3
+ require 'global_roles/methods.rb'
4
+
5
+ module GlobalRoles
6
+ extend ::ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ protected
10
+ def setup_global_roles!(options = {})
11
+ self::ROLES.freeze
12
+ include GlobalRoles::Methods
13
+
14
+ if default = options[:default] || 0
15
+ after_initialize do
16
+ self.global_role = default unless global_role? || persisted?
17
+ end
18
+ end
19
+
20
+ self::ROLES.each do |r|
21
+ define_method("global_#{r}?".to_sym) do
22
+ global_role == r
23
+ end
24
+ end
25
+
26
+ self.class_eval %(
27
+ scope :with_global_role,
28
+ proc { |r|
29
+ where(:global_role => #{self}::global_role_id_for(r))
30
+ }
31
+ )
32
+ end
33
+ end
34
+
35
+ def reload(*)
36
+ super
37
+ reload_role
38
+ self
39
+ end
40
+ end
41
+
42
+ ActiveRecord::Base.send :include, GlobalRoles
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :global_roles do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: global_roles
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - George Vinogradov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: shoulda
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: ammeter
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Simple gem to provide global roles for ActiveRecrod models without using
126
+ of another models
127
+ email:
128
+ - g-vino@yandex.ru
129
+ - g.vinogradov@itima.ru
130
+ executables: []
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - lib/generators/active_record/install_generator.rb
135
+ - lib/generators/active_record/templates/migration.rb
136
+ - lib/generators/global_roles/install_generator.rb
137
+ - lib/global_roles/methods.rb
138
+ - lib/global_roles/version.rb
139
+ - lib/global_roles.rb
140
+ - lib/tasks/global_roles_tasks.rake
141
+ - MIT-LICENSE
142
+ - Rakefile
143
+ - README.md
144
+ homepage: http://github.com/gvino/rails_global_roles
145
+ licenses:
146
+ - MIT
147
+ metadata: {}
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ! '>='
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ! '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ requirements: []
163
+ rubyforge_project:
164
+ rubygems_version: 2.0.3
165
+ signing_key:
166
+ specification_version: 4
167
+ summary: Simple roles gem
168
+ test_files: []