Reworking everything to use RABL

This commit is contained in:
Will Marshall
2014-01-07 15:06:47 +11:00
parent 32c7682da2
commit 4d03f65766
9 changed files with 73 additions and 22 deletions

View File

@@ -5,6 +5,7 @@ window.Shop = angular.module("Shop", ["ngResource", "filters"]).config ($httpPro
angular.module("filters", []).filter "truncate", ->
(text, length, end) ->
text = text || ""
length = 10 if isNaN(length)
end = "..." if end is `undefined`
if text.length <= length or text.length - end.length <= length

View File

@@ -10,25 +10,28 @@ class ShopController < BaseController
end
def products
if products = current_order_cycle.andand.products_distributed_by(@distributor)
render json: products, root: false
else
unless @products = current_order_cycle.andand.products_distributed_by(@distributor)
render json: "", status: 404
end
end
def order_cycle
if oc = OrderCycle.with_distributor(@distributor).active.find_by_id(params[:order_cycle_id])
current_order(true).set_order_cycle! oc
render status: 200, json: oc
if request.post?
if oc = OrderCycle.with_distributor(@distributor).active.find_by_id(params[:order_cycle_id])
current_order(true).set_order_cycle! oc
render partial: "shop/order_cycle"
else
render status: 404, json: ""
end
else
render status: 404, json: ""
render partial: "shop/order_cycle"
end
end
private
def set_distributor
unless @distributor = current_distributor
redirect_to root_path
end

View File

@@ -0,0 +1,3 @@
object current_order_cycle
attributes :orders_close_at
attribute :id => :order_cycle_id

View File

@@ -1,7 +1,7 @@
%ordercycle{"ng-controller" => "OrderCycleCtrl"}
:javascript
angular.module('Shop').value('orderCycleData', #{OrderCycleSerializer.new(current_order_cycle).to_json})
angular.module('Shop').value('orderCycleData', #{render "shop/order_cycle"})
- if @order_cycles.empty?

View File

@@ -13,13 +13,14 @@
%tbody{"ng-repeat" => "product in data.products | filter:query"}
%tr.product
%td
%img{src: "{{ product.master.images[0].small_url }}"}
{{product.master.images[0].alt}}
-#%img{src: "{{ product.master.images[0].small_url }}"}
-#{{product.master.images[0].alt}}
%td
%h5
{{ product.name }}
{{ product.supplier.name }}
%td.notes {{ product.description | truncate:80 }}
%td {{ product.master.options_text }}
%td
%input{type: :number, value: 0, min: 0, name: "variants[{{product.master.id}}]"}

View File

@@ -0,0 +1,25 @@
collection @products
attributes :id, :name, :description, :price, :permalink
child :supplier do
attributes :id, :name, :description
end
child :master => :master do
attributes :id, :is_master, :count_on_hand, :options_text
child :images => :images do
attributes :id, :alt
node do |img|
{:small_url => img.attachment.url(:small, false)}
end
end
end
child :variants => :variants do |variant|
attributes :id, :is_master, :count_on_hand, :options_text
child :images => :images do
attributes :id, :alt
node do |img|
{:small_url => img.attachment.url(:small, false)}
end
end
end

View File

@@ -5,6 +5,7 @@ Openfoodnetwork::Application.routes.draw do
resource :shop, controller: :shop do
get :products
post :order_cycle
get :order_cycle
end
resources :enterprises do

View File

@@ -37,14 +37,27 @@ describe ShopController do
controller.current_order_cycle.should == oc2
end
it "should return the order cycle details when the oc is selected" do
oc1 = create(:order_cycle, distributors: [d])
oc2 = create(:order_cycle, distributors: [d])
spree_post :order_cycle, order_cycle_id: oc2.id
response.body.should have_content OrderCycleSerializer.new(oc2).to_json
context "RABL tests" do
render_views
it "should return the order cycle details when the oc is selected" do
oc1 = create(:order_cycle, distributors: [d])
oc2 = create(:order_cycle, distributors: [d])
spree_post :order_cycle, order_cycle_id: oc2.id
response.should be_success
response.body.should have_content oc2.id
end
it "should return the current order cycle when hit with GET" do
oc1 = create(:order_cycle, distributors: [d])
controller.stub(:current_order_cycle).and_return oc1
spree_get :order_cycle
response.body.should have_content oc1.id
end
end
it "should not allow the user to select an invalid order cycle" do
oc1 = create(:order_cycle, distributors: [d])
oc2 = create(:order_cycle, distributors: [d])
@@ -95,11 +108,15 @@ describe ShopController do
response.body.should be_empty
end
it "only returns products for the current order cycle" do
controller.stub(:current_order_cycle).and_return order_cycle
xhr :get, :products
response.body.should == [Spree::ProductSerializer.new(product)].to_json
context "RABL tests" do
render_views
it "only returns products for the current order cycle" do
controller.stub(:current_order_cycle).and_return order_cycle
xhr :get, :products
response.body.should have_content product.name
end
end
end
end
end

View File

@@ -108,8 +108,8 @@ feature "As a consumer I want to shop with a distributor", js: true do
select "frogs", :from => "order_cycle_id"
end
it "should let us add products to our cart" do
fill_in "quantity_variant_#{variant.id}", with: "1"
find("form.custom > input.button.right:first-child").click
fill_in "variants[#{variant.id}]", with: "1"
first("form.custom > input.button.right").click
current_path.should == "/cart"
page.should have_content product.name
end