has_filter 0.1.0 → 0.1.1
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.
- data/CHANGELOG.md +15 -8
- data/lib/has_filter.rb +58 -34
- metadata +2 -2
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,15 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
## 0.1.1
|
2
|
+
* Fix nil filtering
|
3
|
+
* Fix boolean filtering
|
4
|
+
|
5
|
+
|
6
|
+
## 0.1.0
|
7
|
+
|
8
|
+
* Rails 2.3 compatibility
|
9
|
+
* `filtering` method become `filter`
|
10
|
+
* Fix filtering with invalid params, now filter only model's attribute
|
11
|
+
* Fix empty filtering
|
12
|
+
|
13
|
+
## 0.0.1
|
14
|
+
|
15
|
+
* Initial release
|
data/lib/has_filter.rb
CHANGED
@@ -15,66 +15,90 @@ module HasFilter
|
|
15
15
|
conditions = filtering
|
16
16
|
filters = []
|
17
17
|
|
18
|
-
conditions = conditions
|
19
|
-
|
20
|
-
if @_filters.present?
|
21
|
-
conditions = conditions.select { |key| @_filters.include? key }
|
22
|
-
end
|
18
|
+
conditions = _normalize_conditions(conditions)
|
23
19
|
|
24
20
|
if conditions.empty?
|
25
|
-
filters << _hash_conditions(:id
|
21
|
+
filters << _hash_conditions(:id)
|
26
22
|
else
|
27
23
|
filters << _set_filters(conditions)
|
28
24
|
end
|
29
25
|
|
30
|
-
|
31
|
-
find(:all, :conditions => filters)
|
26
|
+
find(:all, :conditions => filters.flatten)
|
32
27
|
end
|
33
28
|
|
34
29
|
private
|
35
30
|
|
36
|
-
def _hash_conditions(key, value)
|
37
|
-
if value.is_a? Array
|
38
|
-
["#{key.to_s} in (:#{key.to_s})"]
|
39
|
-
else
|
40
|
-
["#{key.to_s} = :#{key.to_s}"]
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def _like_conditions(key, value)
|
45
|
-
if value.is_a? Array
|
46
|
-
["#{key.to_s} in (:#{key.to_s})"]
|
47
|
-
else
|
48
|
-
["#{key.to_s} like :#{key.to_s}"]
|
49
|
-
end
|
50
|
-
end
|
51
31
|
|
52
|
-
def _set_filters(
|
32
|
+
def _set_filters(conditions)
|
53
33
|
filters = []
|
54
|
-
conditions = filtering
|
55
|
-
conditions.reject! { |k, v| v.nil? }
|
56
34
|
|
57
35
|
conditions.each do |key, value|
|
58
36
|
next if value.nil?
|
59
|
-
if
|
37
|
+
if _column_type(key) == :string
|
60
38
|
filters << _like_conditions(key, value)
|
61
39
|
else
|
62
40
|
filters << _hash_conditions(key, value)
|
63
41
|
end
|
64
42
|
end
|
65
43
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
44
|
+
[filters.join(" AND "), _bind_conditions(conditions)]
|
45
|
+
end
|
46
|
+
|
47
|
+
def _hash_conditions(key, value = nil)
|
48
|
+
return _join_condition(key, :in) if value.is_a? Array
|
49
|
+
_join_condition(key, :eq)
|
50
|
+
end
|
51
|
+
|
52
|
+
def _like_conditions(key, value)
|
53
|
+
return _join_condition(key, :in) if value.is_a? Array
|
54
|
+
_join_condition(key, :like)
|
55
|
+
end
|
56
|
+
|
57
|
+
def _join_condition(key, kind)
|
58
|
+
types = { :in => "%s in (:%s)", :like => "%s like :%s", :eq => "%s = :%s" }
|
59
|
+
[types[kind] % [key.to_s, key.to_s]]
|
60
|
+
end
|
61
|
+
|
62
|
+
def _column_type(column)
|
63
|
+
self.columns_hash[column.to_s].type
|
64
|
+
end
|
65
|
+
|
66
|
+
def _to_bool(value)
|
67
|
+
ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
|
68
|
+
end
|
69
|
+
|
70
|
+
def _normalize_column(key, value)
|
71
|
+
return _to_bool(value) if _column_type(key.to_s) == :boolean
|
72
|
+
value
|
73
|
+
end
|
74
|
+
|
75
|
+
def _normalize_conditions(filtering)
|
76
|
+
conditions = filtering
|
77
|
+
conditions = conditions.select { |k, v| self.column_names.include? k.to_s }
|
78
|
+
conditions = conditions.select { |k, v| @_filters.include? k } if @_filters.present?
|
79
|
+
conditions = conditions.reject { |k, v| v && v.blank? || v.nil? }
|
80
|
+
|
81
|
+
conditions.inject({}) do |hash, (key, value)|
|
82
|
+
key = key.to_sym
|
83
|
+
|
84
|
+
if value.is_a? Array
|
85
|
+
value.reject! { |a| a.to_s.blank? }
|
86
|
+
value.collect! { |v| _normalize_column(key, v) }
|
87
|
+
hash[key] = value
|
71
88
|
else
|
72
|
-
hash[key
|
89
|
+
hash[key] = _normalize_column(key, value)
|
73
90
|
end
|
91
|
+
|
74
92
|
hash
|
75
93
|
end
|
94
|
+
end
|
76
95
|
|
77
|
-
|
96
|
+
def _bind_conditions(conditions)
|
97
|
+
conditions.each do |key, value|
|
98
|
+
if !value.is_a?(Array) && _column_type(key) == :string
|
99
|
+
conditions[key] = "%#{value}%"
|
100
|
+
end
|
101
|
+
end
|
78
102
|
end
|
79
103
|
end
|
80
104
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_filter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-13 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Active Record filter conditions
|
15
15
|
email:
|