has_filter 0.1.1 → 0.1.2

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 +5 -0
  2. data/README.md +10 -0
  3. data/lib/has_filter.rb +11 -16
  4. metadata +2 -2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.1.2
2
+ * Add limit option for filtering, default = 100.
3
+ * Fix empty filtering again - Only return nothing when filtering
4
+ options is empty and filter fields is allowed
5
+
1
6
  ## 0.1.1
2
7
  * Fix nil filtering
3
8
  * Fix boolean filtering
data/README.md CHANGED
@@ -52,3 +52,13 @@ end
52
52
  Post.filter(:active => true) #=> No filtering
53
53
  Post.filter(:title => "Something", :active => true) #=> All that match with title Something (title like %Something%) ignoring active condition
54
54
  ```
55
+
56
+ ### Limit
57
+
58
+ By default the max of records is 100, by you can specify other amount:
59
+
60
+ ```ruby
61
+ Article.filter({:status => "open"}, 1) #=> Retrieve just one element
62
+ ```
63
+
64
+ Note which when we specify a limit, we need to wrap our options in a hash
data/lib/has_filter.rb CHANGED
@@ -1,5 +1,3 @@
1
- require 'active_record'
2
-
3
1
  module HasFilter
4
2
  def self.included(base)
5
3
  base.extend ClassMethods
@@ -10,24 +8,22 @@ module HasFilter
10
8
  @_filters = *allowed_fields
11
9
  end
12
10
 
13
- def filter(filtering = nil)
14
- return [] unless filtering
11
+ def filter(filtering = nil, limit = 100)
15
12
  conditions = filtering
16
- filters = []
17
-
18
- conditions = _normalize_conditions(conditions)
19
-
20
- if conditions.empty?
21
- filters << _hash_conditions(:id)
22
- else
23
- filters << _set_filters(conditions)
13
+ if @_filters.present?
14
+ conditions = _valid_filters(conditions)
15
+ return [] if conditions.empty?
24
16
  end
25
17
 
26
- find(:all, :conditions => filters.flatten)
18
+ conditions = _normalize_conditions(conditions)
19
+ all(:conditions => _set_filters(conditions).flatten, :limit => limit)
27
20
  end
28
21
 
29
22
  private
30
23
 
24
+ def _valid_filters(conditions)
25
+ conditions.select { |k, v| @_filters.include? k }
26
+ end
31
27
 
32
28
  def _set_filters(conditions)
33
29
  filters = []
@@ -74,9 +70,8 @@ module HasFilter
74
70
 
75
71
  def _normalize_conditions(filtering)
76
72
  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? }
73
+ conditions = conditions.select{ |k, v| self.column_names.include? k.to_s }
74
+ conditions = conditions.reject{ |k, v| v && v.blank? || v.nil? }
80
75
 
81
76
  conditions.inject({}) do |hash, (key, value)|
82
77
  key = key.to_sym
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.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -44,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
44
  version: '0'
45
45
  requirements: []
46
46
  rubyforge_project:
47
- rubygems_version: 1.8.10
47
+ rubygems_version: 1.8.24
48
48
  signing_key:
49
49
  specification_version: 3
50
50
  summary: Active Record filter conditions.