Sha256: 47d88c0f33a25b9bbdaa5e72849e7eb8ce0db8e4c2f9861d289638ede638dcb9

Contents?: true

Size: 777 Bytes

Versions: 1

Compression:

Stored size: 777 Bytes

Contents

# frozen_string_literal: true

class ProductCategory < ApplicationRecord
  has_many :products, class_name: 'Product', foreign_key: :category_id, inverse_of: :category, dependent: :nullify

  validates :name, presence: true

  def self.predicate_search
    @predicate_search ||= Pursuit::PredicateSearch.new(
      left_outer_joins(:products).group(:id)
    ) do
      permit_attribute :name
      permit_attribute :product, Product.arel_table[:id]
      permit_attribute :product_title, Product.arel_table[:title]
    end
  end

  def self.term_search
    @term_search ||= Pursuit::TermSearch.new(all) do
      search_attribute :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_category.rb