searchlogic 1.5.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +228 -0
- data/MIT-LICENSE +20 -0
- data/Manifest +123 -0
- data/README.rdoc +383 -0
- data/Rakefile +15 -0
- data/TODO.rdoc +6 -0
- data/examples/README.rdoc +4 -0
- data/init.rb +1 -0
- data/lib/searchlogic.rb +89 -0
- data/lib/searchlogic/active_record/associations.rb +52 -0
- data/lib/searchlogic/active_record/base.rb +218 -0
- data/lib/searchlogic/active_record/connection_adapters/mysql_adapter.rb +172 -0
- data/lib/searchlogic/active_record/connection_adapters/postgresql_adapter.rb +168 -0
- data/lib/searchlogic/active_record/connection_adapters/sqlite_adapter.rb +75 -0
- data/lib/searchlogic/condition/base.rb +159 -0
- data/lib/searchlogic/condition/begins_with.rb +17 -0
- data/lib/searchlogic/condition/blank.rb +21 -0
- data/lib/searchlogic/condition/child_of.rb +11 -0
- data/lib/searchlogic/condition/descendant_of.rb +24 -0
- data/lib/searchlogic/condition/ends_with.rb +17 -0
- data/lib/searchlogic/condition/equals.rb +27 -0
- data/lib/searchlogic/condition/greater_than.rb +15 -0
- data/lib/searchlogic/condition/greater_than_or_equal_to.rb +15 -0
- data/lib/searchlogic/condition/inclusive_descendant_of.rb +11 -0
- data/lib/searchlogic/condition/keywords.rb +47 -0
- data/lib/searchlogic/condition/less_than.rb +15 -0
- data/lib/searchlogic/condition/less_than_or_equal_to.rb +15 -0
- data/lib/searchlogic/condition/like.rb +15 -0
- data/lib/searchlogic/condition/nil.rb +21 -0
- data/lib/searchlogic/condition/not_begin_with.rb +20 -0
- data/lib/searchlogic/condition/not_blank.rb +19 -0
- data/lib/searchlogic/condition/not_end_with.rb +20 -0
- data/lib/searchlogic/condition/not_equal.rb +26 -0
- data/lib/searchlogic/condition/not_have_keywords.rb +20 -0
- data/lib/searchlogic/condition/not_like.rb +20 -0
- data/lib/searchlogic/condition/not_nil.rb +19 -0
- data/lib/searchlogic/condition/sibling_of.rb +14 -0
- data/lib/searchlogic/condition/tree.rb +17 -0
- data/lib/searchlogic/conditions/base.rb +484 -0
- data/lib/searchlogic/conditions/protection.rb +36 -0
- data/lib/searchlogic/config.rb +31 -0
- data/lib/searchlogic/config/helpers.rb +289 -0
- data/lib/searchlogic/config/search.rb +53 -0
- data/lib/searchlogic/core_ext/hash.rb +75 -0
- data/lib/searchlogic/helpers/control_types/link.rb +310 -0
- data/lib/searchlogic/helpers/control_types/links.rb +241 -0
- data/lib/searchlogic/helpers/control_types/remote_link.rb +87 -0
- data/lib/searchlogic/helpers/control_types/remote_links.rb +72 -0
- data/lib/searchlogic/helpers/control_types/remote_select.rb +36 -0
- data/lib/searchlogic/helpers/control_types/select.rb +82 -0
- data/lib/searchlogic/helpers/form.rb +208 -0
- data/lib/searchlogic/helpers/utilities.rb +197 -0
- data/lib/searchlogic/modifiers/absolute.rb +15 -0
- data/lib/searchlogic/modifiers/acos.rb +11 -0
- data/lib/searchlogic/modifiers/asin.rb +11 -0
- data/lib/searchlogic/modifiers/atan.rb +11 -0
- data/lib/searchlogic/modifiers/base.rb +27 -0
- data/lib/searchlogic/modifiers/ceil.rb +15 -0
- data/lib/searchlogic/modifiers/char_length.rb +15 -0
- data/lib/searchlogic/modifiers/cos.rb +15 -0
- data/lib/searchlogic/modifiers/cot.rb +15 -0
- data/lib/searchlogic/modifiers/day_of_month.rb +15 -0
- data/lib/searchlogic/modifiers/day_of_week.rb +15 -0
- data/lib/searchlogic/modifiers/day_of_year.rb +15 -0
- data/lib/searchlogic/modifiers/degrees.rb +11 -0
- data/lib/searchlogic/modifiers/exp.rb +15 -0
- data/lib/searchlogic/modifiers/floor.rb +15 -0
- data/lib/searchlogic/modifiers/hex.rb +11 -0
- data/lib/searchlogic/modifiers/hour.rb +11 -0
- data/lib/searchlogic/modifiers/log.rb +15 -0
- data/lib/searchlogic/modifiers/log10.rb +11 -0
- data/lib/searchlogic/modifiers/log2.rb +11 -0
- data/lib/searchlogic/modifiers/lower.rb +15 -0
- data/lib/searchlogic/modifiers/ltrim.rb +15 -0
- data/lib/searchlogic/modifiers/md5.rb +11 -0
- data/lib/searchlogic/modifiers/microseconds.rb +11 -0
- data/lib/searchlogic/modifiers/milliseconds.rb +11 -0
- data/lib/searchlogic/modifiers/minute.rb +15 -0
- data/lib/searchlogic/modifiers/month.rb +15 -0
- data/lib/searchlogic/modifiers/octal.rb +15 -0
- data/lib/searchlogic/modifiers/radians.rb +11 -0
- data/lib/searchlogic/modifiers/round.rb +11 -0
- data/lib/searchlogic/modifiers/rtrim.rb +15 -0
- data/lib/searchlogic/modifiers/second.rb +15 -0
- data/lib/searchlogic/modifiers/sign.rb +11 -0
- data/lib/searchlogic/modifiers/sin.rb +11 -0
- data/lib/searchlogic/modifiers/square_root.rb +15 -0
- data/lib/searchlogic/modifiers/tan.rb +15 -0
- data/lib/searchlogic/modifiers/trim.rb +15 -0
- data/lib/searchlogic/modifiers/upper.rb +15 -0
- data/lib/searchlogic/modifiers/week.rb +11 -0
- data/lib/searchlogic/modifiers/year.rb +11 -0
- data/lib/searchlogic/search/base.rb +148 -0
- data/lib/searchlogic/search/conditions.rb +53 -0
- data/lib/searchlogic/search/ordering.rb +244 -0
- data/lib/searchlogic/search/pagination.rb +121 -0
- data/lib/searchlogic/search/protection.rb +89 -0
- data/lib/searchlogic/search/searching.rb +31 -0
- data/lib/searchlogic/shared/utilities.rb +50 -0
- data/lib/searchlogic/shared/virtual_classes.rb +39 -0
- data/lib/searchlogic/version.rb +79 -0
- data/searchlogic.gemspec +39 -0
- data/test/fixtures/accounts.yml +15 -0
- data/test/fixtures/cats.yml +3 -0
- data/test/fixtures/dogs.yml +3 -0
- data/test/fixtures/orders.yml +14 -0
- data/test/fixtures/user_groups.yml +13 -0
- data/test/fixtures/users.yml +36 -0
- data/test/test_active_record_associations.rb +81 -0
- data/test/test_active_record_base.rb +93 -0
- data/test/test_condition_base.rb +52 -0
- data/test/test_condition_types.rb +143 -0
- data/test/test_conditions_base.rb +242 -0
- data/test/test_conditions_protection.rb +16 -0
- data/test/test_config.rb +23 -0
- data/test/test_helper.rb +134 -0
- data/test/test_search_base.rb +227 -0
- data/test/test_search_conditions.rb +19 -0
- data/test/test_search_ordering.rb +165 -0
- data/test/test_search_pagination.rb +72 -0
- data/test/test_search_protection.rb +24 -0
- data/test_libs/acts_as_tree.rb +98 -0
- data/test_libs/ordered_hash.rb +9 -0
- data/test_libs/rexml_fix.rb +14 -0
- metadata +317 -0
@@ -0,0 +1,87 @@
|
|
1
|
+
module Searchlogic
|
2
|
+
module Helpers
|
3
|
+
module ControlTypes
|
4
|
+
# = Remote Link Control Types
|
5
|
+
#
|
6
|
+
# These helpers use rails built in remote_function as links.
|
7
|
+
module RemoteLink
|
8
|
+
# The same thing as order_by_link but instead of using rails link_to it uses link_to_remote
|
9
|
+
#
|
10
|
+
# === Examples
|
11
|
+
#
|
12
|
+
# remote_order_by_link
|
13
|
+
# remote_order_by_link(:remote => {:update => "users"})
|
14
|
+
#
|
15
|
+
# === Options
|
16
|
+
#
|
17
|
+
# Please look at order_by_link, this accepts the same options with the following addition:
|
18
|
+
#
|
19
|
+
# * <tt>:remote</tt> -- default: {}, the options to pass into link_to_remote remote options. Such as :update => {:success => "alert('success')"}
|
20
|
+
def remote_order_by_link(order_by, options = {})
|
21
|
+
add_remote_defaults!(options)
|
22
|
+
order_by_link(order_by, options)
|
23
|
+
end
|
24
|
+
|
25
|
+
# The same thing as order_as_link but instead of using rails link_to it uses link_to_remote
|
26
|
+
#
|
27
|
+
# === Examples
|
28
|
+
#
|
29
|
+
# remote_order_as_link
|
30
|
+
# remote_order_as_link(:remote => {:update => "users"})
|
31
|
+
#
|
32
|
+
# === Options
|
33
|
+
#
|
34
|
+
# Please look at order_as_link, this accepts the same options with the following addition:
|
35
|
+
#
|
36
|
+
# * <tt>:remote</tt> -- default: {}, the options to pass into link_to_remote remote options. Such as :update => {:success => "alert('success')"}
|
37
|
+
def remote_order_as_link(order_as, options = {})
|
38
|
+
add_remote_defaults!(options)
|
39
|
+
order_as_link(order_as, options)
|
40
|
+
end
|
41
|
+
|
42
|
+
# The same thing as per_page_link but instead of using rails link_to it uses link_to_remote
|
43
|
+
#
|
44
|
+
# === Examples
|
45
|
+
#
|
46
|
+
# remote_per_page_link
|
47
|
+
# remote_per_page_link(:remote => {:update => "users"})
|
48
|
+
#
|
49
|
+
# === Options
|
50
|
+
#
|
51
|
+
# Please look at per_page_link, this accepts the same options with the following addition:
|
52
|
+
#
|
53
|
+
# * <tt>:remote</tt> -- default: {}, the options to pass into link_to_remote remote options. Such as :update => {:success => "alert('success')"}
|
54
|
+
def remote_per_page_link(per_page, options = {})
|
55
|
+
add_remote_defaults!(options)
|
56
|
+
per_page_link(per_page, options)
|
57
|
+
end
|
58
|
+
|
59
|
+
# The same thing as page_link but instead of using rails link_to it uses link_to_remote
|
60
|
+
#
|
61
|
+
# === Examples
|
62
|
+
#
|
63
|
+
# remote_page_link
|
64
|
+
# remote_page_link(:remote => {:update => "users"})
|
65
|
+
#
|
66
|
+
# === Options
|
67
|
+
#
|
68
|
+
# Please look at page_link, this accepts the same options with the following addition:
|
69
|
+
#
|
70
|
+
# * <tt>:remote</tt> -- default: {}, the options to pass into link_to_remote remote options. Such as :update => {:success => "alert('success')"}
|
71
|
+
def remote_page_link(page, options = {})
|
72
|
+
add_remote_defaults!(options)
|
73
|
+
page_link(page, options)
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
def add_remote_defaults!(options)
|
78
|
+
options[:remote] ||= {}
|
79
|
+
options[:remote][:method] ||= :get
|
80
|
+
options[:is_remote] = true
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
ActionController::Base.helper Searchlogic::Helpers::ControlTypes::RemoteLink if defined?(ActionController)
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Searchlogic
|
2
|
+
module Helpers
|
3
|
+
module ControlTypes
|
4
|
+
# = Remote Links Control Types
|
5
|
+
#
|
6
|
+
# These helpers use rails built in remote_function as links. They are the same thing as the Links control type, but just use rails built in remote helpers.
|
7
|
+
module RemoteLinks
|
8
|
+
# Same as order_by_links, but uses link_to_remote instead of remote.
|
9
|
+
#
|
10
|
+
# === Examples
|
11
|
+
#
|
12
|
+
# remote_order_by_links
|
13
|
+
# remote_order_by_links(:remote => {:update => "users"})
|
14
|
+
#
|
15
|
+
# === Options
|
16
|
+
#
|
17
|
+
# Please look at remote_order_by_link and order_by_links. All options there are applicable here. This is just a wrapper method for those 2 methods.
|
18
|
+
def remote_order_by_links(options = {})
|
19
|
+
add_remote_defaults!(options)
|
20
|
+
order_by_links(options)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Same as order_as_links, but uses link_to_remote instead of remote.
|
24
|
+
#
|
25
|
+
# === Examples
|
26
|
+
#
|
27
|
+
# remote_order_as_links
|
28
|
+
# remote_order_as_links(:remote => {:update => "users"})
|
29
|
+
#
|
30
|
+
# === Options
|
31
|
+
#
|
32
|
+
# Please look at remote_order_as_link and order_as_links. All options there are applicable here. This is just a wrapper method for those 2 methods.
|
33
|
+
def remote_order_as_links(options = {})
|
34
|
+
add_remote_defaults!(options)
|
35
|
+
order_as_link(options)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Same as per_page_links, but uses link_to_remote instead of remote.
|
39
|
+
#
|
40
|
+
# === Examples
|
41
|
+
#
|
42
|
+
# remote_per_page_links
|
43
|
+
# remote_per_page_links(:remote => {:update => "users"})
|
44
|
+
#
|
45
|
+
# === Options
|
46
|
+
#
|
47
|
+
# Please look at remote_per_page_link and per_page_links. All options there are applicable here. This is just a wrapper method for those 2 methods.
|
48
|
+
def remote_per_page_links(options = {})
|
49
|
+
add_remote_defaults!(options)
|
50
|
+
per_page_links(options)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Same as page_links, but uses link_to_remote instead of remote.
|
54
|
+
#
|
55
|
+
# === Examples
|
56
|
+
#
|
57
|
+
# remote_page_links
|
58
|
+
# remote_page_links(:remote => {:update => "users"})
|
59
|
+
#
|
60
|
+
# === Options
|
61
|
+
#
|
62
|
+
# Please look at remote_page_link and page_links. All options there are applicable here. This is just a wrapper method for those 2 methods.
|
63
|
+
def remote_page_links(options = {})
|
64
|
+
add_remote_defaults!(options)
|
65
|
+
page_links(options)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
ActionController::Base.helper Searchlogic::Helpers::ControlTypes::RemoteLinks if defined?(ActionController)
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Searchlogic
|
2
|
+
module Helpers
|
3
|
+
module ControlTypes
|
4
|
+
# = Remote Select Control Types
|
5
|
+
#
|
6
|
+
# These helpers use rails built in remote_function as links. They are the same thing as the Select control type, but just use rails built in remote helpers.
|
7
|
+
module RemoteSelect
|
8
|
+
# Please see order_by_links. All options are the same and applicable here. The only difference is that instead of a group of links, this gets returned as a select form element that will perform the same function when the value is changed.
|
9
|
+
def remote_order_by_select(options = {})
|
10
|
+
add_remote_defaults!(options)
|
11
|
+
order_by_select(options)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Please see order_as_links. All options are the same and applicable here. The only difference is that instead of a group of links, this gets returned as a select form element that will perform the same function when the value is changed.
|
15
|
+
def remote_order_as_select(options = {})
|
16
|
+
add_remote_defaults!(options)
|
17
|
+
order_as_select(options)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Please see per_page_links. All options are the same and applicable here. The only difference is that instead of a group of links, this gets returned as a select form element that will perform the same function when the value is changed.
|
21
|
+
def remote_per_page_select(options = {})
|
22
|
+
add_remote_defaults!(options)
|
23
|
+
per_page_select(options)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Please see page_links. All options are the same and applicable here, except the :prev, :next, :first, and :last options. The only difference is that instead of a group of links, this gets returned as a select form element that will perform the same function when the value is changed.
|
27
|
+
def remote_page_select(options = {})
|
28
|
+
add_remote_defaults!(options)
|
29
|
+
page_select(options)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
ActionController::Base.helper Searchlogic::Helpers::ControlTypes::RemoteSelect if defined?(ActionController)
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Searchlogic
|
2
|
+
module Helpers
|
3
|
+
module ControlTypes
|
4
|
+
# = Select Control Types
|
5
|
+
#
|
6
|
+
# These create <select> tags to help navigate through search data. This is here as an alternative to the Links control types.
|
7
|
+
module Select
|
8
|
+
# Please see order_by_links. All options are the same and applicable here. The only difference is that instead of a group of links, this gets returned as a select form element that will perform the same function when the value is changed.
|
9
|
+
def order_by_select(options = {})
|
10
|
+
add_order_by_select_defaults!(options)
|
11
|
+
searchlogic_state(options) + select(options[:params_scope], :order_by, options[:choices], options[:tag], options[:html] || {})
|
12
|
+
end
|
13
|
+
|
14
|
+
# Please see order_as_links. All options are the same and applicable here. The only difference is that instead of a group of links, this gets returned as a select form element that will perform the same function when the value is changed.
|
15
|
+
def order_as_select(options = {})
|
16
|
+
add_order_as_select_defaults!(options)
|
17
|
+
searchlogic_state(options) + select(options[:params_scope], :order_as, options[:choices], options[:tag], options[:html])
|
18
|
+
end
|
19
|
+
|
20
|
+
# Please see per_page_links. All options are the same and applicable here. The only difference is that instead of a group of links, this gets returned as a select form element that will perform the same function when the value is changed.
|
21
|
+
def per_page_select(options = {})
|
22
|
+
add_per_page_select_defaults!(options)
|
23
|
+
searchlogic_state(options) + select(options[:params_scope], :per_page, options[:choices], options[:tag], options[:html])
|
24
|
+
end
|
25
|
+
|
26
|
+
# Please see page_links. All options are the same and applicable here, excep the :prev, :next, :first, and :last options. The only difference is that instead of a group of links, this gets returned as a select form element that will perform the same function when the value is changed.
|
27
|
+
def page_select(options = {})
|
28
|
+
add_page_select_defaults!(options)
|
29
|
+
searchlogic_state(options) + select(options[:params_scope], :page, (options[:first_page]..options[:last_page]), options[:tag], options[:html])
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
def add_order_by_select_defaults!(options)
|
34
|
+
add_order_by_links_defaults!(options)
|
35
|
+
searchlogic_add_class!(options[:html], Config.helpers.order_by_select_class_name)
|
36
|
+
add_searchlogic_select_defaults!(:order_by, options)
|
37
|
+
options
|
38
|
+
end
|
39
|
+
|
40
|
+
def add_order_as_select_defaults!(options)
|
41
|
+
add_order_as_links_defaults!(options)
|
42
|
+
searchlogic_add_class!(options[:html], Config.helpers.order_as_select_class_name)
|
43
|
+
add_searchlogic_select_defaults!(:order_as, options)
|
44
|
+
options
|
45
|
+
end
|
46
|
+
|
47
|
+
def add_per_page_select_defaults!(options)
|
48
|
+
add_per_page_links_defaults!(options)
|
49
|
+
searchlogic_add_class!(options[:html], Config.helpers.per_page_select_class_name)
|
50
|
+
add_searchlogic_select_defaults!(:per_page, options)
|
51
|
+
options
|
52
|
+
end
|
53
|
+
|
54
|
+
def add_page_select_defaults!(options)
|
55
|
+
add_page_links_defaults!(options)
|
56
|
+
searchlogic_add_class!(options[:html], Config.helpers.page_select_class_name)
|
57
|
+
add_searchlogic_select_defaults!(:page, options)
|
58
|
+
options
|
59
|
+
end
|
60
|
+
|
61
|
+
def add_searchlogic_select_defaults!(option, options)
|
62
|
+
options[:tag] ||= {}
|
63
|
+
options[:tag][:object] = options[:search_obj]
|
64
|
+
|
65
|
+
url = searchlogic_url(options.merge(:literal_search_params => {option => "'+this.value+'"}))
|
66
|
+
|
67
|
+
options[:html][:onchange] ||= ""
|
68
|
+
options[:html][:onchange] += ";"
|
69
|
+
if !options[:remote]
|
70
|
+
options[:html][:onchange] += "window.location='#{url}';"
|
71
|
+
else
|
72
|
+
options[:html][:onchange] += remote_function(:url => url, :method => :get).gsub(/\\'\+this.value\+\\'/, "'+this.value+'")
|
73
|
+
end
|
74
|
+
options[:html][:id] ||= "#{option}_select"
|
75
|
+
options
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
ActionController::Base.helper Searchlogic::Helpers::ControlTypes::Select if defined?(ActionController)
|
@@ -0,0 +1,208 @@
|
|
1
|
+
module Searchlogic
|
2
|
+
module Helpers
|
3
|
+
# = Form Helper
|
4
|
+
#
|
5
|
+
# Enables you to use form_for and fields_for just like you do with an ActiveRecord object.
|
6
|
+
#
|
7
|
+
# === Examples
|
8
|
+
#
|
9
|
+
# Let's assume @search is searching Address
|
10
|
+
#
|
11
|
+
# form_for(@search) # is equivalent to form_for(:search, @search, :url => addresses_path)
|
12
|
+
# form_for([@current_user, @search]) # is equivalent to form_for(:search, @search, :url => user_addresses_path(@current_user))
|
13
|
+
# form_for([:admin, @search]) # is equivalent to form_for(:search, @search, :url => admin_addresses_path)
|
14
|
+
# form_for(:search, @search, :url => whatever_path)
|
15
|
+
#
|
16
|
+
# The goal was to mimic how ActiveRecord objects are treated. You can also pass a Searchlogic::Conditions::Base object as well and it will function the same way.
|
17
|
+
#
|
18
|
+
# === Automatic hidden fields generation
|
19
|
+
#
|
20
|
+
# If you pass a Searchlogic::Search::Base object it automatically adds the :order_by, :order_as, :priority_order_by, :priority_order_as, and :per_page hidden fields. This is done so that when someone
|
21
|
+
# creates a new search, their options are remembered. It keeps the search consistent and is much more user friendly. If you want to override this you can pass the
|
22
|
+
# following options or you can set this up in your configuration, see Searchlogic::Config for more details.
|
23
|
+
#
|
24
|
+
# Lastly some light javascript is added to the "onsubmit" action. You will notice that the control type helpers add in hidden fields in the page, as a way to declare its "state". The form
|
25
|
+
# finds these elements, gets their values and updates its hidden fields so that the correct values will be submitted during the search. The end result is having these search options remembered.
|
26
|
+
#
|
27
|
+
# === Options
|
28
|
+
#
|
29
|
+
# * <tt>:hidden_fields</tt> --- Array, a list of hidden fields to include. Defaults to [:order_by, :order_as, :per_page]. Pass false, nil, or a blank array to not include any.
|
30
|
+
module Form
|
31
|
+
module Shared # :nodoc:
|
32
|
+
private
|
33
|
+
def searchlogic_object?(object)
|
34
|
+
object.is_a?(Search::Base) || object.is_a?(Conditions::Base)
|
35
|
+
end
|
36
|
+
|
37
|
+
def find_searchlogic_object(args)
|
38
|
+
search_object = nil
|
39
|
+
|
40
|
+
case args.first
|
41
|
+
when String, Symbol
|
42
|
+
begin
|
43
|
+
search_object = searchlogic_object?(args[1]) ? args[1] : instance_variable_get("@#{args.first}")
|
44
|
+
rescue Exception
|
45
|
+
end
|
46
|
+
when Array
|
47
|
+
search_object = args.first.last
|
48
|
+
else
|
49
|
+
search_object = args.first
|
50
|
+
end
|
51
|
+
|
52
|
+
searchlogic_object?(search_object) ? search_object : nil
|
53
|
+
end
|
54
|
+
|
55
|
+
def extract_searchlogic_options!(args)
|
56
|
+
options = args.extract_options!
|
57
|
+
searchlogic_options = {}
|
58
|
+
[:hidden_fields].each { |option| searchlogic_options[option] = options.has_key?(option) ? options.delete(option) : Config.helpers.send(option) }
|
59
|
+
searchlogic_options[:hidden_fields] = [searchlogic_options[:hidden_fields]].flatten.compact
|
60
|
+
args << options
|
61
|
+
searchlogic_options
|
62
|
+
end
|
63
|
+
|
64
|
+
def searchlogic_args(args, search_object, search_options, for_helper = nil)
|
65
|
+
args = args.dup
|
66
|
+
first = args.shift
|
67
|
+
|
68
|
+
# Rearrange args with name first, search_obj second
|
69
|
+
case first
|
70
|
+
when String, Symbol
|
71
|
+
args.unshift(search_object).unshift(first)
|
72
|
+
else
|
73
|
+
name = search_object.is_a?(Conditions::Base) ? (search_object.relationship_name || :conditions) : :search
|
74
|
+
args.unshift(search_object).unshift(name)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Now that we are consistent, get the name
|
78
|
+
name = args.first
|
79
|
+
|
80
|
+
# Add in some form magic to keep searching consisten and user friendly
|
81
|
+
if for_helper != :fields_for
|
82
|
+
options = args.extract_options!
|
83
|
+
|
84
|
+
# Set some defaults
|
85
|
+
options[:html] ||= {}
|
86
|
+
options[:html][:method] ||= :get
|
87
|
+
options[:method] ||= options[:html][:method] if for_helper == :remote_form_for
|
88
|
+
#options[:html][:id] ||= searchlogic_form_id(search_object)
|
89
|
+
|
90
|
+
if !search_options[:hidden_fields].blank?
|
91
|
+
options[:html][:onsubmit] ||= ""
|
92
|
+
options[:html][:onsubmit] += ";"
|
93
|
+
|
94
|
+
javascript = ""
|
95
|
+
javascript += "if(typeof(Prototype) != 'undefined') {" if Config.helpers.javascript_library.blank?
|
96
|
+
search_options[:hidden_fields].each { |field| javascript += "field = $('#{name}_#{field}'); if(field) { $('#{name}_#{field}_hidden').value = field.value; }" } if Config.helpers.javascript_library.blank? || Config.helpers.javascript_library == :prototype
|
97
|
+
javascript += "} else if(typeof(jQuery) != 'undefined') {" if Config.helpers.javascript_library.blank?
|
98
|
+
search_options[:hidden_fields].each { |field| javascript += "field = $('##{name}_#{field}'); if(field) { $('##{name}_#{field}_hidden').val(field.val()); }" } if Config.helpers.javascript_library.blank? || Config.helpers.javascript_library == :jquery
|
99
|
+
javascript += "}" if Config.helpers.javascript_library.blank?
|
100
|
+
|
101
|
+
options[:html][:onsubmit] += javascript
|
102
|
+
end
|
103
|
+
|
104
|
+
# Setup options
|
105
|
+
case first
|
106
|
+
when Array
|
107
|
+
first.pop
|
108
|
+
first << search_object.klass.new
|
109
|
+
options[:url] ||= polymorphic_path(first)
|
110
|
+
else
|
111
|
+
options[:url] ||= polymorphic_path(search_object.klass.new)
|
112
|
+
end
|
113
|
+
|
114
|
+
args << options
|
115
|
+
end
|
116
|
+
|
117
|
+
args
|
118
|
+
end
|
119
|
+
|
120
|
+
def insert_searchlogic_fields(args, search_object, search_options, &block)
|
121
|
+
return unless search_object.is_a?(Search::Base)
|
122
|
+
name = args.first
|
123
|
+
options = args.extract_options!
|
124
|
+
options
|
125
|
+
search_options[:hidden_fields].each do |field|
|
126
|
+
html = hidden_field(name, field, :object => search_object, :id => "#{name}_#{field}_hidden", :value => (field == :order_by ? searchlogic_base64_value(search_object.order_by) : search_object.send(field)))
|
127
|
+
|
128
|
+
# For edge rails and older version compatibility, passing a binding to concat was deprecated
|
129
|
+
begin
|
130
|
+
concat(html)
|
131
|
+
rescue ArgumentError, NameError
|
132
|
+
concat(html, block.binding)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
args << options
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
module Base # :nodoc:
|
140
|
+
include Shared
|
141
|
+
|
142
|
+
def fields_for_with_searchlogic(*args, &block)
|
143
|
+
search_object = find_searchlogic_object(args)
|
144
|
+
if search_object
|
145
|
+
searchlogic_options = extract_searchlogic_options!(args)
|
146
|
+
new_args = searchlogic_args(args, search_object, searchlogic_options, :fields_for)
|
147
|
+
insert_searchlogic_fields(new_args, search_object, searchlogic_options, &block)
|
148
|
+
fields_for_without_searchlogic(*new_args, &block)
|
149
|
+
else
|
150
|
+
fields_for_without_searchlogic(*args, &block)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def form_for_with_searchlogic(*args, &block)
|
155
|
+
search_object = find_searchlogic_object(args)
|
156
|
+
if search_object
|
157
|
+
searchlogic_options = extract_searchlogic_options!(args)
|
158
|
+
form_for_without_searchlogic(*searchlogic_args(args, search_object, searchlogic_options, :form_for), &block)
|
159
|
+
else
|
160
|
+
form_for_without_searchlogic(*args, &block)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def remote_form_for_with_searchlogic(*args, &block)
|
165
|
+
search_object = find_searchlogic_object(args)
|
166
|
+
if search_object
|
167
|
+
searchlogic_options = extract_searchlogic_options!(args)
|
168
|
+
remote_form_for_without_searchlogic(*searchlogic_args(args, search_object, searchlogic_options, :remote_form_for), &block)
|
169
|
+
else
|
170
|
+
remote_form_for_without_searchlogic(*args, &block)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
module FormBuilder # :nodoc:
|
176
|
+
include Shared
|
177
|
+
|
178
|
+
def fields_for_with_searchlogic(*args, &block)
|
179
|
+
search_object = find_searchlogic_object(args)
|
180
|
+
if search_object
|
181
|
+
searchlogic_options = extract_searchlogic_options!(args)
|
182
|
+
new_args = searchlogic_args(args, search_object, searchlogic_options, :fields_for)
|
183
|
+
insert_searchlogic_fields(new_args, search_object, searchlogic_options, &block)
|
184
|
+
fields_for_without_searchlogic(*new_args, &block)
|
185
|
+
else
|
186
|
+
fields_for_without_searchlogic(*args, &block)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
if defined?(ActionView)
|
195
|
+
ActionView::Base.send(:include, Searchlogic::Helpers::Form::Base)
|
196
|
+
|
197
|
+
ActionView::Base.class_eval do
|
198
|
+
alias_method_chain :fields_for, :searchlogic
|
199
|
+
alias_method_chain :form_for, :searchlogic
|
200
|
+
alias_method_chain :remote_form_for, :searchlogic
|
201
|
+
end
|
202
|
+
|
203
|
+
ActionView::Helpers::FormBuilder.send(:include, Searchlogic::Helpers::Form::FormBuilder)
|
204
|
+
|
205
|
+
ActionView::Helpers::FormBuilder.class_eval do
|
206
|
+
alias_method_chain :fields_for, :searchlogic
|
207
|
+
end
|
208
|
+
end
|