Skip to content

Commit

Permalink
Merge pull request #2147 from jhawthorn/api_jbuilder
Browse files Browse the repository at this point in the history
Rewrite API RABL views as Jbuilder
  • Loading branch information
jhawthorn authored Aug 23, 2017
2 parents aa7a7ba + 81a5638 commit 96b30d6
Show file tree
Hide file tree
Showing 214 changed files with 648 additions and 717 deletions.
4 changes: 4 additions & 0 deletions api/app/views/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
inherit_from: ../../../.rubocop.yml

Style/LambdaCall:
EnforcedStyle: braces
11 changes: 11 additions & 0 deletions api/app/views/spree/api/address_books/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
json.array! @user_addresses do |user_address|
json.partial!("spree/api/addresses/address", address: user_address.address)

json.default user_address.default

# This is a bit of a hack.
# This attribute is only shown on the update action
if @address
json.update_target @address == user_address.address
end
end
7 changes: 0 additions & 7 deletions api/app/views/spree/api/address_books/show.v1.rabl

This file was deleted.

17 changes: 17 additions & 0 deletions api/app/views/spree/api/addresses/_address.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
json.cache! address do
json.(address, *address_attributes)
json.country do
if address.country
json.(address.country, *country_attributes)
else
json.nil!
end
end
json.state do
if address.state
json.(address.state, *state_attributes)
else
json.nil!
end
end
end
1 change: 1 addition & 0 deletions api/app/views/spree/api/addresses/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial!("spree/api/addresses/address", address: @address)
10 changes: 0 additions & 10 deletions api/app/views/spree/api/addresses/show.v1.rabl

This file was deleted.

4 changes: 4 additions & 0 deletions api/app/views/spree/api/adjustments/_adjustment.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.cache! [I18n.locale, adjustment] do
json.(adjustment, *adjustment_attributes)
json.display_amount(adjustment.display_amount.to_s)
end
4 changes: 0 additions & 4 deletions api/app/views/spree/api/adjustments/show.v1.rabl

This file was deleted.

1 change: 1 addition & 0 deletions api/app/views/spree/api/config/money.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.symbol(::Money.new(1, Spree::Config[:currency]).symbol)
2 changes: 0 additions & 2 deletions api/app/views/spree/api/config/money.v1.rabl

This file was deleted.

2 changes: 2 additions & 0 deletions api/app/views/spree/api/config/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.default_country_id(Spree::Country.default.id)
json.default_country_iso(Spree::Config[:default_country_iso])
3 changes: 0 additions & 3 deletions api/app/views/spree/api/config/show.v1.rabl

This file was deleted.

4 changes: 4 additions & 0 deletions api/app/views/spree/api/countries/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.countries(@countries) { |country| json.(country, *country_attributes) }
json.count(@countries.count)
json.current_page(@countries.current_page)
json.pages(@countries.total_pages)
7 changes: 0 additions & 7 deletions api/app/views/spree/api/countries/index.v1.rabl

This file was deleted.

4 changes: 4 additions & 0 deletions api/app/views/spree/api/countries/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.(@country, *country_attributes)
json.states(@country.states) do |state|
json.(state, :id, :name, :abbr, :country_id)
end
5 changes: 0 additions & 5 deletions api/app/views/spree/api/countries/show.v1.rabl

This file was deleted.

10 changes: 10 additions & 0 deletions api/app/views/spree/api/credit_cards/_credit_card.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
json.cache! [I18n.locale, credit_card] do
json.(credit_card, *creditcard_attributes)
json.address do
if credit_card.address
json.partial!("spree/api/addresses/address", address: credit_card.address)
else
json.nil!
end
end
end
6 changes: 6 additions & 0 deletions api/app/views/spree/api/credit_cards/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
json.credit_cards(@credit_cards) do |credit_card|
json.partial!("spree/api/credit_cards/credit_card", credit_card: credit_card)
end
json.count(@credit_cards.count)
json.current_page(@credit_cards.current_page)
json.pages(@credit_cards.total_pages)
7 changes: 0 additions & 7 deletions api/app/views/spree/api/credit_cards/index.v1.rabl

This file was deleted.

1 change: 1 addition & 0 deletions api/app/views/spree/api/credit_cards/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial!("spree/api/credit_cards/credit_card", credit_card: @credit_card)
7 changes: 0 additions & 7 deletions api/app/views/spree/api/credit_cards/show.v1.rabl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.error I18n.t(:delete_restriction_error, scope: "spree.api")
2 changes: 0 additions & 2 deletions api/app/views/spree/api/errors/delete_restriction.v1.rabl

This file was deleted.

1 change: 1 addition & 0 deletions api/app/views/spree/api/errors/gateway_error.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.error(I18n.t(:gateway_error, scope: "spree.api", text: @error))
2 changes: 0 additions & 2 deletions api/app/views/spree/api/errors/gateway_error.v1.rabl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.error(I18n.t(:invalid_api_key, key: api_key, scope: "spree.api"))
2 changes: 0 additions & 2 deletions api/app/views/spree/api/errors/invalid_api_key.v1.rabl

This file was deleted.

2 changes: 2 additions & 0 deletions api/app/views/spree/api/errors/invalid_resource.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.error(I18n.t(:invalid_resource, scope: "spree.api"))
json.errors(@resource.errors.to_hash)
3 changes: 0 additions & 3 deletions api/app/views/spree/api/errors/invalid_resource.v1.rabl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.error(I18n.t(:must_specify_api_key, scope: "spree.api"))
2 changes: 0 additions & 2 deletions api/app/views/spree/api/errors/must_specify_api_key.v1.rabl

This file was deleted.

1 change: 1 addition & 0 deletions api/app/views/spree/api/errors/not_found.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.error(I18n.t(:resource_not_found, scope: "spree.api"))
2 changes: 0 additions & 2 deletions api/app/views/spree/api/errors/not_found.v1.rabl

This file was deleted.

1 change: 1 addition & 0 deletions api/app/views/spree/api/errors/unauthorized.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.error(I18n.t(:unauthorized, scope: "spree.api"))
2 changes: 0 additions & 2 deletions api/app/views/spree/api/errors/unauthorized.v1.rabl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.error(Spree.t(:item_not_in_stock_transfer))

This file was deleted.

5 changes: 5 additions & 0 deletions api/app/views/spree/api/images/_image.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
json.(image, *image_attributes)
json.(image, :viewable_type, :viewable_id)
Spree::Image.attachment_definitions[:attachment][:styles].each do |k, _v|
json.set! "#{k}_url", image.attachment.url(k)
end
3 changes: 3 additions & 0 deletions api/app/views/spree/api/images/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json.images(@images) do |image|
json.partial!("spree/api/images/image", image: image)
end
4 changes: 0 additions & 4 deletions api/app/views/spree/api/images/index.v1.rabl

This file was deleted.

1 change: 1 addition & 0 deletions api/app/views/spree/api/images/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial!("spree/api/images/image", image: @image)
6 changes: 0 additions & 6 deletions api/app/views/spree/api/images/show.v1.rabl

This file was deleted.

1 change: 1 addition & 0 deletions api/app/views/spree/api/inventory_units/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.(@inventory_unit, *inventory_unit_attributes)
2 changes: 0 additions & 2 deletions api/app/views/spree/api/inventory_units/show.rabl

This file was deleted.

16 changes: 16 additions & 0 deletions api/app/views/spree/api/line_items/_line_item.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
json.cache! [I18n.locale, line_item] do
json.(line_item, *line_item_attributes)
json.single_display_amount(line_item.single_display_amount.to_s)
json.display_amount(line_item.display_amount.to_s)
json.total(line_item.total)
json.variant do
json.partial!("spree/api/variants/small", variant: line_item.variant)
json.(line_item.variant, :product_id)
json.images(line_item.variant.images) do |image|
json.partial!("spree/api/images/image", image: image)
end
end
json.adjustments(line_item.adjustments) do |adjustment|
json.partial!("spree/api/adjustments/adjustment", adjustment: adjustment)
end
end
2 changes: 2 additions & 0 deletions api/app/views/spree/api/line_items/new.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.attributes(([*line_item_attributes] - [:id]))
json.required_attributes([:variant_id, :quantity])
3 changes: 0 additions & 3 deletions api/app/views/spree/api/line_items/new.v1.rabl

This file was deleted.

1 change: 1 addition & 0 deletions api/app/views/spree/api/line_items/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial!("spree/api/line_items/line_item", line_item: @line_item)
15 changes: 0 additions & 15 deletions api/app/views/spree/api/line_items/show.v1.rabl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.(option_type, *option_type_attributes)
json.option_values(option_type.option_values) do |option_value|
json.(option_value, *option_value_attributes)
end
3 changes: 3 additions & 0 deletions api/app/views/spree/api/option_types/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json.array! @option_types do |option_type|
json.partial!("spree/api/option_types/option_type", option_type: option_type)
end
3 changes: 0 additions & 3 deletions api/app/views/spree/api/option_types/index.v1.rabl

This file was deleted.

1 change: 1 addition & 0 deletions api/app/views/spree/api/option_types/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial!("spree/api/option_types/option_type", option_type: @option_type)
5 changes: 0 additions & 5 deletions api/app/views/spree/api/option_types/show.v1.rabl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.(option_value, *option_value_attributes)
3 changes: 3 additions & 0 deletions api/app/views/spree/api/option_values/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json.array! @option_values do |option_value|
json.partial!("spree/api/option_values/option_value", option_value: option_value)
end
3 changes: 0 additions & 3 deletions api/app/views/spree/api/option_values/index.v1.rabl

This file was deleted.

1 change: 1 addition & 0 deletions api/app/views/spree/api/option_values/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial!("spree/api/option_values/option_value", option_value: @option_value)
2 changes: 0 additions & 2 deletions api/app/views/spree/api/option_values/show.v1.rabl

This file was deleted.

49 changes: 49 additions & 0 deletions api/app/views/spree/api/orders/_big.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
json.partial!("spree/api/orders/order", order: order)
json.payment_methods(order.available_payment_methods) do |payment_method|
json.(payment_method, :id, :name, :partial_name)
json.method_type payment_method.partial_name
end
json.bill_address do
if order.billing_address
json.partial!("spree/api/addresses/address", address: order.billing_address)
else
json.nil!
end
end
json.ship_address do
if order.shipping_address
json.partial!("spree/api/addresses/address", address: order.shipping_address)
else
json.nil!
end
end
json.line_items(order.line_items) do |line_item|
json.partial!("spree/api/line_items/line_item", line_item: line_item)
end
json.payments(order.payments) do |payment|
json.(payment, *payment_attributes)
json.payment_method { json.(payment.payment_method, :id, :name) }
json.source do
if payment.source
json.(payment.source, *payment_source_attributes)

if @current_user_roles.include?("admin")
json.(payment.source, :gateway_customer_profile_id, :gateway_payment_profile_id)
end
else
json.nil!
end
end
end
json.shipments(order.shipments) do |shipment|
json.partial!("spree/api/shipments/small", shipment: shipment)
end
json.adjustments(order.adjustments) do |adjustment|
json.partial!("spree/api/adjustments/adjustment", adjustment: adjustment)
end
json.permissions do
json.can_update current_ability.can?(:update, order)
end
json.credit_cards(order.valid_credit_cards) do |credit_card|
json.partial!("spree/api/credit_cards/credit_card", credit_card: credit_card)
end
10 changes: 10 additions & 0 deletions api/app/views/spree/api/orders/_order.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
json.cache! [I18n.locale, order] do
json.(order, *order_attributes)
json.display_item_total(order.display_item_total.to_s)
json.total_quantity(order.line_items.sum(:quantity))
json.display_total(order.display_total.to_s)
json.display_ship_total(order.display_ship_total)
json.display_tax_total(order.display_tax_total)
json.token(order.guest_token)
json.checkout_steps(order.checkout_steps)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.error(@coupon_message)
2 changes: 0 additions & 2 deletions api/app/views/spree/api/orders/could_not_apply_coupon.v1.rabl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.error(I18n.t(:could_not_transition, scope: "spree.api.order"))
json.errors(@order.errors.to_hash)
3 changes: 0 additions & 3 deletions api/app/views/spree/api/orders/could_not_transition.v1.rabl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.errors(expected_total: [I18n.t(:expected_total_mismatch, scope: "spree.api.order")])

This file was deleted.

6 changes: 6 additions & 0 deletions api/app/views/spree/api/orders/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
json.orders(@orders) do |order|
json.partial!("spree/api/orders/order", order: order)
end
json.count(@orders.count)
json.current_page(@orders.current_page)
json.pages(@orders.total_pages)
7 changes: 0 additions & 7 deletions api/app/views/spree/api/orders/index.v1.rabl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.errors([I18n.t(:invalid_shipping_method, scope: "spree.api.order")])

This file was deleted.

6 changes: 6 additions & 0 deletions api/app/views/spree/api/orders/mine.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
json.orders(@orders) do |order|
json.partial!("spree/api/orders/big", order: order)
end
json.count(@orders.count)
json.current_page(@orders.current_page)
json.pages(@orders.total_pages)
Loading

0 comments on commit 96b30d6

Please sign in to comment.