Skip to content

Commit

Permalink
Merge pull request #40 from jtrost/hash-with-indifferent-access
Browse files Browse the repository at this point in the history
Return a HashWithIndifferentAccess from json_for_autocomplete
  • Loading branch information
bigtunacan committed Jan 12, 2016
2 parents dbff245 + 307ef9f commit 31da179
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rails-jquery-autocomplete/autocomplete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_object(model_sym)
#
def json_for_autocomplete(items, method, extra_data=[])
items = items.collect do |item|
hash = {"id" => item.id.to_s, "label" => item.send(method), "value" => item.send(method)}
hash = HashWithIndifferentAccess.new({"id" => item.id.to_s, "label" => item.send(method), "value" => item.send(method)})
extra_data.each do |datum|
hash[datum] = item.send(datum)
end if extra_data
Expand Down
11 changes: 11 additions & 0 deletions test/lib/rails-jquery-autocomplete/autocomplete_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ class AutocompleteTest < Test::Unit::TestCase
assert_equal response["label"], "Object Name"
end

should 'return an instance of HashWithIndifferentAccess' do
item = mock(Object)
mock(item).send(:name).times(2) { 'Object Name' }
mock(item).id { 1 }
items = [item]
response = self.json_for_autocomplete(items, :name).first
assert_equal response.is_a?(HashWithIndifferentAccess), true
assert_equal response["id"], "1"
assert_equal response[:id], "1"
end

context 'with extra data' do
should 'add that extra data to result' do
item = mock(Object)
Expand Down

0 comments on commit 31da179

Please sign in to comment.