smart_filter 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ Manifest
2
+ README.rdoc
3
+ Rakefile
4
+ init.rb
5
+ lib/smart_filter.rb
6
+ smart_filter.gemspec
@@ -0,0 +1,63 @@
1
+ = SmartFilter
2
+
3
+ Rails gem to extend ActiveRecord with a smart_filter method.
4
+
5
+
6
+ == Installation
7
+
8
+ [As a gem (using Bundler)] gem "smart_filter", "~> 0.1.0" # hosted on rubygems.com
9
+ [As a plugin] script/plugin install git://github.com/vvohra87/smart_filter.git
10
+
11
+
12
+ == Why smart_filter?
13
+
14
+ In most applications, I need the following types of search / filter options:
15
+ 1. %LIKE% matches for any text or string field such as "name", "description", "email", "address", etc
16
+ 2. Exact matches for any integer, date or datetime field such as "quantity", "id", "created_at", "start", "age", etc
17
+ 3. Range matches for any integer, decimal, float, date or datetime such as "price", "age", "created_at", etc
18
+
19
+ SmartFilter simply provides a simple way to do just that.
20
+
21
+ This is not meant to replace gems like meta-where / arel-table based query builders / search engines, just make life easier by having to write less code! (and apparently more documentation!)
22
+
23
+
24
+ == How it works
25
+
26
+ Once you have included the gem in your app, you now have a +smart_filter+ method for all your models.
27
+
28
+ The method takes in a hash whose keys are dependent on the model definition.
29
+
30
+ For numeric and datetime based columns it will accept 3 different inputs:
31
+ 1. column => value # will return exact match for value
32
+ 2. column_min => value # will return all records where attribute has value greater than or equal to specified value
33
+ 3. column_max => value # will return all records where attribute has value less than or equal to specified value
34
+
35
+ For string and text based columns it will accept only the column name.
36
+
37
+
38
+ == Example
39
+
40
+ Let's say that you have a Product model with the following attributes:
41
+
42
+ title: string
43
+ description: text
44
+ price: float
45
+ current_stock: integer
46
+ created_at: datetime
47
+
48
+
49
+ So, on this the full set of options you would have is:
50
+
51
+ Product.smart_filter(
52
+ :title => "abc" # where the title contains 'abc'
53
+ :description => "cba" # where the description contains 'cba'
54
+ :price => 5.0 # where the price is exactly 5.0
55
+ :price_min => 10.0 # where the price is greater than or equal to 10.0
56
+ :price_max => 20.0 # where the price is less than or equal to 20.0
57
+ :current_stock => 55 # where the current stock is exactly 55
58
+ :current_stock_min => 20 # .....etc.....
59
+ :current_stock_max => 100 # .....etc.....
60
+ :created_at => Time.now
61
+ :created_at_min => Time.now - 1.week
62
+ :created_at_max => Time.now - 2.days
63
+ )
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('smart_filter', '0.1.1') do |p|
6
+ p.description = "Extend Active Record to provide a smart filter function."
7
+ p.url = "http://github.com/vvohra87/smart_filter"
8
+ p.author = "Varun Vohra"
9
+ p.email = "varun@genii.in"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'smart_filter'
@@ -0,0 +1,46 @@
1
+ module SmartFilter
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+
8
+ def smart_filter(args = {})
9
+ numerics = []
10
+ strings = []
11
+ self.columns.each do |column|
12
+ case column.type
13
+ when :integer, :decimal, :float, :date, :datetime
14
+ numerics.push(column.name.to_sym)
15
+ numerics.push((column.name.to_s + "_min").to_sym)
16
+ numerics.push((column.name.to_s + "_max").to_sym)
17
+ when :string, :text
18
+ strings.push(column.name.to_sym)
19
+ end
20
+ end
21
+
22
+ results = self.select("*")
23
+
24
+ args.each do |key, value|
25
+ if numerics.include?(key)
26
+ if key.ends_with("_min")
27
+ results = results.where("#{key} >= #{value}")
28
+ elsif key.ends_with("_max")
29
+ results = results.where("#{key} <= #{value}")
30
+ else
31
+ results = results.where("#{key} = #{value}")
32
+ end
33
+ end
34
+ if strings.include?(key)
35
+ results = results.where("#{key} like '%#{value}%'")
36
+ end
37
+ end
38
+ results
39
+ end
40
+
41
+ end
42
+ end
43
+
44
+ class ActiveRecord::Base
45
+ include SmartFilter
46
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "smart_filter"
5
+ s.version = "0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Varun Vohra"]
9
+ s.date = "2012-01-31"
10
+ s.description = "Extend Active Record to provide a smart filter function."
11
+ s.email = "varun@genii.in"
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/smart_filter.rb"]
13
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "init.rb", "lib/smart_filter.rb", "smart_filter.gemspec"]
14
+ s.homepage = "http://github.com/vvohra87/smart_filter"
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Smart_filter", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = "smart_filter"
18
+ s.rubygems_version = "1.8.13"
19
+ s.summary = "Extend Active Record to provide a smart filter function."
20
+
21
+ if s.respond_to? :specification_version then
22
+ s.specification_version = 3
23
+
24
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
+ else
26
+ end
27
+ else
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart_filter
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Varun Vohra
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-31 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Extend Active Record to provide a smart filter function.
22
+ email: varun@genii.in
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.rdoc
29
+ - lib/smart_filter.rb
30
+ files:
31
+ - Manifest
32
+ - README.rdoc
33
+ - Rakefile
34
+ - init.rb
35
+ - lib/smart_filter.rb
36
+ - smart_filter.gemspec
37
+ homepage: http://github.com/vvohra87/smart_filter
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --line-numbers
43
+ - --inline-source
44
+ - --title
45
+ - Smart_filter
46
+ - --main
47
+ - README.rdoc
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 11
65
+ segments:
66
+ - 1
67
+ - 2
68
+ version: "1.2"
69
+ requirements: []
70
+
71
+ rubyforge_project: smart_filter
72
+ rubygems_version: 1.8.13
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Extend Active Record to provide a smart filter function.
76
+ test_files: []
77
+