unscoped_associations 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  Unscoped Associations
2
2
  =====================
3
- It may be that you've ever needed to skip `default_scope` pulling objects via associations (for some strange reasons).
4
- Do it easily with this lib. Supported associations:
3
+ Want to skip the `default_scope` when you get objects through associations (for some strange reasons)? Do it easily with this lib. Supported associations:
5
4
  * `:belongs_to`
6
5
  * `:has_one`
7
6
  * `:has_many`
@@ -24,38 +23,41 @@ Basic usage example:
24
23
 
25
24
  ```ruby
26
25
  class User < ActiveRecord::Base
27
-
28
26
  has_many :comments # or , unscoped: false
29
27
  has_many :all_comments, class_name: 'Comment', unscoped: true
30
28
  has_one :last_comment, class_name: 'Comment', order: 'created_at DESC', unscoped: true
31
29
 
32
30
  default_scope where( active: true )
33
-
34
31
  end
35
32
 
36
33
  class Comment < ActiveRecord::Base
37
-
38
34
  belongs_to :user, unscoped: true
39
35
 
40
36
  default_scope where( public: true )
41
-
42
37
  end
43
38
 
44
39
  @user.comments # => return public comments
45
40
  @user.all_comments # => return all comments skipping default_scope
46
41
  @user.last_comment # => return last comment skipping default_scope
47
-
48
42
  @topic.user # => return user w/o taking account 'active' flag
49
43
 
50
44
  ```
51
45
 
52
46
  ## Status
53
- Tested on Rails 3.x series. In Rails 4 you are able to customize data retrieving via a scope block, so you can skip `default_scope` using a block.
47
+ Tested on Rails 3.x series and Rails 4.0.0. Originally thought and built for Rails 3, Rails 4 supported.
48
+
49
+ NOTE: Rails 4 introduces some updates (and more planned for upcoming releases) related to this part. For example, in Rails 4, you are able to customize associations using a scope block, so you can skip `default_scope`:
54
50
 
51
+ ```ruby
52
+ class User < ActiveRecord::Base
53
+ has_many :all_comments, -> { where public: [true, flase] }, class_name: 'Comment'
54
+ end
55
+ ```
56
+
57
+ Anyway, you can use `unscoped` option, if you prefer.
55
58
 
56
59
  ## Contributing
57
60
  Ideas, fixes, improvements or any comment are welcome!
58
61
 
59
-
60
62
  ## License
61
63
  Copyright (c) 2013 Marc Anguera. Unscoped Associations is released under the [MIT](http://opensource.org/licenses/MIT) License.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
@@ -11,29 +11,37 @@ module UnscopedAssociations
11
11
 
12
12
  module ClassMethods
13
13
 
14
- def belongs_to_with_unscoped(name, options = {})
15
- if options.delete(:unscoped)
16
- add_unscoped_association(name)
17
- end
18
- belongs_to_without_unscoped(name, options)
14
+ def belongs_to_with_unscoped(name, scope = nil, options = {})
15
+ build_unscoped(:belongs_to, name, scope, options)
19
16
  end
20
17
 
21
- def has_many_with_unscoped(name, options = {})
22
- if options.delete(:unscoped)
23
- add_unscoped_association(name)
24
- end
25
- has_many_without_unscoped(name, options)
18
+ def has_many_with_unscoped(name, scope = nil, options = {})
19
+ build_unscoped(:has_many, name, scope, options)
26
20
  end
27
21
 
28
- def has_one_with_unscoped(name, options = {})
29
- if options.delete(:unscoped)
30
- add_unscoped_association(name)
31
- end
32
- has_one_without_unscoped(name, options)
22
+ def has_one_with_unscoped(name, scope = nil, options = {})
23
+ build_unscoped(:has_one, name, scope, options)
33
24
  end
34
25
 
35
26
  private
36
27
 
28
+ def build_unscoped(assoc_type, assoc_name, scope = nil, options = {})
29
+ if scope.is_a?(Hash)
30
+ options = scope
31
+ scope = nil
32
+ end
33
+
34
+ if options.delete(:unscoped)
35
+ add_unscoped_association(assoc_name)
36
+ end
37
+
38
+ if scope
39
+ self.send("#{assoc_type}_without_unscoped", assoc_name, scope, options)
40
+ else
41
+ self.send("#{assoc_type}_without_unscoped", assoc_name, options)
42
+ end
43
+ end
44
+
37
45
  def add_unscoped_association(association_name)
38
46
  define_method(association_name) do
39
47
  self.class.reflect_on_association(association_name).klass.unscoped do
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "unscoped_associations"
8
- s.version = "0.3.1"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["markets"]
12
- s.date = "2013-09-07"
12
+ s.date = "2013-09-11"
13
13
  s.description = "Skip default_scope in your associations"
14
14
  s.email = "srmarc.ai@gmail.com"
15
15
  s.extra_rdoc_files = [
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.3.1
4
+ version: 0.4.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: 2013-09-07 00:00:00.000000000 Z
12
+ date: 2013-09-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -103,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
103
  version: '0'
104
104
  segments:
105
105
  - 0
106
- hash: 596675425
106
+ hash: -618493999
107
107
  required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  none: false
109
109
  requirements: