test/inline_test.rb in representable-1.7.7 vs test/inline_test.rb in representable-1.8.0

- old
+ new

@@ -25,11 +25,11 @@ end { :hash => [Representable::Hash, {"songs"=>[{"name"=>"Alive"}]}, {"songs"=>[{"name"=>"You've Taken Everything"}]}], :json => [Representable::JSON, "{\"songs\":[{\"name\":\"Alive\"}]}", "{\"songs\":[{\"name\":\"You've Taken Everything\"}]}"], - :xml => [Representable::XML, "<open_struct>\n <song>\n <name>Alive</name>\n </song>\n</open_struct>", "<open_struct><song><name>You've Taken Everything</name></song></open_struct>", { :from => :song }], + :xml => [Representable::XML, "<open_struct>\n <song>\n <name>Alive</name>\n </song>\n</open_struct>", "<open_struct><song><name>You've Taken Everything</name></song></open_struct>", { :as => :song }], :yaml => [Representable::YAML, "---\nsongs:\n- name: Alive\n", "---\nsongs:\n- name: You've Taken Everything\n"], }.each do |format, cfg| mod, output, input, collection_options = cfg collection_options ||= {} @@ -183,11 +183,11 @@ representer!(:module => mod, :decorator => true) do self.representation_wrap = :request if format == :xml property :requester property :song do - property :name, :decorator_scope => true + property :name, :exec_context => :decorator define_method :name do represented.name.upcase end @@ -198,8 +198,46 @@ let (:request) { representer.prepare(OpenStruct.new(:song => Song.new("Alive"))) } it do render(request).must_equal_document output end + end + end + + + describe "deprecate mixing :extend and inline representers" do # TODO: remove in 2.0. + representer! do + rpr_module = Module.new do + include Representable::Hash + property :title + end + property :song, :extend => rpr_module do + property :artist + end + end + + it do OpenStruct.new(:song => OpenStruct.new(:title => "The Fever And The Sound", :artist => "Strung Out")).extend(representer). + to_hash. + must_equal({"song"=>{"artist"=>"Strung Out", "title"=>"The Fever And The Sound"}}) + end + end + + + describe "include module in inline representers" do + representer! do + extension = Module.new do + include Representable::Hash + property :title + end + + property :song do + include extension + property :artist + end + end + + it do OpenStruct.new(:song => OpenStruct.new(:title => "The Fever And The Sound", :artist => "Strung Out")).extend(representer). + to_hash. + must_equal({"song"=>{"artist"=>"Strung Out", "title"=>"The Fever And The Sound"}}) end end end