Sha256: c9d45b3e82959f43f6529f565402b6f62979f49b3de4afa73e7cca044ee7ee21

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

class Product < ApplicationRecord
  belongs_to :category, class_name: 'ProductCategory', inverse_of: :products, optional: true

  has_many :variations, class_name: 'ProductVariation', inverse_of: :product

  validates :title, presence: true

  def self.predicate_search
    @predicate_search ||= Pursuit::PredicateSearch.new(
      left_outer_joins(:category, :variations).group(:id).order(:title)
    ) do
      permit_attribute :title
      permit_attribute :category, ProductCategory.arel_table[:id]
      permit_attribute :category_name, ProductCategory.arel_table[:name]
      permit_attribute :variation, ProductVariation.arel_table[:id]
      permit_attribute :variation_title, ProductVariation.arel_table[:title]
      permit_attribute :variation_currency, ProductVariation.arel_table[:currency]
      permit_attribute :variation_amount, ProductVariation.arel_table[:amount]
    end
  end

  def self.term_search
    @term_search ||= Pursuit::TermSearch.new(
      left_outer_joins(:category).group(:id).order(:title)
    ) do
      search_attribute :title
      search_attribute ProductCategory.arel_table[:name]
    end
  end

  def self.search(query)
    predicate_search.apply(query)
  rescue Parslet::ParseFailed
    term_search.apply(query)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pursuit-1.0.1 spec/internal/app/models/product.rb