search_fu 0.0.7 → 0.0.8.beta

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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -1,7 +1,6 @@
1
1
  jQuery(function(){
2
2
  add_remove_row.bindAddLink('#search_fu_add', '#hidden_search_fields', '#search_fu_tbody');
3
3
  add_remove_row.bindRemoveLink('.search_fu_remove', '.search_fu_rows');
4
-
5
4
  $('form#search_fu_form').live("ajax:beforeSend", function() {
6
5
  $('body').css('cursor', 'wait');
7
6
  });
@@ -10,26 +9,6 @@ jQuery(function(){
10
9
  $($(this).attr("element")).html(b);
11
10
  $('body').css('cursor', 'default');
12
11
  });
13
-
14
- $('#search_fu_more_link').live("click", function(a, b, c, d) {
15
- var going_to_fetch_page, old_text;
16
- $('body').css('cursor', 'wait');
17
- old_text = $('#search_fu_more_link').text();
18
- if(typeof going_to_fetch_page === 'undefined') { going_to_fetch_page = 2; }
19
- $('#search_fu_more_link').attr("href", $('form#search_fu_form').attr('action'));
20
- $('#search_fu_more_link').data("params", $("form#search_fu_form").serialize() + "&page=" + going_to_fetch_page);
21
- $('#search_fu_more_link').hide();
22
- $('#search_fu_more_span').text("Wait, fetching more result....");
23
- $.get($(this).attr("href"), $(this).data("params"), function(data) {
24
- $($("#search_fu_more_link").attr("element")).append(data);
25
- $('#search_fu_more_span').text("");
26
- going_to_fetch_page++;
27
- $('#search_fu_more_link').show();
28
- if(data.trim() === "") { $('#search_fu_more_span').text("No more result. Try again? ") }
29
- $('body').css('cursor', 'default');
30
- });
31
- return false;
32
- });
33
12
  });
34
13
 
35
14
  var add_remove_row = {
@@ -1,7 +1,7 @@
1
1
  module SearchFu
2
2
  module ControllerAdditions
3
3
  def store_search_fu_session
4
- session["#{self.class.name.sub(/Controller/, '').underscore.singularize}_search_fu"] = params[:search_fu] if params[:search_fu]
4
+ session["#{self.class.name.sub(/Controller/, '').underscore.singularize}_search_fu"] = params[:search_fu] if params[:search_fu_commit]
5
5
  end
6
6
  end
7
7
  end
@@ -9,4 +9,3 @@ end
9
9
  if defined?(ActionController::Base)
10
10
  ActionController::Base.send :include, SearchFu::ControllerAdditions
11
11
  end
12
-
@@ -28,9 +28,9 @@ module SearchFu
28
28
  a
29
29
  end
30
30
 
31
- def search_fu(name, hash)
31
+ def search_fu(*args)
32
32
  @_search_fu_attributes ||= []
33
- c_attr = create_search_fu_attribute_hash(name, hash)
33
+ c_attr = create_search_fu_attribute_hash(*args)
34
34
  @_search_fu_attributes << c_attr unless @_search_fu_attributes.detect { |t| t[:name] == c_attr[:name]}
35
35
  end
36
36
 
@@ -65,12 +65,54 @@ module SearchFu
65
65
  {"~" => "like", "=" => "=", ">=" => ">=", "<=" => "<=", "<" => "<", ">" => ">", "<>" => "<>"}[val]
66
66
  end
67
67
 
68
- def create_search_fu_attribute_hash(name, hash)
69
- raise "No Query Key found in query hash" unless hash[:ar_query]
68
+ def create_search_fu_attribute_hash(*args)
69
+ # args[0] - column name, args[1] - view name, args[3] - search_fu hash
70
+ # args[0] - view name, args[1] - search_fu_hash
71
+ # args[0] - column name, args[1] - view name
72
+ # args[0] - column name
73
+ hash = {}
74
+ hash = args.delete args.last if args.last.class == Hash
75
+
76
+ if args.size == 2
77
+ search_fu_attribute_hash(args[1], ar_query(args[0], hash))
78
+ elsif args.size == 1
79
+ search_fu_attribute_hash(args[0].to_s.humanize, ar_query(args[0], hash))
80
+ end
81
+ end
82
+
83
+ def ar_query(col_name, hash)
84
+ arq = hash[:ar_query]
85
+ operator = hash[:operator]
86
+ converter = hash[:convert]
87
+ col = columns.detect { |t| t.name == col_name.to_s }
88
+ if col
89
+ case
90
+ when col.type == :string || col.type == :text
91
+ operator ||= 'like'
92
+ arq ||= %Q(where("upper(#{self.table_name}.#{col_name}) <op> upper('<value>')"))
93
+ when col.type == :datetime || col.type == :date
94
+ converter ||= Proc.new{ |op, val| Date.parse(val.to_s).to_s(:db) }
95
+ arq ||= %Q(where("#{self.table_name}.#{col_name} <op> '<value>'"))
96
+ when col.type == :decimal || col.type == :integer
97
+ arq ||= %Q(where("#{self.table_name}.#{col_name} <op> <value>"))
98
+ when col.type == :boolean
99
+ converter ||= Proc.new { |op, val| ['t', 'y', 'true', 'yes'].include?(val.downcase) ? 'true' : 'false' }
100
+ arq ||= %Q(where("#{self.table_name}.#{col_name} <op> <value>"))
101
+ end
102
+ end
103
+ hsh = {}
104
+ hsh.merge!(ar_query: arq)
105
+ hsh.merge!(operator: operator || "=")
106
+ hsh.merge!(convert: converter)
107
+ return hsh
108
+ end
109
+
110
+ def search_fu_attribute_hash(name, hash)
111
+ raise "No Query found in hash!" unless hash[:ar_query]
70
112
  {
71
113
  name: name,
72
114
  ar_query: hash[:ar_query],
73
- operator: hash[:operator] || '=',
115
+ operator: hash[:operator],
74
116
  convert: hash[:convert]
75
117
  }
76
118
  end
@@ -1,4 +1,4 @@
1
1
  module SearchFu
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8.beta"
3
3
  end
4
4
 
@@ -1,6 +1,6 @@
1
1
  require 'action_view'
2
2
 
3
- module FilterFu
3
+ module SearchFu
4
4
  module ViewHelper
5
5
 
6
6
  def search_fu_form_for(model_klass, element="#content", options={})
@@ -14,13 +14,7 @@ module FilterFu
14
14
  end
15
15
  end
16
16
 
17
- def link_to_more(text, element, options={})
18
- content_tag(:span, "", id: "search_fu_more_span") +
19
- link_to(text, '#', options.merge(id: "search_fu_more_link", element: element))
20
- end
21
-
22
-
23
- def filter_table(model_klass, button_class)
17
+ def filter_table(model_klass, button_class)
24
18
  content_tag(:table, id: 'search_fu_table') do
25
19
  content_tag(:thead, filter_header, id: "search_fu_thead") +
26
20
  content_tag(:tbody, render_fields(model_klass, button_class[:remove]), id: "search_fu_tbody") +
@@ -51,7 +45,7 @@ module FilterFu
51
45
  def filter_footer(add_apply_button_class)
52
46
  content_tag(:tr) do
53
47
  content_tag(:td, link_to("Add", "#", id: "search_fu_add", class: add_apply_button_class) +
54
- submit_tag("Apply", class: add_apply_button_class, disable_with: "Searching..."), colspan: 3)
48
+ submit_tag("Apply", name: "search_fu_commit", class: add_apply_button_class, disable_with: "Searching..."), colspan: 3)
55
49
  end
