Sha256: 8541e6594b7bac02279459edc68555e0141eb8aa94109c9c7c6301092e1cd240

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module TableSortable
  class Column
    class Filter
      include TableSortable::Concerns::Proc

      attr_accessor :query, :default_value, :collection, :sort_collection, :static

      def initialize(*args)
        options = args.extract_options!
        @default_value = options[:filter_initial_value]
        @collection = options[:filter_collection]
        @static = options[:filter_static] || false
        @sort_collection = options[:filter_sort_collection] || false
        super :filter, options
      end

      def static?
        static == true
      end

      def array_proc
        -> (value, col=nil) { select{|record| col.value(record).to_s.downcase.include? value.downcase} }
      end

      def active_record_proc
        -> (value, col=nil) { where("LOWER(#{col.name.to_s.underscore}) LIKE (?)", "%#{value.to_s.downcase}%") }
      end

      def proc_wrapper(proc)
        -> (value, col=nil) { instance_exec(value, &proc) }
      end

      def run(records)
        records.instance_exec(query, column, &proc)
      end

      def used?
        !query.nil?
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
table_sortable-1.0.0.pre.alpha.21 lib/table_sortable/column/filter.rb