unscoped_associations 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +9 -0
- data/README.md +50 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/unscoped_associations.rb +70 -0
- data/unscoped_associations.gemspec +53 -0
- metadata +119 -0
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
Unscoped Associations
|
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:
|
5
|
+
* `:belongs_to`
|
6
|
+
* `:has_one`
|
7
|
+
* `:has_many`
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
Add this line to you Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'unscoped_associations', git: 'git@github.com:markets/unscoped_associations.git'
|
14
|
+
```
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
Basic usage example:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class User < ActiveRecord::Base
|
21
|
+
|
22
|
+
has_many :comments # or , unscoped: false
|
23
|
+
has_many :all_comments, class_name: 'Comment', unscoped: true
|
24
|
+
has_one :last_comment, class_name: 'Comment', order: 'created_at DESC', unscoped: true
|
25
|
+
|
26
|
+
default_scope where( active: true )
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
class Comment < ActiveRecord::Base
|
31
|
+
|
32
|
+
belongs_to :user, unscoped: true
|
33
|
+
|
34
|
+
default_scope where( public: true )
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
@user.comments # => return public comments
|
39
|
+
@user.all_comments # => return all comments skipping default_scope
|
40
|
+
@user.last_comment # => return last comment skipping default_scope
|
41
|
+
|
42
|
+
@topic.user # => return user w/o taking account 'active' flag
|
43
|
+
|
44
|
+
```
|
45
|
+
|
46
|
+
## Todo
|
47
|
+
A lot of stuff.
|
48
|
+
|
49
|
+
## License
|
50
|
+
Copyright (c) 2013 Marc Anguera. Unscoped Associations is released under the [MIT](http://opensource.org/licenses/MIT) License.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "unscoped_associations"
|
18
|
+
gem.homepage = "http://github.com/markets/unscoped_associations"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Skip default_scope in your associations}
|
21
|
+
gem.description = %Q{Skip default_scope in your associations}
|
22
|
+
gem.email = "srmarc.ai@gmail.com"
|
23
|
+
gem.authors = ["markets"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
# Release tasks
|
29
|
+
namespace :release do
|
30
|
+
desc "create a patch release, create tag and push to github"
|
31
|
+
task :patch do
|
32
|
+
Rake::Task['version:bump:patch'].invoke
|
33
|
+
Rake::Task['gemspec'].invoke
|
34
|
+
`git commit -am 'Regenerate gemspec'`
|
35
|
+
Rake::Task['git:release'].invoke
|
36
|
+
end
|
37
|
+
|
38
|
+
desc "create a minor, create tag and push to github"
|
39
|
+
task :minor do
|
40
|
+
Rake::Task['version:bump:minor'].invoke
|
41
|
+
Rake::Task['gemspec'].invoke
|
42
|
+
`git commit -am 'Regenerate gemspec'`
|
43
|
+
Rake::Task['git:release'].invoke
|
44
|
+
end
|
45
|
+
|
46
|
+
desc "create a major, create tag and push to github"
|
47
|
+
task :major do
|
48
|
+
Rake::Task['version:bump:major'].invoke
|
49
|
+
Rake::Task['gemspec'].invoke
|
50
|
+
`git commit -am 'Regenerate gemspec'`
|
51
|
+
Rake::Task['git:release'].invoke
|
52
|
+
end
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.0
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module UnscopedAssociations
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
(class << base; self; end).instance_eval do
|
6
|
+
alias_method_chain :belongs_to, :unscoped
|
7
|
+
alias_method_chain :has_many, :unscoped
|
8
|
+
alias_method_chain :has_one, :unscoped
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
|
14
|
+
def belongs_to_with_unscoped(name, options = {})
|
15
|
+
if unscoped_attribute = options.delete(:unscoped)
|
16
|
+
add_unscoped_belongs_to(name, options)
|
17
|
+
end
|
18
|
+
belongs_to_without_unscoped(name, options)
|
19
|
+
end
|
20
|
+
|
21
|
+
def has_many_with_unscoped(name, options = {})
|
22
|
+
if unscoped_attribute = options.delete(:unscoped)
|
23
|
+
add_unscoped_has_many(name, options)
|
24
|
+
end
|
25
|
+
has_many_without_unscoped(name, options)
|
26
|
+
end
|
27
|
+
|
28
|
+
def has_one_with_unscoped(name, options = {})
|
29
|
+
if unscoped_attribute = options.delete(:unscoped)
|
30
|
+
add_unscoped_has_one(name, options)
|
31
|
+
end
|
32
|
+
has_one_without_unscoped(name, options)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def add_unscoped_belongs_to(association_name, options)
|
38
|
+
define_singular_association(association_name, options)
|
39
|
+
end
|
40
|
+
|
41
|
+
def add_unscoped_has_many(association_name, options)
|
42
|
+
define_collection_association(association_name, options)
|
43
|
+
end
|
44
|
+
|
45
|
+
def add_unscoped_has_one(association_name, options)
|
46
|
+
define_singular_association(association_name, options)
|
47
|
+
end
|
48
|
+
|
49
|
+
def define_singular_association(association_name, options)
|
50
|
+
define_method(association_name) do
|
51
|
+
klass_name = options[:class_name] || association_name.to_s.camelize
|
52
|
+
instance = "@unscoped_#{association_name}"
|
53
|
+
instance_variable_get(instance) ||
|
54
|
+
instance_variable_set(instance, klass_name.constantize.unscoped { super(association_name) })
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def define_collection_association(association_name, options)
|
59
|
+
define_method(association_name) do
|
60
|
+
klass_name = options[:class_name] || association_name.to_s.camelize.singularize
|
61
|
+
instances = "@unscoped_#{association_name}"
|
62
|
+
instance_variable_get(instances) ||
|
63
|
+
instance_variable_set(instances, klass_name.constantize.unscoped { super(association_name) })
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
ActiveRecord::Base.instance_eval { include UnscopedAssociations }
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "unscoped_associations"
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["markets"]
|
12
|
+
s.date = "2013-05-21"
|
13
|
+
s.description = "Skip default_scope in your associations"
|
14
|
+
s.email = "srmarc.ai@gmail.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.md"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"Gemfile",
|
20
|
+
"README.md",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"lib/unscoped_associations.rb",
|
24
|
+
"unscoped_associations.gemspec"
|
25
|
+
]
|
26
|
+
s.homepage = "http://github.com/markets/unscoped_associations"
|
27
|
+
s.licenses = ["MIT"]
|
28
|
+
s.require_paths = ["lib"]
|
29
|
+
s.rubygems_version = "1.8.24"
|
30
|
+
s.summary = "Skip default_scope in your associations"
|
31
|
+
|
32
|
+
if s.respond_to? :specification_version then
|
33
|
+
s.specification_version = 3
|
34
|
+
|
35
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
36
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 0"])
|
37
|
+
s.add_development_dependency(%q<bundler>, [">= 0"])
|
38
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
39
|
+
s.add_development_dependency(%q<debugger>, [">= 0"])
|
40
|
+
else
|
41
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
42
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
43
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
44
|
+
s.add_dependency(%q<debugger>, [">= 0"])
|
45
|
+
end
|
46
|
+
else
|
47
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
48
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
49
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
50
|
+
s.add_dependency(%q<debugger>, [">= 0"])
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unscoped_associations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- markets
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: jeweler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: debugger
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Skip default_scope in your associations
|
79
|
+
email: srmarc.ai@gmail.com
|
80
|
+
executables: []
|
81
|
+
extensions: []
|
82
|
+
extra_rdoc_files:
|
83
|
+
- README.md
|
84
|
+
files:
|
85
|
+
- Gemfile
|
86
|
+
- README.md
|
87
|
+
- Rakefile
|
88
|
+
- VERSION
|
89
|
+
- lib/unscoped_associations.rb
|
90
|
+
- unscoped_associations.gemspec
|
91
|
+
homepage: http://github.com/markets/unscoped_associations
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
hash: 915012721
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 1.8.24
|
116
|
+
signing_key:
|
117
|
+
specification_version: 3
|
118
|
+
summary: Skip default_scope in your associations
|
119
|
+
test_files: []
|