From 5cdbe57dece1e2e49bde0eab39af92361742b228 Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Wed, 9 Sep 2015 09:12:30 +0200 Subject: [PATCH] switch to JSONAPI for suppliers --- .../translation.js.coffee | 4 +- .../modals/base_controller.js.coffee | 3 +- .../app/routes/employees_route.js.coffee | 2 +- .../employees/employee_shifts_controller.rb | 2 +- .../employees/suppliers_controller.rb | 2 +- app/controllers/suppliers/lists_controller.rb | 1 + .../employees/employee_shift_serializer.rb | 7 +-- .../employees/supplier_serializer.rb | 4 +- lib/qwaiter.rb | 1 + lib/qwaiter/employee_base_serializer.rb | 43 +++++++++++++++++++ 10 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 lib/qwaiter/employee_base_serializer.rb diff --git a/app/assets/javascripts/shared-ember-helpers/translation.js.coffee b/app/assets/javascripts/shared-ember-helpers/translation.js.coffee index 920d378a..41549ef3 100644 --- a/app/assets/javascripts/shared-ember-helpers/translation.js.coffee +++ b/app/assets/javascripts/shared-ember-helpers/translation.js.coffee @@ -1,8 +1,8 @@ App.THelper = Ember.Helper.helper (params, options)-> path = params[0] toptions = options - if params.length > 1 and typeof(params[1].serialize) is 'function' - $.extend toptions, params[1].serialize() + if params.length > 1 and typeof(params[1].toJSON) is 'function' + $.extend toptions, params[1].toJSON() text = t(path, toptions) tag = if options.bare then text else "#{text}" tag.htmlSafe() diff --git a/app/assets/javascripts/supplier/app/controllers/modals/base_controller.js.coffee b/app/assets/javascripts/supplier/app/controllers/modals/base_controller.js.coffee index a76d195b..bff43d1b 100644 --- a/app/assets/javascripts/supplier/app/controllers/modals/base_controller.js.coffee +++ b/app/assets/javascripts/supplier/app/controllers/modals/base_controller.js.coffee @@ -7,7 +7,7 @@ # return translated title_path if directly set by controller translation_params = {} if model = @get('model') - translation_params = model.serialize() if model.serialize + translation_params = model.toJSON() if model.toJSON # return translated title_path if directly set by options # return translated title_path if directly set by controller @@ -19,6 +19,7 @@ #return tspan(@title_path, translation_params).htmlSafe() if @title_path # infer title path based on controller name App.modals.AddSectionController => add_section underscored = `this.constructor.toString().substr(11).replace(/Controller$/, '').underscore()` + #underscored = @constructor.toString().replace(/[a-zA-Z_-]+\@controller:modals\//, '').replace(/:$/, '').underscore() # ember cli # find translated title or humanize the controller name if convention_translation = ttry("modal.#{underscored}.title", translation_params) tspan(@get("modal.#{underscored}.title"), translation_params).htmlSafe() diff --git a/app/assets/javascripts/supplier/app/routes/employees_route.js.coffee b/app/assets/javascripts/supplier/app/routes/employees_route.js.coffee index cb97a486..cc308dbd 100644 --- a/app/assets/javascripts/supplier/app/routes/employees_route.js.coffee +++ b/app/assets/javascripts/supplier/app/routes/employees_route.js.coffee @@ -16,7 +16,7 @@ App.EmployeesRoute = Ember.Route.extend ac = @controllerFor('application') if ac.get('employee.id') isnt employee.id ac.modal 'confirm', - title: t('employee.destroy.modal.title', employee.serialize()) + title: t('employee.destroy.modal.title', employee.toJSON()) ok: -> employee.destroyRecord() else ac.modal 'alert', diff --git a/app/controllers/employees/employee_shifts_controller.rb b/app/controllers/employees/employee_shifts_controller.rb index cad9825f..2ac4807e 100644 --- a/app/controllers/employees/employee_shifts_controller.rb +++ b/app/controllers/employees/employee_shifts_controller.rb @@ -2,7 +2,7 @@ module Employees class EmployeeShiftsController < Employees::ApplicationController def index @employee_shifts = EmployeeShift.for_employee(current_employee).include_relation(:supplier) - render json: @employee_shifts, each_serializer: Employees::EmployeeShiftSerializer + render json: JSONAPI::Serializer.serialize(@employee_shifts, serializer: Employees::EmployeeShiftSerializer, is_collection: true) end end end diff --git a/app/controllers/employees/suppliers_controller.rb b/app/controllers/employees/suppliers_controller.rb index 7ad02b91..9dc1b9e0 100644 --- a/app/controllers/employees/suppliers_controller.rb +++ b/app/controllers/employees/suppliers_controller.rb @@ -2,7 +2,7 @@ module Employees class SuppliersController < Employees::ApplicationController def index @suppliers = current_employee.suppliers - render json: @suppliers, each_serializer: Employees::SupplierSerializer + render json: JSONAPI::Serializer.serialize(@suppliers, serializer: Employees::SupplierSerializer, is_collection: true) end end end diff --git a/app/controllers/suppliers/lists_controller.rb b/app/controllers/suppliers/lists_controller.rb index 19c87774..17af00b0 100644 --- a/app/controllers/suppliers/lists_controller.rb +++ b/app/controllers/suppliers/lists_controller.rb @@ -23,6 +23,7 @@ module Suppliers render json: JSONAPI::Serializer.serialize(@lists, serializer: Suppliers::ListSerializer, is_collection: true, include: %w[ orders + orders.list orders.product_orders orders.product_orders.order orders.product_orders.product diff --git a/app/serializers/employees/employee_shift_serializer.rb b/app/serializers/employees/employee_shift_serializer.rb index 3ad162b7..69f2c4da 100644 --- a/app/serializers/employees/employee_shift_serializer.rb +++ b/app/serializers/employees/employee_shift_serializer.rb @@ -1,5 +1,6 @@ -class Employees::EmployeeShiftSerializer < Qwaiter::Serializer - self.root = :employee_shift - attributes :start_from, :end_on, :employee_id, :description +class Employees::EmployeeShiftSerializer + include Qwaiter::EmployeeBaseSerializer + attributes :start_from, :end_on, :description has_one :supplier + has_one :employee end diff --git a/app/serializers/employees/supplier_serializer.rb b/app/serializers/employees/supplier_serializer.rb index 19a4f81c..1a5aab82 100644 --- a/app/serializers/employees/supplier_serializer.rb +++ b/app/serializers/employees/supplier_serializer.rb @@ -1,5 +1,5 @@ -class Employees::SupplierSerializer < Qwaiter::Serializer - self.root = :supplier +class Employees::SupplierSerializer + include Qwaiter::EmployeeBaseSerializer attributes :open, :name, :lat, :lng, :time_zone, :address, :house_number, :house_number_addition, :postal_code, :city, :country, :facebook_promotion_url, :iens_profile, :week_starts_on_monday, :orders_in_process_count, :orders_placed_count end diff --git a/lib/qwaiter.rb b/lib/qwaiter.rb index b6e925c4..d051695f 100644 --- a/lib/qwaiter.rb +++ b/lib/qwaiter.rb @@ -6,6 +6,7 @@ module Qwaiter autoload :Serializer autoload :UserBaseSerializer autoload :SupplierBaseSerializer + autoload :EmployeeBaseSerializer autoload :Counter autoload :Broadcaster autoload :Couchbase diff --git a/lib/qwaiter/employee_base_serializer.rb b/lib/qwaiter/employee_base_serializer.rb new file mode 100644 index 00000000..26c440f5 --- /dev/null +++ b/lib/qwaiter/employee_base_serializer.rb @@ -0,0 +1,43 @@ +module Qwaiter::EmployeeBaseSerializer + extend ActiveSupport::Concern + include JSONAPI::Serializer + included do + class_attribute :related_link_for_attributes + attribute :created_at + attribute :updated_at + end + + def base_url + nil + 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 + + #alias_method :default_relationship_related_link, :relationship_related_link + def relationship_related_link(attribute_name) + super if related_link_for_attributes.include?(attribute_name) + end + + def relationship_self_link(attribute_name) + end + + module ClassMethods + def attributes(*attrs) + attrs.each do |attr| + attribute attr + end + end + + def related_link_for(*attributes) + self.related_link_for_attributes = attributes + end + end +end