simple_roles 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ source "http://rubygems.org"
5
5
  # development dependencies will be added by default to the :development group.
6
6
 
7
7
  gem 'activerecord', '>=3.1.0'
8
+ # gem 'activerecord', '4.0.0.beta1'
8
9
 
9
10
  group :development, :test do
10
11
  gem 'rake-kit'
@@ -16,4 +17,5 @@ group :development, :test do
16
17
  gem 'cutter'
17
18
  gem 'rspec'
18
19
  gem 'factory_girl', '2.6.4'
20
+ gem 'database_cleaner'
19
21
  end
@@ -1,7 +1,7 @@
1
1
  module SimpleRoles
2
2
  module Configuration
3
3
  extend self
4
-
4
+
5
5
  attr_writer :strategy, :user_models
6
6
 
7
7
  def user_models
@@ -78,7 +78,7 @@ module SimpleRoles
78
78
  end
79
79
  end if strategy == :many
80
80
  end
81
-
81
+
82
82
  def default_strategy
83
83
  :one
84
84
  end
@@ -26,7 +26,7 @@ module SimpleRoles
26
26
  has_role? r
27
27
  end
28
28
  end
29
-
29
+
30
30
  module DynamicMethods
31
31
  class << self
32
32
  def extended base
@@ -36,8 +36,12 @@ module SimpleRoles
36
36
 
37
37
  def register_dynamic_methods
38
38
  SimpleRoles.config.valid_roles.each do |r|
39
- scope :"#{r}s", where(:role => r.to_s)
40
-
39
+ self.class_eval %{
40
+ def self.#{r}s
41
+ where(:role => :#{r})
42
+ end
43
+ }
44
+
41
45
  define_method :"#{r}?" do
42
46
  role == r
43
47
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleRoles
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "simple_roles"
8
- s.version = "0.0.11"
8
+ s.version = "0.0.12"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["stanislaw"]
12
- s.date = "2013-04-10"
12
+ s.date = "2013-04-16"
13
13
  s.description = "Simple Role System for Rails Apps"
14
14
  s.email = "s.pankevich@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -123,13 +123,10 @@ Gem::Specification.new do |s|
123
123
  "spec/dummy/test/unit/post_test.rb",
124
124
  "spec/dummy/test/unit/user_test.rb",
125
125
  "spec/dummy_spec_helper.rb",
126
- "spec/integration/main_spec.rb",
127
- "spec/integration/requests/main_spec.rb",
128
126
  "spec/simple_roles/configuration_spec.rb",
129
127
  "spec/simple_roles/integration_many_spec.rb",
130
128
  "spec/simple_roles/integration_one_spec.rb",
131
129
  "spec/simple_roles/macros_spec.rb",
132
- "spec/simple_roles/many/persistence_spec.rb",
133
130
  "spec/simple_roles/many_spec.rb",
134
131
  "spec/simple_roles/one_spec.rb",
135
132
  "spec/spec_helper.rb",
@@ -141,9 +138,7 @@ Gem::Specification.new do |s|
141
138
  "spec/support/models/.gitkeep",
142
139
  "spec/support/models/one_user.rb",
143
140
  "spec/support/models/user.rb",
144
- "spec/support/setup_roles.rb",
145
- "spec/support/transaction.rb",
146
- "spec/transaction_spec.rb"
141
+ "spec/support/setup_roles.rb"
147
142
  ]
148
143
  s.homepage = "http://github.com/stanislaw/simple_roles"
149
144
  s.licenses = ["MIT"]
@@ -165,6 +160,7 @@ Gem::Specification.new do |s|
165
160
  s.add_development_dependency(%q<cutter>, [">= 0"])
166
161
  s.add_development_dependency(%q<rspec>, [">= 0"])
167
162
  s.add_development_dependency(%q<factory_girl>, ["= 2.6.4"])
163
+ s.add_development_dependency(%q<database_cleaner>, [">= 0"])
168
164
  else
169
165
  s.add_dependency(%q<activerecord>, [">= 3.1.0"])
170
166
  s.add_dependency(%q<rake-kit>, [">= 0"])
