diff --git a/app/views/json/_enterprises.rabl b/app/views/json/_enterprises.rabl new file mode 100644 index 0000000000..61db6f9e8d --- /dev/null +++ b/app/views/json/_enterprises.rabl @@ -0,0 +1,10 @@ +# DON'T USE DIRECTLY - for inheritance +attributes :name, :id + +child :taxons => :taxons do + attributes :name, :id +end + +child :address do + extends "json/partials/address" +end diff --git a/app/views/json/_hubs.rabl b/app/views/json/_hubs.rabl index dbd0538caf..2f91520d73 100644 --- a/app/views/json/_hubs.rabl +++ b/app/views/json/_hubs.rabl @@ -1,16 +1,5 @@ collection Enterprise.is_distributor -attributes :name, :id - -child :taxons do - attributes :name, :id -end - -child :address do - attributes :city, :zipcode - node :state do |address| - address.state.abbr - end -end +extends 'json/enterprises' node :pickup do |hub| not hub.shipping_methods.where(:require_ship_address => false).empty? diff --git a/app/views/json/_producers.rabl b/app/views/json/_producers.rabl index b0fd6265c6..318eeacbf7 100644 --- a/app/views/json/_producers.rabl +++ b/app/views/json/_producers.rabl @@ -1,5 +1,5 @@ collection @producers -attributes :name, :id +extends 'json/enterprises' node :path do |producer| producer_path(producer) diff --git a/app/views/json/partials/_address.rabl b/app/views/json/partials/_address.rabl new file mode 100644 index 0000000000..b40bbc790b --- /dev/null +++ b/app/views/json/partials/_address.rabl @@ -0,0 +1,4 @@ +attributes :city, :zipcode +node :state do |address| + address.state.abbr +end diff --git a/app/views/producers/index.haml b/app/views/producers/index.haml index 33d5093ea3..c368de6011 100644 --- a/app/views/producers/index.haml +++ b/app/views/producers/index.haml @@ -2,6 +2,8 @@ :javascript angular.module('Darkswarm').value('producers', #{render partial: "json/producers", object: @producers}) + -#%pre + -#{{ Producers.producers | json }} .row{bindonce: true} .small-12.columns .active_table diff --git a/spec/support/views/rabl_helper.rb b/spec/support/views/rabl_helper.rb new file mode 100644 index 0000000000..4ffcbef690 --- /dev/null +++ b/spec/support/views/rabl_helper.rb @@ -0,0 +1,9 @@ +module RablHelper + # See https://github.com/nesquena/rabl/issues/231 + # Allows us to test RABL views using URL helpers + class FakeContext + include Singleton + include Rails.application.routes.url_helpers + end +end + diff --git a/spec/views/json/producers.json.rabl_spec.rb b/spec/views/json/producers.json.rabl_spec.rb new file mode 100644 index 0000000000..55e9dafc35 --- /dev/null +++ b/spec/views/json/producers.json.rabl_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe 'json/_producers.json.rabl' do + let!(:producer) { create(:supplier_enterprise) } + let(:render) { Rabl.render([producer], 'json/producers', view_path: 'app/views', scope: RablHelper::FakeContext.instance) } + + it "renders a list of producers" do + render.should have_json_type(Array).at_path '' + render.should have_json_type(Object).at_path '0' + end + + it "renders names" do + render.should be_json_eql(producer.name.to_json).at_path '0/name' + end +end