Module: Asari::ActiveRecord::ClassMethods

Defined in:
lib/asari/active_record.rb

Instance Method Summary collapse

Instance Method Details

#asari_add_item(obj) ⇒ Object

Internal: method for adding a newly created item to the CloudSearch index. Should probably only be called from asari_add_to_index above.



85
86
87
88
89
90
91
92
93
# File 'lib/asari/active_record.rb', line 85

def asari_add_item(obj)
  if self.asari_when
    return unless asari_should_index?(obj)
  end
  data = self.asari_data_item(obj)
  self.asari_instance.add_item(obj.send(:id), data)
rescue Asari::DocumentUpdateException => e
  self.asari_on_error(e)
end

#asari_data_item(obj) ⇒ Object

Gather all the data to send to the CloudSearch Can be overriden by the model to adapt to special cases. Returns a hash of the data to send to the CloudSearch



113
114
115
116
117
118
119
# File 'lib/asari/active_record.rb', line 113

def asari_data_item(obj)
  data = {}
  self.asari_fields.each do |field|
    data[field] = obj.send(field) || ""
  end
  data
end

#asari_fieldsObject



75
76
77
# File 'lib/asari/active_record.rb', line 75

def asari_fields
  self.class_variable_get(:@@asari_fields)
end

#asari_find(term, options = {}) ⇒ Object

Public: method for searching the index for the specified term and

returning all model objects that match.

Returns: a list of all matching AR model objects, or an empty list if no

records are found that match.

Raises: an Asari::SearchException error if there are issues

communicating with the CloudSearch server.


148
149
150
151
152
153
# File 'lib/asari/active_record.rb', line 148

def asari_find(term, options = {})
  records = self.asari_instance.search(term, options)
  ids = records.map { |id| id.to_i }

  records.replace(Array(self.where("id in (?)", ids)))
end

#asari_index(search_domain, fields, options = {}) ⇒ Object

Public: DSL method for adding this model object to the asari search index.

This method must be called in any object that includes Asari::ActiveRecord, or your methods will be very sad.

search_domain - the CloudSearch domain to use for indexing this model.
fields - an array of Symbols representing the list of fields that
  should be included in this index.
options - a hash of extra options to consider when indexing this
  model. Options:
    when - a string or symbol representing a method name, or a Proc to
      evaluate to determine if this model object should be indexed. On
      creation, if the method or Proc specified returns false, the
      model will not be indexed. On update, if the method or Proc
      specified returns false, the model will be removed from the
      index (if it exists there).
    aws_region - if this model is indexed on an AWS region other than
    us-east-1, specify it with this option.

Examples:

class User < ActiveRecord::Base
  include Asari::ActiveRecord

  asari_index("my-companies-users-asglkj4rsagkjlh34", [:name, :email])
  # or
  asari_index("my-companies-users-asglkj4rsagkjlh34", [:name, :email], :when => :should_be_indexed)
  # or
  asari_index("my-companies-users-asglkj4rsagkjlh34", [:name, :email], :when => Proc.new({ |user| user.published && !user.admin? }))


64
65
66
67
68
69
# File 'lib/asari/active_record.rb', line 64

def asari_index(search_domain, fields, options = {})
  aws_region = options.delete(:aws_region)
  self.class_variable_set(:@@asari_instance, Asari.new(search_domain,aws_region))
  self.class_variable_set(:@@asari_fields, fields)
  self.class_variable_set(:@@asari_when, options.delete(:when))
end

#asari_instanceObject



71
72
73
# File 'lib/asari/active_record.rb', line 71

def asari_instance
  self.class_variable_get(:@@asari_instance)
end

#asari_on_error(exception) ⇒ Object

Public: method for handling errors from Asari document updates. By default, this method causes all such exceptions (generated by issues from updates, creates, or deletes to the index) to be raised immediately to the caller; override this method on your activerecord object to handle the errors in a custom fashion. Be sure to return true if you don’t want the AR callbacks to halt execution.



162
163
164
# File 'lib/asari/active_record.rb', line 162

def asari_on_error(exception)
  raise exception
end

#asari_remove_item(obj) ⇒ Object

Internal: method for removing a soon-to-be deleted item from the CloudSearch index. Should probably only be called from asari_remove_from_index above.



123
124
125
126
127
# File 'lib/asari/active_record.rb', line 123

def asari_remove_item(obj)
  self.asari_instance.remove_item(obj.send(:id))
rescue Asari::DocumentUpdateException => e
  self.asari_on_error(e)
end

#asari_should_index?(object) ⇒ Boolean

Internal: method for looking at the when method/Proc (if defined) to

determine whether this model should be indexed.

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
# File 'lib/asari/active_record.rb', line 131

def asari_should_index?(object)
  when_test = self.asari_when
  if when_test.is_a? Proc
    return when_test.call(object)
  else
    return object.send(when_test)
  end
end

#asari_update_item(obj) ⇒ Object

Internal: method for updating a freshly edited item to the CloudSearch index. Should probably only be called from asari_update_in_index above.



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/asari/active_record.rb', line 97

def asari_update_item(obj)
  if self.asari_when
    unless asari_should_index?(obj)
      self.asari_remove_item(obj)
      return
    end
  end
  data = self.asari_data_item(obj)
  self.asari_instance.update_item(obj.send(:id), data)
rescue Asari::DocumentUpdateException => e
  self.asari_on_error(e)
end

#asari_whenObject



79
80
81
# File 'lib/asari/active_record.rb', line 79

def asari_when
  self.class_variable_get(:@@asari_when)
end