@@ -176,6 +172,7 @@ Gem::Specification.new do |s|
176
172
  s.add_dependency(%q<cutter>, [">= 0"])
177
173
  s.add_dependency(%q<rspec>, [">= 0"])
178
174
  s.add_dependency(%q<factory_girl>, ["= 2.6.4"])
175
+ s.add_dependency(%q<database_cleaner>, [">= 0"])
179
176
  end
180
177
  else
181
178
  s.add_dependency(%q<activerecord>, [">= 3.1.0"])
@@ -188,6 +185,7 @@ Gem::Specification.new do |s|
188
185
  s.add_dependency(%q<cutter>, [">= 0"])
189
186
  s.add_dependency(%q<rspec>, [">= 0"])
190
187
  s.add_dependency(%q<factory_girl>, ["= 2.6.4"])
188
+ s.add_dependency(%q<database_cleaner>, [">= 0"])
191
189
  end
192
190
  end
193
191
 
@@ -21,7 +21,7 @@ describe SimpleRoles::Many do
21
21
 
22
22
  context "Instance methods" do
23
23
  subject { User.new }
24
-
24
+
25
25
  [:roles, :roles_list].each do |meth|
26
26
  specify { should respond_to(meth) }
27
27
  its(:"#{meth}") { should be_empty }
@@ -106,28 +106,29 @@ describe SimpleRoles::Many do
106
106
  context "Integration for roles methods" do
107
107
  it "should work when #flatten is called over #roles" do
108
108
  user.roles = [ :admin ]
109
-
109
+
110
110
  user.roles_list.should == Array.new([:admin])
111
111
  user.roles_list.flatten.should == Array.new([:admin])
112
112
  end
113
-
114
- it "should add :roles to accessible_attributes if they are Whitelisted" do
113
+
114
+ # Need to investigate how stuff like that is done in Rails 4
115
+ pending "should add :roles to accessible_attributes if they are Whitelisted" do
115
116
  user.roles = [ :admin ]
116
117
 
117
118
  user.roles_list.should include(:admin)
118
119
  user.save!
119
- User.find_by_name!("stanislaw").should be_kind_of(User)
120
+ User.find_by_name!("stanislaw").should_not be_nil
120
121
  User.delete_all
121
122
 
122
123
  User.attr_accessible :name
123
-
124
+
124
125
  user = User.new(:name => "stanislaw")
125
126
  user.roles = [ :admin ]
126
127
  user.roles_list.should include(:admin)
127
128
  user.save!
128
129
  User.find_by_name!("stanislaw").should be_kind_of(User)
129
130
  end
130
-
131
+
131
132
  it "should not duplicate roles when adding" do
132
133
  user.roles = [ :admin ]
133
134
  user.roles.should == [ :admin ]
@@ -162,11 +163,17 @@ describe SimpleRoles::Many do
162
163
 
163
164
  describe "Dynamic scopes" do
164
165
  subject { User }
165
- SimpleRoles.config.valid_roles.each do |r|
166
- it { should respond_to :"#{r}s" }
167
- it { should respond_to(:"#{r}s_ids") }
168
166
 
169
- its(:"#{r}s") { should be_kind_of(Array) }
167
+ SimpleRoles.config.valid_roles.each do |role|
168
+ it { should respond_to :"#{role}s" }
169
+ it { should respond_to(:"#{role}s_ids") }
170
+
171
+ it "should return the users of role :#{role}" do
172
+ user = create(:user, :roles => [ role ])
173
+
174
+ User.send(:"#{role}s").count.should == 1
175
+ User.send(:"#{role}s").should include user
176
+ end
170
177
  end
171
178
  end
172
179
 
@@ -184,7 +191,7 @@ describe SimpleRoles::Many do
184
191
  user.user?.should == true
185
192
  end
186
193
  end
187
-
194
+
188
195
  describe "#is_user?, #is_admin?, ..." do
189
196
  specify do
190
197
  user.set_role(:admin)
@@ -1,9 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
+ SimpleRoles.configure do |config|
4
+ config.valid_roles = [:user, :admin, :editor]
5
+ end
6
+
3
7
  describe SimpleRoles::One do
4
8
  subject { SimpleRoles::One }
