Skip to content

Commit

Permalink
pulled out typecasting class
Browse files Browse the repository at this point in the history
  • Loading branch information
James Vanneman committed Mar 18, 2013
1 parent 2e5c71c commit e01a0ba
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/searchlogic/active_record_ext/scopes/conditions.rb
Expand Up @@ -48,7 +48,7 @@ def generate_scope(method, args, &block)
condition_klasses.each do |ck|
scope = ck.generate_scope(self, method, args, &block)
if scope
# memoized_scopes[method.to_sym]
memoized_scopes[method.to_sym]
return scope
end
end
Expand Down
Expand Up @@ -20,8 +20,6 @@ def find_column
def applicable?
!(/^(#{klass.column_names.join("|")})#{self.class.matcher}$/ =~ method_name).nil?
end


end
end
end
Expand Down
Expand Up @@ -20,7 +20,6 @@ def find_column
def applicable?
!(/^(#{klass.column_names.join("|")})#{self.class.matcher}$/ =~ method_name).nil?
end

end
end
end
Expand Down
Expand Up @@ -8,7 +8,7 @@ def scope
find_column
klass.where("#{table_name}.#{column_name} is not NULL")
elsif applicable? && args.first == false
__send___to_null
send_to_null
else
false
end
Expand All @@ -29,7 +29,7 @@ def applicable?
!(/^(#{klass.column_names.join("|")})#{self.class.matcher}$/ =~ method_name).nil?
end

def __send___to_null
def send_to_null
klass.__send__(find_column + "_null")
end
end
Expand Down
20 changes: 10 additions & 10 deletions lib/searchlogic/active_record_ext/scopes/conditions/oor.rb
Expand Up @@ -16,7 +16,7 @@ def scope
if applicable?
methods_array = MethodConstructor.new(method_name).methods_array
methods_array.each do |m|
__send___and_store(m)
send_and_store(m)
end
!joins_values.flatten.empty? ? klass.includes(joins_values.flatten).where(where_values.flatten.join(" OR ")) : klass.where(where_values.flatten.join(" OR "))
end
Expand All @@ -26,7 +26,7 @@ def self.matcher
end
private

def __send___and_store(m)
def send_and_store(m)
scope_key = ScopeReflection.scope_name(m)
if no_arg_scope?(scope_key)
scope = klass.__send__(m)
Expand All @@ -53,15 +53,15 @@ def value
end


def find_condition
klass.joined_condition_klasses.split("|").find{ |jck| last_method.include?(jck)}
end
def find_condition
klass.joined_condition_klasses.split("|").find{ |jck| last_method.include?(jck)}
end

def applicable?
return nil if /(find_or_)/ =~ method_name
named_scopes = klass.named_scopes.keys.map(&:to_s).join("|")
!(/_or_(#{klass.column_names.join("|")}|#{klass.association_names.join("|")}#{'|'+ named_scopes unless named_scopes.empty?})/ =~ method_name).nil?
end
def applicable?
return nil if /(find_or_)/ =~ method_name
named_scopes = klass.named_scopes.keys.map(&:to_s).join("|")
!(/_or_(#{klass.column_names.join("|")}|#{klass.association_names.join("|")}#{'|'+ named_scopes unless named_scopes.empty?})/ =~ method_name).nil?
end
end
end
end
Expand Down
16 changes: 2 additions & 14 deletions lib/searchlogic/search_ext/type_cast.rb
@@ -1,4 +1,3 @@
require 'chronic'
module Searchlogic
module SearchExt
module TypeCast
Expand All @@ -11,22 +10,11 @@ def typecast(method, *val)
elsif value.kind_of?(Array)
value.collect{|v| typecast(method, v)}
else
column_for_type_cast = ::ActiveRecord::ConnectionAdapters::Column.new("", nil)
column_for_type_cast.instance_variable_set(:@type, type)
value = sanitize_cdl_in_date(value) if (type == :datetime || type == :date || type == :time) && value.kind_of?(String)
if defined?(Chronic) && value.kind_of?(String) && (type == :date || type == :time || type == :datetime)
column_for_type_cast.type_cast(value) || Chronic.try(:parse, value)
else
column_for_type_cast.type_cast(value)
end
typecaster = TypeCaster.new(value, type)
typecaster.column_type
end
end

def sanitize_cdl_in_date(value)
value.gsub(",", "/")

end

def ordering?(scope_name)
scope_name.to_s == "order"
end
Expand Down
34 changes: 34 additions & 0 deletions lib/searchlogic/search_ext/type_caster.rb
@@ -0,0 +1,34 @@
module Searchlogic
module SearchExt
class TypeCaster
attr_reader :value, :type, :column_for_typecast
def initialize(value, type)
@value = value
@type = type
@column_for_typecast = set_column(type)
binding.pry
end

def column_type
if defined?(Chronic) && value.kind_of?(String) && date_or_time?
column_for_typecast.type_cast(value) || Chronic.try(:parse, sanitize_cdl_in_date(value))
else
column_for_typecast.type_cast(value)
end
end

private
def set_column(type)
::ActiveRecord::ConnectionAdapters::Column.new("", nil).tap{|col| col.instance_variable_set(:@type, type)}
end

def date_or_time?
type == :datetime || type == :date || type == :time
end

def sanitize_cdl_in_date(value)
value.gsub(",", "/")
end
end
end
end
3 changes: 2 additions & 1 deletion spec/searchlogic/scopes/joins_spec.rb
Expand Up @@ -20,7 +20,8 @@

it "returns all users with order total greater than 20" do
users = User.orders__total_greater_than(20)
# users.size.should eq(2)
users.size.should eq(2)

users.map(&:name).should eq(["James", "Ben"])
end

Expand Down

0 comments on commit e01a0ba

Please sign in to comment.