Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/nazrin/active_record/data_accessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ def results(client)

# load from activerecord
def load_all(ids)
key = @model.class_eval { class_variable_get(:@@nazrin_doc_id) }
records_table = {}
@options.each do |k, v|
@model = @model.send(k, v)
end
@model.where(id: ids).each do |record|
records_table[record.id] = record
@model.where("#{key} in (?)", ids).each do |record|
records_table[record.send("#{key}")] = record
end
ids.map do |id|
records_table.select { |k, _| k == id.to_i }[id.to_i]
Expand Down
7 changes: 4 additions & 3 deletions lib/nazrin/active_record/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ def nazrin_search(options = {})
client
end

def nazrin_searchable(&block)
def nazrin_searchable(key=:id, &block)
class_variable_set(
:@@nazrin_doc_client,
Nazrin::DocumentClient.new)
class_variable_set(:@@nazrin_doc_id, key)
class_variable_set(:@@nazrin_search_field_data, {})
block.call
end
Expand Down Expand Up @@ -79,15 +80,15 @@ def nazrin_eval_field_data(obj)

def nazrin_add_document(obj)
nazrin_doc_client.add_document(
obj.send(:id), nazrin_eval_field_data(obj))
obj.send(class_variable_get(:@@nazrin_doc_id)), nazrin_eval_field_data(obj))
end

def nazrin_update_document(obj)
nazrin_add_document(obj)
end

def nazrin_delete_document(obj)
nazrin_doc_client.delete_document(obj.send(:id))
nazrin_doc_client.delete_document(obj.send(class_variable_get(:@@nazrin_doc_id)))
end
end
end
Expand Down