5
9
  let(:user) { create :one_user, :role => 'user' }
6
-
10
+
7
11
  before(:all) do
8
12
  SimpleRoles::Packager.package OneUser, :one
9
13
  end
@@ -59,7 +63,7 @@ describe SimpleRoles::One do
59
63
  user.role.should == :admin
60
64
  end
61
65
  end
62
-
66
+
63
67
  describe "#update_role" do
64
68
  it "should set role" do
65
69
  user.update_role(:admin)
@@ -91,7 +95,7 @@ describe SimpleRoles::One do
91
95
  user.has_any_role?(:admin, :editor).should be_true
92
96
  user.has_any_role?(:editor, :user).should be_false
93
97
  end
94
-
98
+
95
99
  describe "Dynamic methods" do
96
100
  SimpleRoles.config.valid_roles.each do |r|
97
101
  specify { user.should respond_to :"#{r}?" }
@@ -106,7 +110,7 @@ describe SimpleRoles::One do
106
110
  user.user?.should == true
107
111
  end
108
112
  end
109
-
113
+
110
114
  describe "#is_user?, #is_admin?, ..." do
111
115
  specify do
112
116
  user.set_role(:admin)
@@ -118,4 +122,19 @@ describe SimpleRoles::One do
118
122
  end
119
123
  end
120
124
  end
125
+
126
+ describe "Scope methods" do
127
+ subject { OneUser }
128
+
129
+ SimpleRoles.config.valid_roles.each do |role|
130
+ it { should respond_to :"#{role}s" }
131
+
132
+ it "should return the users of role :#{role}" do
133
+ user = create(:one_user, :role => role)
134
+
135
+ OneUser.send(:"#{role}s").count.should == 1
136
+ OneUser.send(:"#{role}s").should include user
137
+ end
138
+ end
139
+ end
121
140
  end
@@ -4,12 +4,14 @@ require 'rubygems'
4
4
  ENV["RAILS_ENV"] ||= 'test'
5
5
 
6
6
  #require File.expand_path("../dummy/config/environment", __FILE__)
7
+
7
8
  require 'logger'
8
9
  require 'require_all'
9
10
  require 'cutter'
10
11
  require 'sugar-high/dsl'
11
12
 
12
13
  require 'active_record'
14
+ require 'database_cleaner'
13
15
 
14
16
  require 'rspec/core'
15
17
 
@@ -25,19 +27,22 @@ dbfile = File.open(path)
25
27
  dbconfig = YAML::load(dbfile)
26
28
  ActiveRecord::Base.establish_connection(dbconfig)
27
29
 
28
- class ActiveRecord::Base
29
- mattr_accessor :shared_connection
30
- @@shared_connection = nil
30
+ # class ActiveRecord::Base
31
+ # mattr_accessor :shared_connection
32
+ # @@shared_connection = nil
31
33
 
32
- def self.connection
33
- @@shared_connection || retrieve_connection
34
- end
35
- end
34
+ # def self.connection
35
+ # @@shared_connection || retrieve_connection
36
+ # end
37
+ # end
36
38
 
37
- ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
39
+ #ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
38
40
 
39
41
  # ActiveRecord::Base.logger = Logger.new(STDERR)
40
42
 
43
+ # TODO: do not preserve roles table
44
+ DatabaseCleaner.strategy = :truncation, { :except => %w[roles], :pre_count => true, :reset_ids => true }
45
+
41
46
  RSpec.configure do |config|
42
47
  config.mock_with :rspec
43
48
 
@@ -53,19 +58,10 @@ RSpec.configure do |config|
53
58
  # Helper migration - users table
54
59
  migrate File.expand_path('../support/migrations', __FILE__)
55
60
  end if tables.empty?
56
-
57
- # (tables - ['schema_migrations']).map do |table|
58
- # table_count = execute("SELECT COUNT(*) FROM #{table}").first.first
59
- # execute "TRUNCATE #{table}" unless table_count == 0
60
- # end
61
61
  end
62
62
  end
63
63
 
64
- config.before(:each) do
65
- Transaction.start
66
- end
67
-
68
64
  config.after(:each) do
69
- Transaction.clean
65
+ DatabaseCleaner.clean
70
66
  end
