Skip to content

Commit

Permalink
Updated searchgasm
Browse files Browse the repository at this point in the history
  • Loading branch information
binarylogic committed Sep 29, 2008
1 parent bcf6b5f commit 46b6722
Show file tree
Hide file tree
Showing 42 changed files with 735 additions and 257 deletions.
1 change: 1 addition & 0 deletions app/controllers/non_ajax/users_controller.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 0 additions & 7 deletions config/database.example.yml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
21 changes: 21 additions & 0 deletions vendor/plugins/searchgasm/CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
13 changes: 9 additions & 4 deletions vendor/plugins/searchgasm/Manifest
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion vendor/plugins/searchgasm/README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions vendor/plugins/searchgasm/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions vendor/plugins/searchgasm/TODO.rdoc
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions vendor/plugins/searchgasm/lib/searchgasm.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
36 changes: 26 additions & 10 deletions vendor/plugins/searchgasm/lib/searchgasm/condition/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading

0 comments on commit 46b6722

Please sign in to comment.