unscoped_associations 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -53,7 +53,7 @@ NOTE: Rails 4 introduces some updates (and more planned for upcoming releases) r
53
53
 
54
54
  ```ruby
55
55
  class User < ActiveRecord::Base
56
- has_many :all_comments, -> { where public: [true, flase] }, class_name: 'Comment'
56
+ has_many :all_comments, -> { where(public: [true, false]) }, class_name: 'Comment'
57
57
  end
58
58
  ```
59
59
 
@@ -1,3 +1,3 @@
1
1
  module UnscopedAssociations
2
- VERSION = "0.6.1"
2
+ VERSION = "0.6.2"
3
3
  end
@@ -17,8 +17,8 @@ module UnscopedAssociations
17
17
  build_unscoped(:belongs_to, name, scope, options)
18
18
  end
19
19
 
20
- def has_many_with_unscoped(name, scope = nil, options = {})
21
- build_unscoped(:has_many, name, scope, options)
20
+ def has_many_with_unscoped(name, scope = nil, options = {}, &extension)
21
+ build_unscoped(:has_many, name, scope, options, &extension)
22
22
  end
23
23
 
24
24
  def has_one_with_unscoped(name, scope = nil, options = {})
@@ -27,7 +27,7 @@ module UnscopedAssociations
27
27
 
28
28
  private
29
29
 
30
- def build_unscoped(assoc_type, assoc_name, scope = nil, options = {})
30
+ def build_unscoped(assoc_type, assoc_name, scope = nil, options = {}, &extension)
31
31
  if scope.is_a?(Hash)
32
32
  options = scope
33
33
  scope = nil
@@ -38,9 +38,9 @@ module UnscopedAssociations
38
38
  end
39
39
 
40
40
  if scope
41
- self.send("#{assoc_type}_without_unscoped", assoc_name, scope, options)
41
+ self.send("#{assoc_type}_without_unscoped", assoc_name, scope, options, &extension)
42
42
  else
43
- self.send("#{assoc_type}_without_unscoped", assoc_name, options)
43
+ self.send("#{assoc_type}_without_unscoped", assoc_name, options, &extension)
44
44
  end
45
45
  end
46
46
 
@@ -54,4 +54,4 @@ module UnscopedAssociations
54
54
  end
55
55
  end
56
56
 
57
- ActiveRecord::Base.instance_eval { include UnscopedAssociations }
57
+ ActiveRecord::Base.instance_eval { include UnscopedAssociations }
@@ -1,6 +1,16 @@
1
1
  class User < ActiveRecord::Base
2
- has_many :comments
3
- has_many :unscoped_comments, class_name: 'Comment', unscoped: true
2
+ has_many :comments do
3
+ def today
4
+ where('created_at <= ?', Time.now.end_of_day)
5
+ end
6
+ end
7
+
8
+ has_many :unscoped_comments, class_name: 'Comment', unscoped: true do
9
+ def today
10
+ where('created_at <= ?', Time.now.end_of_day)
11
+ end
12
+ end
13
+
4
14
  has_one :last_comment, class_name: 'Comment', order: 'created_at DESC'
5
15
  has_one :unscoped_last_comment, class_name: 'Comment', order: 'created_at DESC', unscoped: true
6
16
 
@@ -1,50 +1,54 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe UnscopedAssociations do
4
- context 'unscoped association omits default_scope' do
5
- it 'belongs_to' do
6
- user = User.create(active: false)
7
- comment = Comment.create(user_id: user.id)
4
+ let(:user) { User.create active: false }
5
+ let(:comment) { Comment.create user_id: user.id, public: false }
8
6
 
9
- expect(comment.unscoped_user).to eq(user)
10
- end
11
-
12
- it 'has_many' do
13
- user = User.create
14
- comment = Comment.create(user_id: user.id, public: false)
7
+ context 'a belongs to association' do
8
+ subject { comment }
15
9
 
16
- expect(user.unscoped_comments).to be_present
10
+ context 'scoped' do
11
+ its(:user) { should be_nil }
12
+ its(:scoped_user) { should be_nil }
17
13
  end
18
14
 
19
- it 'has_one' do
20
- user = User.create
21
- comment = Comment.create(user_id: user.id, public: false)
22
-
23
- expect(user.unscoped_last_comment).to be_present
15
+ context 'unscoped' do
16
+ its(:unscoped_user) { should eq user }
24
17
  end
25
18
  end
26
19
 
27
- context 'no unscoped association takes default_scope' do
28
- it 'belongs_to' do
29
- user = User.create(active: false)
30
- comment = Comment.create(user_id: user.id)
20
+ context 'a has one association' do
21
+ subject { user }
31
22
 
32
- expect(comment.user).to be_nil
33
- expect(comment.scoped_user).to be_nil
23
+ context 'scoped' do
24
+ its(:last_comment) { should be_nil }
34
25
  end
35
26
 
36
- it 'has_many' do
37
- user = User.create
38
- comment = Comment.create(user_id: user.id, public: false)
27
+ context 'unscoped' do
28
+ its(:unscoped_last_comment) { should eq comment }
29
+ end
30
+ end
31
+
32
+ context 'a has many association' do
33
+ subject { user }
34
+
35
+ context 'scoped' do
36
+ its(:comments) { should be_empty }
39
37
 
40
- expect(user.comments).to be_empty
38
+ context 'with an extension' do
39
+ its('comments.today') { should be_empty }
40
+ end
41
41
  end
42
42
 
43
- it 'has_one' do
44
- user = User.create
45
- comment = Comment.create(user_id: user.id, public: false)
43
+ context 'unscoped' do
44
+ its(:unscoped_comments) { should eq [comment] }
46
45
 
47
- expect(user.last_comment).to be_nil
46
+ context 'with an extension' do
47
+ # Extended methods take the default_scope
48
+ its('unscoped_comments.today') { should be_empty }
49
+ # Ideally, it should skip the default_scope
50
+ # its('unscoped_comments.today') { should eq [comment] }
51
+ end
48
52
  end
49
53
  end
50
- end
54
+ 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.1
4
+ version: 0.6.2
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-03-31 00:00:00.000000000 Z
12
+ date: 2014-04-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord