search_fu 0.0.2 → 0.0.3

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.
@@ -3,34 +3,35 @@ module SearchFu
3
3
  def self.included(base)
4
4
  base.extend ClassMethods
5
5
  base.class_eval do
6
- class_attribute :_search_fu_attributes
7
- self._search_fu_attributes = []
6
+ attr_accessor :_search_fu_attributes
8
7
  end
9
8
  end
10
9
 
11
10
  module ClassMethods
12
11
  def search(hash)
13
- return self.where("1=1") unless hash
12
+ rtnval = self.scopes[:search_fu_before] ? self.search_fu_before : self.where("1=1")
13
+ return rtnval unless hash
14
14
  @rel = [%q(where("1=1"))]
15
15
  hash.each do |k,v|
16
- search_fu_attr = self._search_fu_attributes.detect { |t| t[:name] == v[:name]}
16
+ search_fu_attr = @_search_fu_attributes.detect { |t| t[:name] == v[:name]}
17
17
  raise "Column '#{v[:name]}' not found." unless search_fu_attr
18
18
  @rel << generate_where(search_fu_attr, v[:value])
19
19
  end
20
- self.instance_eval(@rel.join('.'))
20
+ rtnval.instance_eval(@rel.join('.'))
21
21
  end
22
22
 
23
23
  def search_fu_names
24
24
  a = []
25
- self._search_fu_attributes.each do |h|
25
+ @_search_fu_attributes.each do |h|
26
26
  a << h[:name]
27
27
  end
28
28
  a
29
29
  end
30
30
 
31
31
  def search_fu(name, hash)
32
+ @_search_fu_attributes ||= []
32
33
  c_attr = create_search_fu_attribute_hash(name, hash)
33
- self._search_fu_attributes << c_attr unless self._search_fu_attributes.detect { |t| t[:name] == c_attr[:name]}
34
+ @_search_fu_attributes << c_attr unless @_search_fu_attributes.detect { |t| t[:name] == c_attr[:name]}
34
35
  end
35
36
 
36
37
  private
@@ -41,7 +42,11 @@ module SearchFu
41
42
  return %q(where("1=1")) if a[1].blank?
42
43
  val = assign_like(op, a[1])
43
44
  val = attr[:convert].call(op, a[1]) if attr[:convert]
44
- w = attr[:ar_query]
45
+ if attr[:ar_query].class == Proc
46
+ w = attr[:ar_query].call
47
+ else
48
+ w = attr[:ar_query]
49
+ end
45
50
  w.gsub(/<op>/, op).gsub(/<value>/, val)
46
51
  end
47
52
 
@@ -74,6 +79,7 @@ module SearchFu
74
79
  end
75
80
  end
76
81
 
77
- if defined?(ActiveRecord::Base)
78
- ActiveRecord::Base.send :include, SearchFu::ModelAdditions
79
- end
82
+ #if defined?(ActiveRecord::Base)
83
+ #ActiveRecord::Base.send :include, SearchFu::ModelAdditions
84
+ #end
85
+
@@ -1,3 +1,3 @@
1
1
  module SearchFu
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -4,9 +4,11 @@ module FilterFu
4
4
  module ViewHelper
5
5
  def search_fu_form_for(model_klass, options={})
6
6
  hidden_search_fields(model_klass) +
7
- form_tag(polymorphic_path(Payment), options.merge(method: :get, id: 'search_fu_form')) do
7
+ form_tag(polymorphic_path(model_klass), options.merge(method: :get, id: 'search_fu_form')) do
8
8
  filter_table model_klass
9
- end
9
+ end +
10
+ javascript_tag("add_remove_row.bindAddLink('#search_fu_add', '#hidden_search_fields', '#search_fu_tbody');") +
11
+ javascript_tag("add_remove_row.bindRemoveLink('.search_fu_remove', '.search_fu_rows');")
10
12
  end
11
13
 
12
14
  def filter_table(model_klass)
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe FilterFu do
3
+ describe SearchFu do
4
4
  before(:each) {
5
- Payment._search_fu_attributes = []
5
+ # Payment._search_fu_attributes = []
6
6
  @err_attr2 = ['Error', { operator: '=' }]
7
7
  @pay_attr1 = ['Collector', { ar_query: %q(where("payments.collector <op> '<value>'")), operator: 'like'}]
8
8
  @pay_attr2 = ['Pay to', { ar_query: %q(where("accounts.name1 <op> '<value>' or accounts.name2 <op> '<value>'").joins(:to_account)),
@@ -13,7 +13,7 @@ describe FilterFu do
13
13
  operator: '=', convert: Proc.new {|op, t| Date.parse(t.to_s).to_s } } ]
14
14
  }
15
15
 
16
- describe "#filter" do
16
+ describe "#search_fu" do
17
17
  before(:each) do
