unscoped_associations 0.6.2 → 0.6.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -46,8 +46,14 @@ module UnscopedAssociations
|
|
46
46
|
|
47
47
|
def add_unscoped_association(association_name)
|
48
48
|
define_method(association_name) do
|
49
|
-
self.class.reflect_on_association(association_name).
|
50
|
-
|
49
|
+
if self.class.reflect_on_association(association_name).polymorphic?
|
50
|
+
self.association(association_name).klass.unscoped do
|
51
|
+
super(association_name)
|
52
|
+
end
|
53
|
+
else
|
54
|
+
self.class.reflect_on_association(association_name).klass.unscoped do
|
55
|
+
super(association_name)
|
56
|
+
end
|
51
57
|
end
|
52
58
|
end
|
53
59
|
end
|
data/spec/support/models.rb
CHANGED
@@ -14,6 +14,8 @@ class User < ActiveRecord::Base
|
|
14
14
|
has_one :last_comment, class_name: 'Comment', order: 'created_at DESC'
|
15
15
|
has_one :unscoped_last_comment, class_name: 'Comment', order: 'created_at DESC', unscoped: true
|
16
16
|
|
17
|
+
has_many :votes, as: :votable, unscoped: true
|
18
|
+
|
17
19
|
default_scope { where(active: true) }
|
18
20
|
end
|
19
21
|
|
@@ -21,6 +23,13 @@ class Comment < ActiveRecord::Base
|
|
21
23
|
belongs_to :user
|
22
24
|
belongs_to :scoped_user, class_name: 'User', foreign_key: 'user_id', unscoped: false
|
23
25
|
belongs_to :unscoped_user, class_name: 'User', foreign_key: 'user_id', unscoped: true
|
26
|
+
has_many :votes, as: :votable
|
27
|
+
|
28
|
+
default_scope { where(public: true) }
|
29
|
+
end
|
30
|
+
|
31
|
+
class Vote < ActiveRecord::Base
|
32
|
+
belongs_to :votable, polymorphic: true, unscoped: true
|
24
33
|
|
25
34
|
default_scope { where(public: true) }
|
26
35
|
end
|
data/spec/support/schema.rb
CHANGED
@@ -3,6 +3,8 @@ require 'spec_helper'
|
|
3
3
|
describe UnscopedAssociations do
|
4
4
|
let(:user) { User.create active: false }
|
5
5
|
let(:comment) { Comment.create user_id: user.id, public: false }
|
6
|
+
let(:user_vote) { user.votes.create public: false }
|
7
|
+
let(:comment_vote) { comment.votes.create }
|
6
8
|
|
7
9
|
context 'a belongs to association' do
|
8
10
|
subject { comment }
|
@@ -51,4 +53,20 @@ describe UnscopedAssociations do
|
|
51
53
|
end
|
52
54
|
end
|
53
55
|
end
|
56
|
+
|
57
|
+
context 'has_many with unscoped polymorphic' do
|
58
|
+
subject { user }
|
59
|
+
|
60
|
+
context 'unscoped_votable' do
|
61
|
+
its(:votes) { should eq [user_vote] }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'belongs_to with unscoped polymorphic' do
|
66
|
+
subject { comment_vote }
|
67
|
+
|
68
|
+
context 'unscoped_votable' do
|
69
|
+
its(:votable) { should eq comment }
|
70
|
+
end
|
71
|
+
end
|
54
72
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unscoped_associations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.3
|
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: 2014-04-
|
12
|
+
date: 2014-04-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|