has_filter 0.0.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/Gemfile +9 -0
- data/MIT-LICENSE +20 -0
- data/README.md +44 -0
- data/Rakefile +29 -0
- data/lib/has_filter.rb +53 -0
- metadata +50 -0
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 Jonathan Duarte
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
= HasFilter
|
2
|
+
|
3
|
+
Active Record filter conditions
|
4
|
+
|
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
|
+
|
7
|
+
|
8
|
+
== Getting started
|
9
|
+
|
10
|
+
Install - Gemfile
|
11
|
+
|
12
|
+
`gem has_filter`
|
13
|
+
|
14
|
+
|
15
|
+
`$ bundle install`
|
16
|
+
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
class Post < ActiveRecord::Base
|
20
|
+
has_filter
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
And now you can use:
|
25
|
+
|
26
|
+
```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%)
|
30
|
+
```
|
31
|
+
You can also specify what attributes should be filtered
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class Post < ActiveRecord::Base
|
35
|
+
has_filter :title #=> It will only filter by title conditions
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
```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
|
42
|
+
```
|
43
|
+
|
44
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rubygems'
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'rake'
|
10
|
+
require 'rdoc/task'
|
11
|
+
|
12
|
+
require 'rake/testtask'
|
13
|
+
|
14
|
+
Rake::TestTask.new(:test) do |t|
|
15
|
+
t.libs << 'lib'
|
16
|
+
t.libs << 'test'
|
17
|
+
t.pattern = 'test/**/*_test.rb'
|
18
|
+
t.verbose = false
|
19
|
+
end
|
20
|
+
|
21
|
+
task :default => :test
|
22
|
+
|
23
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
24
|
+
rdoc.rdoc_dir = 'rdoc'
|
25
|
+
rdoc.title = 'HasFilter'
|
26
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
27
|
+
rdoc.rdoc_files.include('README.rdoc')
|
28
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
29
|
+
end
|
data/lib/has_filter.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module HasFilter
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def has_filter(allowed_fields = nil)
|
10
|
+
@allowed_fields = *allowed_fields
|
11
|
+
end
|
12
|
+
|
13
|
+
def filtering(conditions)
|
14
|
+
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
|
28
|
+
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
|
37
|
+
end
|
38
|
+
find(:all, *filters)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def hash_conditions(key, value)
|
44
|
+
{ :conditions => { key => value } }
|
45
|
+
end
|
46
|
+
|
47
|
+
def like_conditions(key, value)
|
48
|
+
{ :conditions => ["#{key.to_s} like ?", "%#{value}%"] }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
ActiveRecord::Base.send(:include, HasFilter)
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_filter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jonathan Duarte
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-10 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Active Record filter conditions
|
15
|
+
email:
|
16
|
+
- jonathan.duarte@rocketmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/has_filter.rb
|
22
|
+
- MIT-LICENSE
|
23
|
+
- Rakefile
|
24
|
+
- Gemfile
|
25
|
+
- README.md
|
26
|
+
homepage: https://github.com/jhonnyquest/has_filter
|
27
|
+
licenses: []
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 1.8.10
|
47
|
+
signing_key:
|
48
|
+
specification_version: 3
|
49
|
+
summary: Active Record filter conditions.
|
50
|
+
test_files: []
|