has_filter 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/CHANGELOG.md +8 -0
  2. data/README.md +19 -9
  3. data/lib/has_filter.rb +57 -28
  4. metadata +3 -2
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ * 0.1.0
2
+ Rails 2.3 compatibility
3
+ `filtering` method become `filter`
4
+ Fix filtering with invalid params, now filter only model's attribute
5
+ Fix empty filtering
6
+
7
+ * 0.0.1
8
+ Initial release
data/README.md CHANGED
@@ -1,20 +1,32 @@
1
- = HasFilter
1
+ # HasFilter
2
2
 
3
3
  Active Record filter conditions
4
4
 
5
5
  It's allows you to find a model with a specific set of conditions, eliminating complex set of filter conditions in filter forms.
6
6
 
7
7
 
8
- == Getting started
8
+ ## Getting started
9
9
 
10
10
  Install - Gemfile
11
11
 
12
12
  `gem has_filter`
13
13
 
14
14
 
15
+ ### Rails 2.3
16
+
17
+ In rails 2.3 also needs add to `envoriment.rb`
18
+
19
+ ```ruby
20
+ config.gem 'has_filter'
21
+ ```
22
+
23
+ ### Bundle
24
+
15
25
  `$ bundle install`
16
26
 
17
27
 
28
+ ### Usage
29
+
18
30
  ```ruby
19
31
  class Post < ActiveRecord::Base
20
32
  has_filter
@@ -24,9 +36,9 @@ end
24
36
  And now you can use:
25
37
 
26
38
  ```ruby
27
- Post.filtering(:active => true) #=> All active posts
28
- Post.filtering(:active => [true, false]) #=> All posts active or not
29
- Post.filtering(:title => "Something") #=> All that match with title Something (title like %Something%)
39
+ Post.filter(:active => true) #=> All active posts
40
+ Post.filter(:active => [true, false]) #=> All posts active or not
41
+ Post.filter(:title => "Something") #=> All that match with title Something (title like %Something%)
30
42
  ```
31
43
  You can also specify what attributes should be filtered
32
44
 
@@ -37,8 +49,6 @@ end
37
49
  ```
38
50
 
39
51
  ```ruby
40
- Post.filtering(:active => true) #=> No filtering
41
- Post.filtering(:title => "Something", :active => true) #=> All that match with title Something (title like %Something%) ignoring active condition
52
+ Post.filter(:active => true) #=> No filtering
53
+ Post.filter(:title => "Something", :active => true) #=> All that match with title Something (title like %Something%) ignoring active condition
42
54
  ```
43
-
44
-
data/lib/has_filter.rb CHANGED
@@ -7,45 +7,74 @@ module HasFilter
7
7
 
8
8
  module ClassMethods
9
9
  def has_filter(allowed_fields = nil)
10
- @allowed_fields = *allowed_fields
10
+ @_filters = *allowed_fields
11
11
  end
12
12
 
13
- def filtering(conditions)
13
+ def filter(filtering = nil)
14
+ return [] unless filtering
15
+ conditions = filtering
14
16
  filters = []
15
- if @allowed_fields.present?
16
- conditions = conditions.select { |key| @allowed_fields.include? key }
17
- if conditions.empty?
18
- filters << hash_conditions(:id, nil)
19
- end
20
- conditions.each do |key, value|
21
- next if value.nil?
22
- if self.columns_hash[key.to_s].type == :string
23
- filters << like_conditions(key, value)
24
- else
25
- filters << hash_conditions(key, value)
26
- end
27
- end
17
+
18
+ conditions = conditions.select { |k, v| self.column_names.include? k.to_s }
19
+
20
+ if @_filters.present?
21
+ conditions = conditions.select { |key| @_filters.include? key }
22
+ end
23
+
24
+ if conditions.empty?
25
+ filters << _hash_conditions(:id, nil)
28
26
  else
29
- conditions.each do |key, value|
30
- next if value.nil?
31
- if self.columns_hash[key.to_s].type == :string
32
- filters << like_conditions(key, value)
33
- else
34
- filters << hash_conditions(key, value)
35
- end
36
- end
27
+ filters << _set_filters(conditions)
37
28
  end
38
- find(:all, *filters)
29
+
30
+ filters = filters.flatten
31
+ find(:all, :conditions => filters)
39
32
  end
40
33
 
41
34
  private
42
35
 
43
- def hash_conditions(key, value)
44
- { :conditions => { key => value } }
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
45
50
  end
46
51
 
47
- def like_conditions(key, value)
48
- { :conditions => ["#{key.to_s} like ?", "%#{value}%"] }
52
+ def _set_filters(filtering)
53
+ filters = []
54
+ conditions = filtering
55
+ conditions.reject! { |k, v| v.nil? }
56
+
57
+ conditions.each do |key, value|
58
+ next if value.nil?
59
+ if self.columns_hash[key.to_s].type == :string
60
+ filters << _like_conditions(key, value)
61
+ else
62
+ filters << _hash_conditions(key, value)
63
+ end
64
+ end
65
+
66
+ conditions = conditions.inject({}) do |hash, (key, value)|
67
+ if !value.is_a?(Array) && self.columns_hash[key.to_s].type == :string
68
+ hash[key.to_sym] = "%#{value}%"
69
+ elsif value.is_a?(Array)
70
+ hash[key.to_sym] = value.delete_if { |a| a.to_s.blank? }
71
+ else
72
+ hash[key.to_sym] = value
73
+ end
74
+ hash
75
+ end
76
+
77
+ [filters.join(" AND "), conditions]
49
78
  end
50
79
  end
51
80
  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.0.1
4
+ version: 0.1.0
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-10 00:00:00.000000000 Z
12
+ date: 2012-07-12 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Active Record filter conditions
15
15
  email:
@@ -23,6 +23,7 @@ files:
23
23
  - Rakefile
24
24
  - Gemfile
25
25
  - README.md
26
+ - CHANGELOG.md
26
27
  homepage: https://github.com/jhonnyquest/has_filter
27
28
  licenses: []
28
29
  post_install_message: