Sha256: 35fd70da993d8c5913ca224b892b0fed8a54b97bee7025e00a76b0fd25f586a5

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

module Snaptable
  module Constructor
    module Collection

      def collection
        @collection ||= model.includes(belongs_to_associations)
      end

      def records
        @records ||= filter(collection).paginate(page: params[:page], per_page: 30).order(sort_column + " " + sort_direction)
      end

      def filter(collection)
        if options[:search] == true
          collection.joins(search_associations).where(query_fields, query: "%#{params[:query]}%", id: params[:query].to_i)
        else
          collection
        end
      end

      private

      def query_fields
        self.class::Search.fields.map do |key, values|
          values.map do |value|
            values.map{ |v| "#{key}.#{v} LIKE :query OR"}.join(" ")
          end
        end.join(" ") + " #{column_name('id')} = :id"
      end

      def search_associations
        self.class::Search.associations
      end

      def belongs_to_associations
        model.reflect_on_all_associations(:belongs_to).map{ |a| a.name }
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
snaptable-0.2.1 lib/snaptable/constructor/collection.rb
snaptable-0.2.0 lib/snaptable/constructor/collection.rb