simple_roles 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,24 +1,23 @@
1
1
  # SimpleRoles
2
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.
3
+ SimpleRoles is a Rails Engine providing simple Role System for any Rails 3 app.
4
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.
5
+ Initially, it was created as demo role-system to accompany [CanTango gem](https://github.com/kristianmandrup/cantango) initial installiation and usage, and intended to be very easy to setup & use.
6
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)
7
+ Now it is good to be used as a real role system inspite of or due to its almost maximum simplicity.
8
8
 
9
9
  ## Installiation
10
10
 
11
11
  ### Prerequisites
12
12
 
13
- SimpleRoles requires you have User model. That's all.
13
+ SimpleRoles requires you have have User model in your Rails app. That's all.
14
14
 
15
- ### Not a Gem yet
15
+ ### It is a Gem
16
16
 
17
17
  include in Gemfile:
18
18
 
19
19
  ```ruby
20
- gem 'simple_roles', :git => "git://github.com/stanislaw/simple_roles.git"
21
- bundle update
20
+ gem 'simple_roles'
22
21
  ```
23
22
 
24
23
  ### Set up valid roles you're gonna have in your app and choose a Strategy.
@@ -134,6 +133,34 @@ end
134
133
 
135
134
  ## Usage example
136
135
 
136
+ ### One
137
+
138
+ ```ruby
139
+ user = User.create
140
+
141
+ user.role # => nil
142
+
143
+ user.role = :admin
144
+ user.role # => :admin
145
+ user.admin? # => true
146
+ user.is_admin? # => true
147
+
148
+ # Accepts strings too
149
+ user.role = 'instructor'
150
+ user.role # => :instructor
151
+
152
+ # #set_role and #update_role are persistent - #save is called also
153
+ user.set_role(:editor)
154
+ user.reload
155
+ user.role # => :editor
156
+
157
+ user.update_role(:user)
158
+ user.reload
159
+ user.role # => :user
160
+ ```
161
+
162
+ ### Many
163
+
137
164
  ```ruby
138
165
  user = User.new
139
166
  user.roles # => []
@@ -2,11 +2,22 @@ module SimpleRoles
2
2
  module One
3
3
  module Persistence
4
4
  def role
5
- super.to_sym
5
+ (r = super) ? r.to_sym : nil
6
6
  end
7
7
 
8
8
  def role= r
9
- super r.to_s
9
+ check_role r
10
+ r ? super(r.to_s) : super(nil)
11
+ end
12
+
13
+ private
14
+
15
+ def check_role role
16
+ return unless role
17
+
18
+ valid_roles = SimpleRoles.config.valid_roles
19
+
20
+ raise "Not a valid role! Try on of: #{valid_roles.join(', ')}" if ([role.to_sym] - valid_roles).size > 0
10
21
  end
11
22
  end
12
23
  end
@@ -13,6 +13,7 @@ module SimpleRoles
13
13
  self.role= r
14
14
  save!
15
15
  end
16
+ alias_method :update_role, :set_role
16
17
 
17
18
  module DynamicMethods
18
19
  class << self
@@ -1,3 +1,3 @@
1
1
  module SimpleRoles
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
data/simple_roles.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "simple_roles"
8
- s.version = "0.0.6"
8
+ s.version = "0.0.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["stanislaw"]
@@ -134,6 +134,7 @@ Gem::Specification.new do |s|
134
134
  "spec/integration/requests/main_spec.rb",
135
135
  "spec/simple_roles/configuration_spec.rb",
136
136
  "spec/simple_roles/integration_many_spec.rb",
137
+ "spec/simple_roles/integration_one_spec.rb",
137
138
  "spec/simple_roles/macros_spec.rb",
138
139
  "spec/simple_roles/many/persistence_spec.rb",
139
140
  "spec/simple_roles/many_spec.rb",
@@ -3,8 +3,8 @@ class User < ActiveRecord::Base
3
3
 
4
4
  simple_roles
5
5
 
6
- devise :database_authenticatable, :registerable,
7
- :recoverable, :rememberable, :trackable, :validatable
6
+ # devise :database_authenticatable, :registerable,
7
+ # :recoverable, :rememberable, :trackable, :validatable
8
8
 
9
9
  # Setup accessible (or protected) attributes for your model
10
10
  # DEVISE
@@ -1,4 +1,4 @@
1
1
  SimpleRoles.configure do |config|
2
- strategy :many
2
+ strategy :one
3
3
  valid_roles :user, :admin, :editor
4
4
  end
@@ -1,10 +1,10 @@
1
1
  class DeviseCreateUsers < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table(:users) do |t|
4
- t.database_authenticatable :null => false
5
- t.recoverable
6
- t.rememberable
7
- t.trackable
4
+ # t.database_authenticatable :null => false
5
+ # t.recoverable
6
+ # t.rememberable
7
+ # t.trackable
8
8
  # t.encryptable
9
9
  # t.confirmable
10
10
  # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
@@ -12,12 +12,13 @@ class DeviseCreateUsers < ActiveRecord::Migration
12
12
 
13
13
  t.string :name
14
14
  t.string :username
15
-
15
+ t.string :role
16
+
16
17
  t.timestamps
17
18
  end
18
19
 
19
- add_index :users, :email, :unique => true
20
- add_index :users, :reset_password_token, :unique => true
20
+ # add_index :users, :email, :unique => true
21
+ # add_index :users, :reset_password_token, :unique => true
21
22
  # add_index :users, :confirmation_token, :unique => true
22
23
  # add_index :users, :unlock_token, :unique => true
23
24
  # add_index :users, :authentication_token, :unique => true
@@ -15,37 +15,25 @@ ActiveRecord::Schema.define(:version => 20110925210727) do
15
15
 
16
16
  create_table "roles", :force => true do |t|
17
17
  t.string "name"
18
- t.datetime "created_at"
19
- t.datetime "updated_at"
18
+ t.datetime "created_at", :null => false
19
+ t.datetime "updated_at", :null => false
20
20
  end
21
21
 
22
22
  create_table "user_roles", :force => true do |t|
23
23
  t.integer "user_id"
24
24
  t.integer "role_id"
25
- t.datetime "created_at"
26
- t.datetime "updated_at"
25
+ t.datetime "created_at", :null => false
26
+ t.datetime "updated_at", :null => false
27
27
  end
28
28
 
29
29
  add_index "user_roles", ["user_id", "role_id"], :name => "index_user_roles_on_user_id_and_role_id"
30
30
 
31
31
  create_table "users", :force => true do |t|
32
- t.string "email", :default => "", :null => false
33
- t.string "encrypted_password", :limit => 128, :default => "", :null => false
34
- t.string "reset_password_token"
35
- t.datetime "reset_password_sent_at"
36
- t.datetime "remember_created_at"
37
- t.integer "sign_in_count", :default => 0
38
- t.datetime "current_sign_in_at"
39
- t.datetime "last_sign_in_at"
40
- t.string "current_sign_in_ip"
41
- t.string "last_sign_in_ip"
42
32
  t.string "name"
43
33
  t.string "username"
44
- t.datetime "created_at"
45
- t.datetime "updated_at"
34
+ t.string "role"
35
+ t.datetime "created_at", :null => false
36
+ t.datetime "updated_at", :null => false
46
37
  end
47
38
 
48
- add_index "users", ["email"], :name => "index_users_on_email", :unique => true
49
- add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
50
-
51
39
  end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ SimpleRoles.configure do |config|
4
+ config.valid_roles = [:user, :admin, :editor]
5
+ end
6
+
7
+ describe 'Integration for SimpleRoles::One' do
8
+ before do
9
+ setup_roles
10
+ SimpleRoles::Packager.package OneUser, :one
11
+ end
12
+
13
+ it "should all work" do
14
+ user = create :one_user, :role => nil
15
+ user.role.should be_nil
16
+
17
+ user.role = :admin
18
+ user.role.should == :admin
19
+ user.admin?.should be_true
20
+ user.is_admin?.should be_true
21
+
22
+ user.role = 'admin' # Accepts strings too
23
+ user.role.should == :admin
24
+
25
+ user.set_role(:editor)
26
+ user.role.should == :editor
27
+
28
+ user.update_role(:user)
29
+ user.role.should == :user
30
+
31
+ user.user?.should be_true
32
+ user.is_user?.should be_true
33
+
34
+ user.admin?.should be_false
35
+ user.is_admin?.should be_false
36
+ end
37
+ end
@@ -4,72 +4,97 @@ describe SimpleRoles::One do
4
4
  subject { SimpleRoles::One }
5
5
  let(:user) { create :one_user, :role => 'user' }
6
6
 
7
- describe ".package" do
8
- before(:all) do
9
- SimpleRoles::Packager.package OneUser, :one
10
- end
7
+ before(:all) do
8
+ SimpleRoles::Packager.package OneUser, :one
9
+ end
10
+
11
+ describe "Persistence" do
12
+ describe "#role" do
13
+ it "should return nil if there is no role for user" do
14
+ user.role = nil
15
+ user.role.should == nil
16
+ end
11
17
 
12
- describe "Persistence" do
13
18
  it "should read role as symbol" do
14
19
  user.update_attribute :role, 'user'
15
20
  user.role.should == :user
16
21
  end
22
+ end
17
23
 
24
+ describe "#role=" do
18
25
  it "should set roles" do
19
26
  user.role = :admin
20
27
  user.role.should == :admin
21
28
  end
22
29
 
23
- it "should persist roles" do
24
- user.role = :admin
25
- user.save
30
+ it "should also accept strings" do
31
+ user.role = 'admin'
26
32
  user.role.should == :admin
27
33
  end
34
+
35
+ it "should not persist roles" do
36
+ user.role = :admin
37
+ user.reload
38
+ user.role.should == :user
39
+ end
40
+
41
+ it "should not allow non-valid role" do
42
+ expect {
43
+ user.role = :wrong!
44
+ }.to raise_error
45
+ end
28
46
  end
47
+ end
29
48
 
30
- describe "Roles methods" do
31
- describe "#set_role" do
32
- it "should set role" do
33
- user.set_role(:admin)
34
- user.role.should == :admin
35
- end
49
+ describe "Roles methods" do
50
+ describe "#set_role" do
51
+ it "should set role" do
52
+ user.set_role(:admin)
53
+ user.role.should == :admin
54
+ end
36
55
 
37
- it "should persist role" do
38
- user.set_role(:admin)
39
- user.reload
40
- user.role.should == :admin
41
- end
56
+ it "should persist role" do
57
+ user.set_role(:admin)
58
+ user.reload
59
+ user.role.should == :admin
60
+ end
61
+ end
62
+
63
+ describe "#update_role" do
64
+ it "should set role" do
65
+ user.update_role(:admin)
66
+ user.role.should == :admin
42
67
  end
43
68
 
44
- describe "Dynamic scopes" do
45
- SimpleRoles.config.valid_roles.each do |r|
46
- specify { OneUser.should respond_to :"#{r}s" }
47
- end
69
+ it "should persist role" do
70
+ user.update_role(:admin)
71
+ user.reload
72
+ user.role.should == :admin
48
73
  end
74
+ end
49
75
 
50
- describe "Dynamic methods" do
51
- SimpleRoles.config.valid_roles.each do |r|
52
- specify { user.should respond_to :"#{r}?" }
53
- end
76
+ describe "Dynamic methods" do
77
+ SimpleRoles.config.valid_roles.each do |r|
78
+ specify { user.should respond_to :"#{r}?" }
79
+ end
54
80
 
55
- describe "#user?, #admin?, ..." do
56
- specify do
57
- user.set_role(:admin)
58
- user.admin?.should == true
81
+ describe "#user?, #admin?, ..." do
82
+ specify do
83
+ user.set_role(:admin)
84
+ user.admin?.should == true
59
85
 
60
- user.set_role(:user)
61
- user.user?.should == true
62
- end
86
+ user.set_role(:user)
87
+ user.user?.should == true
63
88
  end
64
-
65
- describe "#is_user?, #is_admin?, ..." do
66
- specify do
67
- user.set_role(:admin)
68
- user.is_admin?.should == true
89
+ end
90
+
91
+ describe "#is_user?, #is_admin?, ..." do
92
+ specify do
93
+ user.set_role(:admin)
94
+ user.is_admin?.should == true
69
95
 
70
- user.set_role(:user)
71
- user.is_user?.should == true
72
- end
96
+ user.set_role(:user)
97
+ user.is_user?.should == true
73
98
  end
74
99
  end
75
100
  end
data/spec/spec_helper.rb CHANGED
@@ -47,12 +47,12 @@ RSpec.configure do |config|
47
47
  with ActiveRecord::Base.connection do
48
48
  # tables.each {|t| drop_table t }
49
49
 
50
- # with ActiveRecord::Migrator do
51
- # # SimpleRoles's own migrations
52
- # migrate File.expand_path('../../db/migrate', __FILE__)
53
- # # Helper migration - users table
54
- # migrate File.expand_path('../support/migrations', __FILE__)
55
- # end if tables.empty?
50
+ with ActiveRecord::Migrator do
51
+ # SimpleRoles's own migrations
52
+ migrate File.expand_path('../../db/migrate', __FILE__)
53
+ # Helper migration - users table
54
+ migrate File.expand_path('../support/migrations', __FILE__)
55
+ end if tables.empty?
56
56
 
57
57
  # (tables - ['schema_migrations']).map do |table|
58
58
  # table_count = execute("SELECT COUNT(*) FROM #{table}").first.first
@@ -1,5 +1,5 @@
1
1
  def setup_roles
2
2
  SimpleRoles::Configuration.valid_roles.each do |role|
3
- Role.create(:name => role.to_s)
3
+ Role.create(:name => role.to_s) if Role.where(:name => role.to_s).empty?
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_roles
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -312,6 +312,7 @@ files:
312
312
  - spec/integration/requests/main_spec.rb
313
313
  - spec/simple_roles/configuration_spec.rb
314
314
  - spec/simple_roles/integration_many_spec.rb
315
+ - spec/simple_roles/integration_one_spec.rb
315
316
  - spec/simple_roles/macros_spec.rb
316
317
  - spec/simple_roles/many/persistence_spec.rb
317
318
  - spec/simple_roles/many_spec.rb
@@ -343,7 +344,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
343
344
  version: '0'
344
345
  segments:
345
346
  - 0
346
- hash: -387698285
347
+ hash: -398652943
347
348
  required_rubygems_version: !ruby/object:Gem::Requirement
348
349
  none: false
349
350
  requirements: