Sha256: 859d23aa62ece1fce3c36ef993fc87bea0b77c6b9a4c07a10ea237adbbfe84d9
Contents?: true
Size: 1.12 KB
Versions: 1
Compression:
Stored size: 1.12 KB
Contents
require 'ruby-debug' module BarelySearchable module ModelInjections module ClassMethods def searches_on *fields if @searches.nil? init_searches_on end @searches << fields @searches.flatten! end def init_searches_on @searches = [] end def search query, limit = nil where = '0' like = '%' + query.to_s.gsub(' ','%') + '%' @searches.each do |field| field = field.to_s unless self.columns_hash.include? field raise NameError, "#{field} is not a valid column on #{self}" end if [:integer, :decimal, :float].include? self.columns_hash[ field.to_s ].type where += ' or ' + self.arel_table[field].eq( query ).to_sql else where += ' or ' + self.arel_table[field].matches( like ).to_sql end end if limit.nil? self.where( where ) else self.where( where ).limit( limit ) end end end def self.included to to.extend ClassMethods to.init_searches_on end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
barely_searchable-0.0.1 | lib/barely_searchable/model_injections.rb |