acts_as_user 1.2.5 → 1.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/acts_as_user/is_user.rb +2 -2
- data/lib/acts_as_user/railtie.rb +4 -2
- data/lib/acts_as_user/user_delegate.rb +16 -5
- data/lib/acts_as_user/version.rb +1 -1
- data/spec/models/active_record/partner_spec.rb +25 -0
- data/spec/models/active_record/user_spec.rb +9 -1
- data/spec/support/acts_as_user_initializer.rb +1 -1
- data/spec/support/models.rb +5 -1
- data/spec/support/schema.rb +5 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83f6ee78d21aa3ba484e1ffb50d2bd473bfff98a
|
4
|
+
data.tar.gz: f8e7ac9d1ec1f307c6f046bbf5443e22c563dfba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ae1a038f289cbed8d1464febc3ea8905a5b66f4289c4ad7f32f12b834edb9d7042aab976613c99ed0d12c33afd4675e242535d8784e4bbf40f8c86274e4b53a
|
7
|
+
data.tar.gz: d9ab729c639a062dc5f9f797bc2e313e7411f564e48978bd39dc191880ccbbaa5e9d329caa923347452203748f4f1fda210f70aec6a96fa84db8bcca21b069c5
|
data/lib/acts_as_user/is_user.rb
CHANGED
@@ -2,7 +2,7 @@ module ActsAsUser
|
|
2
2
|
module IsUser
|
3
3
|
|
4
4
|
def self.included(base)
|
5
|
-
base.belongs_to :userable, polymorphic: true
|
5
|
+
base.belongs_to :userable, polymorphic: true, inverse_of: :user
|
6
6
|
base.extend ClassMethods
|
7
7
|
#loads models acting as users when the hook is loaded
|
8
8
|
base.define_models_acting_as_users
|
@@ -13,7 +13,7 @@ module ActsAsUser
|
|
13
13
|
models_acting_as_users = ActsAsUser.models_acting_as_users.map(&:to_s).map(&:downcase)
|
14
14
|
models_acting_as_users.each do |model_class_name|
|
15
15
|
define_method("#{model_class_name}?") do
|
16
|
-
self.userable_type.to_s.
|
16
|
+
self.userable_type.to_s.underscore == model_class_name
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
data/lib/acts_as_user/railtie.rb
CHANGED
@@ -7,8 +7,10 @@ module ActsAsUser
|
|
7
7
|
ActsAsUser.add_devise_attributes_to_ignore
|
8
8
|
|
9
9
|
class ActiveRecord::Base
|
10
|
-
|
11
|
-
|
10
|
+
cattr_accessor :acts_as_user_options
|
11
|
+
def self.acts_as_user options={}
|
12
|
+
self.acts_as_user_options = options
|
13
|
+
include ActsAsUser::UserDelegate
|
12
14
|
end
|
13
15
|
def self.is_user
|
14
16
|
include ActsAsUser::IsUser
|
@@ -1,17 +1,28 @@
|
|
1
1
|
module ActsAsUser
|
2
2
|
module UserDelegate
|
3
3
|
def self.included(base)
|
4
|
-
base.
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
options = base.class_variable_get('@@acts_as_user_options')
|
5
|
+
if !!options[:has_many]
|
6
|
+
base.has_many :users, :as => :userable, :dependent => :destroy, :autosave => true
|
7
|
+
base.alias_method_chain :users, :autobuild
|
8
|
+
base.accepts_nested_attributes_for :users, :allow_destroy => true
|
9
|
+
else
|
10
|
+
base.has_one :user, :as => :userable, :dependent => :destroy, :autosave => true
|
11
|
+
base.alias_method_chain :user, :autobuild
|
12
|
+
base.validate :user_must_be_valid
|
13
|
+
base.extend ClassMethods
|
14
|
+
base.define_user_accessors
|
15
|
+
end
|
9
16
|
end
|
10
17
|
|
11
18
|
def user_with_autobuild
|
12
19
|
user_without_autobuild || build_user
|
13
20
|
end
|
14
21
|
|
22
|
+
def users_with_autobuild
|
23
|
+
users_without_autobuild.present? ? users_without_autobuild : (users_without_autobuild << User.new)
|
24
|
+
end
|
25
|
+
|
15
26
|
def method_missing(meth, *args, &blk)
|
16
27
|
user.send(meth, *args, &blk)
|
17
28
|
rescue NoMethodError
|
data/lib/acts_as_user/version.rb
CHANGED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Partner do
|
4
|
+
it { should have_many(:users).dependent(:destroy) }
|
5
|
+
it { should respond_to :users }
|
6
|
+
it { should respond_to :name }
|
7
|
+
it { should accept_nested_attributes_for(:users).allow_destroy(true) }
|
8
|
+
|
9
|
+
describe 'partner with many users' do
|
10
|
+
let(:partner) { Partner.create(name: 'Company', users: users) }
|
11
|
+
let(:users) { Array.new(2) { User.create(email: 'test@icalialabs.com') } }
|
12
|
+
|
13
|
+
it 'gives back all users on partner' do
|
14
|
+
partner.users.to_a.should =~ users
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'users autobuild' do
|
19
|
+
let(:partner) { Partner.new }
|
20
|
+
|
21
|
+
it 'has to have a user already' do
|
22
|
+
partner.users.first.should be_an_instance_of User
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -6,6 +6,7 @@ describe User do
|
|
6
6
|
it { should respond_to :userable_id }
|
7
7
|
it { should respond_to :customer? }
|
8
8
|
it { should respond_to :admin? }
|
9
|
+
it { should respond_to :partner? }
|
9
10
|
|
10
11
|
|
11
12
|
describe '#customer?' do
|
@@ -24,7 +25,7 @@ describe User do
|
|
24
25
|
@current_user = @fake_customer_user.user
|
25
26
|
end
|
26
27
|
it 'returns false' do
|
27
|
-
expect(@current_user).not_to be_customer
|
28
|
+
expect(@current_user).not_to be_customer
|
28
29
|
end
|
29
30
|
end
|
30
31
|
context 'when is an admin' do
|
@@ -36,5 +37,12 @@ describe User do
|
|
36
37
|
expect(@current_user).to be_admin
|
37
38
|
end
|
38
39
|
end
|
40
|
+
context 'when is a partner' do
|
41
|
+
let(:partner) { Partner.create name: 'Fake Name', users: [ User.create(email: 'test@icalialabs.com') ] }
|
42
|
+
let(:user) { partner.users.first }
|
43
|
+
it 'returns true' do
|
44
|
+
expect(user).to be_partner
|
45
|
+
end
|
46
|
+
end
|
39
47
|
end
|
40
48
|
end
|
data/spec/support/models.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class User < ActiveRecord::Base
|
2
|
-
is_user
|
2
|
+
is_user
|
3
3
|
validates :email, presence: true
|
4
4
|
end
|
5
5
|
|
@@ -10,3 +10,7 @@ end
|
|
10
10
|
class Admin < ActiveRecord::Base
|
11
11
|
acts_as_user
|
12
12
|
end
|
13
|
+
|
14
|
+
class Partner < ActiveRecord::Base
|
15
|
+
acts_as_user :has_many => true
|
16
|
+
end
|
data/spec/support/schema.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
ActiveRecord::Schema.define(:version => 0) do
|
2
2
|
create_table :users do |t|
|
3
3
|
t.string :email
|
4
|
-
t.string :userable_type
|
4
|
+
t.string :userable_type
|
5
5
|
t.integer :userable_id
|
6
6
|
end
|
7
7
|
add_index :users, [:userable_id, :userable_type]
|
@@ -13,4 +13,8 @@ ActiveRecord::Schema.define(:version => 0) do
|
|
13
13
|
|
14
14
|
create_table :admins do |t|
|
15
15
|
end
|
16
|
+
|
17
|
+
create_table :partners do |t|
|
18
|
+
t.string :name
|
19
|
+
end
|
16
20
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_user
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Abraham Kuri
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2015-06-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -198,6 +198,7 @@ files:
|
|
198
198
|
- lib/generators/templates/acts_as_user.rb
|
199
199
|
- spec/fake_gem.rb
|
200
200
|
- spec/models/active_record/customer_spec.rb
|
201
|
+
- spec/models/active_record/partner_spec.rb
|
201
202
|
- spec/models/active_record/user_spec.rb
|
202
203
|
- spec/models/config/setup_spec.rb
|
203
204
|
- spec/spec_helper.rb
|
@@ -226,13 +227,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
226
227
|
version: '0'
|
227
228
|
requirements: []
|
228
229
|
rubyforge_project:
|
229
|
-
rubygems_version: 2.
|
230
|
+
rubygems_version: 2.4.5
|
230
231
|
signing_key:
|
231
232
|
specification_version: 4
|
232
233
|
summary: Handles multiple user roles on a rails app
|
233
234
|
test_files:
|
234
235
|
- spec/fake_gem.rb
|
235
236
|
- spec/models/active_record/customer_spec.rb
|
237
|
+
- spec/models/active_record/partner_spec.rb
|
236
238
|
- spec/models/active_record/user_spec.rb
|
237
239
|
- spec/models/config/setup_spec.rb
|
238
240
|
- spec/spec_helper.rb
|