71
67
  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.11
4
+ version: 0.0.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-10 00:00:00.000000000 Z
12
+ date: 2013-04-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -171,6 +171,22 @@ dependencies:
171
171
  - - '='
172
172
  - !ruby/object:Gem::Version
173
173
  version: 2.6.4
174
+ - !ruby/object:Gem::Dependency
175
+ name: database_cleaner
176
+ requirement: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ! '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ type: :development
183
+ prerelease: false
184
+ version_requirements: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ! '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
174
190
  description: Simple Role System for Rails Apps
175
191
  email: s.pankevich@gmail.com
176
192
  executables: []
@@ -285,13 +301,10 @@ files:
285
301
  - spec/dummy/test/unit/post_test.rb
286
302
  - spec/dummy/test/unit/user_test.rb
287
303
  - spec/dummy_spec_helper.rb
288
- - spec/integration/main_spec.rb
289
- - spec/integration/requests/main_spec.rb
290
304
  - spec/simple_roles/configuration_spec.rb
291
305
  - spec/simple_roles/integration_many_spec.rb
292
306
  - spec/simple_roles/integration_one_spec.rb
293
307
  - spec/simple_roles/macros_spec.rb
294
- - spec/simple_roles/many/persistence_spec.rb
295
308
  - spec/simple_roles/many_spec.rb
296
309
  - spec/simple_roles/one_spec.rb
297
310
  - spec/spec_helper.rb
@@ -304,8 +317,6 @@ files:
304
317
  - spec/support/models/one_user.rb
305
318
  - spec/support/models/user.rb
306
319
  - spec/support/setup_roles.rb
307
- - spec/support/transaction.rb
308
- - spec/transaction_spec.rb
309
320
  homepage: http://github.com/stanislaw/simple_roles
310
321
  licenses:
311
322
  - MIT
@@ -321,7 +332,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
321
332
  version: '0'
322
333
  segments:
323
334
  - 0
324
- hash: -2073500389955326145
335
+ hash: -429828586756405293
325
336
  required_rubygems_version: !ruby/object:Gem::Requirement
326
337
  none: false
327
338
  requirements:
@@ -1,7 +0,0 @@
1
- require 'spec_helper'
2
-
3
- # describe "" do
4
- # pending "truth" do
5
- # Rails.application.should be_kind_of(Dummy::Application)
6
- # end
7
- # end
@@ -1,20 +0,0 @@
1
- require 'spec_helper'
2
-
3
- # describe "Requests" do
4
- # pending "truth" do
5
- # Rails.application.should be_kind_of(Dummy::Application)
6
- # end
7
-
8
- # pending "Basic pages" do
9
- # before do
10
- # login_as(User.first)
11
- # end
12
-
13
- # it "should get root" do
14
- # pending
15
-
16
- # get '/'
17
- # response.status.should be(200)
18
- # end
19
- # end
20
- # end
@@ -1,30 +0,0 @@
1
- # Stolen from Bmabey's database_cleaner gem for educational purposes ;)
2
-
3
- module Transaction
4
- class << self
5
- def connection_klass
6
- ActiveRecord::Base
7
- end
8
-
9
- def start
10
- if connection_klass.connection.respond_to?(:increment_open_transactions)
11
- connection_klass.connection.increment_open_transactions
12
- else
13
- connection_klass.__send__(:increment_open_transactions)
14
- end
15
- connection_klass.connection.begin_db_transaction
16
- end
17
-
18
- def clean
19
- return unless connection_klass.connection.open_transactions > 0
20
-
21
- connection_klass.connection.rollback_db_transaction
22
-
23
- if connection_klass.connection.respond_to?(:decrement_open_transactions)
24
- connection_klass.connection.decrement_open_transactions
25
- else
26
- connection_klass.__send__(:decrement_open_transactions)
27
- end
28
- end
29
- end
30
- end
@@ -1,15 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "Transactions test" do
4
- specify "We create some records" do
5
- 3.times do
6
- create :one_user
7
- create :user
8
- end
9
- end
10
-
11
- specify "All tables must empty" do
12
- OneUser.count.should == 0
13
- User.count.should == 0
14
- end
15
- end