Module: Skyline::SearchableItem

Included in:
Page
Defined in:
lib/skyline/searchable_item.rb

Overview

Use this module in all content models that need to be indexed and searchable by Solr

Usage: class Model < ActiveRecord::Base

include Skyline::SearchableItem

searchable_field :title => :title,
                 :body => :body,
                 :documentdate => :publication_date,
                 :url => :url,
                 :theme_s => :theme_title,
                 :year_i => :publication_year

[indexer_option :if => :published_publication_id_changed?]

end

1) Gives your Model the following interface:

class  Model < ActiveRecord::Base
  after_save :add_index
  after_destroy :remove_from_index

  self << class
    def searchable_field(fields)
  end
end

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#add_indexObject

Add/Update solr index



106
107
108
109
110
111
112
113
114
115
# File 'lib/skyline/searchable_item.rb', line 106

def add_index
	  return unless self.evaluate_if_option
	  
	  indexer = Skyline::Indexer.instance	
	  if (self.class.publishable? && self.published?) || !self.class.publishable?
	  indexer.add_index(hash_searchable_fields)
	else
	  indexer.remove_from_index(self.solr_id)
	end
end

#evaluate_if_optionObject



123
124
125
126
127
128
129
130
131
# File 'lib/skyline/searchable_item.rb', line 123

def evaluate_if_option
 return true unless self.indexer_options.has_key?(:if)
 return case self.indexer_options[:if].class.name
         when "Proc"
           self.indexer_options[:if].call(self)
         else
           self.send(self.indexer_options[:if])
         end
end

#hash_searchable_fieldsObject

create hash for solr from self.searchable_fields

Returns

Hash

hash with solr_field => value pairs



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/skyline/searchable_item.rb', line 89

def hash_searchable_fields
	hash = {}
	self.searchable_fields.each do |sf, f|
	  value = case f.class.name
	          when "Proc"
	            f.call(self)
	          when "String"
	            f
	          else
	            self.send(f)
	          end
    hash[sf] = solr_typecast(value)			
	end
 	hash.merge!({:id => self.solr_id, :cat => self.class.name})
end

#remove_from_indexObject

Remove item from solr-index



118
119
120
121
# File 'lib/skyline/searchable_item.rb', line 118

def remove_from_index
	indexer = Skyline::Indexer.instance
	indexer.remove_from_index(self.solr_id)
end

#solr_idObject

Return the solr id



82
83
84
# File 'lib/skyline/searchable_item.rb', line 82

def solr_id
	solr_id = "#{self.class.name}-#{self.id}"
end

#solr_typecast(v) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/skyline/searchable_item.rb', line 70

def solr_typecast(v)
  case v
  when ActiveSupport::TimeWithZone, Date, Time, DateTime
    v.to_time.utc.iso8601(3)
  when String
    ActionController::Base.helpers.strip_tags(v)
  else
    v
  end
end