Permalink
Browse files

Updated searchgasm

  • Loading branch information...
1 parent bcf6b5f commit 46b67220a8d4d2d73b14f71b780f8768283195cf @binarylogic committed Sep 29, 2008
Showing with 735 additions and 257 deletions.
  1. +1 −0 app/controllers/non_ajax/users_controller.rb
  2. +0 −7 config/database.example.yml
  3. +5 −5 config/environment.rb
  4. +21 −0 vendor/plugins/searchgasm/CHANGELOG.rdoc
  5. +9 −4 vendor/plugins/searchgasm/Manifest
  6. +1 −1 vendor/plugins/searchgasm/README.rdoc
  7. +2 −3 vendor/plugins/searchgasm/Rakefile
  8. +5 −0 vendor/plugins/searchgasm/TODO.rdoc
  9. +4 −3 vendor/plugins/searchgasm/lib/searchgasm.rb
  10. +15 −0 vendor/plugins/searchgasm/lib/searchgasm/active_record/connection_adapters/mysql_adapter.rb
  11. 0 vendor/plugins/searchgasm/lib/searchgasm/active_record/connection_adapters/postgresql_adapter.rb
  12. 0 vendor/plugins/searchgasm/lib/searchgasm/active_record/connection_adapters/sqlite_adapter.rb
  13. +26 −10 vendor/plugins/searchgasm/lib/searchgasm/condition/base.rb
  14. +5 −6 vendor/plugins/searchgasm/lib/searchgasm/condition/{is_blank.rb → blank.rb}
  15. +1 −1 vendor/plugins/searchgasm/lib/searchgasm/condition/does_not_equal.rb
  16. +32 −0 vendor/plugins/searchgasm/lib/searchgasm/condition/during_evening.rb
  17. +1 −1 vendor/plugins/searchgasm/lib/searchgasm/condition/equals.rb
  18. +2 −1 vendor/plugins/searchgasm/lib/searchgasm/condition/greater_than.rb
  19. +7 −1 vendor/plugins/searchgasm/lib/searchgasm/condition/greater_than_or_equal_to.rb
  20. +0 −23 vendor/plugins/searchgasm/lib/searchgasm/condition/is_nil.rb
  21. +2 −1 vendor/plugins/searchgasm/lib/searchgasm/condition/less_than.rb
  22. +7 −1 vendor/plugins/searchgasm/lib/searchgasm/condition/less_than_or_equal_to.rb
  23. +21 −0 vendor/plugins/searchgasm/lib/searchgasm/condition/nil.rb
  24. +6 −3 vendor/plugins/searchgasm/lib/searchgasm/conditions/base.rb
  25. +24 −2 vendor/plugins/searchgasm/lib/searchgasm/config.rb
  26. +37 −3 vendor/plugins/searchgasm/lib/searchgasm/core_ext/hash.rb
  27. +80 −25 vendor/plugins/searchgasm/lib/searchgasm/helpers/control_types/link.rb
  28. +1 −1 vendor/plugins/searchgasm/lib/searchgasm/helpers/control_types/remote_select.rb
  29. +4 −4 vendor/plugins/searchgasm/lib/searchgasm/helpers/control_types/select.rb
  30. +1 −1 vendor/plugins/searchgasm/lib/searchgasm/helpers/form.rb
  31. +102 −14 vendor/plugins/searchgasm/lib/searchgasm/helpers/utilities.rb
  32. +12 −16 vendor/plugins/searchgasm/lib/searchgasm/search/base.rb
  33. +133 −50 vendor/plugins/searchgasm/lib/searchgasm/search/ordering.rb
  34. +3 −3 vendor/plugins/searchgasm/lib/searchgasm/search/protection.rb
  35. +10 −0 vendor/plugins/searchgasm/lib/searchgasm/shared/utilities.rb
  36. +1 −1 vendor/plugins/searchgasm/lib/searchgasm/version.rb
  37. +1 −0 vendor/plugins/searchgasm/test/test_active_record_base.rb
  38. +10 −8 vendor/plugins/searchgasm/test/test_condition_base.rb
  39. +30 −30 vendor/plugins/searchgasm/test/test_condition_types.rb
  40. +1 −0 vendor/plugins/searchgasm/test/test_conditions_base.rb
  41. +4 −0 vendor/plugins/searchgasm/test/test_search_base.rb
  42. +108 −28 vendor/plugins/searchgasm/test/test_search_ordering.rb
