arel-helpers 1.0.0 → 1.1.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.
- checksums.yaml +4 -4
- data/History.txt +5 -1
- data/lib/arel-helpers.rb +1 -0
- data/lib/arel-helpers/query_builder.rb +24 -0
- data/lib/arel-helpers/version.rb +1 -1
- data/spec/join_association_spec.rb +2 -2
- data/spec/query_builder_spec.rb +47 -0
- data/spec/spec_helper.rb +8 -6
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61f2686d698c0636f36437e6d74562670d5ff0c4
|
4
|
+
data.tar.gz: 1f9126e6e36e9c45104fd11b5ffeb7afb6d82118
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e4fe390a4e6fb4e1cd8d8ddd105e1172b26e5c378ce74127fad57ed6cc5bcb2469702f264e02f13fb655e3f523d4b75e237ea7ccbce2029d1f60299b14d4f636
|
7
|
+
data.tar.gz: 6b2f0c69c50bdb7e36b8d58614b986861a699bf4f39b930c8c1c7caba393ef8bd189df3f2142035a46b5f775408d046454dfa6f22aa7d997193c369b42582870
|
data/History.txt
CHANGED
data/lib/arel-helpers.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'forwardable'
|
4
|
+
require 'enumerator'
|
5
|
+
|
6
|
+
module ArelHelpers
|
7
|
+
class QueryBuilder
|
8
|
+
extend Forwardable
|
9
|
+
include Enumerable
|
10
|
+
|
11
|
+
attr_reader :query
|
12
|
+
def_delegators :@query, :to_a, :to_sql, :each
|
13
|
+
|
14
|
+
def initialize(query)
|
15
|
+
@query = query
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def reflect(query)
|
21
|
+
self.class.new(query)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/arel-helpers/version.rb
CHANGED
@@ -26,9 +26,9 @@ describe ArelHelpers::JoinAssociation do
|
|
26
26
|
it "should work for two models, one directly associated and the other indirectly" do
|
27
27
|
Post
|
28
28
|
.joins(join_association(Post, :comments))
|
29
|
-
.joins(join_association(Comment, :
|
29
|
+
.joins(join_association(Comment, :author))
|
30
30
|
.to_sql.should ==
|
31
|
-
'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" INNER JOIN "
|
31
|
+
'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" INNER JOIN "authors" ON "authors"."comment_id" = "comments"."id"'
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should be able to handle multiple associations" do
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
class TestQueryBuilder < ArelHelpers::QueryBuilder
|
6
|
+
def initialize(query = nil)
|
7
|
+
super(query || Post.unscoped)
|
8
|
+
end
|
9
|
+
|
10
|
+
def noop
|
11
|
+
reflect(query)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe ArelHelpers::QueryBuilder do
|
16
|
+
let(:builder) { TestQueryBuilder.new }
|
17
|
+
|
18
|
+
it "returns (i.e. reflects) new instances of QueryBuilder" do
|
19
|
+
builder.tap do |original_builder|
|
20
|
+
original_builder.noop.object_id.should_not == original_builder.object_id
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "forwards #to_a" do
|
25
|
+
Post.create(title: "Foobar")
|
26
|
+
builder.to_a.tap do |posts|
|
27
|
+
posts.size.should == 1
|
28
|
+
posts.first.title.should == "Foobar"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it "forwards #to_sql" do
|
33
|
+
builder.to_sql.strip.should == 'SELECT "posts".* FROM "posts"'
|
34
|
+
end
|
35
|
+
|
36
|
+
it "forwards #each" do
|
37
|
+
created_post = Post.create(title: "Foobar")
|
38
|
+
builder.each do |post|
|
39
|
+
post.should == created_post
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
it "forwards other enumerable methods via #each" do
|
44
|
+
Post.create(title: "Foobar")
|
45
|
+
builder.map(&:title).should == ["Foobar"]
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -24,10 +24,10 @@ end
|
|
24
24
|
class Comment < ActiveRecord::Base
|
25
25
|
include ArelHelpers::ArelTable
|
26
26
|
belongs_to :post
|
27
|
-
has_one :
|
27
|
+
has_one :author
|
28
28
|
end
|
29
29
|
|
30
|
-
class
|
30
|
+
class Author < ActiveRecord::Base
|
31
31
|
include ArelHelpers::ArelTable
|
32
32
|
belongs_to :comment
|
33
33
|
end
|
@@ -39,7 +39,9 @@ end
|
|
39
39
|
|
40
40
|
class CreatePostsTable < ActiveRecord::Migration
|
41
41
|
def change
|
42
|
-
create_table :posts
|
42
|
+
create_table :posts do |t|
|
43
|
+
t.column :title, :string
|
44
|
+
end
|
43
45
|
end
|
44
46
|
end
|
45
47
|
|
@@ -51,9 +53,9 @@ class CreateCommentsTable < ActiveRecord::Migration
|
|
51
53
|
end
|
52
54
|
end
|
53
55
|
|
54
|
-
class
|
56
|
+
class CreateAuthorsTable < ActiveRecord::Migration
|
55
57
|
def change
|
56
|
-
create_table :
|
58
|
+
create_table :authors do |t|
|
57
59
|
t.references :comment
|
58
60
|
end
|
59
61
|
end
|
@@ -85,7 +87,7 @@ RSpec.configure do |config|
|
|
85
87
|
silence do
|
86
88
|
CreatePostsTable.new.change
|
87
89
|
CreateCommentsTable.new.change
|
88
|
-
|
90
|
+
CreateAuthorsTable.new.change
|
89
91
|
CreateFavoritesTable.new.change
|
90
92
|
end
|
91
93
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arel-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cameron Dutro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -40,9 +40,11 @@ files:
|
|
40
40
|
- lib/arel-helpers.rb
|
41
41
|
- lib/arel-helpers/arel_table.rb
|
42
42
|
- lib/arel-helpers/join_association.rb
|
43
|
+
- lib/arel-helpers/query_builder.rb
|
43
44
|
- lib/arel-helpers/version.rb
|
44
45
|
- spec/arel_table_spec.rb
|
45
46
|
- spec/join_association_spec.rb
|
47
|
+
- spec/query_builder_spec.rb
|
46
48
|
- spec/spec_helper.rb
|
47
49
|
homepage: http://github.com/camertron
|
48
50
|
licenses: []
|