search_fu 0.0.1 → 0.0.2
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/lib/search_fu/model_additions.rb +11 -4
- data/lib/search_fu/version.rb +1 -1
- data/lib/search_fu/view_helper.rb +3 -3
- data/spec/search_fu_spec.rb +29 -22
- metadata +1 -2
- data/filter_fu.gemspec +0 -25
@@ -9,8 +9,9 @@ module SearchFu
|
|
9
9
|
end
|
10
10
|
|
11
11
|
module ClassMethods
|
12
|
-
def
|
13
|
-
|
12
|
+
def search(hash)
|
13
|
+
return self.where("1=1") unless hash
|
14
|
+
@rel = [%q(where("1=1"))]
|
14
15
|
hash.each do |k,v|
|
15
16
|
search_fu_attr = self._search_fu_attributes.detect { |t| t[:name] == v[:name]}
|
16
17
|
raise "Column '#{v[:name]}' not found." unless search_fu_attr
|
@@ -19,7 +20,7 @@ module SearchFu
|
|
19
20
|
self.instance_eval(@rel.join('.'))
|
20
21
|
end
|
21
22
|
|
22
|
-
def
|
23
|
+
def search_fu_names
|
23
24
|
a = []
|
24
25
|
self._search_fu_attributes.each do |h|
|
25
26
|
a << h[:name]
|
@@ -37,9 +38,15 @@ module SearchFu
|
|
37
38
|
def generate_where(attr, value)
|
38
39
|
a = grab_operator_value(value)
|
39
40
|
op = a[0] || attr[:operator]
|
41
|
+
return %q(where("1=1")) if a[1].blank?
|
42
|
+
val = assign_like(op, a[1])
|
40
43
|
val = attr[:convert].call(op, a[1]) if attr[:convert]
|
41
44
|
w = attr[:ar_query]
|
42
|
-
w.gsub(/<op>/, op).gsub(/<value>/, val
|
45
|
+
w.gsub(/<op>/, op).gsub(/<value>/, val)
|
46
|
+
end
|
47
|
+
|
48
|
+
def assign_like(op, val)
|
49
|
+
op == "like" ? "%#{val.to_s}%" : val.to_s
|
43
50
|
end
|
44
51
|
|
45
52
|
def grab_operator_value(value)
|
data/lib/search_fu/version.rb
CHANGED
@@ -3,7 +3,7 @@ require 'action_view'
|
|
3
3
|
module FilterFu
|
4
4
|
module ViewHelper
|
5
5
|
def search_fu_form_for(model_klass, options={})
|
6
|
-
|
6
|
+
hidden_search_fields(model_klass) +
|
7
7
|
form_tag(polymorphic_path(Payment), options.merge(method: :get, id: 'search_fu_form')) do
|
8
8
|
filter_table model_klass
|
9
9
|
end
|
@@ -43,9 +43,9 @@ module FilterFu
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
def
|
46
|
+
def hidden_search_fields(model_klass)
|
47
47
|
content_tag(:table, :style=>"display:none") do
|
48
|
-
content_tag(:tbody, :id => "
|
48
|
+
content_tag(:tbody, :id => "hidden_search_fields") do
|
49
49
|
content_tag(:tr, :id => "search_fu_row_NEW_", :class => "search_fu_rows") do
|
50
50
|
content_tag(:td, select_tag("search_fu[_NEW_][name]", options_for_select(model_klass.search_fu_names))) +
|
51
51
|
content_tag(:td, text_field_tag("search_fu[_NEW_][value]")) +
|
data/spec/search_fu_spec.rb
CHANGED
@@ -4,14 +4,13 @@ describe FilterFu do
|
|
4
4
|
before(:each) {
|
5
5
|
Payment._search_fu_attributes = []
|
6
6
|
@err_attr2 = ['Error', { operator: '=' }]
|
7
|
-
@pay_attr1 = ['Collector', { ar_query: %q(where("payments.collector <op> '<value>'")), operator: 'like'
|
8
|
-
convert: Proc.new{|op,t| op == "like" ? "%#{t.to_s}%" : t.to_s}}]
|
7
|
+
@pay_attr1 = ['Collector', { ar_query: %q(where("payments.collector <op> '<value>'")), operator: 'like'}]
|
9
8
|
@pay_attr2 = ['Pay to', { ar_query: %q(where("accounts.name1 <op> '<value>' or accounts.name2 <op> '<value>'").joins(:to_account)),
|
10
|
-
operator: 'like'
|
9
|
+
operator: 'like'}]
|
11
10
|
@pay_attr3 = ['Pay from', { ar_query: %q(where("accounts.name1 <op> '<value>' or accounts.name2 <op> '<value>'").joins(:from_account)),
|
12
|
-
operator: 'like'
|
11
|
+
operator: 'like'}]
|
13
12
|
@pay_attr4 = ['Date', { ar_query: %q(where("payments.date <op> '<value>'")),
|
14
|
-
operator: '=', convert: Proc.new {|op, t| Date.parse(t.to_s) } } ]
|
13
|
+
operator: '=', convert: Proc.new {|op, t| Date.parse(t.to_s).to_s } } ]
|
15
14
|
}
|
16
15
|
|
17
16
|
describe "#filter" do
|
@@ -22,49 +21,57 @@ describe FilterFu do
|
|
22
21
|
Payment.search_fu @pay_attr4[0], @pay_attr4[1]
|
23
22
|
end
|
24
23
|
|
24
|
+
it "should not query if hash empty" do
|
25
|
+
Payment.search(nil).to_sql.downcase.should == Payment.where("1=1").to_sql.downcase
|
26
|
+
end
|
27
|
+
|
25
28
|
it "should return an array of names" do
|
26
|
-
Payment.
|
29
|
+
Payment.search_fu_names.sort.should == ['Collector', 'Date', 'Pay from', 'Pay to']
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should not query if value nil" do
|
33
|
+
Payment.search( { b: { name: 'Date', value: '' } }).to_sql.downcase.should ==
|
34
|
+
Payment.where("1=1").to_sql.downcase
|
27
35
|
end
|
28
36
|
|
29
37
|
it "should combine where relation" do
|
30
|
-
Payment.
|
31
|
-
Payment.where("accounts.name1 like '%smith%' or accounts.name2 like '%smith%'").joins(:to_account).
|
38
|
+
Payment.search( { b: { name: 'Pay to', value: 'smith' } , a: { name: 'Pay from', value: 'tkh'} }).to_sql.downcase.should ==
|
39
|
+
Payment.where("1=1").where("accounts.name1 like '%smith%' or accounts.name2 like '%smith%'").joins(:to_account).
|
32
40
|
where("accounts.name1 like '%tkh%' or accounts.name2 like '%tkh%'").joins(:from_account).to_sql.downcase
|
33
41
|
end
|
34
42
|
|
35
43
|
it "should return join where relation" do
|
36
|
-
Payment.
|
37
|
-
Payment.where("accounts.name1 like '%smith%' or accounts.name2 like '%smith%'").joins(:to_account).to_sql.downcase
|
44
|
+
Payment.search( { b: { name: 'Pay to', value: 'smith' } }).to_sql.downcase.should ==
|
45
|
+
Payment.where("1=1").where("accounts.name1 like '%smith%' or accounts.name2 like '%smith%'").joins(:to_account).to_sql.downcase
|
38
46
|
end
|
39
47
|
|
40
48
|
it "should use value operator return simple where relation and convert value" do
|
41
|
-
Payment.
|
42
|
-
Payment.where('payments.date >= ?', Date.parse('12-Jun-2010')).to_sql.downcase
|
49
|
+
Payment.search( { b: { name: 'Date', value: '>= 12-Jun-2010' } }).to_sql.downcase.should ==
|
50
|
+
Payment.where("1=1").where('payments.date >= ?', Date.parse('12-Jun-2010')).to_sql.downcase
|
43
51
|
end
|
44
52
|
|
45
53
|
it "should return simple where relation and convert value" do
|
46
|
-
Payment.
|
47
|
-
Payment.where("payments.date = '#{Date.parse('12-Jun-2010')}'").to_sql.downcase
|
54
|
+
Payment.search( { b: { name: 'Date', value: '12-Jun-2010' } }).to_sql.downcase.should ==
|
55
|
+
Payment.where("1=1").where("payments.date = '#{Date.parse('12-Jun-2010')}'").to_sql.downcase
|
48
56
|
end
|
49
57
|
|
50
58
|
it "should use value operator and return simple where relation" do
|
51
|
-
Payment.
|
52
|
-
Payment.where("payments.collector = 'smith ma'").to_sql.downcase
|
59
|
+
Payment.search({a: { name: 'Collector', value: '= smith ma'}}).to_sql.downcase.should ==
|
60
|
+
Payment.where("1=1").where("payments.collector = 'smith ma'").to_sql.downcase
|
53
61
|
end
|
54
62
|
|
55
63
|
it "should return simple where relation" do
|
56
|
-
Payment.
|
57
|
-
Payment.where('payments.collector like ?', '%smith ma%').to_sql.downcase
|
64
|
+
Payment.search({a: { name: 'Collector', value: 'smith ma'}}).to_sql.downcase.should ==
|
65
|
+
Payment.where("1=1").where('payments.collector like ?', '%smith ma%').to_sql.downcase
|
58
66
|
end
|
59
67
|
|
60
68
|
it "should raise error, if :name ont found" do
|
61
|
-
lambda { Payment.
|
69
|
+
lambda { Payment.search({a: { name: 'haha', value: 'mama'}}) }.should raise_error("Column 'haha' not found.")
|
62
70
|
end
|
63
|
-
|
64
71
|
end
|
65
72
|
|
66
|
-
it "should respond_to #
|
67
|
-
Payment.respond_to?(:
|
73
|
+
it "should respond_to #search" do
|
74
|
+
Payment.respond_to?(:search).should be_true
|
68
75
|
end
|
69
76
|
|
70
77
|
it "should respond_to #search_fu" do
|
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.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Tan Kwang How
|
@@ -59,7 +59,6 @@ files:
|
|
59
59
|
- .gitignore
|
60
60
|
- Gemfile
|
61
61
|
- Rakefile
|
62
|
-
- filter_fu.gemspec
|
63
62
|
- javascript/add_remove_row.js
|
64
63
|
- lib/filter_fu.rb
|
65
64
|
- lib/filter_fu/controller_additions.rb
|
data/filter_fu.gemspec
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require "filter_fu/version"
|
4
|
-
|
5
|
-
Gem::Specification.new do |s|
|
6
|
-
s.name = "filter_fu"
|
7
|
-
s.version = FilterFu::VERSION
|
8
|
-
s.platform = Gem::Platform::RUBY
|
9
|
-
s.authors = ["Tan Kwang How"]
|
10
|
-
s.email = ["tankwanghow@gmail.com"]
|
11
|
-
s.homepage = "http://github.com/tankwanghow"
|
12
|
-
s.summary = %q{Combine query in views}
|
13
|
-
s.description = %q{A ActiveRecord method to allow user specify and combine query in views}
|
14
|
-
|
15
|
-
s.rubyforge_project = "filter_fu"
|
16
|
-
|
17
|
-
s.files = `git ls-files`.split("\n")
|
18
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
-
s.require_paths = ["lib"]
|
21
|
-
|
22
|
-
s.add_development_dependency "rspec"
|
23
|
-
s.add_development_dependency "sqlite3-ruby"
|
24
|
-
s.add_development_dependency "rails"
|
25
|
-
end
|