@@ -1,6 +1,7 @@
class NonAjax::UsersController < ApplicationController
def index
@search = User.new_search(params[:search])
+ @search.order_by ||= :first_name
@users, @users_count = @search.all, @search.count
end
end
@@ -1,24 +1,17 @@
login: &login
username: root
- password:
development:
adapter: mysql
database: searchgasm_development
- encoding: utf8
- socket: /tmp/mysql.sock
<<: *login
test:
adapter: mysql
database: searchgasm_test
- encoding: utf8
- socket: /tmp/mysql.sock
<<: *login
production:
adapter: mysql
database: searchgasm_production
- encoding: utf8
- socket: /tmp/mysql.sock
<<: *login
View
@@ -45,11 +45,11 @@
# Run "rake -D time" for a list of tasks for finding time zone names. Uncomment to use default local time.
config.time_zone = 'UTC'
- #if RAILS_ENV == "development" # putting this in environments/development.rb doesn't work, not sure why
- # config.plugin_paths = ["#{RAILS_ROOT}/../../Libs"]
- # config.plugins = [:searchgasm]
- # require "ruby-debug"
- #end
+ if RAILS_ENV == "development" # putting this in environments/development.rb doesn't work, not sure why
+ config.plugin_paths = ["#{RAILS_ROOT}/../../Libs"]
+ config.plugins = [:searchgasm]
+ require "ruby-debug"
+ end
# Your secret key for verifying cookie session data integrity.
# If you change this key, all old sessions will become invalid!
@@ -1,3 +1,24 @@
+== 1.2.2 released 2008-09-29
+
+* Fixed bug when reverse engineering order to order_by, assumed ASC and DESC would always be present when they are not.
+* False is a meaningful value for some conditions, and false.blank? == true. So instead of using value.blank? to ignore conditions we use meaningless?(value), which returns false if it is false.
+* Fixed aliases for lt, lte, gt, and gte.
+* Fixed bug when writing conditions on associations via a hash with string keys
+* Added Config.remove_duplicates to turn off the "automatic" removing of duplicates if desired.
+* Updated searchgasm_state helper to insert the entire state all at once.
+* Added CSS class "ordering" to order_by_link if the search is being ordered by that.
+
+== 1.2.1 released 2008-09-25
+
+* Fixed problem when determining if an order_by_link is currently being ordered. Just "stringified" both comparable values.
+* Removed default order_by and order_as. They will ONLY have values if you specify how to order, otherwise they are nil.
+* Removed order_as requirement. order_as is optional.
+* Added in deep_merge methods for hash, copied over from ActiveSupport 2.1
+* Improved order by auto joins to be based off of what order_by returns instead of setting it when setting order_by.
+* Added priority_order_by. Useful if you want to order featured products first and then order as usual. See documentation in Searchgasm::Search::Ordering for more info.
+* Added in base64 support for order_by and priority_order_by so that it's value is safe in the URL
+* Added priority_order_by_link
+
== 1.2.0 released 2008-09-24
* Added searchgasm_params and searchgasm_url helper to use outside of the control type helpers.
@@ -3,22 +3,26 @@ examples/README.rdoc
init.rb
lib/searchgasm/active_record/associations.rb
lib/searchgasm/active_record/base.rb
+lib/searchgasm/active_record/connection_adapters/mysql_adapter.rb
+lib/searchgasm/active_record/connection_adapters/postgresql_adapter.rb
+lib/searchgasm/active_record/connection_adapters/sqlite_adapter.rb
lib/searchgasm/condition/base.rb
lib/searchgasm/condition/begins_with.rb
+lib/searchgasm/condition/blank.rb
lib/searchgasm/condition/child_of.rb
lib/searchgasm/condition/contains.rb
lib/searchgasm/condition/descendant_of.rb
lib/searchgasm/condition/does_not_equal.rb
+lib/searchgasm/condition/during_evening.rb
lib/searchgasm/condition/ends_with.rb
lib/searchgasm/condition/equals.rb
lib/searchgasm/condition/greater_than.rb
lib/searchgasm/condition/greater_than_or_equal_to.rb
lib/searchgasm/condition/inclusive_descendant_of.rb
-lib/searchgasm/condition/is_blank.rb
-lib/searchgasm/condition/is_nil.rb
lib/searchgasm/condition/keywords.rb
lib/searchgasm/condition/less_than.rb
lib/searchgasm/condition/less_than_or_equal_to.rb
+lib/searchgasm/condition/nil.rb
lib/searchgasm/condition/sibling_of.rb
lib/searchgasm/condition/tree.rb
lib/searchgasm/conditions/base.rb
@@ -38,7 +42,7 @@ lib/searchgasm/search/conditions.rb
lib/searchgasm/search/ordering.rb
lib/searchgasm/search/pagination.rb
lib/searchgasm/search/protection.rb
-lib/searchgasm/shared/searching.rb
+lib/searchgasm/search/searching.rb
lib/searchgasm/shared/utilities.rb
lib/searchgasm/shared/virtual_classes.rb
lib/searchgasm/version.rb
@@ -49,6 +53,7 @@ Rakefile
README.rdoc
test/fixtures/accounts.yml
test/fixtures/orders.yml
+test/fixtures/user_groups.yml
test/fixtures/users.yml
test/libs/acts_as_tree.rb
test/libs/rexml_fix.rb
@@ -65,4 +70,4 @@ test/test_search_conditions.rb
test/test_search_ordering.rb
test/test_search_pagination.rb
test/test_search_protection.rb
-test/text_config.rb
+TODO.rdoc
@@ -345,7 +345,7 @@ What that rule means is that any options you pass when searching get "sanitized"
Author: {Ben Johnson}[http://github.com/binarylogic] of {Binary Logic}[http://www.binarylogic.com]
-Credit to {Zack Ham}[http://github.com/zackham] and {Robert Malko}[http://github.com/malkomalko/] for helping with feature suggestions.
+Credit to {Zack Ham}[http://github.com/zackham] for helping with feature suggestions.
Copyright (c) 2008 {Ben Johnson}[http://github.com/binarylogic] of {Binary Logic}[http://www.binarylogic.com], released under the MIT license
@@ -10,7 +10,6 @@ Echoe.new 'searchgasm' do |p|
p.project = 'searchgasm'
p.summary = "Object based ActiveRecord searching, ordering, pagination, and more!"
p.url = "http://github.com/binarylogic/searchgasm"
- p.dependencies = ['activerecord', 'activesupport >= 2.1.0']
+ p.dependencies = %w(activerecord activesupport)
p.include_rakefile = true
-end
-
+end
@@ -0,0 +1,5 @@
+= To Do
+
+1. Add month condition for date and datetime columns. So you can specify a specific month, by name, number, etc.
+2. Add day condition for time and datetime columns. So you can specify that the time is during the day or the night.
+3. Add weekend condition for date and datetime columns. So you can specify if the date is on a friday, saturday, or sunday
@@ -1,6 +1,7 @@
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
require "active_record"
+require "active_record/version"
require "active_support"
# Core Ext
@@ -33,17 +34,17 @@
# Condition
require "searchgasm/condition/base"
require "searchgasm/condition/begins_with"
+require "searchgasm/condition/blank"
require "searchgasm/condition/contains"
require "searchgasm/condition/does_not_equal"
require "searchgasm/condition/ends_with"
require "searchgasm/condition/equals"
require "searchgasm/condition/greater_than"
require "searchgasm/condition/greater_than_or_equal_to"
-require "searchgasm/condition/is_blank"
-require "searchgasm/condition/is_nil"
require "searchgasm/condition/keywords"
require "searchgasm/condition/less_than"
require "searchgasm/condition/less_than_or_equal_to"
+require "searchgasm/condition/nil"
require "searchgasm/condition/tree"
require "searchgasm/condition/child_of"
require "searchgasm/condition/descendant_of"
@@ -77,7 +78,7 @@ class Base
include Protection
end
- [:begins_with, :child_of, :contains, :descendant_of, :does_not_equal, :ends_with, :equals, :greater_than, :greater_than_or_equal_to, :inclusive_descendant_of, :is_blank, :is_nil, :keywords, :less_than, :less_than_or_equal_to, :sibling_of].each do |condition|
+ [:begins_with, :blank, :child_of, :contains, :descendant_of, :does_not_equal, :ends_with, :equals, :greater_than, :greater_than_or_equal_to, :inclusive_descendant_of, :nil, :keywords, :less_than, :less_than_or_equal_to, :sibling_of].each do |condition|
Base.register_condition("Searchgasm::Condition::#{condition.to_s.camelize}".constantize)
end
end
@@ -0,0 +1,15 @@
+module Searchgasm
+ module ActiveRecord
+ module ConnectionAdapters
+ module MysqlAdapter
+ def hour_sql
+ "HOUR(?)"
+ end
+
+ def month_sql
+ "MONTH(?)"
+ end
+ end
+ end
+ end
+end
@@ -8,9 +8,8 @@ class Base
include Shared::Utilities
attr_accessor :column, :klass
- class_inheritable_accessor :ignore_blanks, :type_cast_value
- self.ignore_blanks = true
- self.type_cast_value = true
+ class_inheritable_accessor :ignore_meaningless, :type_cast_sql_type
+ self.ignore_meaningless = true
class << self
# Name of the condition inferred from the class name
@@ -29,12 +28,8 @@ def aliases_for_column(column)
[]
end
- def ignore_blanks? # :nodoc:
- ignore_blanks == true
- end
-
- def type_cast_value? # :nodoc:
- type_cast_value == true
+ def ignore_meaningless? # :nodoc:
+ ignore_meaningless == true
end
# Sane as name_for_column but for the class as a whole. For example the tree methods apply to the class as a whole and not
@@ -48,6 +43,16 @@ def aliases_for_klass(klass)
[]
end
+ # A utility method for using in name_for_column. Determines if a column contains a date.
+ def date_column?(column)
+ [:datetime, :date, :timestamp].include?(column.type)
+ end
+
+ # A utility method for using in name_for_column. Determines if a column contains a date and a time.
+ def datetime_column?(column)
+ [:datetime, :timestamp, :time, :date].include?(column.type)
+ end
+
# A utility method for using in name_for_column. For example the keywords condition only applied to string columns, the great than condition doesnt.
def string_column?(column)
[:string, :text].include?(column.type)
@@ -57,6 +62,11 @@ def string_column?(column)
def comparable_column?(column)
[:integer, :float, :decimal, :datetime, :timestamp, :time, :date].include?(column.type)
end
+
+ # A utility method for using in name_for_column. Determines if a column contains a time.
+ def time_column?(column)
+ [:datetime, :timestamp, :time].include?(column.type)
+ end
end
def initialize(klass, column = nil)
@@ -118,14 +128,20 @@ def sanitize(alt_value = nil) # :nodoc:
# The value for the condition
def value
- self.class.type_cast_value? && @value.is_a?(String) ? column.type_cast(@value) : @value
+ @value.is_a?(String) ? column_for_type_cast.type_cast(@value) : @value
end
# Sets the value for the condition
def value=(v)
+ return if self.class.ignore_meaningless? && meaningless?(v)
self.explicitly_set_value = true
@value = v
end
+
+ private
+ def column_for_type_cast
+ @column_for_type_cast ||= self.class.type_cast_sql_type ? self.column.class.new(column.name, column.default.to_s, self.class.type_cast_sql_type.to_s, column.null) : column
+ end
end
end
end
@@ -1,20 +1,19 @@
module Searchgasm
module Condition
- class IsBlank < Base
- self.ignore_blanks = false
- self.type_cast_value = false
+ class Blank < Base
+ self.type_cast_sql_type = "boolean"
class << self
def aliases_for_column(column)
- ["#{column.name}_blank"]
+ ["#{column.name}_is_blank"]
end
end
def to_conditions(value)
# Some databases handle null values differently, let AR handle this
- if value == true || value == "true" || value == 1 || value == "1"
+ if value == true
"#{quoted_table_name}.#{quoted_column_name} is NULL or #{quoted_table_name}.#{quoted_column_name} = ''"
- elsif value == false || value == "false" || value == 0 || value == "0"
+ elsif value == false
"#{quoted_table_name}.#{quoted_column_name} is NOT NULL and #{quoted_table_name}.#{quoted_column_name} != ''"
end
end
@@ -1,7 +1,7 @@
module Searchgasm
module Condition
class DoesNotEqual < Base
- self.ignore_blanks = false
+ self.ignore_meaningless = false
class << self
def aliases_for_column(column)
@@ -0,0 +1,32 @@
+module Searchgasm
+ module Condition
+ class DuringEvening < Base
+ class << self
+ def name_for_column(column)
+ return unless time_column?(column)
+ super
+ end
+
+ def aliases_for_column(column)
+ column_names = [column.name]
+ column_names << column.name.gsub(/_(at|on)$/, "") if column.name =~ /_(at|on)$/
+
+ aliases = []
+ column_names.each { |column_name| aliases += ["#{column_name}_in_the_evening", "#{column_name}_in_evening", "#{column_name}_evening"] }
+ aliases << "#{column_names.last}_during_evening" if column_names.size > 1
+ aliases
+ end
+ end
+
+ def to_conditions(value)
+ evening_start = 17
+ evening_end = 22
+
+ # Need to set up a funcion in each adapter for dealing with dates. Mysql uses HOUR(), sqlite uses strftime(), postgres uses date_part('hour', date). Could potentially be a pain in the ass.
+ # Also, you could set up an hour = condition, and leverage that to do this.
+ if value == true
+ ["#{quoted_table_name}.#{quoted_column_name} >= ? AND #{quoted_table_name}.#{quoted_column_name} <= ?", value]
+ end
+ end
+ end
+end
@@ -1,7 +1,7 @@
module Searchgasm
module Condition
class Equals < Base
- self.ignore_blanks = false
+ self.ignore_meaningless = false
class << self
def aliases_for_column(column)
@@ -9,10 +9,11 @@ def name_for_column(column)
def aliases_for_column(column)
column_names = [column.name]
- column_names << column.name.gsub(/_at$/, "") if [:datetime, :timestamp, :time, :date].include?(column.type) && column.name =~ /_at$/
+ column_names << column.name.gsub(/_(at|on)$/, "") if datetime_column?(column) && column.name =~ /_(at|on)$/
aliases = []
column_names.each { |column_name| aliases += ["#{column_name}_gt", "#{column_name}_after"] }
+ aliases << "#{column_names.last}_greater_than" if column_names.size > 1
aliases
end
end
@@ -8,7 +8,13 @@ def name_for_column(column)
end
def aliases_for_column(column)
- ["#{column.name}_gte", "#{column.name}_at_least"]
+ column_names = [column.name]
+ column_names << column.name.gsub(/_(at|on)$/, "") if datetime_column?(column) && column.name =~ /_(at|on)$/
+
+ aliases = []
+ column_names.each { |column_name| aliases += ["#{column_name}_gte", "#{column_name}_at_least"] }
+ aliases << "#{column_names.last}_greater_than_or_equal_to" if column_names.size > 1
+ aliases
end
end
Oops, something went wrong.

0 comments on commit 46b6722

Please sign in to comment.