module Snaptable::Constructor::Collection

Public Instance Methods

collection() click to toggle source
# File lib/snaptable/constructor/collection.rb, line 5
def collection
  @collection.includes(belongs_to_associations)
end
filter(collection) click to toggle source
# File lib/snaptable/constructor/collection.rb, line 13
def filter(collection)
  if options[:search] == true && !params[:query].blank?
    collection.joins(search_associations).where(query, query: "%#{params[:query].downcase}%", id: params[:query].to_i)
  else
    collection
  end
end
records() click to toggle source
# File lib/snaptable/constructor/collection.rb, line 9
def records
  @records ||= filter(collection).paginate(page: params[paginate_key], per_page: 30).order(sort_column + " " + sort_direction)
end

Private Instance Methods

belongs_to_associations() click to toggle source
# File lib/snaptable/constructor/collection.rb, line 43
def belongs_to_associations
  model.reflect_on_all_associations(:belongs_to).map{ |a| a.name }
end
query() click to toggle source
# File lib/snaptable/constructor/collection.rb, line 23
def query
  query_fields.map do |key, values|
    values.map do |value|
      "LOWER(#{key}.#{value}) LIKE :query OR"
    end.join(" ")
  end.join(" ") + " #{column_name('id')} = :id"
end
query_fields() click to toggle source
# File lib/snaptable/constructor/collection.rb, line 31
def query_fields
  if self.class.const_defined?(:Search)
    self.class::Search.fields
  else
    { model.table_name => model.columns.select{ |c| c.type == :string }.map{ |c| c.name } }
  end
end
search_associations() click to toggle source
# File lib/snaptable/constructor/collection.rb, line 39
def search_associations
  self.class::Search.associations if self.class.const_defined?(:Search)
end