JSONApi progress

This commit is contained in:
2015-09-03 20:42:48 +02:00
parent f47a8a9ed0
commit e4dde28dd0
27 changed files with 215 additions and 64 deletions
+10 -5
View File
@@ -8,14 +8,21 @@ gem 'rack-cors', require: 'rack/cors'
# gem 'sqlite3'
#source 'https://rails-assets.org' do
#end
source 'https://rails-assets.org' do
group :test do
gem 'rails-assets-qunit'
gem 'rails-assets-ember-qunit'
end
end
gem 'slim-rails'
# Gems used only for assets and not required
# in production environments by default.
gem 'active_model_serializers', '~> 0.10.0.rc2' # explicitly outside assets
#gem 'active_model_serializers', '~> 0.10.0.rc2' # explicitly outside assets
gem 'active_model_serializers', github: 'rails-api/active_model_serializers' # explicitly outside assets
#gem 'jsonapi-serializers', github: 'bterkuile/jsonapi-serializers'
gem 'jsonapi-serializers', path: '/home/benjamin/development/rails/components/jsonapi-serializers'
group :assets do
gem 'jquery-rails'
gem 'jquery-ui-rails'
@@ -97,8 +104,6 @@ group :development, :test do
gem 'rspec-rails'
gem 'pry-rails'
gem 'test_squad'
gem 'rails-assets-qunit'
gem 'rails-assets-ember-qunit'
gem 'factory_girl_rails'
end
+26 -11
View File
@@ -31,15 +31,31 @@ GIT
GIT
remote: git://github.com/bterkuile/simply_stored.git
revision: 05cb3a06a9d4a3588efd4491db42fac84276befb
revision: 57773e8d4b3fc3410875576b3257ffa628e8d4ec
specs:
simply_stored (1.0.0)
activesupport
couch_potato (>= 0.2.15)
rest-client (>= 1.4.2)
GIT
remote: git://github.com/rails-api/active_model_serializers.git
revision: 6784866a2d4a57e5b1c571afcbf76362708b11ab
specs:
active_model_serializers (0.10.0.rc2)
actionpack (>= 4.0)
activemodel (>= 4.0)
railties (>= 4.0)
PATH
remote: /home/benjamin/development/rails/components/jsonapi-serializers
specs:
jsonapi-serializers (0.2.6)
activesupport
GEM
remote: https://rubygems.org/
remote: https://rails-assets.org/
specs:
ace-rails-ap (4.0.0)
actionmailer (4.2.4)
@@ -61,14 +77,12 @@ GEM
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
active-model-adapter-source (2.0.0)
active-model-adapter-source (2.0.1)
ember-data-source (>= 1.13, < 3.0)
active_attr (0.8.5)
activemodel (>= 3.0.2, < 5.0)
activesupport (>= 3.0.2, < 5.0)
active_decorator (0.5.3)
active_model_serializers (0.10.0.rc2)
activemodel (>= 4.0)
activejob (4.2.4)
activesupport (= 4.2.4)
globalid (>= 0.3.0)
@@ -261,7 +275,7 @@ GEM
mime-types (>= 1.16, < 3)
method_source (0.8.2)
mime-types (2.6.1)
mini_magick (4.2.10)
mini_magick (4.3.1)
mini_portile (0.6.2)
minitest (5.8.0)
multi_json (1.11.2)
@@ -324,7 +338,7 @@ GEM
railties (= 4.2.4)
sprockets-rails
rails-assets-ember-qunit (0.4.10)
rails-assets-qunit (1.18.0)
rails-assets-qunit (1.19.0)
rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha)
rails-dom-testing (1.0.7)
@@ -405,7 +419,7 @@ GEM
spring (1.3.6)
spring-commands-rspec (1.0.4)
spring (>= 0.9.1)
sprockets (3.3.3)
sprockets (3.3.4)
rack (~> 1.0)
sprockets-rails (2.3.2)
actionpack (>= 3.0)
@@ -426,7 +440,7 @@ GEM
thor (0.19.1)
thread_safe (0.3.5)
tilt (1.4.1)
timers (4.0.1)
timers (4.0.4)
hitimes
tinymce-rails (4.2.4)
railties (>= 3.1.1)
@@ -465,7 +479,7 @@ DEPENDENCIES
ace-rails-ap
active_attr
active_decorator
active_model_serializers (~> 0.10.0.rc2)
active_model_serializers!
airbrussh
bourbon
cancancan
@@ -494,6 +508,7 @@ DEPENDENCIES
jquery-rails
jquery-ui-rails
js-routes
jsonapi-serializers!
kaminari
launchy
letter_opener
@@ -508,8 +523,8 @@ DEPENDENCIES
quiet_assets
rack-cors
rails (~> 4.2.3)
rails-assets-ember-qunit
rails-assets-qunit
rails-assets-ember-qunit!
rails-assets-qunit!
rqrcode
rspec-its
rspec-rails
@@ -4,6 +4,7 @@ App.MenuProductCategoriesComponent = Ember.Component.extend
active_product_categories: (->
console.log "Reevaluate product categories"
list = @get('product_categories')
return [] unless list
now = new Date()
list = list.filter (product_category) =>
return false unless product_category.get("active_on_#{$day_names[now.getDay()]}")
@@ -8,7 +8,7 @@ App.TableController = Ember.Controller.extend
else
# no list and open supplier
if @get('model.occupied') then false else true
).property('globals.active_list.id', 'supplier.can_take_orders', 'model.occupied', 'model.id', 'globals.active_list.table.id')
).property('globals.active_list.id', 'model.supplier.can_take_orders', 'model.occupied', 'model.id', 'globals.active_list.table.id', 'model.supplier.id')
supplier: Ember.computed.alias 'model.supplier'
showJoinButton: Ember.computed 'globals.active_list.id', 'supplier.can_take_orders', 'model.occupied', 'model.id', 'globals.active_list.table.id', ->
return false unless @get('supplier.can_take_orders')
@@ -1,6 +1,6 @@
.row: .small-12.columns
h3
span.supplier-name= supplier.name
span.supplier-name= model.supplier.name
span.table-number
' #
= model.number
@@ -13,7 +13,7 @@
button.join-table-button{action "joinOccupiedTable"}=t 'join_request.requestor.join_this_table'
.row
if tableCanTakeOrders
.large-6.columns= menu-product-categories product_categories=supplier.product_categories orderProducts=true
.large-6.columns= menu-product-categories product_categories=model.supplier.product_categories orderProducts=true
.large-6.columns= new-product-orders-list table=model
else
.large12= menu-product-categories product_categories=supplier.product_categories orderProducts=false
.large12= menu-product-categories product_categories=model.supplier.product_categories orderProducts=false
+9 -3
View File
@@ -8,7 +8,7 @@ module Users
lists.reject!{|l| l.id == params[:exclude_list]} if params[:exclude_list].present?
lists.reject!{|l| l.id == current_user.active_list_id } if current_user && current_user.active_list_id.present? # see spec Loading lists and switching to the order products view works, lists loading may unlink active list orders
lists.include_relation(:users, :supplier)
render json: lists, each_serializer: Users::ListSerializer, meta: {total_pages: lists.total_pages, page: lists.current_page} #, root: :lists
render json: JSONAPI::Serializer.serialize(lists, serializer: Users::ListSerializer, include: %w[supplier users], is_collection: true, meta: {total_pages: lists.total_pages, page: lists.current_page}) #, root: :lists
end
#EMBER
@@ -17,11 +17,17 @@ module Users
show
end
def table
list = List.find(params[:id])
render json: {}, status: :not_found and return unless list.present?
@table = list.table
render json: JSONAPI::Serializer.serialize(@table, serializer: Users::TableSerializer)
end
def show
@list ||= List.find(params[:id])
render json: {}, status: :not_found and return unless @list.present? && Array.wrap(@list.user_ids).include?(current_user.id)
[@list].include_relation(:users)
render json: @list, serializer: Users::ListSerializer, include: %w[supplier users]
render json: JSONAPI::Serializer.serialize(@list, serializer: Users::ListSerializer, include: %w[supplier users])
end
end
end
+1 -1
View File
@@ -8,7 +8,7 @@ module Users
@list = List.find(params[:list_id])
render json: {}, status: :not_found and return unless @list.present? && Array.wrap(@list.user_ids).include?(current_user.id)
orders = @list.orders.include_relation(:product_orders)
render json: orders, each_serializer: Users::OrderSerializer, include: %w[product_orders]
render json: JSONAPI::Serializer.serialize(orders, serializer: Users::OrderSerializer, include: %w[product_orders product_orders.order], is_collection: true)
end
# Used by the user Ember app
+8 -1
View File
@@ -2,7 +2,14 @@ module Users
class TablesController < Users::ApplicationController
def show
@table = Table.find(params[:id])
render json: @table, serializer: Users::TableSerializer
render json: JSONAPI::Serializer.serialize(@table, serializer: Users::TableSerializer)
end
def supplier
table = Table.find(params[:id])
supplier = table.supplier
supplier.product_categories.include_relations(:products)
render json: JSONAPI::Serializer.serialize(supplier, serializer: Users::SupplierSerializer, include: %w[product_categories product_categories.products product_categories.supplier])
end
end
end
+8 -3
View File
@@ -1,11 +1,16 @@
class Users::ListSerializer < Qwaiter::Serializer
class Users::ListSerializer < Qwaiter::UserBaseSerializer
# user ids for facebook pictures
self.root = :list
#self.root = :list
attributes :state, :needs_help, :needs_payment, :user_requests_closing,
:is_paid, :price, :closed_at
has_many :users, serializer: Users::UserSerializer
has_many :orders, url: ->(list){ "/user/lists/#{list.id}/orders" }, serializer: OrderSerializer
has_many(:orders, serializer: OrderSerializer){}
has_one :supplier, serializer: Users::SupplierSerializer
has_one :table
def include_orders?
@_include_linkages.any?{|link| link =~ /orders/}
end
#belongs_to :table
end
+2 -2
View File
@@ -1,8 +1,8 @@
class Users::OrderSerializer < Qwaiter::Serializer
class Users::OrderSerializer < Qwaiter::UserBaseSerializer
attributes :state #, :list_id, :section_id, :table_id #, :price
has_many :product_orders, serializer: Users::ProductOrderSerializer
belongs_to :list
has_one(:list, serializer: Users::ListSerializer ) #{ List.new(id: object.list_id )}
#belongs_to :section
#belongs_to :table
end
@@ -0,0 +1,7 @@
class Users::ProductCategorySerializer < Qwaiter::UserBaseSerializer
attributes :name, :supplier_id, :active_on_sunday, :active_on_monday, :active_on_tuesday, :active_on_wednesday,
:active_on_thursday, :active_on_friday, :active_on_saturday, :full_day, :start_from, :end_on,
:position
has_many :products, serializer: Users::ProductSerializer
has_one :supplier, serializer: Users::SupplierSerializer
end
@@ -1,6 +1,6 @@
# Used for user ember1
class Users::ProductOrderSerializer < Qwaiter::Serializer
class Users::ProductOrderSerializer < Qwaiter::UserBaseSerializer
attributes :quantity, :price, :product_name, :product_variant
# belongs_to :product #DO NOT USE THIS, THIS IS NOT NEEDED
belongs_to :order
has_one :order, serializer: Users::OrderSerializer
end
@@ -0,0 +1,13 @@
class Users::ProductSerializer < Qwaiter::UserBaseSerializer
attributes :name, :price, :description, :image, :code, :position, :visible, :active, :product_category_id
has_many :product_variants, serializer: Users::ProductVariantSerializer
def image
if object.image.present?
{small: object.image.url(:small)}
else
nil
end
end
end
@@ -0,0 +1,3 @@
class Users::ProductVariantSerializer < Qwaiter::UserBaseSerializer
attributes :name
end
+7 -7
View File
@@ -1,9 +1,9 @@
class Users::SupplierSerializer < Qwaiter::Serializer
self.root = :supplier
attributes :extended_version, :open, :name
def extended_version
false
end
class Users::SupplierSerializer < Qwaiter::UserBaseSerializer
#self.root = :supplier
attributes :open, :name
has_many :product_categories, serializer: Users::ProductCategorySerializer
#def extended_version
# false
#end
end
+4 -3
View File
@@ -1,6 +1,7 @@
class Users::TableSerializer < Qwaiter::Serializer
self.root = :table
attributes :number, :width, :height, :position_x, :position_y, :section_id, :occupied, :supplier_id #, :alist_id
class Users::TableSerializer < Qwaiter::UserBaseSerializer
#self.root = :table
attributes :number, :width, :height, :position_x, :position_y, :section_id, :occupied #, :supplier_id #, :alist_id
has_one :supplier, serializer: Users::SupplierSerializer
#def list_id
#object.active_list_id || object.active_list.try(:id)
+2 -2
View File
@@ -1,5 +1,5 @@
class Users::UserSerializer < Qwaiter::Serializer
self.root = :user
class Users::UserSerializer < Qwaiter::UserBaseSerializer
#self.root = :user
attributes :email, :provider, :uid, :name, :avatar
def name
@@ -10,10 +10,13 @@ module ActiveModel::SerializerSupport
end
class Qwaiter::JsonAdapter < ActiveModel::Serializer::Adapter::JsonApi
def add_resource_relationships(attrs, serializer, options = {})
def add_resource_relationships_old1(attrs, serializer, options = {})
options[:add_included] = options.fetch(:add_included, true)
serializer.class._associations.dup.each do |name, association_options| #do |name, association, opts|
serializer.associations.each do |association| #do |name, association, opts|
name = association.key
association_options = association.options
next unless object = serializer.object
=begin
if association_options[:type] == :has_many
# todo check for object["#{name}_ids"]
association_value = association_options[:association_options][:url] ? [] : serializer.send(name)
@@ -42,8 +45,12 @@ class Qwaiter::JsonAdapter < ActiveModel::Serializer::Adapter::JsonApi
elsif !association_value.nil? && !association_value.instance_of?(Object)
association_options[:association_options][:virtual_value] = association_value
end
=end
association_serializer = association.serializer
opts = association_options[:association_options]
opts = association.options
attrs[:relationships] ||= {}
binding.pry
if association_serializer.respond_to?(:each)
if opts[:url]
+8 -1
View File
@@ -83,10 +83,17 @@ Qwaiter::Application.routes.draw do
get :current
end
resources :orders, only: [:index]
member do
get :table
end
end
resources :orders, only: [:create]
resources :tables, only: [:show]
resources :tables, only: [:show] do
member do
get :supplier
end
end
end
+1
View File
@@ -4,6 +4,7 @@ module Qwaiter
extend ActiveSupport::Autoload
autoload :Distribution
autoload :Serializer
autoload :UserBaseSerializer
autoload :Counter
autoload :Broadcaster
autoload :Couchbase
+30
View File
@@ -0,0 +1,30 @@
class Qwaiter::UserBaseSerializer
include JSONAPI::Serializer
def base_url
"/user"
end
def format_name(attribute_name)
#attribute_name.to_s.dasherize
attribute_name.to_s
end
def unformat_name(attribute_name)
#attribute_name.to_s.underscore
attribute_name.to_s
end
def relationship_related_link(attribute_name)
super
end
def relationship_self_link(attribute_name)
end
def self.attributes(*attrs)
attrs.each do |attr|
attribute attr
end
end
end
+26
View File
@@ -0,0 +1,26 @@
require 'spec_helper'
describe Users::ListSerializer, type: :serializer do
it "does not perform extra queries" do
l = create :list
o1 = create :order, list: l
create :product_order, order: o1
create :product_order, order: o1
o2 = create :order, list: l
create :product_order, order: o2
create :product_order, order: o2
l.add_user create :user
l.add_user create :user
list = List.find(l.id)
[list].include_relations(:users)
result = nil
#result = expect{ JSONAPI::Serializer.serialize(list, serializer: described_class) }.not_to perform_any_queries
expect{ result = JSONAPI::Serializer.serialize(list, serializer: described_class, include: %w[supplier users]) }.not_to exceed_query_limit 1 # supplier
binding.pry
#expect{ object_as_json orders }.not_to exceed_query_limit 0
#expect{ object_as_json list }.not_to exceed_query_limit 0
end
end
+4 -13
View File
@@ -1,7 +1,6 @@
require 'spec_helper'
describe Users::OrderSerializer do
let(:adapter){ ActiveModel::Serializer.config.adapter }
describe Users::OrderSerializer, type: :serializer do
it "does not perform extra queries" do
l = create :list
o1 = create :order, list: l
@@ -13,18 +12,10 @@ describe Users::OrderSerializer do
list = List.find(l.id)
orders = list.orders.include_relation(:product_orders)
#expect{ object_as_json orders }.not_to exceed_query_limit 0
expect{ object_as_json orders }.not_to perform_any_queries
result = object_as_json(orders)
result = nil
expect{ result = JSONAPI::Serializer.serialize(orders, serializer: described_class, include: 'product_orders', is_collection: true)}.not_to perform_any_queries
binding.pry
#result[:included].size.should eq 4
result[:included].size.should eq 4
end
def object_as_json(obj)
serializer = if obj.is_a?(Array)
ActiveModel::Serializer::ArraySerializer.new(obj, serializer: described_class, add_included: true)
else
serializer = described_class.new(obj)
end
adapter.new(serializer, include: %w[product_orders]).as_json
end
end
@@ -0,0 +1,11 @@
require 'spec_helper'
describe Users::TableSerializer, type: :serializer do
it "does not perform extra queries" do
table = create :table
result = object_as_json(table)
binding.pry
result[:included].size.should eq 4
end
end
+3 -2
View File
@@ -1,6 +1,6 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'simplecov'
SimpleCov.start 'rails'
#require 'simplecov'
#SimpleCov.start 'rails'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../config/environment", File.dirname(__FILE__))
require 'rspec/rails'
@@ -102,6 +102,7 @@ RSpec.configure do |config|
config.include Features::BasicHelpers, type: :feature
config.include SpecRouteHelpers, type: :feature
config.include SpecEmberHelpers, type: :feature
config.include SerializersTestHelpers, type: :serializer
#config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = true
config.filter_run_excluding broken: true
@@ -28,7 +28,7 @@ RSpec::Matchers.define :exceed_query_limit do |expected|
begin
record = CouchRest.get(q)
if record.is_a?(SimplyStored::Couch)
info += " #{record.class.name}"
info = "#{record.class.name.ljust(20)} => #{q.last(32)}"
end
rescue
end
@@ -64,7 +64,7 @@ RSpec::Matchers.define :perform_any_queries do |expected|
begin
record = CouchRest.get(q)
if record.is_a?(SimplyStored::Couch)
info += " #{record.class.name}"
info = "#{record.class.name.ljust(20)} => #{q.last(32)}"
end
rescue
end
+14
View File
@@ -0,0 +1,14 @@
module SerializersTestHelpers
def object_as_json(obj)
serializer = if obj.is_a?(Array)
ActiveModel::Serializer::ArraySerializer.new(obj, serializer: described_class, add_included: true)
else
serializer = described_class.new(obj)
end
adapter.new(serializer, include: %w[product_orders]).as_json
end
def adapter
@active_model_adapter = ActiveModel::Serializer.config.adapter
end
end