Skip to content

Commit

Permalink
Make sure that secondary indices go through a custom serialization layer
Browse files Browse the repository at this point in the history
  • Loading branch information
braintreeps committed Jul 3, 2012
1 parent e8e417e commit f323aa8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/curator/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def _indexed_fields
end

def _indexes(object)
index_values = _indexed_fields.map { |field| [field, object.send(field)] }
index_values = _indexed_fields.map { |field| [field, _serialize(object)[field.to_s]] }
index_values += [
[:created_at, _format_time_for_index(object.send(:created_at))],
[:updated_at, _format_time_for_index(object.send(:updated_at))],
Expand Down
32 changes: 32 additions & 0 deletions spec/curator/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,38 @@
TestModelRepository.find_by_multiple_values("third").should == []
end

it "can index a ruby Set serialized as an array" do
def_transient_class(:TestModelRepository) do
include Curator::Repository
attr_reader :id, :multiple_values
indexed_fields :multiple_values

def self.serialize(model)
super.tap do |attributes|
attributes["multiple_values"] = model.multiple_values.to_a
end
end

end

def_transient_class(:TestModel) do
include Curator::Model
attr_reader :id, :multiple_values

def initialize(args={})
args.each {|k,v| instance_variable_set "@#{k}", v}
@multiple_values = Set.new(args[:multiple_values])
end
end

model = TestModel.new(:multiple_values => ["first", "second"])
TestModelRepository.save(model)

TestModelRepository.find_by_multiple_values("first").should == [model]
TestModelRepository.find_by_multiple_values("second").should == [model]
TestModelRepository.find_by_multiple_values("third").should == []
end

it "indexes created_at and updated_at by default" do
def_transient_class(:TestModelRepository) do
include Curator::Repository
Expand Down

0 comments on commit f323aa8

Please sign in to comment.