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
94
95
96
# 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_fields.each do |field|
    data[field] = obj.send(field) || ""
  end
  self.asari_instance.add_item(obj.send(:id), data)
rescue Asari::DocumentUpdateException => e
  self.asari_on_error(e)
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.


143
144
145
146
147
148
# File 'lib/asari/active_record.rb', line 143

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.



157
158
159
# File 'lib/asari/active_record.rb', line 157

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.



118
119
120
121
122
# File 'lib/asari/active_record.rb', line 118

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)


126
127
128
129
130
131
132
133
# File 'lib/asari/active_record.rb', line 126

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.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/asari/active_record.rb', line 100

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_fields.each do |field|
    data[field] = obj.send(field)
  end
  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