Skip to content
Browse files

Released v 9.0.9.10. Some small bug fixes

  • Loading branch information...
1 parent 3966652 commit 2ec93e327908b03768558cb4321b8630db931385 @binarylogic committed Sep 8, 2008
View
4 CHANGELOG
@@ -1,10 +1,12 @@
+v0.9.10. Hardened more tests, fixed bug with setting the per_page configuration to only take effect on protected searches, thus staying out of the way of normal searching.
+
v0.9.9. Fixed setting per_page to nil, false, or ''. This is done to "show all" results.
v0.9.8. Fixed order_by helper bug when determing the text with arrays. Should use the first value instead of last. Added in per_page config option.
v0.9.7. Complete class restructure, much more organized, added in documentation, added in helpers for using searchgasm in a rails app, updated readme with link to documentation as well as a live example, some bug fixes, more tests
-v0.9.6. Fix bug when instantiating with nil options
+v0.9.6. Fixed bug when instantiating with nil options
v0.9.5. Enhanced searching with conditions only. Updated read me to include example on adding your own conditions.
View
8 README.rdoc
@@ -163,9 +163,9 @@ Any of the options used in the above example can be used in these, but for the s
User.first(:conditions => {:age_gt => 18}, :per_page => 20)
- User.find(:all, :conditions => {::age_gt => 18}, :per_page => 20)
+ User.find(:all, :conditions => {:age_gt => 18}, :per_page => 20)
- User.find(:first, :conditions => {::age_gt => 18}, :per_page => 20)
+ User.find(:first, :conditions => {:age_gt => 18}, :per_page => 20)
search = User.new_search(:conditions => {:age_gt => 18}) # build_search is an alias
search.conditions.first_name_contains = "Ben"
@@ -181,6 +181,8 @@ If you want to use Searchgasm directly:
== Search with conditions only
+Don't need pagination, ordering, or any of the other options? Search with conditions only.
+
conditions = User.new_conditions(:age_gt => 18)
conditions.first_name_contains = "Ben"
conditions.all
@@ -196,8 +198,6 @@ Again, if you want to use Searchgasm directly:
conditions.first_name_contains = "Ben"
conditions.all
-Now pass the conditions object right into form\_for or fields\_for (see above for example).
-
== Scoped searching
@current_user.orders.find(:all, :conditions => {:total_lte => 500})
View
2 lib/searchgasm.rb
@@ -50,8 +50,8 @@ module Search
class Base
include Conditions
include Ordering
- include Pagination
include Protection
+ include Pagination
end
end
View
16 lib/searchgasm/config.rb
@@ -43,6 +43,19 @@ def desc_indicator=(value)
@desc_indicator = value
end
+ def pages_text # :nodoc:
+ @set_pages_text ? @pages_text : "Page %p"
+ end
+
+ # The default value for the :text options in the pages helper.
+ #
+ # * <tt>Default:</tt> "Page %p"
+ # * <tt>Accepts:</tt> "Your string %p": where %p is replaced by the page number, or a block: Proc.new { |p| "Page #{p}" }
+ def pages_text=(value)
+ @set_pages_text = true
+ @pages_text
+ end
+
def pages_type # :nodoc:
@pages_type ||= :select
end
@@ -59,7 +72,8 @@ def per_page # :nodoc:
@per_page
end
- # The default for per page.
+ # The default for per page. This is only applicaple for protected searches. Meaning you start the search with new_search or new_conditions.
+ # The reason for this not to disturb regular queries such as Whatever.find(:all). You would not expect that to be limited.
#
# * <tt>Default:</tt> nil, nil means "show all"
# * <tt>Accepts:</tt> Any value in your per_page choices
View
18 lib/searchgasm/helpers/search_helper.rb
@@ -73,6 +73,7 @@ def order_by(column_name, options = {}, &block)
# === Options
# * <tt>:type</tt> -- default: :select, pass :links as an alternative to have flickr like pagination
# * <tt>:remote</tt> -- default: false, if true requests will be AJAX
+ # * <tt>:text</tt> -- default: "1", "2", etc, if you want to change the text to say "Page 1", "Page 2", etc. Set this to: "Page %p". I will replace %p with the page #. Or pass a block and I will pass you the page number: Proc.new { |p| "Page #{p}" }
#
# === Advanced Options
# * <tt>:action</tt> -- this is automatically determined for you based on the type. For a :select type, its :onchange. For a :links type, its :onclick. You shouldn't have to use this option unless you are doing something out of the norm. The point of this option is to return a URL that will include this.value or not
@@ -85,6 +86,19 @@ def pages(options = {})
add_searchgasm_helper_defaults!(options, :page)
return "" if options[:search_obj].page_count <= 1
+ page_range = (1..options[:search_obj].page_count)
+ options[:text] = Config.pages_text unless options.has_key?(:text)
+
+ choices = nil
+ case options[:text]
+ when String
+ choices = page_range.collect { |p| options[:text].gsub(/%p/, p.to_s) }
+ when Proc
+ choices = page_range.collect { |p| yield p }
+ else
+ choices = page_range
+ end
+
if block_given?
yield options
else
@@ -94,7 +108,7 @@ def pages(options = {})
options[:html][options[:action]] ||= ""
options[:html][options[:action]] += ";"
options[:html][options[:action]] += options[:url]
- select(:search, :page, (1..options[:search_obj].page_count), {}, options[:html])
+ select(options[:params_scope], :page, choices, {:selected => options[:search_obj].page}, options[:html])
else
# HTML for links
end
@@ -153,7 +167,7 @@ def per_page(options = {})
options[:html][options[:action]] ||= ""
options[:html][options[:action]] += ";"
options[:html][options[:action]] += options[:url]
- select(:search, :per_page, options[:choices], {}, options[:html])
+ select(options[:params_scope], :per_page, options[:choices], {:selected => options[:search_obj].per_page}, options[:html])
end
end
end
View
5 lib/searchgasm/search/base.rb
@@ -49,12 +49,7 @@ def inspect
"#<#{klass} #{options_as_nice_string}>"
end
- def limit
- @set_limit ? @limit : Config.per_page
- end
-
def limit=(value)
- @set_limit = true
@limit = value.blank? || value == 0 ? nil : value.to_i
end
View
8 lib/searchgasm/search/pagination.rb
@@ -11,15 +11,15 @@ def self.included(klass)
end
def limit_with_pagination=(value)
- self.limit_without_pagination = value
+ r_value = self.limit_without_pagination = value
self.page = @page unless @page.nil? # retry page now that the limit has changed
- limit
+ r_value
end
def offset_with_pagination=(value)
- self.offset_without_pagination = value
+ r_value = self.offset_without_pagination = value
@page = nil
- offset
+ r_value
end
def page
View
13 lib/searchgasm/search/protection.rb
@@ -10,10 +10,22 @@ module Protection
def self.included(klass)
klass.class_eval do
attr_reader :protect
+ alias_method_chain :limit, :protection
+ alias_method_chain :limit=, :protection
alias_method_chain :options=, :protection
end
end
+ def limit_with_protection
+ return Config.per_page if protected? && !@set_limit
+ limit_without_protection
+ end
+
+ def limit_with_protection=(value)
+ @set_limit = true
+ self.limit_without_protection = value
+ end
+
def options_with_protection=(values)
return unless values.is_a?(Hash)
self.protect = values.delete(:protect) if values.has_key?(:protect) # make sure we do this first
@@ -29,6 +41,7 @@ def protect=(value)
def protect?
protect == true
end
+ alias_method :protected?, :protect?
private
def order_by_safe?(order_by, alt_klass = nil)
View
2 lib/searchgasm/version.rb
@@ -67,7 +67,7 @@ def to_a
MAJOR = 0
MINOR = 9
- TINY = 9
+ TINY = 10
# The current version as a Version instance
CURRENT = new(MAJOR, MINOR, TINY)
View
2 test/test_active_record_base.rb
@@ -35,7 +35,7 @@ def test_valid_find_options
def test_build_search
search = Account.new_search
assert_kind_of Searchgasm::Search::Base, search
-
+
search = Account.build_search(:conditions => {:name_keywords => "awesome"}, :page => 2, :per_page => 15)
assert_kind_of Searchgasm::Search::Base, search
assert_equal Account, search.klass
View
2 test/test_search_base.rb
@@ -40,7 +40,7 @@ def test_setting_first_level_options
assert_equal "test", search.joins
search.page = 5
- assert_equal 1, search.page # haven't set a limit yet
+ assert_equal 1, search.page
assert_equal nil, search.offset
search.limit = 20
View
1 test/text_config.rb
@@ -0,0 +1 @@
+# need to test that setting config doesn't mess up regular searches, only protected ones, etc

0 comments on commit 2ec93e3

Please sign in to comment.
Something went wrong with that request. Please try again.