From 5ff4eb1f2821cd71ff3de03cb13cc64e5cfbfa5f Mon Sep 17 00:00:00 2001 From: Johnathon Wright Date: Wed, 4 Dec 2013 15:07:02 -0600 Subject: [PATCH] add collection.template.data[n].options because duh. This is not canon but we need it to move forward. Yes, this means you could do collection.items[n].data[y].options. Don't do that. hat tip: http://schinckel.net/2012/03/10/collection%2Bjson-primer-%28and-comments%29/ reference: https://github.com/mamund/collection-json/pull/6 --- lib/collection-json/attributes/data.rb | 4 ++++ spec/collection-json_spec.rb | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/collection-json/attributes/data.rb b/lib/collection-json/attributes/data.rb index 8ee1ad4..e8e925e 100644 --- a/lib/collection-json/attributes/data.rb +++ b/lib/collection-json/attributes/data.rb @@ -5,5 +5,9 @@ class Data < Attribute attribute :name attribute :value attribute :prompt + attribute :options, + transform: lambda { |data| data.each.map { |d| Data.from_hash(d) }}, + default: [], + find_method: {method_name: :datum, key: 'name'} end end diff --git a/spec/collection-json_spec.rb b/spec/collection-json_spec.rb index 37dc252..7e60b2b 100644 --- a/spec/collection-json_spec.rb +++ b/spec/collection-json_spec.rb @@ -60,6 +60,23 @@ response.queries.first.data.first.name.should eq('search') response.query('search').prompt.should eq('Search') end + + it 'includes the unapproved but totally necessary "options" attribute on data.' do + CollectionJSON.generate_for('/friends/') do |api| + api.set_template do |api| + api.add_data "force-side", options: [ + { + value: 'dark', + prompt: 'Dark Side' + }, + { + value: 'light', + prompt: 'Light Side' + } + ] + end + end.to_json.should == %|{"collection":{"href":"/friends/","template":{"data":[{"name":"force-side","options":[{"value":"dark","prompt":"Dark Side"},{"value":"light","prompt":"Light Side"}]}]}}}| + end end describe :parse do