Class: ForestLiana::SearchQueryBuilder
- Inherits:
-
Object
- Object
- ForestLiana::SearchQueryBuilder
- Defined in:
- app/services/forest_liana/search_query_builder.rb
Instance Method Summary collapse
- #association?(field) ⇒ Boolean
- #belongs_to_association?(field) ⇒ Boolean
- #belongs_to_filter ⇒ Object
- #belongs_to_subfield_filter(field, value) ⇒ Object
- #filter_param ⇒ Object
- #has_many_association?(field) ⇒ Boolean
- #has_many_field_filter(field, value) ⇒ Object
- #has_many_filter ⇒ Object
- #has_many_subfield_filter(field, value) ⇒ Object
-
#initialize(resource, params) ⇒ SearchQueryBuilder
constructor
A new instance of SearchQueryBuilder.
- #perform ⇒ Object
- #search_param ⇒ Object
Constructor Details
#initialize(resource, params) ⇒ SearchQueryBuilder
Returns a new instance of SearchQueryBuilder.
4 5 6 7 |
# File 'app/services/forest_liana/search_query_builder.rb', line 4 def initialize(resource, params) @resource = @records = resource @params = params end |
Instance Method Details
#association?(field) ⇒ Boolean
66 67 68 69 |
# File 'app/services/forest_liana/search_query_builder.rb', line 66 def association?(field) field = field.split(':').first if field.include?(':') @resource.reflect_on_association(field.to_sym).present? end |
#belongs_to_association?(field) ⇒ Boolean
130 131 132 133 134 |
# File 'app/services/forest_liana/search_query_builder.rb', line 130 def belongs_to_association?(field) field = field.split(':').first if field.include?(':') association = @resource.reflect_on_association(field.to_sym) [:belongs_to, :has_one].include?(association.try(:macro)) end |
#belongs_to_filter ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'app/services/forest_liana/search_query_builder.rb', line 158 def belongs_to_filter if @params[:filter] @params[:filter].each do |field, values| next unless belongs_to_association?(field) values.split(',').each do |value| @records = belongs_to_subfield_filter(field, value) end end end @records end |
#belongs_to_subfield_filter(field, value) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'app/services/forest_liana/search_query_builder.rb', line 136 def belongs_to_subfield_filter(field, value) field, subfield = field.split(':') association = @resource.reflect_on_association(field.to_sym) return if association.blank? operator, value = OperatorValueParser.parse(value) @records = @records .joins(field.to_sym) operator_date_interval_parser = OperatorDateIntervalParser.new(value) if operator_date_interval_parser.is_interval_date_value() filter = operator_date_interval_parser.get_interval_date_filter() @records = @records.where("#{association.table_name}.#{subfield} #{filter}") else where = "#{association.table_name}.#{subfield} #{operator}" where += " '#{value}'" if value @records = @records.where(where) end end |
#filter_param ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/services/forest_liana/search_query_builder.rb', line 51 def filter_param if @params[:filter] @params[:filter].each do |field, values| next if association?(field) values.split(',').each do |value| operator, value = OperatorValueParser.parse(value) @records = OperatorValueParser.add_where(@records, field, operator, value, @resource) end end end @records end |
#has_many_association?(field) ⇒ Boolean
71 72 73 74 75 76 |
# File 'app/services/forest_liana/search_query_builder.rb', line 71 def has_many_association?(field) field = field.split(':').first if field.include?(':') association = @resource.reflect_on_association(field.to_sym) [:has_many, :has_and_belongs_to_many].include?(association.try(:macro)) end |
#has_many_field_filter(field, value) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'app/services/forest_liana/search_query_builder.rb', line 96 def has_many_field_filter(field, value) association = @resource.reflect_on_association(field.to_sym) return if association.blank? operator, value = OperatorValueParser.parse(value) @records = @records .select("#{@resource.table_name}.*, COUNT(#{association.table_name}.id) #{association.table_name}_has_many_count") .joins(ArelHelpers.join_association(@resource, association.name, Arel::Nodes::OuterJoin)) .group("#{@resource.table_name}.id") .having("COUNT(#{association.table_name}) #{operator} #{value}") end |
#has_many_filter ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/services/forest_liana/search_query_builder.rb', line 78 def has_many_filter if @params[:filter] @params[:filter].each do |field, values| next unless has_many_association?(field) values.split(',').each do |value| if field.include?(':') @records = has_many_subfield_filter(field, value) else @records = has_many_field_filter(field, value) end end end end @records end |
#has_many_subfield_filter(field, value) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'app/services/forest_liana/search_query_builder.rb', line 112 def has_many_subfield_filter(field, value) field, subfield = field.split(':') association = @resource.reflect_on_association(field.to_sym) return if association.blank? operator, value = OperatorValueParser.parse(value) @records = @records .select("#{@resource.table_name}.*, COUNT(#{association.table_name}.id) #{association.table_name}_has_many_count") .joins(ArelHelpers.join_association(@resource, association.name, Arel::Nodes::OuterJoin)) .group("#{@resource.table_name}.id, #{association.table_name}.#{subfield}") .having("#{association.table_name}.#{subfield} #{operator} '#{value}'") end |
#perform ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'app/services/forest_liana/search_query_builder.rb', line 9 def perform @records = search_param @records = filter_param @records = has_many_filter @records = belongs_to_filter @records end |
#search_param ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/services/forest_liana/search_query_builder.rb', line 18 def search_param if @params[:search] conditions = [] @resource.columns.each_with_index do |column, index| if column.name == 'id' conditions << "#{@resource.table_name}.id = #{@params[:search].to_i}" elsif !column.array && (column.type == :string || column.type == :text) conditions << "\"#{@resource.table_name}\".\"#{column.name}\" ILIKE '%#{@params[:search].downcase}%'" end end SchemaUtils.one_associations(@resource).map(&:name).each do |association| resource = @resource.reflect_on_association(association.to_sym) resource.klass.columns.each do |column| if !column.array && (column.type == :string || column.type == :text) conditions << "\"#{resource.table_name}\".\"#{column.name}\" ILIKE " + "'%#{@params[:search].downcase}%'" end end @resource = @resource.joins(association.to_sym) end @records = @resource.where(conditions.join(' OR ')) end @records end |