query_methods_extend 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +55 -11
- data/lib/query_methods_extend.rb +4 -0
- data/lib/query_methods_extend/association.rb +33 -0
- data/lib/query_methods_extend/basic.rb +9 -0
- data/lib/query_methods_extend/or.rb +2 -2
- data/lib/query_methods_extend/version.rb +1 -1
- 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: 5537e9ae6543ee2f654652c6f1cb653980a93457
|
4
|
+
data.tar.gz: 5a93706ff44adffed4fcd8faa2520bc7bd751b2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01356c41c4b19c33bba359a8f146f1fce1d96f01ed8965e272ba80add857dcde335c3245a7be9afa4e42cbd191cd15ee09dbde9b54fd9f37df870f98107c91c7
|
7
|
+
data.tar.gz: 9a8a773fcd5e2340303745fa97d46d858ef3d98da07a4d760edd98a8f5d5641daa991bbf71a39184f3c8044238434c0d99e988a5dbd367632f13820a6ac50c0c
|
data/README.md
CHANGED
@@ -18,14 +18,58 @@ include QueryMethodsExtend
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
The structure book
|
21
|
+
The structure book models:
|
22
22
|
```ruby
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
class Store < ActiveRecord::Base
|
24
|
+
include QueryMethodsExtend
|
25
|
+
has_many :categories
|
26
|
+
end
|
27
|
+
// Store(id: integer, address: string)
|
28
|
+
|
29
|
+
class Category < ActiveRecord::Base
|
30
|
+
include QueryMethodsExtend
|
31
|
+
belongs_to :store
|
32
|
+
has_many :books
|
33
|
+
end
|
34
|
+
// Category(id: integer, name: string, store_id: integer)
|
35
|
+
|
36
|
+
class Book < ActiveRecord::Base
|
37
|
+
include QueryMethodsExtend
|
38
|
+
belongs_to :category
|
39
|
+
end
|
40
|
+
// Book(id: integer, name: string, price: integer, category_id: integer)
|
41
|
+
```
|
42
|
+
|
43
|
+
## Has_many with collection
|
44
|
+
Before with asscociation **:has_many**, take **all categories in the stores** in Vietnam, we can write:
|
45
|
+
```ruby
|
46
|
+
Category.where(store_id: Store.where(address: 'Vietnam'))
|
47
|
+
```
|
48
|
+
And now we can write in collection:
|
49
|
+
```ruby
|
50
|
+
Store.where(address: 'Vietnam').categories
|
51
|
+
|
52
|
+
***
|
53
|
+
SELECT "categories".* FROM "categories"
|
54
|
+
WHERE (categories.store_id IN (
|
55
|
+
SELECT "stores"."id" FROM "stores" WHERE "stores"."address" = 'Vietnam'
|
56
|
+
)
|
57
|
+
)
|
58
|
+
```
|
59
|
+
|
60
|
+
And take **all books in the stores** in Vietnam (without use **:through**):
|
61
|
+
```ruby
|
62
|
+
Store.where(address: 'Vietnam').categories.books
|
63
|
+
|
64
|
+
***
|
65
|
+
SELECT "books".* FROM "books"
|
66
|
+
WHERE (books.category_id IN (
|
67
|
+
SELECT "categories"."id" FROM "categories"
|
68
|
+
WHERE (categories.store_id IN (
|
69
|
+
SELECT "stores"."id" FROM "stores" WHERE "stores"."address" = 'Vietnam'
|
70
|
+
)
|
71
|
+
)
|
72
|
+
)
|
29
73
|
)
|
30
74
|
```
|
31
75
|
|
@@ -108,9 +152,9 @@ SELECT "books".* FROM "books"
|
|
108
152
|
|
109
153
|
## Like query
|
110
154
|
```ruby
|
111
|
-
Book.like(name: 'ruby')
|
155
|
+
Book.like(name: 'ruby', price: '$')
|
112
156
|
***
|
113
|
-
SELECT "books".* FROM "books" WHERE (books.name LIKE '%ruby%')
|
157
|
+
SELECT "books".* FROM "books" WHERE (books.name LIKE '%ruby%' AND books.price LIKE '%$%')
|
114
158
|
|
115
159
|
Book.l_like(name: 'ruby')
|
116
160
|
***
|
@@ -127,9 +171,9 @@ SELECT "books".* FROM "books" WHERE (books.name LIKE '%ruby%')
|
|
127
171
|
|
128
172
|
## Operators query
|
129
173
|
```ruby
|
130
|
-
Book.lt(price: 5)
|
174
|
+
Book.lt(price: 5, id: 10)
|
131
175
|
***
|
132
|
-
SELECT "books".* FROM "books" WHERE (books.price < 5)
|
176
|
+
SELECT "books".* FROM "books" WHERE (books.price < 5 AND books.id < 10)
|
133
177
|
|
134
178
|
Book.lteq(price: 5)
|
135
179
|
***
|
data/lib/query_methods_extend.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require "query_methods_extend/version"
|
2
|
+
require "query_methods_extend/basic"
|
3
|
+
require "query_methods_extend/association"
|
2
4
|
require "query_methods_extend/or"
|
3
5
|
require "query_methods_extend/like"
|
4
6
|
require "query_methods_extend/operators"
|
@@ -6,6 +8,8 @@ require "query_methods_extend/union"
|
|
6
8
|
|
7
9
|
module QueryMethodsExtend extend ActiveSupport::Concern
|
8
10
|
included do
|
11
|
+
include Basic
|
12
|
+
include Association
|
9
13
|
include OrQuery
|
10
14
|
include Like
|
11
15
|
include Operators
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module QueryMethodsExtend
|
2
|
+
module Association
|
3
|
+
@check_association_exist_flag = false
|
4
|
+
|
5
|
+
def method_missing(method_name, *args, &block)
|
6
|
+
if respond_to? method_name
|
7
|
+
super
|
8
|
+
else
|
9
|
+
check_association_exist(method_name) || super
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def check_association_exist method_name
|
14
|
+
if @check_association_exist_flag
|
15
|
+
return false
|
16
|
+
end
|
17
|
+
|
18
|
+
@check_association_exist_flag = true
|
19
|
+
|
20
|
+
association = self.try(:reflect_on_association, method_name)
|
21
|
+
result = false
|
22
|
+
if association && association.macro == :has_many
|
23
|
+
model_reflect = association.inverse_of
|
24
|
+
model = model_reflect.active_record
|
25
|
+
query = self.select(association.primary_key_column.name)
|
26
|
+
result = model.where("#{model.table_name}.#{model_reflect.foreign_key.to_sym} IN (#{query.to_sql})")
|
27
|
+
end
|
28
|
+
|
29
|
+
@check_association_exist_flag = false
|
30
|
+
return result
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module QueryMethodsExtend
|
2
|
-
module
|
2
|
+
module OrExtend
|
3
3
|
@is_query_or = false
|
4
4
|
|
5
5
|
def set_is_query_or value
|
@@ -39,7 +39,7 @@ module QueryMethodsExtend
|
|
39
39
|
raise 'Agruments should be a HASH'
|
40
40
|
end
|
41
41
|
else
|
42
|
-
all.
|
42
|
+
all.set_is_query_or(true)
|
43
43
|
end
|
44
44
|
}
|
45
45
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: query_methods_extend
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alicuche
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -51,6 +51,8 @@ files:
|
|
51
51
|
- README.md
|
52
52
|
- Rakefile
|
53
53
|
- lib/query_methods_extend.rb
|
54
|
+
- lib/query_methods_extend/association.rb
|
55
|
+
- lib/query_methods_extend/basic.rb
|
54
56
|
- lib/query_methods_extend/like.rb
|
55
57
|
- lib/query_methods_extend/operators.rb
|
56
58
|
- lib/query_methods_extend/or.rb
|