Sha256: 1d735785e16eebb3078dfeb7be23274a75779129d18999020c645a78516ca820
Contents?: true
Size: 1.55 KB
Versions: 1
Compression:
Stored size: 1.55 KB
Contents
module ForestLiana class SearchQueryBuilder def initialize(resource, params) @resource = resource @params = params end def perform search_param filter_param associations_param @resource end def search_param if @params[:search] conditions = [] @resource.columns.each_with_index do |column, index| if column.name == 'id' conditions << "id = #{@params[:search].to_i}" elsif column.type == :string || column.type == :text conditions << "#{column.name} LIKE '%#{@params[:search].downcase}%'" end end @resource = @resource.where(conditions.join(' OR ')) end end def filter_param if @params[:filter] @params[:filter].each do |field, value| operator = nil if value.first == '!' operator = '!=' value.slice!(0) elsif value.include?('*') operator = 'LIKE' value.gsub!('*', '%') else operator = '=' end @resource = @resource.where("#{field} #{operator} '#{value}'") end end end def associations_param associations = @resource.reflect_on_all_associations(:belongs_to) associations.each do |association| name = association.name.to_s if @params[name + 'Id'] @resource = @resource.where("#{name.foreign_key} = #{@params[name + 'Id']}") end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
forest_liana-1.0.1 | app/services/forest_liana/search_query_builder.rb |