56
50
  end
57
51
 
@@ -70,6 +64,6 @@ module FilterFu
70
64
  end
71
65
 
72
66
  if defined?(ActionController::Base)
73
- ActionController::Base.helper(FilterFu::ViewHelper)
67
+ ActionController::Base.helper(SearchFu::ViewHelper)
74
68
  end
75
69
 
data/search_fu.gemspec CHANGED
@@ -19,7 +19,8 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
+ s.add_development_dependency "sqlite3"
22
23
  s.add_development_dependency "rspec"
23
- s.add_development_dependency "sqlite3-ruby"
24
- s.add_development_dependency "rails"
24
+ s.add_dependency 'activerecord'
25
+ s.add_dependency 'actionpack'
25
26
  end
@@ -1,98 +1,31 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe SearchFu do
4
- before(:each) {
5
- # Payment._search_fu_attributes = []
6
- @err_attr2 = ['Error', { operator: '=' }]
7
- @pay_attr1 = ['Collector', { ar_query: %q(where("payments.collector <op> '<value>'")), operator: 'like'}]
8
- @pay_attr2 = ['Pay to', { ar_query: %q(where("accounts.name1 <op> '<value>' or accounts.name2 <op> '<value>'").joins(:to_account)),
9
- operator: 'like'}]
10
- @pay_attr3 = ['Pay from', { ar_query: %q(where("accounts.name1 <op> '<value>' or accounts.name2 <op> '<value>'").joins(:from_account)),
11
- operator: 'like'}]
12
- @pay_attr4 = ['Date', { ar_query: %q(where("payments.date <op> '<value>'")),
13
- operator: '=', convert: Proc.new {|op, t| Date.parse(t.to_s).to_s } } ]
14
- }
15
-
16
- describe "#search_fu" do
17
- before(:each) do
18
- Payment.search_fu @pay_attr1[0], @pay_attr1[1]
19
- Payment.search_fu @pay_attr2[0], @pay_attr2[1]
20
- Payment.search_fu @pay_attr3[0], @pay_attr3[1]
21
- Payment.search_fu @pay_attr4[0], @pay_attr4[1]
22
- end
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
-
28
- it "should return an array of names" do
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
35
- end
36
-
37
- it "should combine where relation" do
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).
40
- where("accounts.name1 like '%tkh%' or accounts.name2 like '%tkh%'").joins(:from_account).to_sql.downcase
3
+ describe SearchFu::ModelAdditions do
4
+ describe '.search_fu_attribute_hash' do
5
+ it "should raise error if hash[:ar_query] not found" do
6
+ lambda { SearchFu.search_fu_attribute_hash('name', {}) }.should raise_error("No Query found in hash!")
41
7
  end