18
18
  Payment.search_fu @pay_attr1[0], @pay_attr1[1]
19
19
  Payment.search_fu @pay_attr2[0], @pay_attr2[1]
@@ -81,13 +81,13 @@ describe FilterFu do
81
81
  it "should add #search_fu params to _search_fu_attributes" do
82
82
  Payment.search_fu(@pay_attr1[0], @pay_attr1[1])
83
83
  Payment.search_fu(@pay_attr2[0], @pay_attr2[1])
84
- Payment._search_fu_attributes.count.should == 2
84
+ Payment.search_fu_names.count.should == 2
85
85
  end
86
86
 
87
87
  it "#search_fu should not duplicated :name attr to _search_fu_attributes" do
88
88
  Payment.search_fu(@pay_attr1[0], @pay_attr1[1])
89
89
  Payment.search_fu(@pay_attr1[0], @pay_attr1[1])
90
- Payment._search_fu_attributes.count.should == 1
90
+ Payment.search_fu_names.count == 1
91
91
  end
92
92
 
93
93
  it "#search_fu should raise error if :ar_query in nil" do
@@ -95,3 +95,4 @@ describe FilterFu do
95
95
  end
96
96
 
97
97
  end
98
+
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: search_fu
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Tan Kwang How
@@ -10,8 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-16 00:00:00 +08:00
14
- default_executable:
13
+ date: 2011-05-05 00:00:00 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: rspec
@@ -60,22 +59,15 @@ files:
60
59
  - Gemfile
61
60
  - Rakefile
62
61
  - javascript/add_remove_row.js
63
- - lib/filter_fu.rb
64
- - lib/filter_fu/controller_additions.rb
65
- - lib/filter_fu/model_additions.rb
66
- - lib/filter_fu/version.rb
67
- - lib/filter_fu/view_helper.rb
68
62
  - lib/search_fu.rb
69
63
  - lib/search_fu/controller_additions.rb
70
64
  - lib/search_fu/model_additions.rb
71
65
  - lib/search_fu/version.rb
72
66
  - lib/search_fu/view_helper.rb
73
67
  - search_fu.gemspec
74
- - spec/filter_fu_spec.rb
75
68
  - spec/schema.rb
76
69
  - spec/search_fu_spec.rb
77
70
  - spec/spec_helper.rb
78
- has_rdoc: true
79
71
  homepage: http://github.com/tankwanghow
80
72
  licenses: []
81
73
 
@@ -99,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
91
  requirements: []
100
92
 
101
93
  rubyforge_project: search_fu
102
- rubygems_version: 1.5.2
94
+ rubygems_version: 1.8.0
103
95
  signing_key:
104
96
  specification_version: 3
105
97
  summary: Combine query in views
