active_hash_relation 1.0.5 → 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/README.md +15 -3
- data/lib/active_hash_relation/column_filters.rb +46 -12
- data/lib/active_hash_relation/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd3f17dc4f8f76b811c90453a566ff8356baf769
|
4
|
+
data.tar.gz: 3a2d2ed0091644d391fc8402879220fbd936c624
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f37a7bd401b3c5609d125ffdd669bdf41c75fb5b8b8e5e8f879ec9c5dacf66088afb692dfb3ca1e994927be2375a2a08867b7078b59203985fb90ebfe0da3441
|
7
|
+
data.tar.gz: e749fe442aef147137ef82991b06f70a62cf600762c7f6be420696736fcc824b26f1830197a847bda7c9b4cdac90db5b713ca42013c6304fd1fb10d7d66e1cf1
|
data/README.md
CHANGED
@@ -69,7 +69,12 @@ You can apply a filter a column which is a primary key by value or using an arra
|
|
69
69
|
|
70
70
|
#### Integer, Float, Decimal, Date, Time or Datetime/Timestamp
|
71
71
|
You can apply an equality filter:
|
72
|
+
|
72
73
|
* `{example_column: 500}`
|
74
|
+
|
75
|
+
or using an array
|
76
|
+
* `{example_column: [500, 40]}`
|
77
|
+
|
73
78
|
or using a hash as a value you get more options:
|
74
79
|
* `{example_column: {le: 500}}`
|
75
80
|
* `{example_column: {leq: 500}}`
|
@@ -79,7 +84,7 @@ or using a hash as a value you get more options:
|
|
79
84
|
Of course you can provide a compination of those like:
|
80
85
|
* `{example_column: {geq: 500, le: 1000}}`
|
81
86
|
|
82
|
-
The same api is for Date, Time or Datetime/Timestamp.
|
87
|
+
The same api is for a Float, Decimal, Date, Time or Datetime/Timestamp.
|
83
88
|
|
84
89
|
#### Boolean
|
85
90
|
The boolean value is converted from string using ActiveRecord's `TRUE_VALUES` through `value_to_boolean` method.. So for a value to be true must be one of the following: `[true, 1, '1', 't', 'T', 'true', 'TRUE']`. Anything else is false.
|
@@ -88,9 +93,16 @@ The boolean value is converted from string using ActiveRecord's `TRUE_VALUES` th
|
|
88
93
|
|
89
94
|
#### String or Text
|
90
95
|
You can apply an incensitive matching filter (currently working only for Postgres):
|
91
|
-
* `{example_column: test}`
|
96
|
+
* `{example_column: 'test'}` `#runs EXAMPLE_COLUMN = 'test'`
|
97
|
+
* `{example_column: ['test', 'another test']}` `#runs EXAMPLE_COLUMN = 'test' OR EXAMPLE_COLUMN = 'another test'`
|
98
|
+
|
99
|
+
or using a hash as a value you get more options:
|
100
|
+
* `{example_column: {eq: 'exact value'}}` `#runs: EXAMPLE_COLUMN = 'test'`
|
101
|
+
* `{example_column: {starts_with: 'exac'}}` `#runs: EXAMPLE_COLUMN LIKE 'test%'`
|
102
|
+
* `{example_column: {ends_with: 'alue'}}` `#runs: EXAMPLE_COLUMN LIKE '%test'`
|
103
|
+
* `{example_column: {like: 'ct_va}}` `#runs: EXAMPLE_COLUMN LIKE '%test%'`
|
92
104
|
|
93
|
-
|
105
|
+
**Please note that ILIKE and especially LIKE are quite slow if you have millions of records in the db even with an index.**
|
94
106
|
|
95
107
|
### Limit
|
96
108
|
A limit param defines the number of returned resources. For instance:
|
@@ -4,10 +4,13 @@ module ActiveHashRelation::ColumnFilters
|
|
4
4
|
end
|
5
5
|
|
6
6
|
def filter_integer(resource, column, table_name, param)
|
7
|
-
if
|
8
|
-
|
9
|
-
|
7
|
+
if param.is_a? Array
|
8
|
+
n_param = param.to_s.gsub("\"","'").gsub("[","").gsub("]","") #fix this!
|
9
|
+
return resource.where("#{table_name}.#{column} IN (#{n_param})")
|
10
|
+
elsif param.is_a? Hash
|
10
11
|
return apply_leq_geq_le_ge_filters(resource, table_name, column, param)
|
12
|
+
else
|
13
|
+
return resource.where("#{table_name}.#{column} = ?", param)
|
11
14
|
end
|
12
15
|
end
|
13
16
|
|
@@ -21,10 +24,12 @@ module ActiveHashRelation::ColumnFilters
|
|
21
24
|
|
22
25
|
def filter_string(resource, column, table_name, param)
|
23
26
|
if param.is_a? Array
|
24
|
-
n_param = param.to_s.gsub("\"","'").gsub("[","").gsub("]","")
|
25
|
-
return resource
|
27
|
+
n_param = param.to_s.gsub("\"","'").gsub("[","").gsub("]","") #fix this!
|
28
|
+
return resource.where("#{table_name}.#{column} IN (#{n_param})")
|
29
|
+
elsif param.is_a? Hash
|
30
|
+
return apply_like_filters(resource, table_name, column, param)
|
26
31
|
else
|
27
|
-
return resource
|
32
|
+
return resource.where("#{table_name}.#{column} = ?", param)
|
28
33
|
end
|
29
34
|
end
|
30
35
|
|
@@ -33,20 +38,26 @@ module ActiveHashRelation::ColumnFilters
|
|
33
38
|
end
|
34
39
|
|
35
40
|
def filter_date(resource, column, table_name, param)
|
36
|
-
if
|
37
|
-
|
38
|
-
|
41
|
+
if param.is_a? Array
|
42
|
+
n_param = param.to_s.gsub("\"","'").gsub("[","").gsub("]","") #fix this!
|
43
|
+
return resource.where("#{table_name}.#{column} IN (#{n_param})")
|
44
|
+
elsif param.is_a? Hash
|
39
45
|
return apply_leq_geq_le_ge_filters(resource, table_name, column, param)
|
46
|
+
else
|
47
|
+
resource = resource.where(column => param)
|
40
48
|
end
|
41
49
|
|
42
50
|
return resource
|
43
51
|
end
|
44
52
|
|
45
53
|
def filter_datetime(resource, column, table_name, param)
|
46
|
-
if
|
47
|
-
|
48
|
-
|
54
|
+
if param.is_a? Array
|
55
|
+
n_param = param.to_s.gsub("\"","'").gsub("[","").gsub("]","") #fix this!
|
56
|
+
return resource = resource.where("#{table_name}.#{column} IN (#{n_param})")
|
57
|
+
elsif param.is_a? Hash
|
49
58
|
return apply_leq_geq_le_ge_filters(resource, table_name, column, param)
|
59
|
+
else
|
60
|
+
resource = resource.where(column => param)
|
50
61
|
end
|
51
62
|
|
52
63
|
return resource
|
@@ -75,4 +86,27 @@ module ActiveHashRelation::ColumnFilters
|
|
75
86
|
|
76
87
|
return resource
|
77
88
|
end
|
89
|
+
|
90
|
+
def apply_like_filters(resource, table_name, column, param)
|
91
|
+
like_method = "LIKE"
|
92
|
+
like_method = "ILIKE" if param[:with_ilike]
|
93
|
+
|
94
|
+
if !param[:starts_with].blank?
|
95
|
+
resource = resource.where("#{table_name}.#{column} #{like_method} ?", "#{param[:starts_with]}%")
|
96
|
+
end
|
97
|
+
|
98
|
+
if !param[:ends_with].blank?
|
99
|
+
resource = resource.where("#{table_name}.#{column} #{like_method} ?", "%#{param[:ends_with]}")
|
100
|
+
end
|
101
|
+
|
102
|
+
if !param[:like].blank?
|
103
|
+
resource = resource.where("#{table_name}.#{column} #{like_method} ?", "%#{param[:like]}%")
|
104
|
+
end
|
105
|
+
|
106
|
+
if !param[:eq].blank?
|
107
|
+
resource = resource.where("#{table_name}.#{column} = ?", param[:eq])
|
108
|
+
end
|
109
|
+
|
110
|
+
return resource
|
111
|
+
end
|
78
112
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_hash_relation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Filippos Vasilakis
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-11-
|
12
|
+
date: 2015-11-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|