activerecord-blockwhere 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +36 -25
- data/lib/active_record/blockwhere.rb +6 -3
- data/lib/active_record/blockwhere/version.rb +1 -1
- data/lib/active_record/blockwhere/where_proxy.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6f3b8e8805d1f91e2e41e92362468ce1aae18711
|
4
|
+
data.tar.gz: 2cea97fbc36a99e611048600c028f3e3f096f6a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee18dafb7d43e64f89ab7af77eb7e99050bbe816569f931ccc258e0823a99b1c00b08521a162645e4db9d1117fad865f585534344ad18ce8fcb8c53d4fb74d95
|
7
|
+
data.tar.gz: 9bab3b3a0e237745970230f10e5a40cf69ca9d0590d20cefe72de4113e50e2592e81e4abf8ea55de3b9d3ab1ec9f49cbab8ed6d5518cb6e9225d65fb366d25c6
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# activeecord-blockwhere
|
2
2
|
|
3
3
|
Simply conditions DSL for ActiveRecord.
|
4
4
|
It helps using Arel Predications in the block.
|
@@ -19,30 +19,41 @@ Or install it yourself as:
|
|
19
19
|
|
20
20
|
## Usage
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
22
|
+
# * person has many entries.
|
23
|
+
# Person(id: integer, name: string)
|
24
|
+
# Entry(id: integer, person_id: integer, name: string)
|
25
|
+
|
26
|
+
Person.where { id.eq(1) }.to_sql
|
27
|
+
# => SELECT "people".* FROM "people" WHERE "people"."id" = 1
|
28
|
+
|
29
|
+
Person.where { !id.eq(1) }.to_sql
|
30
|
+
# => SELECT "people".* FROM "people" WHERE (NOT ("people"."id" = 1))
|
31
|
+
|
32
|
+
Person.where { id.eq(1) & name.matches('%alice%') }.to_sql
|
33
|
+
# => SELECT "people".* FROM "people" WHERE ("people"."id" = 1 AND "people"."name" LIKE '%alice%')
|
34
|
+
|
35
|
+
Person.where { id.in([1,2,3]) & name.not_eq('bob') }.to_sql
|
36
|
+
# => SELECT "people".* FROM "people" WHERE ("people"."id" IN (1, 2, 3) AND "people"."name" != 'bob')
|
37
|
+
|
38
|
+
Person.where { name.eq('alice') | name.eq('bob') }.to_sql
|
39
|
+
# => SELECT "people".* FROM "people" WHERE (("people"."name" = 'alice' OR "people"."name" = 'bob'))
|
40
|
+
|
41
|
+
# join association
|
42
|
+
Person.where { name.eq('alice') & entries.name.matches('%hello%') }.to_sql
|
43
|
+
# => SELECT "people".* FROM "people"
|
44
|
+
# INNER JOIN "entries" ON "entries"."person_id" = "people"."id"
|
45
|
+
# WHERE ("people"."name" = 'alice' AND "entries"."name" LIKE '%hello%')
|
46
|
+
|
47
|
+
# in action method
|
48
|
+
def index
|
49
|
+
# you can use controller's method in the block. ex: params,request,etc..
|
50
|
+
# but cannot use instance variables.
|
51
|
+
@person = Person.where { id.eq(params[:id]) }.first
|
52
|
+
end
|
53
|
+
|
54
|
+
## Introductory articles
|
55
|
+
|
56
|
+
* [techscore blog (in Japanese)](http://www.techscore.com/blog/2013/05/08/activerecord-blockwhere/ "techscore")
|
46
57
|
|
47
58
|
## Contributing
|
48
59
|
|
@@ -3,9 +3,12 @@ require "active_record/blockwhere/arel_node_operations"
|
|
3
3
|
|
4
4
|
module ActiveRecord
|
5
5
|
module Blockwhere
|
6
|
-
def where(*args
|
6
|
+
def where(*args)
|
7
7
|
relation = args.empty? ? self : super(*args)
|
8
|
-
|
8
|
+
if block_given?
|
9
|
+
block = Proc.new
|
10
|
+
relation = WhereProxy.where(relation, block.binding.eval('self'), &block)
|
11
|
+
end
|
9
12
|
relation
|
10
13
|
end
|
11
14
|
end
|
@@ -14,4 +17,4 @@ end
|
|
14
17
|
ActiveSupport.on_load(:active_record) do
|
15
18
|
ActiveRecord::Relation.send :include, ActiveRecord::Blockwhere
|
16
19
|
Arel::Nodes::Node.send :include, ActiveRecord::Blockwhere::ArelNodeOperations
|
17
|
-
end
|
20
|
+
end
|
@@ -23,7 +23,7 @@ module ActiveRecord
|
|
23
23
|
reflection = @relation.reflections[name]
|
24
24
|
if ::ActiveRecord::Reflection::AssociationReflection === reflection
|
25
25
|
@relation = @relation.joins(name) unless @relation.joins_values.include?(name)
|
26
|
-
return WhereProxy.new(reflection.klass.scoped
|
26
|
+
return WhereProxy.new(reflection.klass.scoped)
|
27
27
|
end
|
28
28
|
if @context && @context.respond_to?(name)
|
29
29
|
return @context.__send__(name, *args, &block)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-blockwhere
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yuki teraoka
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -177,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
177
177
|
version: '0'
|
178
178
|
requirements: []
|
179
179
|
rubyforge_project:
|
180
|
-
rubygems_version: 2.0.
|
180
|
+
rubygems_version: 2.0.0
|
181
181
|
signing_key:
|
182
182
|
specification_version: 4
|
183
183
|
summary: Simply conditions DSL for ActiveRecord. It helps using Arel Predications
|