Class: Xapit::FacetOption

Inherits:
Object
  • Object
show all
Defined in:
lib/xapit/facet_option.rb

Overview

A facet option is a specific value or choice for a facet. See Xapit::Facet for details on how to use it.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name, facet_attribute, name) ⇒ FacetOption

Returns a new instance of FacetOption.



22
23
24
25
# File 'lib/xapit/facet_option.rb', line 22

def initialize(class_name, facet_attribute, name)
  @facet = class_name.constantize.xapit_facet_blueprint(facet_attribute) if class_name && facet_attribute
  @name = name
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



4
5
6
# File 'lib/xapit/facet_option.rb', line 4

def count
  @count
end

#existing_facet_identifiersObject

Returns the value of attribute existing_facet_identifiers.



4
5
6
# File 'lib/xapit/facet_option.rb', line 4

def existing_facet_identifiers
  @existing_facet_identifiers
end

#facetObject

Returns the value of attribute facet.



4
5
6
# File 'lib/xapit/facet_option.rb', line 4

def facet
  @facet
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/xapit/facet_option.rb', line 4

def name
  @name
end

Class Method Details

.exist?(id) ⇒ Boolean

See if the given facet option exists with this id.

Returns:

  • (Boolean)


18
19
20
# File 'lib/xapit/facet_option.rb', line 18

def self.exist?(id)
  Query.new("Q#{name}-#{id}").count >= 1
end

.find(id) ⇒ Object

Fetch a facet option given an id.



7
8
9
10
11
12
13
14
15
# File 'lib/xapit/facet_option.rb', line 7

def self.find(id)
  match = Query.new("Q#{name}-#{id}").matches(:offset => 0, :limit => 1).first
  if match.nil?
    raise "Unable to find facet option for #{id}."
  else
    class_name, facet_attribute, name = match.document.data.split('|||')
    new(class_name.to_s, facet_attribute.to_s, name.to_s)
  end
end

Instance Method Details

#identifierObject



27
28
29
# File 'lib/xapit/facet_option.rb', line 27

def identifier
  Digest::SHA1.hexdigest(facet.attribute.to_s + name)[0..6]
end

#saveObject

Saves the given facet option to the database if it hasn’t been already.



32
33
34
35
36
37
38
39
# File 'lib/xapit/facet_option.rb', line 32

def save
  unless self.class.exist?(identifier)
    doc = Xapian::Document.new
    doc.data = [facet.member_class.name, facet.attribute, name].join("|||")
    doc.add_term("Q#{self.class.name}-#{identifier}")
    Xapit::Config.writable_database.add_document(doc)
  end
end

#to_paramObject

Converts the facet to be used in a URL. It adds to the existing ones for convenience. If this facet option is currently selected, then this will return all selected facets except this one. This conveniently allows you to use this as both an “add this facet” and “remove this facet” link.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/xapit/facet_option.rb', line 44

def to_param
  if existing_facet_identifiers.include? identifier
    if Xapit::Config.breadcrumb_facets?
      existing_facet_identifiers[0..existing_facet_identifiers.index(identifier)].join('-')
    else
      (existing_facet_identifiers - [identifier]).join('-')
    end
  else
    (existing_facet_identifiers + [identifier]).join('-')
  end
end