42
8
 
43
- it "should return join where relation" do
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
9
+ it "should not rails error if hash[:ar_query] exists" do
10
+ lambda { SearchFu.search_fu_attribute_hash('name', ar_query: 3) }.should_not raise_error("No Query found in hash!")
46
11
  end
47
12
 
48
- it "should use value operator return simple where relation and convert value" do
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
51
- end
52
-
53
- it "should return simple where relation and convert value" do
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
56
- end
13
+ describe 'return hash' do
14
+ it "should be hashed correctly" do
15
+ SearchFu.search_fu_attribute_hash("stuff", ar_query: 'haha').should ==
16
+ { name: 'stuff', ar_query: 'haha', operator: nil, convert: nil }
57
17
 
58
- it "should use value operator and return simple where relation" do
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
61
- end
18
+ SearchFu.search_fu_attribute_hash("stuff", ar_query: 'haha', operator: 'kaka').should ==
19
+ { name: 'stuff', ar_query: 'haha', operator: 'kaka', convert: nil }
62
20
 
63
- it "should return simple where relation" do
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
66
- end
21
+ SearchFu.search_fu_attribute_hash("stuff", ar_query: 'haha', convert: 'caca').should ==
22
+ { name: 'stuff', ar_query: 'haha', operator: nil, convert: 'caca' }
67
23
 
68
- it "should raise error, if :name ont found" do
69
- lambda { Payment.search({a: { name: 'haha', value: 'mama'}}) }.should raise_error("Column 'haha' not found.")
24
+ SearchFu.search_fu_attribute_hash("stuff", ar_query: 'haha', convert: 'caca', operator: 'kaka' ).should ==
25
+ { name: 'stuff', ar_query: 'haha', operator: 'kaka', convert: 'caca' }
26
+ end
70
27
  end
71
28
  end
72
29
 
73
- it "should respond_to #search" do
74
- Payment.respond_to?(:search).should be_true
75
- end
76
-
77
- it "should respond_to #search_fu" do
78
- Payment.respond_to?(:search_fu).should be_true
79
- end
80
-
81
- it "should add #search_fu params to _search_fu_attributes" do
82
- Payment.search_fu(@pay_attr1[0], @pay_attr1[1])
83
- Payment.search_fu(@pay_attr2[0], @pay_attr2[1])
84
- Payment.search_fu_names.count.should == 2
85
- end
86
-
87
- it "#search_fu should not duplicated :name attr to _search_fu_attributes" do
88
- Payment.search_fu(@pay_attr1[0], @pay_attr1[1])
89
- Payment.search_fu(@pay_attr1[0], @pay_attr1[1])
90
- Payment.search_fu_names.count == 1
91
- end
92
-
93
- it "#search_fu should raise error if :ar_query in nil" do
94
- lambda { Payment.search_fu(@err_attr2[0], @err_attr2[1]) }.should raise_error "No Query Key found in query hash"
95
- end
96
-
97
30
  end
98
31
 
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: search_fu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
5
- prerelease:
4
+ version: 0.0.8.beta
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Tan Kwang How
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-29 00:00:00.000000000Z
12
+ date: 2012-02-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rspec
16
- requirement: &82980960 !ruby/object:Gem::Requirement
15
+ name: sqlite3
16
+ requirement: &77688230 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *82980960
24
+ version_requirements: *77688230
25
25
  - !ruby/object:Gem::Dependency
26
- name: sqlite3-ruby
27
- requirement: &82980740 !ruby/object:Gem::Requirement
26
+ name: rspec
27
+ requirement: &77688020 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,18 +32,29 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *82980740
35
+ version_requirements: *77688020
36
36
  - !ruby/object:Gem::Dependency
37
- name: rails
38
- requirement: &82903650 !ruby/object:Gem::Requirement
37
+ name: activerecord
38
+ requirement: &77687810 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
42
42
  - !ruby/object:Gem::Version
43
43
  version: '0'
44
- type: :development
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *77687810
47
+ - !ruby/object:Gem::Dependency
48
+ name: actionpack
49
+ requirement: &77687600 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
45
56
  prerelease: false
46
- version_requirements: *82903650
57
+ version_requirements: *77687600
47
58
  description: A ActiveRecord method to allow user specify and combine query in views
48
59
  email:
49
60
  - tankwanghow@gmail.com
@@ -52,6 +63,7 @@ extensions: []
52
63
  extra_rdoc_files: []
53
64
  files:
54
65
  - .gitignore
66
+ - .rspec
55
67
  - Gemfile
56
68
  - Rakefile
57
69
  - javascript/search_fu.js
@@ -79,9 +91,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
79
91
  required_rubygems_version: !ruby/object:Gem::Requirement
80
92
  none: false
81
93
  requirements:
82
- - - ! '>='
94
+ - - ! '>'
83
95
  - !ruby/object:Gem::Version
84
- version: '0'
96
+ version: 1.3.1
85
97
  requirements: []
86
98
  rubyforge_project: search_fu
87
99
  rubygems_version: 1.8.10