Sha256: 936956648c73e89a58032e764dfccc7ac921c28b49098637af6e5f84ba49cc13

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

module Manage
  module Fields
    class Searcher
      class << self
        def generate_search_object(resource_class, search_fields)
          search_class = Class.new do
            include SearchObject.module(:model, :sorting)

            def escape_search_term(term)
              "%#{term.gsub(/\s+/, '%')}%".downcase
            end

            def parse_date(date)
              Date.strptime(date, '%Y-%m-%d') rescue nil
            end
          end


          search_class.scope { resource_class.all }
          Object.const_set("#{resource_class.name.gsub(/:/, '_') + 'Searcher'}", search_class)

          search_fields.select {|f| not f.to_s.include?('.')}.each do |field|
            field_type = resource_class.columns_hash[field.to_s].type
            case field_type
            when *[:text, :string]
              search_class.option field.to_sym do |scope, value|
                value.blank? ? scope : scope.where("lower(#{field.to_s}) LIKE lower(?)", escape_search_term(value))
              end
            when :datetime
              search_class.option field.to_sym do |scope, value|
                date = parse_date value
                scope.where("DATE(#{field.to_s}) >= ?", date) if date.present?
              end
            when :integer
              search_class.option field.to_sym
            when :boolean
              search_class.option field.to_sym
            else
              search_class.option field.to_sym do |scope, value|
                scope.where "#{field.to_s} = '?'", escape_search_term(value)
              end
            end
          end

          search_class
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
manage-1.3.9 lib/manage/fields/searcher.rb