@@ -1,12 +0,0 @@
1
- module Filterable
2
- module ControllerAdditions
3
- def store_filterable_session
4
- session["#{self.class.name.sub(/Controller/, '').underscore.singularize}_filterable"] = params[:filterable] if params[:filterable_commit]
5
- end
6
- end
7
- end
8
-
9
- if defined?(ActionController::Base)
10
- ActionController::Base.send :include, Filterable::ControllerAdditions
11
- end
12
-
@@ -1,39 +0,0 @@
1
- module FilterFu
2
- module ModelAdditions
3
- def self.included(base)
4
- base.extend ClassMethods
5
- base.class_eval do
6
- class_attribute :_filter_fu_attributes
7
- self._filter_fu_attributes = []
8
- end
9
- end
10
-
11
- module ClassMethods
12
- def filter_fu(*args)
13
- args.each do |attr|
14
- c_attr = create_filter_fu_attribute_hash(attr)
15
- self._filter_fu_attributes << c_attr unless self._filter_fu_attributes.detect { |t| t[:name] == c_attr[:name]}
16
- end
17
- end
18
-
19
- private
20
-
21
- def create_filter_fu_attribute_hash(attr)
22
- raise "Parameter has no :name key" unless attr[:name]
23
- raise "Parameter has no :where key" unless attr[:where]
24
- { name: attr[:name],
25
- where: attr[:where],
26
- operator: attr[:operator] || :eq,
27
- convert: attr[:convert],
28
- joins: attr[:joins],
29
- and: attr[:and] }
30
- end
31
-
32
- end
33
-
34
- end
35
- end
36
-
37
- if defined?(ActiveRecord::Base)
38
- ActiveRecord::Base.send :include, FilterFu::ModelAdditions
39
- end
@@ -1,3 +0,0 @@
1
- module FilterFu
2
- VERSION = "0.0.1"
3
- end
@@ -1,63 +0,0 @@
1
- require 'action_view'
2
-
3
- module FilterFu
4
- module ViewHelper
5
- def filter_fu_form_for(model_klass, options={})
6
- hidden_filter_fields(model_klass) +
7
- form_tag(polymorphic_path(Payment), options.merge(method: :get, id: 'filter_fu_form')) do
8
- filter_table model_klass
9
- end
10
- end
11
-
12
- def filter_table(model_klass)
13
- content_tag(:table, :id => 'filter_fu_table') do
14
- content_tag(:thead, filter_header, :id => "filter_fu_thead") +
15
- content_tag(:tbody, render_fields(model_klass),:id => "filter_fu_tbody") +
16
- content_tag(:tfoot, filter_footer, :id => "filter_fu_tfoot")
17
- end
18
- end
19
-
20
- def render_fields(model_klass)
21
- html = ""
22
- (session["#{model_klass.name.underscore}_filter_fu"] || params[:filter_fu] || {}).each do |k,v|
23
- rand_id = (DateTime.now.to_i * rand * 100).to_i
24
- html << content_tag(:tr, :id => "filter_fu_row_new_#{rand_id}", :class => "filter_fu_rows") do
25
- content_tag(:td, select_tag("filter_fu[_new_#{rand_id}][name]", options_for_select(model_klass.filter_fu_names, v[:name]))) +
26
- content_tag(:td, text_field_tag("filter_fu[_new_#{rand_id}][value]", v[:value])) +
27
- content_tag(:td, link_to("X", "#", :class => 'filter_fu_remove'))
28
- end
29
- end
30
- html.html_safe
31
- end
32
-
33
- def filter_header
34
- content_tag(:tr) do
35
- content_tag(:td, "Filter") +
36
- content_tag(:td, "Value", :colspan => 2)
37
- end
38
- end
39
-
40
- def filter_footer
41
- content_tag(:tr) do
42
- content_tag(:td, link_to("Add", "#", :id => "filter_fu_add") + submit_tag("Apply", :name => "filter_fu_commit"), :colspan => 3)
43
- end
44
- end
45
-
46
- def hidden_filter_fields(model_klass)
47
- content_tag(:table, :style=>"display:none") do
48
- content_tag(:tbody, :id => "hidden_filter_fields") do
49
- content_tag(:tr, :id => "filter_fu_row_NEW_", :class => "filter_fu_rows") do
50
- content_tag(:td, select_tag("filter_fu[_NEW_][name]", options_for_select(model_klass.filter_fu_names))) +
51
- content_tag(:td, text_field_tag("filter_fu[_NEW_][value]")) +
52
- content_tag(:td, link_to("X", "#", :class => 'filter_fu_remove'))
53
- end
54
- end
55
- end
56
- end
57
- end
58
- end
59
-
60
- if defined?(ActionController::Base)
61
- ActionController::Base.helper(FilterFu::ViewHelper)
62
- end
63
-
data/lib/filter_fu.rb DELETED
@@ -1,4 +0,0 @@
1
- require 'filter_fu/model_additions'
2
- require 'filter_fu/view_helper'
3
- require 'filter_fu/controller_additions'
4
-
@@ -1,43 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe FilterFu do
4
- before(:each) {
5
- Payment._filter_fu_attributes = []
6
- @name_err_attr = { where: 'haha', operator: 'like' },
7
- { name: 'Date', where: "date", operator: '=',
8
- value_proc: Proc.new { |val| Date.parse(val) } }
9
-
10
- @where_err_attr = { name: 'haha', operator: 'like' },
11
- { name: 'Date', where: "date", operator: '=',
12
- value_proc: Proc.new { |val| Date.parse(val) } }
13
-
14
-
15
- @pay_attr1 = { name: 'Date', where: "date", operator: '=',
16
- value_proc: Proc.new { |val| Date.parse(val) } },
17
- { name: 'Collector', where: 'collector', operator: 'like' }
18
- }
19
- it "should respond_to #filter_fu" do
20
- Payment.respond_to?(:filter_fu).should be_true
21
- end
22
-
23
- it "should add #filter_fu params to _filter_fu_attributes" do
24
- Payment.filter_fu(*@pay_attr1)
25
- Payment._filter_fu_attributes.count.should == 2
26
- end
27
-
28
- it "#filter_fu should not duplicated :name attr to _filter_fu_attributes" do
29
- Payment.filter_fu(*@pay_attr1)
30
- Payment.filter_fu(*@pay_attr1)
31
- Payment._filter_fu_attributes.count.should == 2
32
- end
33
-
34
- it "should raise error, if #filter_fu params has no name key" do
35
- lambda { Payment.filter_fu(*@name_err_attr) }.should raise_error "Parameter has no :name key"
36
- end
37
-
38
- it "should raise error, if #filter_fu params has no where key" do
39
- lambda { Payment.filter_fu(*@where_err_attr) }.should raise_error "Parameter has no :where key"
40
- end
41
-
42
-
43
- end