profile.rb
4.06 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require_dependency 'profile'
class Profile
  # use for internationalizable human type names in search facets
  # reimplement on subclasses
  def self.type_name
    c_('Profile')
  end
  after_save_reindex [:articles], :with => :delayed_job
  acts_as_faceted :fields => {
      :solr_plugin_f_enabled => {:label => _('Situation'), :type_if => proc { |klass| klass.kind_of?(Enterprise) },
        :proc => proc { |id| solr_plugin_f_enabled_proc(id) }},
      :solr_plugin_f_region => {:label => c_('City'), :proc => proc { |id| solr_plugin_f_region_proc(id) }},
      :solr_plugin_f_profile_type => {:label => c_('Type'), :proc => proc{|klass| solr_plugin_f_profile_type_proc(klass)}},
      :solr_plugin_f_categories => {:multi => true, :proc => proc {|facet, id| solr_plugin_f_categories_proc(facet, id)},
        :label => proc { |env| solr_plugin_f_categories_label_proc(env) }, :label_abbrev => proc{ |env| solr_plugin_f_categories_label_abbrev_proc(env) }},
    }, :category_query => proc { |c| "solr_plugin_category_filter:#{c.id}" },
    :order => [:solr_plugin_f_region, :solr_plugin_f_categories, :solr_plugin_f_enabled, :solr_plugin_f_profile_type]
  acts_as_searchable :fields => facets_fields_for_solr + [:solr_plugin_extra_data_for_index,
      # searched fields
      {:name => {:type => :text, :boost => 2.0}},
      {:identifier => :text}, {:nickname => :text},
      # filtered fields
      {:solr_plugin_public => :boolean}, {:environment_id => :integer},
      {:solr_plugin_category_filter => :integer},
      # ordered/query-boosted fields
      {:solr_plugin_name_sortable => :string}, {:user_id => :integer},
      :enabled, :active, :validated, :public_profile, :visible,
      {:lat => :float}, {:lng => :float},
      :updated_at, :created_at,
    ],
    :include => [
      {:region => {:fields => [:name, :path, :slug, :lat, :lng]}},
      {:categories => {:fields => [:name, :path, :slug, :lat, :lng, :acronym, :abbreviation]}},
    ], :facets => facets_option_for_solr,
    :boost => proc{ |p| 10 if p.enabled }
  handle_asynchronously :solr_save
  handle_asynchronously :solr_destroy
  class_attribute :solr_plugin_extra_index_methods
  self.solr_plugin_extra_index_methods = []
  def solr_plugin_extra_data_for_index
    self.class.solr_plugin_extra_index_methods.map { |meth| meth.to_proc.call(self) }.flatten
  end
  def self.solr_plugin_extra_data_for_index(sym = nil, &block)
    self.solr_plugin_extra_index_methods ||= []
    self.solr_plugin_extra_index_methods.push(sym) if sym
    self.solr_plugin_extra_index_methods.push(block) if block_given?
  end
  def add_category_with_solr_save(c, reload=false)
    add_category_without_solr_save(c, reload)
    if !new_record?
      self.solr_save
    end
  end
  alias_method_chain :add_category, :solr_save
  private
  def self.solr_plugin_f_categories_label_proc(environment)
    ids = environment.solr_plugin_top_level_category_as_facet_ids
    r = Category.find(ids)
    map = {}
    ids.map{ |id| map[id.to_s] = r.detect{|c| c.id == id}.name }
    map
  end
  def self.solr_plugin_f_categories_proc(facet, id)
    id = id.to_i
    return if id.zero?
    c = Category.find(id)
    c.name if c.top_ancestor.id == facet[:label_id].to_i or facet[:label_id] == 0
  end
  def solr_plugin_f_categories
    category_ids - [region_id]
  end
  def solr_plugin_f_region
    self.region_id
  end
  def self.solr_plugin_f_region_proc(id)
    c = Region.find(id)
    s = c.parent
    if c and c.kind_of?(City) and s and s.kind_of?(State) and s.acronym
      [c.name, ', ' + s.acronym]
    else
      c.name
    end
  end
  def self.solr_plugin_f_enabled_proc(enabled)
    enabled = enabled == "true" ? true : false
    enabled ? s_('facets|Enabled') : s_('facets|Not enabled')
  end
  def solr_plugin_f_enabled
    self.enabled
  end
  def solr_plugin_public
    self.public?
  end
  def solr_plugin_category_filter
    categories_including_virtual_ids
  end
  def solr_plugin_name_sortable
    name
  end
  def solr_plugin_f_profile_type
    self.class.name
  end
  def self.solr_plugin_f_profile_type_proc klass
    klass.constantize.type_name
  end
end