cirandas.net

ref: master

plugins/sniffer/models/sniffer_plugin/opportunity.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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
50
51
class SnifferPlugin::Opportunity < ApplicationRecord

  self.table_name = :sniffer_plugin_opportunities

  belongs_to :profile

  belongs_to :opportunity, polymorphic: true

  # for has_many :through
  belongs_to :product_category, -> {
    where 'sniffer_plugin_opportunities.opportunity_type = ?', 'ProductCategory'
  }, class_name: '::ProductsPlugin::ProductCategory', foreign_key: :opportunity_id
  # getter
  def product_category
    opportunity_type == 'ProductCategory' ? opportunity : nil
  end

  scope :product_categories, -> {
    where 'sniffer_plugin_opportunities.opportunity_type = ?', 'ProductCategory'
  }

  if defined? SolrPlugin
    extend SolrPlugin::ActsAsSearchable

    acts_as_searchable fields: [
        # searched fields
        # filtered fields
        # ordered/query-boosted fields
      ], include: [
        {product_category: {fields: [:name, :path, :slug, :lat, :lng, :acronym, :abbreviation]}},
      ]

    handle_asynchronously :solr_save
  end

  delegate :lat, :lng, :to => :product_category, :allow_nil => true

  # delegate missing methods to opportunity
  def method_missing method, *args, &block
    if self.opportunity.respond_to? method
      self.opportunity.send method, *args, &block
    else
      super method, *args, &block
    end
  end
  def respond_to_with_opportunity? method, p2 = true
    respond_to_without_opportunity? method, p2 or (self.opportunity and self.opportunity.respond_to? method)
  end
  alias_method_chain :respond_to?, :opportunity

end