miracle_roles 0.1.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.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Mark Dodwell
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/Manifest ADDED
@@ -0,0 +1,8 @@
1
+ LICENSE
2
+ Manifest
3
+ README.md
4
+ Rakefile
5
+ lib/miracle.rb
6
+ lib/miracle/roles.rb
7
+ lib/miracle_roles.rb
8
+ tasks/deployment.rake
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # Miracle Roles
2
+
3
+ A dead simple roles plugin for Rails.
4
+
5
+ ## Install
6
+
7
+ ### 1 — Get the gem
8
+
9
+ Add the dependency to your config/environent.rb:
10
+
11
+ Rails::Initializer.run do |config|
12
+ ...
13
+ config.gem "miracle_roles"
14
+ ...
15
+ end
16
+
17
+ Now install it
18
+
19
+ sudo rake gems:install
20
+
21
+ ### 2 — Add :roles column to model
22
+
23
+ Generate a migration to add column to your model (Users, in this example):
24
+
25
+ script/generate migration add_roles_to_users
26
+
27
+ Then in the migration that's generated, define the following:
28
+
29
+ class AddRolesToUsers < ActiveRecord::Migration
30
+ def self.up
31
+ add_column :users, :roles, :string
32
+ end
33
+
34
+ def self.down
35
+ remove_column :users, :roles
36
+ end
37
+ end
38
+
39
+ ### 3 &mdash; Include module and define roles
40
+
41
+ Include the Miracle::Roles in your model:
42
+
43
+ class User < ActiveRecord::Base
44
+ include Miracle::Roles
45
+ define_roles :admin, :supervisior, ...
46
+
47
+ ...
48
+ end
49
+
50
+ ## Usage
51
+
52
+ Now you can manage roles in any of the following ways:
53
+
54
+ @user.add_role(:admin)
55
+ @user.remove_role(:admin)
56
+ @user.roles = [:admin, :supervisor]
57
+
58
+ Enjoy!
59
+
60
+ ~ Mark Dodwell (<a href="http://twitter.com/madeofcode">@madeofcode</a>)
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('miracle_roles', '0.1.0') do |p|
6
+ p.description = "A dead simple roles plugin for Rails."
7
+ p.url = "http://github.com/mkdynamic/miracle_roles"
8
+ p.author = 'Mark Dodwell'
9
+ p.email = "http://twitter.com/madeofcode"
10
+ p.development_dependencies = []
11
+ end
12
+
13
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/lib/miracle.rb ADDED
@@ -0,0 +1,2 @@
1
+ module Miracle
2
+ end
@@ -0,0 +1,67 @@
1
+ module Miracle
2
+ module Roles
3
+
4
+ def self.included(base)
5
+ base.class_eval do
6
+ serialize :roles
7
+
8
+ def self.define_roles(*roles)
9
+ @@defined_roles = roles
10
+ @@defined_roles.each do |role|
11
+ class_eval %{
12
+ def #{role}?
13
+ self.roles.include?(:#{role})
14
+ end
15
+
16
+ # HAXX: Not the best code ever written...
17
+ named_scope :#{role.to_s.pluralize},
18
+ :conditions => ['roles LIKE ?', "%\n-\s:#{role}\n%"]
19
+ }
20
+ end
21
+ end
22
+
23
+ def self.defined_roles
24
+ @@defined_roles
25
+ end
26
+ end
27
+ end
28
+
29
+ # NOTE: This may break something if it's already defined in the including class
30
+ # ...or if it's redefined later
31
+ def after_initialize
32
+ self.roles ||= []
33
+ end
34
+
35
+ def add_role(role)
36
+ role = role.to_sym
37
+ validate_role! role
38
+ self.roles << role
39
+ self.roles.uniq!
40
+ end
41
+
42
+ def remove_role(role)
43
+ self.roles.reject! { |r| r == role.to_sym }
44
+ end
45
+
46
+ def roles=(list)
47
+ list = list.reject(&:blank?).map { |r| r.to_sym }.reject { |r| !valid_role?(r) }
48
+ write_attribute(:roles, list.uniq)
49
+ end
50
+
51
+ # for declarative_authorization support
52
+ def role_symbols
53
+ self.roles
54
+ end
55
+
56
+ private
57
+
58
+ def validate_role!(role)
59
+ raise "Unknown role!" unless valid_role?(role)
60
+ end
61
+
62
+ def valid_role?(role)
63
+ self.class.defined_roles.include?(role)
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1 @@
1
+ require 'miracle/roles'
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{miracle_roles}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Mark Dodwell"]
9
+ s.date = %q{2010-01-21}
10
+ s.description = %q{A dead simple roles plugin for Rails.}
11
+ s.email = %q{http://twitter.com/madeofcode}
12
+ s.extra_rdoc_files = ["LICENSE", "README.md", "lib/miracle.rb", "lib/miracle/roles.rb", "lib/miracle_roles.rb", "tasks/deployment.rake"]
13
+ s.files = ["LICENSE", "Manifest", "README.md", "Rakefile", "lib/miracle.rb", "lib/miracle/roles.rb", "lib/miracle_roles.rb", "tasks/deployment.rake", "miracle_roles.gemspec"]
14
+ s.homepage = %q{http://github.com/mkdynamic/miracle_roles}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Miracle_roles", "--main", "README.md"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{miracle_roles}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{A dead simple roles plugin for Rails.}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
@@ -0,0 +1,2 @@
1
+ desc "Build the manifest and gemspec files."
2
+ task :build => [:build_manifest, :build_gemspec]
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: miracle_roles
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mark Dodwell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-21 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A dead simple roles plugin for Rails.
17
+ email: http://twitter.com/madeofcode
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.md
25
+ - lib/miracle.rb
26
+ - lib/miracle/roles.rb
27
+ - lib/miracle_roles.rb
28
+ - tasks/deployment.rake
29
+ files:
30
+ - LICENSE
31
+ - Manifest
32
+ - README.md
33
+ - Rakefile
34
+ - lib/miracle.rb
35
+ - lib/miracle/roles.rb
36
+ - lib/miracle_roles.rb
37
+ - tasks/deployment.rake
38
+ - miracle_roles.gemspec
39
+ has_rdoc: true
40
+ homepage: http://github.com/mkdynamic/miracle_roles
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --line-numbers
46
+ - --inline-source
47
+ - --title
48
+ - Miracle_roles
49
+ - --main
50
+ - README.md
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "1.2"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project: miracle_roles
68
+ rubygems_version: 1.3.5
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: A dead simple roles plugin for Rails.
72
+ test_files: []
73
+