unscoped_associations 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Unscoped Associations
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/unscoped_associations.png)](http://badge.fury.io/rb/unscoped_associations)
3
+ [![Gem Version](https://badge.fury.io/rb/unscoped_associations.svg)](http://badge.fury.io/rb/unscoped_associations)
4
4
  [![Build Status](https://travis-ci.org/markets/unscoped_associations.svg?branch=master)](https://travis-ci.org/markets/unscoped_associations)
5
5
 
6
6
  Want to skip the `default_scope` when you get objects through associations (for some strange reasons)? Do it easily with this lib. Supported associations:
@@ -30,13 +30,13 @@ class User < ActiveRecord::Base
30
30
  has_many :all_comments, class_name: 'Comment', unscoped: true
31
31
  has_one :last_comment, class_name: 'Comment', order: 'created_at DESC', unscoped: true
32
32
 
33
- default_scope where( active: true )
33
+ default_scope { where(active: true) }
34
34
  end
35
35
 
36
36
  class Comment < ActiveRecord::Base
37
37
  belongs_to :user, unscoped: true
38
38
 
39
- default_scope where( public: true )
39
+ default_scope { where(public: true) }
40
40
  end
41
41
 
42
42
  @user.comments # => return public comments
@@ -1,3 +1,3 @@
1
1
  module UnscopedAssociations
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -45,9 +45,9 @@ module UnscopedAssociations
45
45
  end
46
46
 
47
47
  def add_unscoped_association(association_name)
48
- define_method(association_name) do |*args|
48
+ define_method(association_name) do
49
49
  self.class.reflect_on_association(association_name).klass.unscoped do
50
- super(*args)
50
+ super(association_name)
51
51
  end
52
52
  end
53
53
  end
data/spec/spec_helper.rb CHANGED
@@ -6,42 +6,11 @@ require 'unscoped_associations'
6
6
 
7
7
  ActiveRecord::Base.logger = Logger.new(STDOUT)
8
8
 
9
- ActiveRecord::Base.establish_connection(
10
- :adapter => 'sqlite3',
11
- :database => ':memory:'
12
- )
13
-
14
- ActiveRecord::Schema.define do
15
- self.verbose = false
16
-
17
- create_table :users, :force => true do |t|
18
- t.boolean :active
19
- end
20
-
21
- create_table :comments, :force => true do |t|
22
- t.integer :user_id
23
- t.boolean :public
24
- end
25
- end
26
-
27
- class User < ActiveRecord::Base
28
- has_many :comments
29
- has_many :all_comments, class_name: 'Comment', unscoped: true
30
- has_one :last_comment, class_name: 'Comment', order: 'id DESC', unscoped: true
31
-
32
- default_scope where( active: true )
33
- end
34
-
35
- class Comment < ActiveRecord::Base
36
- belongs_to :user, unscoped: true
37
-
38
- default_scope where( public: true )
39
- end
9
+ Dir["./spec/support/**/*.rb"].sort.each {|f| require f}
40
10
 
41
11
  RSpec.configure do |config|
42
12
  config.treat_symbols_as_metadata_keys_with_true_values = true
43
13
  config.run_all_when_everything_filtered = true
44
- config.filter_run :focus
45
14
  config.mock_with :rspec
46
15
  config.order = 'random'
47
16
  end
@@ -0,0 +1,16 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :comments
3
+ has_many :unscoped_comments, class_name: 'Comment', unscoped: true
4
+ has_one :last_comment, class_name: 'Comment', order: 'created_at DESC'
5
+ has_one :unscoped_last_comment, class_name: 'Comment', order: 'created_at DESC', unscoped: true
6
+
7
+ default_scope { where(active: true) }
8
+ end
9
+
10
+ class Comment < ActiveRecord::Base
11
+ belongs_to :user
12
+ belongs_to :scoped_user, class_name: 'User', foreign_key: 'user_id', unscoped: false
13
+ belongs_to :unscoped_user, class_name: 'User', foreign_key: 'user_id', unscoped: true
14
+
15
+ default_scope { where(public: true) }
16
+ end
@@ -0,0 +1,18 @@
1
+ ActiveRecord::Base.establish_connection(
2
+ adapter: 'sqlite3',
3
+ database: ':memory:'
4
+ )
5
+
6
+ ActiveRecord::Schema.define do
7
+ self.verbose = false
8
+
9
+ create_table :users, force: true do |t|
10
+ t.boolean :active
11
+ end
12
+
13
+ create_table :comments, force: true do |t|
14
+ t.integer :user_id
15
+ t.boolean :public
16
+ t.timestamps
17
+ end
18
+ end
@@ -1,10 +1,50 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe UnscopedAssociations do
4
- it 'belongs_to with unscoped option should skip default_scope' do
5
- user = User.create(:active => false)
6
- comment = Comment.create(:user_id => user.id)
4
+ context 'unscoped association omits default_scope' do
5
+ it 'belongs_to' do
6
+ user = User.creatse(active: false)
7
+ comment = Comment.create(user_id: user.id)
7
8
 
8
- expect(comment.user).to eq(user)
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)
15
+
16
+ expect(user.unscoped_comments).to be_present
17
+ end
18
+
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
24
+ end
25
+ end
26
+
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)
31
+
32
+ expect(comment.user).to be_nil
33
+ expect(comment.scoped_user).to be_nil
34
+ end
35
+
36
+ it 'has_many' do
37
+ user = User.create
38
+ comment = Comment.create(user_id: user.id, public: false)
39
+
40
+ expect(user.comments).to be_empty
41
+ end
42
+
43
+ it 'has_one' do
44
+ user = User.create
45
+ comment = Comment.create(user_id: user.id, public: false)
46
+
47
+ expect(user.last_comment).to be_nil
48
+ end
9
49
  end
10
50
  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.5.0
4
+ version: 0.6.0
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-30 00:00:00.000000000 Z
12
+ date: 2014-03-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -91,6 +91,8 @@ files:
91
91
  - lib/unscoped_associations.rb
92
92
  - lib/unscoped_associations/version.rb
93
93
  - spec/spec_helper.rb
94
+ - spec/support/models.rb
95
+ - spec/support/schema.rb
94
96
  - spec/unscoped_associations_spec.rb
95
97
  - unscoped_associations.gemspec
96
98
  homepage: https://github.com/markets/unscoped_associations
@@ -120,4 +122,6 @@ specification_version: 3
120
122
  summary: Skip default_scope in your associations
121
123
  test_files:
122
124
  - spec/spec_helper.rb
125
+ - spec/support/models.rb
126
+ - spec/support/schema.rb
123
127
  - spec/unscoped_associations_spec.rb