Module: SupplejackApi::Support::Searchable::ClassMethods

Defined in:
app/models/supplejack_api/support/searchable.rb

Instance Method Summary collapse

Instance Method Details

#build_sunspot_schema(builder) ⇒ Object



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
# File 'app/models/supplejack_api/support/searchable.rb', line 69

def build_sunspot_schema(builder)
  schema_class.fields.each do |_name, field|
    options = {}
    search_as = field.search_as || []

    value_block = nil
    if field.search_value.present?
      value_block = proc do
        field.search_value.call(self)
      end
    end

    options[:as] = field.solr_name if field.solr_name.present?

    if search_as.include? :filter
      filter_options = {}
      filter_options[:multiple] = true if field.multi_value.present?
      type = SUNSPOT_TYPE_NAMES[field.type]

      builder.public_send(type, field.name, options.merge(filter_options), &value_block)
    end

    if search_as.include? :fulltext
      options[:boost] = field.search_boost if field.search_boost.present?
      builder.text field.name, options, &value_block
    end
  end
end

#custom_find(id, scope = nil, options = {}) ⇒ Object

Raises:

  • (Mongoid::Errors::DocumentNotFound)


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
# File 'app/models/supplejack_api/support/searchable.rb', line 42

def custom_find(id, scope = nil, options = {})
  options ||= {}
  class_scope = unscoped
  class_scope = class_scope.active unless options.delete(:status) == :all
  column = "#{name.demodulize.downcase}_id"

  if id.to_s =~ /^\d+$/
    data = class_scope.where(column => id).first
  elsif id.to_s =~ /^[0-9a-f]{24}$/i
    data = class_scope.find(id)
  end

  raise Mongoid::Errors::DocumentNotFound.new(self, [id], [id]) unless data

  begin
    data.find_next_and_previous_records(scope, options) if options.any?
  rescue Sunspot::UnrecognizedFieldError,
         RSolr::Error::Http, Timeout::Error,
         Errno::ECONNREFUSED,
         Errno::ECONNRESET => e

    Rails.logger.error e.inspect
  end

  data
end

#schema_classObject



38
39
40
# File 'app/models/supplejack_api/support/searchable.rb', line 38

def schema_class
  "#{to_s.demodulize}Schema".constantize
end

#valid_facetsObject



98
99
100
101
102
103
104
105
106
107
# File 'app/models/supplejack_api/support/searchable.rb', line 98

def valid_facets
  facets = []

  schema_class.fields.each do |name, field|
    search_as = field.search_as || []
    facets << name.to_sym if search_as.include? :filter
  end

  facets
end

#valid_groupsObject



109
110
111
# File 'app/models/supplejack_api/support/searchable.rb', line 109

def valid_groups
  schema_class.groups.keys
end