filter_fu 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +2 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +113 -0
- data/LICENSE +1 -1
- data/README.rdoc +37 -22
- data/Rakefile +24 -51
- data/lib/filter_fu/active_record.rb +17 -20
- data/lib/filter_fu/view_helper.rb +32 -19
- data/lib/filter_fu.rb +19 -3
- data/spec/active_record_spec.rb +48 -49
- data/spec/db/database.yml +1 -1
- data/spec/db/debug.log +42 -0
- data/spec/db/schema.rb +2 -2
- data/spec/filter_fu_spec.rb +4 -4
- data/spec/helpers/view_helper_spec.rb +119 -0
- data/spec/spec_helper.rb +28 -13
- data/spec/support/dummies.rb +13 -0
- data/spec/{fixtures → support}/employee.rb +3 -5
- metadata +126 -35
- data/.document +0 -5
- data/.gitignore +0 -6
- data/VERSION +0 -1
- data/filter_fu.gemspec +0 -68
- data/rails/init.rb +0 -1
- data/spec/dummies.rb +0 -46
- data/spec/spec.opts +0 -4
- data/spec/view_helper_spec.rb +0 -127
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
filter_fu (0.6.0)
|
5
|
+
rails (~> 3.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ZenTest (4.4.1)
|
11
|
+
abstract (1.0.0)
|
12
|
+
actionmailer (3.0.3)
|
13
|
+
actionpack (= 3.0.3)
|
14
|
+
mail (~> 2.2.9)
|
15
|
+
actionpack (3.0.3)
|
16
|
+
activemodel (= 3.0.3)
|
17
|
+
activesupport (= 3.0.3)
|
18
|
+
builder (~> 2.1.2)
|
19
|
+
erubis (~> 2.6.6)
|
20
|
+
i18n (~> 0.4)
|
21
|
+
rack (~> 1.2.1)
|
22
|
+
rack-mount (~> 0.6.13)
|
23
|
+
rack-test (~> 0.5.6)
|
24
|
+
tzinfo (~> 0.3.23)
|
25
|
+
activemodel (3.0.3)
|
26
|
+
activesupport (= 3.0.3)
|
27
|
+
builder (~> 2.1.2)
|
28
|
+
i18n (~> 0.4)
|
29
|
+
activerecord (3.0.3)
|
30
|
+
activemodel (= 3.0.3)
|
31
|
+
activesupport (= 3.0.3)
|
32
|
+
arel (~> 2.0.2)
|
33
|
+
tzinfo (~> 0.3.23)
|
34
|
+
activeresource (3.0.3)
|
35
|
+
activemodel (= 3.0.3)
|
36
|
+
activesupport (= 3.0.3)
|
37
|
+
activesupport (3.0.3)
|
38
|
+
arel (2.0.6)
|
39
|
+
autotest (4.4.6)
|
40
|
+
ZenTest (>= 4.4.1)
|
41
|
+
builder (2.1.2)
|
42
|
+
diff-lcs (1.1.2)
|
43
|
+
erubis (2.6.6)
|
44
|
+
abstract (>= 1.0.0)
|
45
|
+
haml (2.2.24)
|
46
|
+
hanna (0.1.12)
|
47
|
+
haml (~> 2.2.8)
|
48
|
+
rake (~> 0.8.2)
|
49
|
+
rdoc (~> 2.3.0)
|
50
|
+
i18n (0.5.0)
|
51
|
+
mail (2.2.12)
|
52
|
+
activesupport (>= 2.3.6)
|
53
|
+
i18n (>= 0.4.0)
|
54
|
+
mime-types (~> 1.16)
|
55
|
+
treetop (~> 1.4.8)
|
56
|
+
mime-types (1.16)
|
57
|
+
nokogiri (1.4.4)
|
58
|
+
polyglot (0.3.1)
|
59
|
+
rack (1.2.1)
|
60
|
+
rack-mount (0.6.13)
|
61
|
+
rack (>= 1.0.0)
|
62
|
+
rack-test (0.5.6)
|
63
|
+
rack (>= 1.0)
|
64
|
+
rails (3.0.3)
|
65
|
+
actionmailer (= 3.0.3)
|
66
|
+
actionpack (= 3.0.3)
|
67
|
+
activerecord (= 3.0.3)
|
68
|
+
activeresource (= 3.0.3)
|
69
|
+
activesupport (= 3.0.3)
|
70
|
+
bundler (~> 1.0)
|
71
|
+
railties (= 3.0.3)
|
72
|
+
railties (3.0.3)
|
73
|
+
actionpack (= 3.0.3)
|
74
|
+
activesupport (= 3.0.3)
|
75
|
+
rake (>= 0.8.7)
|
76
|
+
thor (~> 0.14.4)
|
77
|
+
rake (0.8.7)
|
78
|
+
rdoc (2.3.0)
|
79
|
+
rspec (2.2.0)
|
80
|
+
rspec-core (~> 2.2)
|
81
|
+
rspec-expectations (~> 2.2)
|
82
|
+
rspec-mocks (~> 2.2)
|
83
|
+
rspec-core (2.2.1)
|
84
|
+
rspec-expectations (2.2.0)
|
85
|
+
diff-lcs (~> 1.1.2)
|
86
|
+
rspec-mocks (2.2.0)
|
87
|
+
rspec-rails (2.2.1)
|
88
|
+
actionpack (~> 3.0)
|
89
|
+
activesupport (~> 3.0)
|
90
|
+
railties (~> 3.0)
|
91
|
+
rspec (~> 2.2.0)
|
92
|
+
sqlite3-ruby (1.3.2)
|
93
|
+
thor (0.14.6)
|
94
|
+
treetop (1.4.9)
|
95
|
+
polyglot (>= 0.3.1)
|
96
|
+
tzinfo (0.3.23)
|
97
|
+
webrat (0.7.2)
|
98
|
+
nokogiri (>= 1.2.0)
|
99
|
+
rack (>= 1.0)
|
100
|
+
rack-test (>= 0.5.3)
|
101
|
+
|
102
|
+
PLATFORMS
|
103
|
+
ruby
|
104
|
+
|
105
|
+
DEPENDENCIES
|
106
|
+
autotest (>= 4.3.2)
|
107
|
+
filter_fu!
|
108
|
+
hanna (>= 0.1.12)
|
109
|
+
rails (~> 3.0.0)
|
110
|
+
rspec (~> 2.0)
|
111
|
+
rspec-rails (~> 2.0)
|
112
|
+
sqlite3-ruby
|
113
|
+
webrat (>= 0.7.2)
|
data/LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= filter_fu
|
2
2
|
|
3
|
-
This Ruby on Rails plugin adds a filtered_by method to your models. It accepts a hash of filters that are applied using
|
3
|
+
This Ruby on Rails plugin adds a <tt>.filtered_by</tt> method to your models. It accepts a hash of filters that are applied using scopes. In addition the plugin adds some view helpers to easily build filter forms.
|
4
4
|
|
5
5
|
== Install
|
6
6
|
|
@@ -8,15 +8,17 @@ You can install filter_fu as a gem or as a rails plugin.
|
|
8
8
|
|
9
9
|
=== Install as gem
|
10
10
|
|
11
|
-
|
11
|
+
To install the filter_fu gem, simply add it to your Gemfile:
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
=== Install as plugin
|
13
|
+
gem 'filter_fu'
|
16
14
|
|
17
|
-
|
15
|
+
In order to get the latest development version of filter_fu:
|
18
16
|
|
19
|
-
|
17
|
+
gem 'filter_fu', :git => 'git://github.com/benedikt/filter_fu.git'
|
18
|
+
|
19
|
+
and run
|
20
|
+
|
21
|
+
bundle install
|
20
22
|
|
21
23
|
== Usage
|
22
24
|
|
@@ -24,52 +26,65 @@ Run this command in your currrent project's +RAILS_ROOT+
|
|
24
26
|
|
25
27
|
To enable filter_fu on a model, simply add one line to its definition:
|
26
28
|
|
27
|
-
class Project
|
29
|
+
class Project < ActiveRecord::Base
|
28
30
|
filter_fu
|
29
31
|
end
|
30
|
-
|
32
|
+
|
31
33
|
Let's say you don't want your Projects list filtered by its +hidden+ column. You can easly
|
32
34
|
tell filter_fu to ignore certian named scopes or columns:
|
33
|
-
|
34
|
-
class Project
|
35
|
+
|
36
|
+
class Project < ActiveRecord::Base
|
35
37
|
filter_fu :except => [:hidden]
|
36
38
|
end
|
37
39
|
|
38
|
-
Of course this also works the other way round using the <tt>:only</tt> option to only allow filtering
|
40
|
+
Of course this also works the other way round using the <tt>:only</tt> option to only allow filtering
|
39
41
|
for the given named scopes or columns:
|
40
42
|
|
41
|
-
class Project
|
43
|
+
class Project < ActiveRecord::Base
|
42
44
|
filter_fu :only => [:starts_on, :ends_on]
|
43
45
|
end
|
44
|
-
|
46
|
+
|
45
47
|
Once you enabled filter_fu on your model it provides a filtered_by method.
|
46
48
|
|
47
49
|
Project.filtered_by(params[:filter])
|
48
50
|
Project.filtered_by(params[:filter]).some.named_scopes
|
49
|
-
Project.some.other.
|
50
|
-
|
51
|
+
Project.some.other.scopes.filtered_by(params[:filter])
|
52
|
+
|
51
53
|
|
52
54
|
=== Helper
|
53
55
|
|
54
56
|
filter_fu comes with helpers to simplify the generation of filter forms:
|
55
57
|
|
56
|
-
|
58
|
+
<%= filter_form do |f| %>
|
57
59
|
# f is a ActionView::Helpers::FormBuilder
|
58
60
|
<% end %>
|
59
61
|
|
60
62
|
You can also specify a name for the filter (Default is <tt>:filter</tt>). This way you're able to have multiple filters on the same page.
|
61
63
|
|
62
|
-
|
64
|
+
<%= filter_form_for(:product_filter) do |f| %>
|
63
65
|
# f is a ActionView::Helpers::FormBuilder
|
64
|
-
<% end %>
|
66
|
+
<% end %>
|
65
67
|
|
66
|
-
filter_fu will automatically include all other parameters for the current page (ie. pagination parameters) within the form.
|
68
|
+
filter_fu will automatically include all other parameters for the current page (ie. pagination parameters) within the form.
|
67
69
|
If you wish to explicitly exclude some you can do this by passing an array as :ignore_parameters option.
|
68
70
|
|
69
|
-
|
71
|
+
<%= filter_form_for(:product_filter, :ignore_parameters => [:page]) do |f| %>
|
70
72
|
# f is a ActionView::Helpers::FormBuilder
|
71
73
|
<% end %>
|
72
74
|
|
75
|
+
It's possible to add default values for <tt>:ignore_parameters</tt>. Simply add something like this to your initializers:
|
76
|
+
|
77
|
+
config.filter_fu.ignore_parameters = [:param_to_ignore]
|
78
|
+
|
79
|
+
== Known issues
|
80
|
+
|
81
|
+
See http://github.com/benedikt/filter_fu/issues
|
82
|
+
|
83
|
+
== Repository
|
84
|
+
|
85
|
+
See http://github.com/benedikt/filter_fu and feel free to fork it!
|
86
|
+
|
87
|
+
|
73
88
|
== Copyright
|
74
89
|
|
75
|
-
Copyright (c) 2009 Benedikt Deicke. See LICENSE for details.
|
90
|
+
Copyright (c) 2009, 2010 Benedikt Deicke. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -1,60 +1,33 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
1
|
+
require 'rspec/core'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
require 'hanna/rdoctask'
|
3
4
|
|
4
|
-
|
5
|
-
require 'jeweler'
|
6
|
-
Jeweler::Tasks.new do |gem|
|
7
|
-
gem.name = "filter_fu"
|
8
|
-
gem.summary = %Q{Filter ActiveRecord models using named_scopes}
|
9
|
-
gem.description = %Q{This Ruby on Rails plugin adds a filtered_by method to your models. It accepts a hash of filters that are applied using named_scopes. In addition the plugin adds some view helpers to easily build filter forms.}
|
10
|
-
gem.email = "benedikt@synatic.net"
|
11
|
-
gem.homepage = "http://github.com/benedikt/filter_fu"
|
12
|
-
gem.authors = ["Benedikt Deicke"]
|
13
|
-
gem.add_development_dependency "rspec"
|
14
|
-
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
-
end
|
16
|
-
Jeweler::GemcutterTasks.new
|
17
|
-
rescue LoadError
|
18
|
-
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
-
end
|
5
|
+
spec = Gem::Specification.load("filter_fu.gemspec")
|
20
6
|
|
21
|
-
|
22
|
-
|
23
|
-
spec.libs << 'lib' << 'spec'
|
24
|
-
spec.spec_opts = ['--options', 'spec/spec.opts']
|
25
|
-
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
-
end
|
7
|
+
RSpec::Core::RakeTask.new
|
8
|
+
task :default => :spec
|
27
9
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
10
|
+
Rake::RDocTask.new do |rdoc|
|
11
|
+
rdoc.rdoc_dir = 'doc'
|
12
|
+
rdoc.title = "#{spec.name} #{spec.version}"
|
13
|
+
rdoc.options += spec.rdoc_options
|
14
|
+
rdoc.rdoc_files.include(spec.extra_rdoc_files)
|
15
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
16
|
+
end
|
35
17
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
18
|
+
desc "Generates a sandbox Rails app for testing"
|
19
|
+
namespace :spec do
|
20
|
+
task "sandbox" do
|
21
|
+
system "mkdir -p tmp/ && bundle exec rails new tmp/sandbox --skip-gemfile"
|
40
22
|
end
|
41
23
|
end
|
42
24
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
require 'rake/rdoctask'
|
48
|
-
Rake::RDocTask.new do |rdoc|
|
49
|
-
if File.exist?('VERSION')
|
50
|
-
version = File.read('VERSION')
|
51
|
-
else
|
52
|
-
version = ""
|
53
|
-
end
|
25
|
+
desc "Build the .gem file"
|
26
|
+
task :build do
|
27
|
+
system "gem build #{spec.name}.gemspec"
|
28
|
+
end
|
54
29
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
rdoc.rdoc_files.include('LICENSE*')
|
59
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
30
|
+
desc "Push the .gem file to rubygems.org"
|
31
|
+
task :release => :build do
|
32
|
+
system "gem push #{spec.name}-#{spec.version}.gem"
|
60
33
|
end
|
@@ -1,34 +1,31 @@
|
|
1
1
|
module FilterFu
|
2
2
|
module ActiveRecord
|
3
|
-
|
4
|
-
|
5
|
-
base.extend(ClassMethods)
|
6
|
-
end
|
7
|
-
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
8
5
|
module ClassMethods
|
9
|
-
|
6
|
+
|
10
7
|
VALID_FILTER_OPTIONS = [:only, :except]
|
11
|
-
|
8
|
+
|
12
9
|
def filter_fu(opts = {})
|
13
10
|
opts = opts.symbolize_keys!
|
14
11
|
opts.each_key { |option| raise "Invalid filter_fu option: #{option}" unless VALID_FILTER_OPTIONS.include?(option) }
|
15
12
|
raise "Use either :only or :except as a filter_fu option." if opts.has_key?(:only) && opts.has_key?(:except)
|
16
|
-
|
13
|
+
|
17
14
|
opts[:only] = [opts[:only]].flatten.collect(&:to_sym) if opts[:only]
|
18
15
|
opts[:except] = [opts[:except]].flatten.collect(&:to_sym) if opts[:except]
|
19
|
-
|
16
|
+
|
20
17
|
@filter_options = opts
|
21
|
-
|
18
|
+
|
22
19
|
extend SingletonMethods
|
23
20
|
end
|
24
|
-
|
21
|
+
|
25
22
|
end
|
26
|
-
|
23
|
+
|
27
24
|
module SingletonMethods
|
28
|
-
|
25
|
+
|
29
26
|
def filtered_by(filter)
|
30
27
|
return scoped({}) if !filter || filter.empty?
|
31
|
-
|
28
|
+
|
32
29
|
filter.inject(self) do |memo, (scope, arg)|
|
33
30
|
scope = scope.to_sym
|
34
31
|
next if protected?(scope)
|
@@ -39,14 +36,14 @@ module FilterFu
|
|
39
36
|
end
|
40
37
|
end || scoped({})
|
41
38
|
end
|
42
|
-
|
39
|
+
|
43
40
|
private
|
44
|
-
|
41
|
+
|
45
42
|
def build_anonymous_scope(scope, arg)
|
46
43
|
return {} unless column_names.include?(scope.to_s) && !arg.blank?
|
47
44
|
{ :conditions => { scope => arg } }
|
48
45
|
end
|
49
|
-
|
46
|
+
|
50
47
|
def protected?(scope)
|
51
48
|
if @filter_options.has_key?(:only)
|
52
49
|
return !@filter_options[:only].include?(scope)
|
@@ -55,8 +52,8 @@ module FilterFu
|
|
55
52
|
end
|
56
53
|
return false
|
57
54
|
end
|
58
|
-
|
55
|
+
|
59
56
|
end
|
60
|
-
|
57
|
+
|
61
58
|
end
|
62
|
-
end
|
59
|
+
end
|
@@ -1,50 +1,63 @@
|
|
1
1
|
require 'ostruct'
|
2
2
|
|
3
3
|
module FilterFu
|
4
|
+
|
5
|
+
class FilterModel < OpenStruct
|
6
|
+
extend ActiveModel::Naming
|
7
|
+
extend ActiveModel::Translation
|
8
|
+
|
9
|
+
def initialize(*args)
|
10
|
+
super
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
4
14
|
module ViewHelper
|
5
|
-
|
15
|
+
|
6
16
|
def self.included(base) # :nodoc:
|
7
17
|
base.send :include, InstanceMethods
|
8
18
|
end
|
9
|
-
|
19
|
+
|
10
20
|
module InstanceMethods
|
11
|
-
|
21
|
+
|
12
22
|
def filter_form_for(*args, &block)
|
13
23
|
raise ArgumentError, 'Missing block' unless block_given?
|
14
|
-
|
24
|
+
|
15
25
|
opts = args.extract_options!
|
16
26
|
name = (args.first || :filter).to_sym
|
17
|
-
|
27
|
+
|
18
28
|
opts[:ignore_parameters] ||= []
|
19
|
-
opts[:ignore_parameters] += [:controller, :action, name]
|
20
|
-
|
29
|
+
opts[:ignore_parameters] += FilterFu.ignore_parameters + [:controller, :action, name]
|
30
|
+
opts[:ignore_parameters].uniq!
|
31
|
+
|
21
32
|
opts[:html] ||= {}
|
22
33
|
opts[:html][:method] ||= :get
|
23
|
-
|
24
|
-
|
34
|
+
opts[:as] = name
|
35
|
+
opts[:url] = {}
|
36
|
+
|
37
|
+
form_for(FilterFu::FilterModel.new(params[name]), opts) do |f|
|
25
38
|
hidden_fields_for(params, opts)
|
26
39
|
block.call(f)
|
27
40
|
end
|
28
41
|
end
|
29
42
|
alias_method :filter_form, :filter_form_for
|
30
|
-
|
43
|
+
|
31
44
|
private
|
32
|
-
|
45
|
+
|
33
46
|
def hidden_fields_for(params, opts, prefix = nil)
|
34
|
-
params.each_pair do |k, v|
|
47
|
+
params.each_pair do |k, v|
|
35
48
|
next if opts[:ignore_parameters].include?(k.to_sym)
|
36
|
-
|
49
|
+
|
37
50
|
k = "[#{k}]" if prefix
|
38
|
-
|
51
|
+
|
39
52
|
if v.kind_of?(Hash)
|
40
53
|
hidden_fields_for(v, opts, "#{prefix}#{k}")
|
41
|
-
else
|
42
|
-
concat(hidden_field_tag("#{prefix}#{k}", v))
|
54
|
+
else
|
55
|
+
concat(hidden_field_tag("#{prefix}#{k}", v))
|
43
56
|
end
|
44
57
|
end
|
45
58
|
end
|
46
|
-
|
59
|
+
|
47
60
|
end
|
48
|
-
|
61
|
+
|
49
62
|
end
|
50
|
-
end
|
63
|
+
end
|
data/lib/filter_fu.rb
CHANGED
@@ -1,4 +1,20 @@
|
|
1
|
-
|
1
|
+
require 'filter_fu/active_record'
|
2
|
+
require 'filter_fu/view_helper'
|
2
3
|
|
3
|
-
|
4
|
-
|
4
|
+
module FilterFu
|
5
|
+
mattr_accessor :ignore_parameters
|
6
|
+
@@ignore_parameters = []
|
7
|
+
|
8
|
+
class Railtie < Rails::Railtie
|
9
|
+
config.filter_fu = FilterFu
|
10
|
+
|
11
|
+
initializer "filter_fu.initialize" do |app|
|
12
|
+
::ActiveRecord::Base.send :include, FilterFu::ActiveRecord if defined? ::ActiveRecord::Base
|
13
|
+
::ActionView::Base.send :include, FilterFu::ViewHelper if defined? ::ActionView::Base
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.configure
|
18
|
+
yield self
|
19
|
+
end
|
20
|
+
end
|