schof-searchlogic 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +302 -0
- data/MIT-LICENSE +20 -0
- data/Manifest +157 -0
- data/README.rdoc +461 -0
- data/Rakefile +13 -0
- data/TODO.rdoc +4 -0
- data/init.rb +1 -0
- data/lib/searchlogic.rb +100 -0
- data/lib/searchlogic/active_record/associations.rb +52 -0
- data/lib/searchlogic/active_record/base.rb +224 -0
- data/lib/searchlogic/active_record/connection_adapters/mysql_adapter.rb +176 -0
- data/lib/searchlogic/active_record/connection_adapters/postgresql_adapter.rb +172 -0
- data/lib/searchlogic/active_record/connection_adapters/sqlite_adapter.rb +80 -0
- data/lib/searchlogic/condition/base.rb +165 -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 +11 -0
- data/lib/searchlogic/condition/ends_with.rb +17 -0
- data/lib/searchlogic/condition/equals.rb +33 -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 +10 -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/nested_set.rb +17 -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 +27 -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/conditions/any_or_all.rb +42 -0
- data/lib/searchlogic/conditions/base.rb +244 -0
- data/lib/searchlogic/conditions/groups.rb +74 -0
- data/lib/searchlogic/conditions/magic_methods.rb +286 -0
- data/lib/searchlogic/conditions/multiparameter_attributes.rb +105 -0
- data/lib/searchlogic/conditions/protection.rb +36 -0
- data/lib/searchlogic/config.rb +31 -0
- data/lib/searchlogic/config/helpers.rb +338 -0
- data/lib/searchlogic/config/search.rb +53 -0
- data/lib/searchlogic/core_ext/hash.rb +75 -0
- data/lib/searchlogic/core_ext/object.rb +19 -0
- data/lib/searchlogic/helpers/control_types/link.rb +310 -0
- data/lib/searchlogic/helpers/control_types/links.rb +242 -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/avg.rb +15 -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/count.rb +11 -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/sum.rb +11 -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 +32 -0
- data/lib/searchlogic/shared/utilities.rb +56 -0
- data/lib/searchlogic/shared/virtual_classes.rb +39 -0
- data/lib/searchlogic/version.rb +79 -0
- data/searchlogic.gemspec +41 -0
- data/test/active_record_tests/associations_test.rb +94 -0
- data/test/active_record_tests/base_test.rb +115 -0
- data/test/condition_tests/base_test.rb +54 -0
- data/test/condition_tests/begins_with_test.rb +11 -0
- data/test/condition_tests/blank_test.rb +31 -0
- data/test/condition_tests/child_of_test.rb +17 -0
- data/test/condition_tests/descendant_of_test.rb +12 -0
- data/test/condition_tests/ends_with_test.rb +11 -0
- data/test/condition_tests/equals_test.rb +28 -0
- data/test/condition_tests/greater_than_or_equal_to_test.rb +11 -0
- data/test/condition_tests/greater_than_test.rb +11 -0
- data/test/condition_tests/inclusive_descendant_of_test.rb +12 -0
- data/test/condition_tests/keywords_test.rb +23 -0
- data/test/condition_tests/less_than_or_equal_to_test.rb +11 -0
- data/test/condition_tests/less_than_test.rb +11 -0
- data/test/condition_tests/like_test.rb +11 -0
- data/test/condition_tests/nil_test.rb +31 -0
- data/test/condition_tests/not_begin_with_test.rb +8 -0
- data/test/condition_tests/not_blank_test.rb +8 -0
- data/test/condition_tests/not_end_with_test.rb +8 -0
- data/test/condition_tests/not_equal_test.rb +19 -0
- data/test/condition_tests/not_have_keywords_test.rb +8 -0
- data/test/condition_tests/not_like_test.rb +8 -0
- data/test/condition_tests/not_nil_test.rb +13 -0
- data/test/condition_tests/sibling_of_test.rb +15 -0
- data/test/conditions_tests/any_or_all_test.rb +23 -0
- data/test/conditions_tests/base_test.rb +185 -0
- data/test/conditions_tests/groups_test.rb +68 -0
- data/test/conditions_tests/magic_methods_test.rb +36 -0
- data/test/conditions_tests/multiparameter_attributes_test.rb +15 -0
- data/test/conditions_tests/protection_test.rb +18 -0
- data/test/config_test.rb +23 -0
- data/test/fixtures/accounts.yml +12 -0
- data/test/fixtures/animals.yml +7 -0
- data/test/fixtures/orders.yml +12 -0
- data/test/fixtures/user_groups.yml +5 -0
- data/test/fixtures/users.yml +45 -0
- data/test/libs/awesome_nested_set.rb +545 -0
- data/test/libs/awesome_nested_set/compatability.rb +29 -0
- data/test/libs/awesome_nested_set/helper.rb +40 -0
- data/test/libs/awesome_nested_set/named_scope.rb +140 -0
- data/test/libs/rexml_fix.rb +14 -0
- data/test/modifier_tests/day_of_month_test.rb +16 -0
- data/test/search_tests/base_test.rb +241 -0
- data/test/search_tests/conditions_test.rb +21 -0
- data/test/search_tests/ordering_test.rb +167 -0
- data/test/search_tests/pagination_test.rb +74 -0
- data/test/search_tests/protection_test.rb +26 -0
- data/test/test_helper.rb +116 -0
- metadata +385 -0
@@ -0,0 +1,53 @@
|
|
1
|
+
module Searchlogic
|
2
|
+
class Config
|
3
|
+
# = Search Configuration
|
4
|
+
# Provides configuration for searchlogic
|
5
|
+
#
|
6
|
+
# === Example
|
7
|
+
# Searchlogic::Config.configure do |config|
|
8
|
+
# config.search.per_page = 50
|
9
|
+
# end
|
10
|
+
class Search
|
11
|
+
class << self
|
12
|
+
# The default for per page. This is only applicaple for protected searches. Meaning you start the search with new_search or new_conditions.
|
13
|
+
# The reason for this not to disturb regular queries such as Whatever.find(:all). You would not expect that to be limited.
|
14
|
+
#
|
15
|
+
# * <tt>Default:</tt> The 2nd option in your per_page_choices, default of 25
|
16
|
+
# * <tt>Accepts:</tt> Any value in your per_page choices, nil or a blank string means "show all"
|
17
|
+
def per_page
|
18
|
+
return @per_page if @set_per_page
|
19
|
+
per_page = Helpers.per_page_select_choices[1]
|
20
|
+
per_page = per_page.last if per_page.is_a?(Array)
|
21
|
+
@per_page = per_page
|
22
|
+
end
|
23
|
+
|
24
|
+
def per_page=(value)
|
25
|
+
@set_per_page = true
|
26
|
+
@per_page = value
|
27
|
+
end
|
28
|
+
|
29
|
+
# If you are using ActiveRecord < 2.2.0 then ActiveRecord does not remove duplicates when using the :joins option, when it should. To fix this problem searchlogic does this for you. Searchlogic tries to act
|
30
|
+
# just like ActiveRecord, but in this instance it doesn't make sense.
|
31
|
+
#
|
32
|
+
# As a result, Searchlogic removes all duplicates results in *ALL* search / calculation queries. It does this by forcing the DISTINCT or GROUP BY operation in your SQL. Which might come as a surprise to you
|
33
|
+
# since it is not the "norm". If you don't want searchlogic to do this, set this to false.
|
34
|
+
#
|
35
|
+
# * <tt>Default:</tt> true
|
36
|
+
# * <tt>Accepts:</tt> Boolean
|
37
|
+
def remove_duplicates
|
38
|
+
return @remove_duplicates if @set_remove_duplicates
|
39
|
+
@remove_duplicates ||= true
|
40
|
+
end
|
41
|
+
|
42
|
+
def remove_duplicates? # :nodoc:
|
43
|
+
remove_duplicates == true
|
44
|
+
end
|
45
|
+
|
46
|
+
def remove_duplicates=(value) # :nodoc:
|
47
|
+
@set_remove_duplicates = true
|
48
|
+
@remove_duplicates = value
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Searchlogic
|
2
|
+
module CoreExt # :nodoc: all
|
3
|
+
module Hash
|
4
|
+
def deep_dup
|
5
|
+
new_hash = {}
|
6
|
+
|
7
|
+
self.each do |k, v|
|
8
|
+
case v
|
9
|
+
when Hash
|
10
|
+
new_hash[k] = v.deep_dup
|
11
|
+
else
|
12
|
+
new_hash[k] = v
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
new_hash
|
17
|
+
end
|
18
|
+
|
19
|
+
def deep_delete_duplicate_keys(hash)
|
20
|
+
hash.each do |k, v|
|
21
|
+
if v.is_a?(Hash) && self[k]
|
22
|
+
self[k].deep_delete_duplicate_keys(v)
|
23
|
+
delete(k) if self[k].blank?
|
24
|
+
else
|
25
|
+
delete(k)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def deep_delete(value)
|
33
|
+
case value
|
34
|
+
when Array
|
35
|
+
value.each { |v| deep_delete(v) }
|
36
|
+
when Hash
|
37
|
+
value.each do |k, v|
|
38
|
+
next unless self[k].is_a?(Hash)
|
39
|
+
|
40
|
+
case v
|
41
|
+
when Hash, Array
|
42
|
+
self[k].deep_delete(v)
|
43
|
+
when String, Symbol
|
44
|
+
self[k].delete(v)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
when String, Symbol
|
48
|
+
delete(value)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def deep_merge(other_hash)
|
53
|
+
self.merge(other_hash) do |key, oldval, newval|
|
54
|
+
oldval = oldval.to_hash if oldval.respond_to?(:to_hash)
|
55
|
+
newval = newval.to_hash if newval.respond_to?(:to_hash)
|
56
|
+
oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ? oldval.deep_merge(newval) : newval
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Returns a new hash with +self+ and +other_hash+ merged recursively.
|
61
|
+
# Modifies the receiver in place.
|
62
|
+
def deep_merge!(other_hash)
|
63
|
+
replace(deep_merge(other_hash))
|
64
|
+
end
|
65
|
+
|
66
|
+
# assert_valid_keys was killing performance. Array.flatten was the culprit, so I rewrote this method, got a 35% performance increase
|
67
|
+
def fast_assert_valid_keys(valid_keys)
|
68
|
+
unknown_keys = keys - valid_keys
|
69
|
+
raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
Hash.send(:include, Searchlogic::CoreExt::Hash)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Searchlogic
|
2
|
+
module CoreExt
|
3
|
+
module Object
|
4
|
+
# Get object's meta (ghost, eigenclass, singleton) class
|
5
|
+
def metaclass
|
6
|
+
class << self
|
7
|
+
self
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# If class_eval is called on an object, add those methods to its metaclass
|
12
|
+
def class_eval(*args, &block)
|
13
|
+
metaclass.class_eval(*args, &block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Object.send(:include, Searchlogic::CoreExt::Object)
|
@@ -0,0 +1,310 @@
|
|
1
|
+
module Searchlogic
|
2
|
+
module Helpers
|
3
|
+
# = Control Type Helpers
|
4
|
+
#
|
5
|
+
# The purpose of these helpers is to make ordering and paginating data, in your view, a breeze. Everyone has their own flavor of displaying data, so I made these helpers extra flexible, just for you.
|
6
|
+
#
|
7
|
+
# === Tutorial
|
8
|
+
#
|
9
|
+
# Check out my tutorial on how to implement searchlogic into a rails app: http://www.binarylogic.com/2008/9/7/tutorial-pagination-ordering-and-searching-with-searchlogic
|
10
|
+
#
|
11
|
+
# === How it's organized
|
12
|
+
#
|
13
|
+
# If we break it down, you can do 4 different things with your data in your view:
|
14
|
+
#
|
15
|
+
# 1. Order your data by a single column or an array of columns
|
16
|
+
# 2. Descend or ascend your data
|
17
|
+
# 3. Change how many items are on each page
|
18
|
+
# 4. Paginate through your data
|
19
|
+
#
|
20
|
+
# Each one of these actions comes with 3 different types of helpers:
|
21
|
+
#
|
22
|
+
# 1. Link - A single link for a single value. Requires that you pass a value as the first parameter.
|
23
|
+
# 2. Links - A group of single links.
|
24
|
+
# 3. Select - A select with choices that perform an action once selected. Basically the same thing as a group of links, but just as a select form element
|
25
|
+
# 4. Remote - lets you prefix any of these helpers with "remote_" and it will use the built in rails ajax helpers. I highly recommend unobstrusive javascript though, using jQuery.
|
26
|
+
#
|
27
|
+
# === Examples
|
28
|
+
#
|
29
|
+
# Sometimes the best way to explain something is with some examples. Let's pretend we are performing these actions on a User model. Check it out:
|
30
|
+
#
|
31
|
+
# order_by_link(:name)
|
32
|
+
# => produces a single link that when clicked will order by the name column, and each time its clicked alternated between "ASC" and "DESC"
|
33
|
+
#
|
34
|
+
# order_by_links
|
35
|
+
# => produces a group of links for all of the columns in your users table, each link is basically order_by_link(column.name)
|
36
|
+
#
|
37
|
+
# order_by_select
|
38
|
+
# => produces a select form element with all of the user's columns as choices, when the value is change (onchange) it will act as if they clicked a link.
|
39
|
+
# => This is just order_by_links as a select form element, nothing fancy
|
40
|
+
#
|
41
|
+
# What about paginating? I got you covered:
|
42
|
+
#
|
43
|
+
# page_link(2)
|
44
|
+
# => creates a link to page 2
|
45
|
+
#
|
46
|
+
# page_links
|
47
|
+
# => creates a group of links for pages, similar to a flickr style of pagination
|
48
|
+
#
|
49
|
+
# page_select
|
50
|
+
# => creates a drop down instead of a group of links. The user can select the page in the drop down and it will be as if they clicked a link for that page.
|
51
|
+
#
|
52
|
+
# You can apply the _link, _links, or _select to any of the following: order_by, order_as, per_page, page. You have your choice on how you want to set up the interface. For more information and options on these individual
|
53
|
+
# helpers check out their source files. Look at the sub modules under this one (Ex: Searchlogic::Helpers::ControlTypes::Select)
|
54
|
+
module ControlTypes
|
55
|
+
# = Link Control Types
|
56
|
+
#
|
57
|
+
# These helpers make ordering and paginating your data a breeze in your view. They only produce links.
|
58
|
+
module Link
|
59
|
+
# Creates a link for ordering data by a column or columns
|
60
|
+
#
|
61
|
+
# === Example uses for a User class that has many orders
|
62
|
+
#
|
63
|
+
# order_by_link(:first_name)
|
64
|
+
# order_by_link([:first_name, :last_name])
|
65
|
+
# order_by_link({:orders => :total})
|
66
|
+
# order_by_link([{:orders => :total}, :first_name])
|
67
|
+
# order_by_link(:id, :text => "Order Number", :html => {:class => "order_number"})
|
68
|
+
#
|
69
|
+
# What's nifty about this is that the value gets "serialized", if it is not a string or a symbol, so that it can be passed via a param in the url. Searchlogic will automatically try to "unserializes" this value and use it. This allows you
|
70
|
+
# to pass complex objects besides strings and symbols, such as arrays and hashes. All of the hard work is done for you.
|
71
|
+
#
|
72
|
+
# Another thing to keep in mind is that this will alternate between "asc" and "desc" each time it is clicked.
|
73
|
+
#
|
74
|
+
# === Options
|
75
|
+
# * <tt>:text</tt> -- default: column_name.to_s.humanize, text for the link
|
76
|
+
# * <tt>:desc_indicator</tt> -- default: ▼, the indicator that this column is descending
|
77
|
+
# * <tt>:asc_indicator</tt> -- default: ▲, the indicator that this column is ascending
|
78
|
+
# * <tt>:html</tt> -- html arrtributes for the <a> tag.
|
79
|
+
#
|
80
|
+
# === Advanced Options
|
81
|
+
# * <tt>:params_scope</tt> -- default: :search, this is the scope in which your search params will be preserved (params[:search]). If you don't want a scope and want your options to be at base level in params such as params[:page], params[:per_page], etc, then set this to nil.
|
82
|
+
# * <tt>:search_obj</tt> -- default: @#{params_scope}, this is your search object, everything revolves around this. It will try to infer the name from your params_scope. If your params_scope is :search it will try to get @search, etc. If it can not be inferred by this, you need to pass the object itself.
|
83
|
+
# * <tt>:params</tt> -- default: nil, Additional params to add to the url, must be a hash
|
84
|
+
# * <tt>:exclude_params</tt> -- default: nil, params you want to exclude. This is nifty because it does a "deep delete". So you can pass {:param1 => {:param2 => :param3}} and it will make sure param3 does not get included. param1 and param2 will not be touched. This also accepts an array or just a symbol or string.
|
85
|
+
# * <tt>:search_params</tt> -- default: nil, Additional search params to add to the url, must be a hash. Adds the options into the :params_scope.
|
86
|
+
# * <tt>:exclude_search_params</tt> -- default: nil, Same as :exclude_params but for the :search_params.
|
87
|
+
def order_by_link(order_by, options = {})
|
88
|
+
add_order_by_link_defaults!(order_by, options)
|
89
|
+
html = searchlogic_state(options)
|
90
|
+
|
91
|
+
if !options[:is_remote]
|
92
|
+
html += link_to(options[:text], options[:url], options[:html])
|
93
|
+
else
|
94
|
+
html += link_to_remote(options[:text], options[:remote].merge(:url => options[:url]), options[:html])
|
95
|
+
end
|
96
|
+
|
97
|
+
html
|
98
|
+
end
|
99
|
+
|
100
|
+
# Creates a link for ascending or descending data.
|
101
|
+
#
|
102
|
+
# === Example uses
|
103
|
+
#
|
104
|
+
# order_as_link("asc")
|
105
|
+
# order_as_link("desc")
|
106
|
+
# order_as_link("asc", :text => "Ascending", :html => {:class => "ascending"})
|
107
|
+
#
|
108
|
+
# === Options
|
109
|
+
# * <tt>:text</tt> -- default: column_name.to_s.humanize, text for the link
|
110
|
+
# * <tt>:html</tt> -- html arrtributes for the <a> tag.
|
111
|
+
#
|
112
|
+
# === Advanced Options
|
113
|
+
# * <tt>:params_scope</tt> -- default: :search, this is the scope in which your search params will be preserved (params[:search]). If you don't want a scope and want your options to be at base level in params such as params[:page], params[:per_page], etc, then set this to nil.
|
114
|
+
# * <tt>:search_obj</tt> -- default: @#{params_scope}, this is your search object, everything revolves around this. It will try to infer the name from your params_scope. If your params_scope is :search it will try to get @search, etc. If it can not be inferred by this, you need to pass the object itself.
|
115
|
+
# * <tt>:params</tt> -- default: nil, Additional params to add to the url, must be a hash
|
116
|
+
# * <tt>:exclude_params</tt> -- default: nil, params you want to exclude. This is nifty because it does a "deep delete". So you can pass {:param1 => {:param2 => :param3}} and it will make sure param3 does not get included. param1 and param2 will not be touched. This also accepts an array or just a symbol or string.
|
117
|
+
# * <tt>:search_params</tt> -- default: nil, Additional search params to add to the url, must be a hash. Adds the options into the :params_scope.
|
118
|
+
# * <tt>:exclude_search_params</tt> -- default: nil, Same as :exclude_params but for the :search_params.
|
119
|
+
def order_as_link(order_as, options = {})
|
120
|
+
add_order_as_link_defaults!(order_as, options)
|
121
|
+
html = searchlogic_state(options)
|
122
|
+
|
123
|
+
if !options[:is_remote]
|
124
|
+
html += link_to(options[:text], options[:url], options[:html])
|
125
|
+
else
|
126
|
+
html += link_to_remote(options[:text], options[:remote].merge(:url => options[:url]), options[:html])
|
127
|
+
end
|
128
|
+
|
129
|
+
html
|
130
|
+
end
|
131
|
+
|
132
|
+
# This is similar to order_by_link but with a small difference. The best way to explain priority ordering is with an example. Let's say you wanted to list products on a page. You have "featured" products
|
133
|
+
# that you want to show up first, no matter what. This is what this is all about. It makes ordering by featured products a priority, then searching by price, quantity, etc. is the same as it has always been.
|
134
|
+
#
|
135
|
+
# The difference between order_by_link and priority_order_by_link is that priority_order_by_link it just a switch. Turn it on or turn it off. You don't neccessarily want to flip between ASC and DESC. If you do
|
136
|
+
# then you should just incorporate this into your regular order_by, like: order_by_link [:featured, :price]
|
137
|
+
#
|
138
|
+
# === Example uses for a User class that has many orders
|
139
|
+
#
|
140
|
+
# priority_order_by_link(:featured, "DESC")
|
141
|
+
# order_by_link([:featured, :created_at], "ASC")
|
142
|
+
# order_by_link({:orders => :featured}, "ASC")
|
143
|
+
# order_by_link([{:orders => :featured}, :featured], "ASC")
|
144
|
+
# order_by_link(:featured, "ASC", :text => "Featured", :html => {:class => "featured_link"})
|
145
|
+
#
|
146
|
+
# === Options
|
147
|
+
# * <tt>:activate_text</tt> -- default: "Show #{column_name.to_s.humanize} first"
|
148
|
+
# * <tt>:deactivate_text</tt> -- default: "Don't show #{column_name.to_s.humanize} first", text for the link, text for the link
|
149
|
+
# * <tt>:column_name</tt> -- default: column_name.to_s.humanize, automatically inferred by what you are ordering by and is added into the active_text and deactive_text strings.
|
150
|
+
# * <tt>:text</tt> -- default: :activate_text or :deactivate_text depending on if its active or not, Overwriting this will make this text stay the same, no matter way. A good alternative would be "Toggle featured first"
|
151
|
+
# * <tt>:html</tt> -- html arrtributes for the <a> tag.
|
152
|
+
#
|
153
|
+
# === Advanced Options
|
154
|
+
# * <tt>:params_scope</tt> -- default: :search, this is the scope in which your search params will be preserved (params[:search]). If you don't want a scope and want your options to be at base level in params such as params[:page], params[:per_page], etc, then set this to nil.
|
155
|
+
# * <tt>:search_obj</tt> -- default: @#{params_scope}, this is your search object, everything revolves around this. It will try to infer the name from your params_scope. If your params_scope is :search it will try to get @search, etc. If it can not be inferred by this, you need to pass the object itself.
|
156
|
+
# * <tt>:params</tt> -- default: nil, Additional params to add to the url, must be a hash
|
157
|
+
# * <tt>:exclude_params</tt> -- default: nil, params you want to exclude. This is nifty because it does a "deep delete". So you can pass {:param1 => {:param2 => :param3}} and it will make sure param3 does not get included. param1 and param2 will not be touched. This also accepts an array or just a symbol or string.
|
158
|
+
# * <tt>:search_params</tt> -- default: nil, Additional search params to add to the url, must be a hash. Adds the options into the :params_scope.
|
159
|
+
# * <tt>:exclude_search_params</tt> -- default: nil, Same as :exclude_params but for the :search_params.
|
160
|
+
def priority_order_by_link(priority_order_by, priority_order_as, options = {})
|
161
|
+
add_priority_order_by_link_defaults!(priority_order_by, priority_order_as, options)
|
162
|
+
html = searchlogic_state(options)
|
163
|
+
|
164
|
+
if !options[:is_remote]
|
165
|
+
html += link_to(options[:text], options[:url], options[:html])
|
166
|
+
else
|
167
|
+
html += link_to_remote(options[:text], options[:remote].merge(:url => options[:url]), options[:html])
|
168
|
+
end
|
169
|
+
|
170
|
+
html
|
171
|
+
end
|
172
|
+
|
173
|
+
# Creates a link for limiting how many items are on each page
|
174
|
+
#
|
175
|
+
# === Example uses
|
176
|
+
#
|
177
|
+
# per_page_link(200)
|
178
|
+
# per_page_link(nil) # => Show all
|
179
|
+
# per_page_link(nil, :text => "All", :html => {:class => "show_all"})
|
180
|
+
#
|
181
|
+
# As you can see above, passing nil means "show all" and the text will automatically revert to "show all"
|
182
|
+
#
|
183
|
+
# === Options
|
184
|
+
# * <tt>:html</tt> -- html arrtributes for the <a> tag.
|
185
|
+
#
|
186
|
+
# === Advanced Options
|
187
|
+
# * <tt>:params_scope</tt> -- default: :search, this is the scope in which your search params will be preserved (params[:search]). If you don't want a scope and want your options to be at base level in params such as params[:page], params[:per_page], etc, then set this to nil.
|
188
|
+
# * <tt>:search_obj</tt> -- default: @#{params_scope}, this is your search object, everything revolves around this. It will try to infer the name from your params_scope. If your params_scope is :search it will try to get @search, etc. If it can not be inferred by this, you need to pass the object itself.
|
189
|
+
# * <tt>:params</tt> -- default: nil, Additional params to add to the url, must be a hash
|
190
|
+
# * <tt>:exclude_params</tt> -- default: nil, params you want to exclude. This is nifty because it does a "deep delete". So you can pass {:param1 => {:param2 => :param3}} and it will make sure param3 does not get included. param1 and param2 will not be touched. This also accepts an array or just a symbol or string.
|
191
|
+
# * <tt>:search_params</tt> -- default: nil, Additional search params to add to the url, must be a hash. Adds the options into the :params_scope.
|
192
|
+
# * <tt>:exclude_search_params</tt> -- default: nil, Same as :exclude_params but for the :search_params.
|
193
|
+
def per_page_link(per_page, options = {})
|
194
|
+
add_per_page_link_defaults!(per_page, options)
|
195
|
+
html = searchlogic_state(options)
|
196
|
+
|
197
|
+
if !options[:is_remote]
|
198
|
+
html += link_to(options[:text], options[:url], options[:html])
|
199
|
+
else
|
200
|
+
html += link_to_remote(options[:text], options[:remote].merge(:url => options[:url]), options[:html])
|
201
|
+
end
|
202
|
+
|
203
|
+
html
|
204
|
+
end
|
205
|
+
|
206
|
+
# Creates a link for changing to a sepcific page of your data
|
207
|
+
#
|
208
|
+
# === Example uses
|
209
|
+
#
|
210
|
+
# page_link(2)
|
211
|
+
# page_link(1)
|
212
|
+
# page_link(5, :text => "Fifth page", :html => {:class => "fifth_page"})
|
213
|
+
#
|
214
|
+
# === Options
|
215
|
+
# * <tt>:text</tt> -- default: column_name.to_s.humanize, text for the link
|
216
|
+
# * <tt>:html</tt> -- html arrtributes for the <a> tag.
|
217
|
+
#
|
218
|
+
# === Advanced Options
|
219
|
+
# * <tt>:params_scope</tt> -- default: :search, this is the scope in which your search params will be preserved (params[:search]). If you don't want a scope and want your options to be at base level in params such as params[:page], params[:per_page], etc, then set this to nil.
|
220
|
+
# * <tt>:search_obj</tt> -- default: @#{params_scope}, this is your search object, everything revolves around this. It will try to infer the name from your params_scope. If your params_scope is :search it will try to get @search, etc. If it can not be inferred by this, you need to pass the object itself.
|
221
|
+
# * <tt>:params</tt> -- default: nil, Additional params to add to the url, must be a hash
|
222
|
+
# * <tt>:exclude_params</tt> -- default: nil, params you want to exclude. This is nifty because it does a "deep delete". So you can pass {:param1 => {:param2 => :param3}} and it will make sure param3 does not get included. param1 and param2 will not be touched. This also accepts an array or just a symbol or string.
|
223
|
+
# * <tt>:search_params</tt> -- default: nil, Additional search params to add to the url, must be a hash. Adds the options into the :params_scope.
|
224
|
+
# * <tt>:exclude_search_params</tt> -- default: nil, Same as :exclude_params but for the :search_params.
|
225
|
+
def page_link(page, options = {})
|
226
|
+
add_page_link_defaults!(page, options)
|
227
|
+
html = searchlogic_state(options)
|
228
|
+
|
229
|
+
if !options[:is_remote]
|
230
|
+
html += link_to(options[:text], options[:url], options[:html])
|
231
|
+
else
|
232
|
+
html += link_to_remote(options[:text], options[:remote].merge(:url => options[:url]), options[:html])
|
233
|
+
end
|
234
|
+
|
235
|
+
html
|
236
|
+
end
|
237
|
+
|
238
|
+
private
|
239
|
+
def add_order_by_link_defaults!(order_by, options = {})
|
240
|
+
add_searchlogic_control_defaults!(options)
|
241
|
+
searchlogic_add_class!(options[:html], Config.helpers.order_by_link_class_name)
|
242
|
+
ordering_by_this = searchlogic_ordering_by?(order_by, options)
|
243
|
+
searchlogic_add_class!(options[:html], "ordering") if ordering_by_this
|
244
|
+
options[:text] ||= determine_order_by_text(order_by)
|
245
|
+
options[:asc_indicator] ||= Config.helpers.order_by_link_asc_indicator
|
246
|
+
options[:desc_indicator] ||= Config.helpers.order_by_link_desc_indicator
|
247
|
+
options[:text] += options[:search_obj].desc? ? options[:desc_indicator] : options[:asc_indicator] if ordering_by_this
|
248
|
+
options[:url] = searchlogic_params(options.merge(:search_params => {:order_by => order_by}))
|
249
|
+
options
|
250
|
+
end
|
251
|
+
|
252
|
+
def add_order_as_link_defaults!(order_as, options = {})
|
253
|
+
add_searchlogic_control_defaults!(options)
|
254
|
+
searchlogic_add_class!(options[:html], Config.helpers.order_as_link_class_name)
|
255
|
+
options[:text] ||= order_as.to_s
|
256
|
+
options[:url] = searchlogic_params(options.merge(:search_params => {:order_as => order_as}))
|
257
|
+
options
|
258
|
+
end
|
259
|
+
|
260
|
+
def add_priority_order_by_link_defaults!(priority_order_by, priority_order_as, options = {})
|
261
|
+
add_searchlogic_control_defaults!(options)
|
262
|
+
searchlogic_add_class!(options[:html], Config.helpers.priority_order_by_link_class_name)
|
263
|
+
options[:column_name] ||= determine_order_by_text(priority_order_by).downcase
|
264
|
+
options[:activate_text] ||= Config.helpers.priority_order_by_link_activate_text % options[:column_name]
|
265
|
+
options[:deactivate_text] ||= Config.helpers.priority_order_by_link_deactivate_text % options[:column_name]
|
266
|
+
active = deep_stringify(options[:search_obj].priority_order_by) == deep_stringify(priority_order_by) && options[:search_obj].priority_order_as == priority_order_as
|
267
|
+
options[:text] ||= active ? options[:deactivate_text] : options[:activate_text]
|
268
|
+
if active
|
269
|
+
options.merge!(:search_params => {:priority_order_by => nil, :priority_order_as => nil})
|
270
|
+
else
|
271
|
+
options.merge!(:search_params => {:priority_order_by => priority_order_by, :priority_order_as => priority_order_as})
|
272
|
+
end
|
273
|
+
options[:url] = searchlogic_params(options)
|
274
|
+
options
|
275
|
+
end
|
276
|
+
|
277
|
+
def add_per_page_link_defaults!(per_page, options = {})
|
278
|
+
add_searchlogic_control_defaults!(options)
|
279
|
+
searchlogic_add_class!(options[:html], Config.helpers.per_page_link_class_name)
|
280
|
+
options[:text] ||= per_page.to_s
|
281
|
+
options[:url] = searchlogic_params(options.merge(:search_params => {:per_page => per_page}))
|
282
|
+
options
|
283
|
+
end
|
284
|
+
|
285
|
+
def add_page_link_defaults!(page, options = {})
|
286
|
+
add_searchlogic_control_defaults!(options)
|
287
|
+
searchlogic_add_class!(options[:html], Config.helpers.page_link_class_name)
|
288
|
+
options[:text] ||= page.to_s
|
289
|
+
options[:url] = searchlogic_params(options.merge(:search_params => {:page => page}))
|
290
|
+
options
|
291
|
+
end
|
292
|
+
|
293
|
+
def determine_order_by_text(column_name, relationship_name = nil)
|
294
|
+
case column_name
|
295
|
+
when String, Symbol
|
296
|
+
relationship_name.blank? ? column_name.to_s.titleize : "#{relationship_name.to_s.titleize} #{column_name.to_s.titleize}"
|
297
|
+
when Array
|
298
|
+
determine_order_by_text(column_name.first)
|
299
|
+
when Hash
|
300
|
+
k = column_name.keys.first
|
301
|
+
v = column_name.values.first
|
302
|
+
determine_order_by_text(v, k)
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
ActionController::Base.helper Searchlogic::Helpers::ControlTypes::Link if defined?(ActionController)
|