edge_rider 0.2.4 → 0.2.5

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
@@ -135,11 +135,11 @@ if it was evaluated right now. For this, Edge Rider gives your relations a metho
135
135
 
136
136
  # Rails 2 scope
137
137
  Post.scoped(:conditions => { :id => [1, 2] }).to_sql
138
- # => SELECT `posts`.* FROM `posts` WHERE `posts.id` IN (1, 2)
138
+ # => "SELECT `posts`.* FROM `posts` WHERE `posts.id` IN (1, 2)"
139
139
 
140
140
  # Rails 3 relation
141
141
  Post.where(:id => [1, 2]).to_sql
142
- # => SELECT `posts`.* FROM `posts` WHERE `posts.id` IN (1, 2)
142
+ # => "SELECT `posts`.* FROM `posts` WHERE `posts.id` IN (1, 2)"
143
143
 
144
144
  *Implementation note*: Rails 3+ implements `#to_sql`. Edge Rider backports this method to Rails 2 so you can use it
145
145
  regardless of your Rails version.
@@ -181,6 +181,24 @@ Edge Rider gives your model classes a method `.preload_associations`. The method
181
181
  which Edge Rider merely makes public. Edge Rider ports this method forward to Rails 3.1+.
182
182
 
183
183
 
184
+
185
+ ### Retrieve the class a relation is based on
186
+
187
+ Edge Rider gives your relations a method `#origin_class` that returns the class the relation is based on.
188
+ This is useful e.g. to perform unscoped method look-up.
189
+
190
+ # Rails 2 scope
191
+ Post.recent.origin_class
192
+ # => Post
193
+
194
+ # Rails 3 relation
195
+ Post.recent.origin_class
196
+ # => Post
197
+
198
+ Note that `#origin_class` it roughly equivalent to the blockless form of [`unscoped`](http://apidock.com/rails/ActiveRecord/Scoping/Default/ClassMethods/unscoped) from Rails 3.2+,
199
+ but it works consistently across all Rails versions.
200
+
201
+
184
202
  Installation
185
203
  ------------
186
204
 
@@ -0,0 +1,23 @@
1
+ module EdgeRider
2
+ module OriginClass
3
+
4
+ def origin_class
5
+ scope = scoped({})
6
+ if Util.activerecord2?
7
+ # Rails 2
8
+ while scope.respond_to?(:proxy_scope, true)
9
+ scope = scope.proxy_scope
10
+ end
11
+ else
12
+ # Rails 3
13
+ while scope.respond_to?(:klass, true)
14
+ scope = scope.klass
15
+ end
16
+ end
17
+ scope
18
+ end
19
+
20
+ ActiveRecord::Base.extend(self)
21
+
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module EdgeRider
2
- VERSION = '0.2.4'
2
+ VERSION = '0.2.5'
3
3
  end
data/lib/edge_rider.rb CHANGED
@@ -6,6 +6,9 @@ require 'edge_rider/util'
6
6
  require 'edge_rider/collect_column'
7
7
  require 'edge_rider/collect_ids'
8
8
  require 'edge_rider/preload_associations'
9
+ require 'edge_rider/origin_class'
9
10
  require 'edge_rider/traverse_association'
10
11
  require 'edge_rider/to_id_query'
11
12
  require 'edge_rider/to_sql'
13
+
14
+
@@ -45,6 +45,41 @@ describe EdgeRider::CollectColumn do
45
45
 
46
46
  end
47
47
 
48
+ context 'when called on a has many association' do
49
+
50
+ it 'should collect the given value' do
51
+ topic = Topic.create!
52
+ post = Post.create!(:topic => topic, :id => 1)
53
+ post = Post.create!(:topic => topic, :id => 2)
54
+ topic.reload
55
+ topic.posts.to_a
56
+ topic.posts.collect_column(:id).should =~ [1,2]
57
+ end
58
+
59
+ it 'should return values when further restricted with an anonymous scope' do
60
+ topic = Topic.create!
61
+ post = Post.create!(:topic => topic, :id => 1)
62
+ post = Post.create!(:topic => topic, :id => 2)
63
+ topic.reload
64
+ topic.posts.to_a
65
+ if topic.posts.respond_to?(:where)
66
+ topic.posts.where(:id => [1]).collect_ids.should =~ [1]
67
+ else
68
+ topic.posts.scoped(:conditions => {:id => [1]}).collect_column(:id).should =~ [1]
69
+ end
70
+ end
71
+
72
+ it 'should return values when further restricted with a named scope' do
73
+ topic = Topic.create!
74
+ post = Post.create!(:topic => topic, :id => 1)
75
+ post = Post.create!(:topic => topic, :id => 2)
76
+ topic.reload
77
+ topic.posts.to_a
78
+ topic.posts.these([1]).collect_column(:id).should =~ [1]
79
+ end
80
+
81
+ end
82
+
48
83
  end
49
84
 
50
85
  end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe EdgeRider::OriginClass do
4
+
5
+ describe '#origin_class' do
6
+
7
+ it "should return the class a scope is based on" do
8
+ Forum.create!(:id => 1)
9
+ Forum.create!(:id => 2)
10
+ scope = Forum.scoped(:conditions => { :id => [1] })
11
+ scope.origin_class.should == Forum
12
+ scope.origin_class.collect_ids.should == [1, 2]
13
+ end
14
+
15
+ it "should return the class a scope chain is based on" do
16
+ Forum.create!(:id => 1, :name => 'A')
17
+ Forum.create!(:id => 2, :name => 'B')
18
+ Forum.create!(:id => 3, :name => 'C')
19
+ scope_chain = Forum.scoped(:conditions => { :id => [1, 2] }).scoped(:conditions => { :name => ['A', 'B'] })
20
+ scope_chain.origin_class.should == Forum
21
+ scope_chain.origin_class.collect_ids.should == [1, 2, 3]
22
+ end
23
+
24
+ it "should return itself when called on an ActiveRecord class" do
25
+ Forum.create!(:id => 1)
26
+ Forum.create!(:id => 2)
27
+ Forum.origin_class.should == Forum
28
+ Forum.origin_class.collect_ids.should == [1, 2]
29
+ end
30
+ end
31
+
32
+ end
metadata CHANGED
@@ -1,35 +1,46 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: edge_rider
3
- version: !ruby/object:Gem::Version
4
- version: 0.2.4
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 5
10
+ version: 0.2.5
5
11
  platform: ruby
6
- authors:
12
+ authors:
7
13
  - Henning Koch
8
14
  autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
- date: 2014-03-12 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2014-05-05 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
14
22
  name: activerecord
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - '>='
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
23
  prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - '>='
25
- - !ruby/object:Gem::Version
26
- version: '0'
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
27
35
  description: Power tools for ActiveRecord relations (scopes)
28
36
  email: henning.koch@makandra.de
29
37
  executables: []
38
+
30
39
  extensions: []
40
+
31
41
  extra_rdoc_files: []
32
- files:
42
+
43
+ files:
33
44
  - .gitignore
34
45
  - .rvmrc
35
46
  - .travis.yml
@@ -41,6 +52,7 @@ files:
41
52
  - lib/edge_rider/collect_column.rb
42
53
  - lib/edge_rider/collect_ids.rb
43
54
  - lib/edge_rider/development.rb
55
+ - lib/edge_rider/origin_class.rb
44
56
  - lib/edge_rider/preload_associations.rb
45
57
  - lib/edge_rider/to_id_query.rb
46
58
  - lib/edge_rider/to_sql.rb
@@ -110,33 +122,45 @@ files:
110
122
  - spec/shared/app_root/db/migrate/001_create_test_tables.rb
111
123
  - spec/shared/spec/edge_rider/collect_column_spec.rb
112
124
  - spec/shared/spec/edge_rider/collect_ids_spec.rb
125
+ - spec/shared/spec/edge_rider/origin_class_spec.rb
113
126
  - spec/shared/spec/edge_rider/preload_associations_spec.rb
114
127
  - spec/shared/spec/edge_rider/to_id_query_spec.rb
115
128
  - spec/shared/spec/edge_rider/to_sql_spec.rb
116
129
  - spec/shared/spec/edge_rider/traverse_association_spec.rb
117
130
  - spec/shared/spec/edge_rider/util_spec.rb
131
+ has_rdoc: true
118
132
  homepage: https://github.com/makandra/edge_rider
119
- licenses:
133
+ licenses:
120
134
  - MIT
121
- metadata: {}
122
135
  post_install_message:
123
136
  rdoc_options: []
124
- require_paths:
137
+
138
+ require_paths:
125
139
  - lib
126
- required_ruby_version: !ruby/object:Gem::Requirement
127
- requirements:
128
- - - '>='
129
- - !ruby/object:Gem::Version
130
- version: '0'
131
- required_rubygems_version: !ruby/object:Gem::Requirement
132
- requirements:
133
- - - '>='
134
- - !ruby/object:Gem::Version
135
- version: '0'
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ hash: 3
146
+ segments:
147
+ - 0
148
+ version: "0"
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ hash: 3
155
+ segments:
156
+ - 0
157
+ version: "0"
136
158
  requirements: []
159
+
137
160
  rubyforge_project:
138
- rubygems_version: 2.2.1
161
+ rubygems_version: 1.3.9.5
139
162
  signing_key:
140
- specification_version: 4
163
+ specification_version: 3
141
164
  summary: Power tools for ActiveRecord relations (scopes)
142
165
  test_files: []
166
+
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 8ed3b13a9ec04ce5b38b0ee4de01b59db38db014
4
- data.tar.gz: 1e2068497c3ac2be587f132e9d3dcb32acf28b5b
5
- SHA512:
6
- metadata.gz: b9fa666dda10ba72f454dbb6f6799f55922a1a8b7942eb93e1b5b7cfde6a48680fbf6b145b2aece148afa8a5ae9cd3ab8c95c798e1625e329d6165a421793026
7
- data.tar.gz: d933ccb1317493550e3293e7ac47cd4e787e8257be9c4c7465d245c2f5096f28c9df779198298be02b74dfb9af2259ba861cee7af779ae79c2baee677b71099f