jeudi 14 janvier 2016

Searches results show array - How to display results

Im trying to make a search function for an app. I have a service object called searches and all search logic is in this object.

When the search form is submitted with an about query @results in Search Controller is the following array.

=> [
    [0] [],
    [1] [
        [0] About {
                          :id => 2,
                       :title => "About",
                        :slug => "about",
            :meta_description => "",
               :meta_keywords => "",
                     :content => "<p>Lorem ipsum about</p>",
                    :position => 1,
                   :published => true,
                     :on_menu => true,
                  :created_at => Wed, 13 Jan 2016 00:44:08 UTC +00:00,
                  :updated_at => Fri, 15 Jan 2016 04:05:52 UTC +00:00
        }
    ],
    [2] [],
    [3] []
]

Here is my Search object. See how the User attributes are different then the Pages and NewsArticle? Good. Now go look at the view.

class SearchService
  attr_accessor :query

  def initialize(query)
    @query = query
  end

  # searchable_fields_by_model
  CONDITIONS_BY_MODEL = {
    Page => [:title, :content],
    NewsArticle => [:title, :content]
    User => [:first_name, :last_name]
  }

  def raw_results
    ActiveRecord::Base.transaction do
      CONDITIONS_BY_MODEL.flat_map do |model, fields|
        Array(fields).map do |field|
          model.where(["#{field} LIKE ?", "%#{query}%"])
        end
      end
    end
  end
end

Below is my view. Currently the view will only display content and title. But my User has first_name and last_name therefore it won't display. I need a way to display the results of all Models and it needs to be clean so if i add another model to search with entirely different attributes it will still display. How would you do this?

.page-header
  %h1 Search#index

  - @results.each do |results|
    - results.each do |r|
      %p= r.title
      %p= r.content

Search controller

class SearchController < ApplicationController
  def index
    @results = SearchService.new(params[:q]).results
  end
end

Aucun commentaire:

Enregistrer un commentaire