active_hash_relation 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fe8ba7c48a6ff435dd220d2b66c7b8945ddd1234
4
- data.tar.gz: 81dc900c0146cc62cc9ec4fbc4f8828e7f8c7292
3
+ metadata.gz: fd3f17dc4f8f76b811c90453a566ff8356baf769
4
+ data.tar.gz: 3a2d2ed0091644d391fc8402879220fbd936c624
5
5
  SHA512:
6
- metadata.gz: 31444ceef4793296c4cc128dd530a5a58653d7117c38fd78d38878c46dcd2e37fbe462c2c5a20add24a29fda19de6097e9e07702c3136c96c7ba137c31e09e1d
7
- data.tar.gz: eec6a91578af20681ff700208b0da56b056ecab33020568b6091e4777194227839985b092b0839a8b1f311df6a8e8474c3707c2e5be6bdb30bc381fecb7de201
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
- The above filter will search all records that include `test` in the `example_column` field. A better would be nice here, for instance, setting the search sensitive or insensitive, start or end with a string etch
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 !param.is_a? Hash
8
- return resource.where(column => param)
9
- else
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 = resource.where("#{table_name}.#{column} IN (#{n_param})")
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 = resource.where("#{table_name}.#{column} ILIKE ?", "%#{param}%")
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 !param.is_a? Hash
37
- resource = resource.where(column => param[column])
38
- else
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 !param.is_a? Hash
47
- resource = resource.where(column => param[column])
48
- else
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
@@ -1,3 +1,3 @@
1
1
  module ActiveHashRelation
2
- VERSION = "1.0.5"
2
+ VERSION = "1.1.0"
3
3
  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.5
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-06 00:00:00.000000000 Z
12
+ date: 2015-11-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler