diff --git a/Gemfile b/Gemfile index 0bb9fcbb..02ce8386 100644 --- a/Gemfile +++ b/Gemfile @@ -77,8 +77,8 @@ group :development do gem 'thin' gem 'faye' gem 'pry-rails' - gem 'spring' - gem 'spring-commands-rspec' + #gem 'spring' + #gem 'spring-commands-rspec' end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index 1484ae32..7115eadd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -348,9 +348,6 @@ GEM railties (>= 3.0, < 4.1) slim (~> 2.0) slop (3.5.0) - spring (1.1.2) - spring-commands-rspec (1.0.1) - spring (>= 0.9.1) sprockets (2.11.0) hike (~> 1.2) multi_json (~> 1.0) @@ -437,8 +434,6 @@ DEPENDENCIES simplecov simply_stored! slim-rails - spring - spring-commands-rspec thin turnip uglifier (>= 1.0.3) diff --git a/app/assets/javascripts/shared-ember-helpers/time.js.coffee b/app/assets/javascripts/shared-ember-helpers/time.js.coffee new file mode 100644 index 00000000..dbea6d8a --- /dev/null +++ b/app/assets/javascripts/shared-ember-helpers/time.js.coffee @@ -0,0 +1,5 @@ +Ember.Handlebars.helper 'time', (time, params..., options = {})-> + return '' unless time + iso = time.toISOString() + tag = if options.bare then iso else $("").text(moment(iso).format(options.format || 'dd D MMM HH:MM')).get(0).outerHTML + new Handlebars.SafeString tag diff --git a/app/assets/javascripts/translations.js.erb b/app/assets/javascripts/translations-not-coffee.js.erb similarity index 87% rename from app/assets/javascripts/translations.js.erb rename to app/assets/javascripts/translations-not-coffee.js.erb index e68b1013..9ea5e28e 100644 --- a/app/assets/javascripts/translations.js.erb +++ b/app/assets/javascripts/translations-not-coffee.js.erb @@ -58,15 +58,17 @@ function setTranslations(selector){ var list = $('#top-navigation-list'); list.find('.locale').show(); list.find('.locale-'+$locale).hide(); + moment.lang($locale); if(selector){ $(selector).find('[data-t]').each(function(){$(this).html(t($(this).data('t'), $(this).data('tAttributes')))}) + $(selector).find('*[data-time]').each(function(){ + $(this).text(moment($(this).data('time')).format($(this).data('timeFormat') || 'dd D MMM HH:MM')) + }) }else{ $('[data-t]').each(function(){$(this).html(t($(this).data('t'),$(this).data('tAttributes')))}) + $('*[data-time]').each(function(){ + $(this).text(moment($(this).data('time')).format($(this).data('timeFormat') || 'dd D MMM HH:MM')) + }) } - moment.lang($locale); - $('[data-time]').each(function(){ - $(this).text(moment($(this).data('time')).format($(this).data('timeFormat') || 'dd D MMM HH:MM')) - }) - $('.datepicker').datepicker("option", $.datepicker.regional[$locale]) } diff --git a/app/assets/javascripts/translations.js.coffee.erb b/app/assets/javascripts/translations.js.coffee.erb new file mode 100644 index 00000000..81e35bff --- /dev/null +++ b/app/assets/javascripts/translations.js.coffee.erb @@ -0,0 +1,80 @@ + +@$translations = + en: + models: <%= I18n.t('activemodel.models', locale: :en).to_json %> + attributes: <%= I18n.t('activemodel.attributes', locale: :en).to_json %> + helpers: <%= I18n.t('helpers', locale: :en).to_json %> + pagination: <%= I18n.t('views.pagination', locale: :en).to_json %> + nl: + models: <%= I18n.t('activemodel.models', locale: :nl).to_json %> + attributes: <%= I18n.t('activemodel.attributes', locale: :nl).to_json %> + helpers: <%= I18n.t('helpers', locale: :nl).to_json %> + pagination: <%= I18n.t('views.pagination', locale: :nl).to_json %> +@t = (path, vars={}) -> + #result = undefined + #m = undefined + #translatable = undefined + #isafety = undefined + #replacable = undefined + parts = path.split(".") + #accessor = "$translations.#{$locale}[\"#{parts.join("\"][\"")}\"]" + result = $translations[$locale] + try + result = result[part] for part in parts + catch err + result = parts[parts.length - 1].capitalize() + return "" if result is "" + return parts[parts.length - 1].capitalize() unless result + + + # allow t('test', {var: 'Benjamin'}) statements + # with $translations.nl.test = "Hello %{var}" + for variable, value of vars + result = result.replace("%{#{variable}}", value) + + isafety = 0 + while result.indexOf("${") > -1 + m = result.match(/\${([\w\.]+(\|\w+)?)}/) + if m[2] + translatable = m[1].replace(m[2], "") + operation = $transformation_mappings[m[2].substr(1) or m[2].substr(1)] + else + translatable = m[1] + operation = null + replacable = t(translatable) + replacable = replacable[operation]() if operation + result = result.replace(m[0], replacable) + break if isafety > 10 # referencing other translations may cause infinite loops + isafety += 1 + result + +@setLocale = (locale, options={}) -> + locale ||= Qstorage.getItem('locale') || options.default || 'en' + Qstorage.setItem "locale", locale + @$locale = locale + setTranslations() + +@setTranslations = (selector) -> + list = $("#top-navigation-list") + list.find(".locale").show() + list.find(".locale-" + $locale).hide() + moment.lang $locale + if selector + $(selector).find("[data-t]").each -> + $(this).html t($(this).data("t"), $(this).data("tAttributes")) + + $(selector).find("*[data-time]").each -> + $(this).text moment($(this).data("time")).format($(this).data("timeFormat") or "dd D MMM HH:MM") + + else + $("[data-t]").each -> + $(this).html t($(this).data("t"), $(this).data("tAttributes")) + + $("*[data-time]").each -> + $(this).text moment($(this).data("time")).format($(this).data("timeFormat") or "dd D MMM HH:MM") + + $(".datepicker").datepicker "option", $.datepicker.regional[$locale] + +$transformation_mappings = + downcase: "toLowerCase" + upcase: "toUpperCase" diff --git a/app/assets/javascripts/user/app/app.js.coffee b/app/assets/javascripts/user/app/app.js.coffee index 67d55f2b..af237e5a 100644 --- a/app/assets/javascripts/user/app/app.js.coffee +++ b/app/assets/javascripts/user/app/app.js.coffee @@ -1,6 +1,7 @@ @App = Ember.Application.create LOG_TRANSITIONS: true rootElement: '#ember-app-container' + Ember.$.ajaxPrefilter (options) -> if options.type.toUpperCase() == 'GET' if auth_token = Qstorage.getItem('auth_token') @@ -13,6 +14,7 @@ Ember.$.ajaxPrefilter (options) -> options.data ||= {} options.data.auth_token = auth_token true + Ember.$.ajaxSetup error: (jqXHR, textStatus, errorThrown)-> console.log "Error: #{textStatus}: #{errorThrown}" diff --git a/app/assets/javascripts/user/app/application.js.coffee b/app/assets/javascripts/user/app/application.js.coffee index e437e6b1..48dc7108 100644 --- a/app/assets/javascripts/user/app/application.js.coffee +++ b/app/assets/javascripts/user/app/application.js.coffee @@ -5,6 +5,7 @@ #= require shared-ember-helpers/all #= require_directory ./modifications #= require ./app +#= require_directory ./modules #= require user/quser #= require_tree . @EmberENV = {FEATURES: {'query-params-new': true}} diff --git a/app/assets/javascripts/user/app/controllers/active_list_controller.js.coffee b/app/assets/javascripts/user/app/controllers/active_list_controller.js.coffee index ec7bcc36..11718531 100644 --- a/app/assets/javascripts/user/app/controllers/active_list_controller.js.coffee +++ b/app/assets/javascripts/user/app/controllers/active_list_controller.js.coffee @@ -1,10 +1,10 @@ -App.ActiveListController = Ember.ArrayController.extend - orders: (-> - @get('list.orders') - ).property('list.orders') +App.ActiveListController = Ember.ObjectController.extend + #orders: (-> + #@get('list.orders') + #).property('list.orders') - list: (-> - @get('controllers.application.list') - ).property('controllers.application.list') + #list: (-> + #@get('controllers.application.list') + #).property('controllers.application.list') - displayTotal: (-> @get('list.orders.length') and @get('list.orders.length') > 1 ).property('list.orders.length') + displayTotal: (-> @get('model.orders.length') and @get('model.orders.length') > 1 ).property('model.orders.length') diff --git a/app/assets/javascripts/user/app/controllers/list_controller.js.coffee b/app/assets/javascripts/user/app/controllers/list_controller.js.coffee new file mode 100644 index 00000000..a5222f3b --- /dev/null +++ b/app/assets/javascripts/user/app/controllers/list_controller.js.coffee @@ -0,0 +1 @@ +App.ListController = Ember.ObjectController.extend {} diff --git a/app/assets/javascripts/user/app/controllers/lists_index_controller.js.coffee b/app/assets/javascripts/user/app/controllers/lists_index_controller.js.coffee new file mode 100644 index 00000000..a249fee2 --- /dev/null +++ b/app/assets/javascripts/user/app/controllers/lists_index_controller.js.coffee @@ -0,0 +1,4 @@ +App.ListsIndexController = Ember.ArrayController.extend App.PaginationModule, + actions: + showList: (list)-> + @transitionToRoute 'list', list diff --git a/app/assets/javascripts/user/app/controllers/product_orders_controller.js.coffee b/app/assets/javascripts/user/app/controllers/product_orders_controller.js.coffee index 42fcc424..7d4d9a26 100644 --- a/app/assets/javascripts/user/app/controllers/product_orders_controller.js.coffee +++ b/app/assets/javascripts/user/app/controllers/product_orders_controller.js.coffee @@ -9,8 +9,12 @@ App.ProductOrdersController = Ember.ArrayController.extend orderProducts: -> order = @store.createRecord('order', list: @get('controllers.application.list')) new_product_orders = @store.all('product_order').filterProperty('order', null) - new_product_orders.forEach (po) -> po.setOrder(order) - order.save() + order.get('product_orders').pushObjects(new_product_orders) + order.save().then -> + # The new versions will be returned in the json response + #new_product_orders.invoke 'rollback' + #new_product_orders.invoke 'transitionTo', 'loaded.saved' + new_product_orders.invoke 'deleteRecord' @transitionToRoute 'active_list' #orders = @store.all('product_order').toArray() diff --git a/app/assets/javascripts/user/app/models/list.js.coffee b/app/assets/javascripts/user/app/models/list.js.coffee index 3e6c7c81..ba54d706 100644 --- a/app/assets/javascripts/user/app/models/list.js.coffee +++ b/app/assets/javascripts/user/app/models/list.js.coffee @@ -3,6 +3,14 @@ App.List = DS.Model.extend orders: DS.hasMany('order') needs_help: attr('boolean') needs_payment: attr('boolean') + supplier_name: attr('string') + price: attr('number') + extended_version: attr('boolean') + supplier_orders_in_process_count: attr('number') + supplier_orders_placed_count: attr('number') total: (-> @get('orders').getEach('total').reduce(((sum, total) -> sum + total), 0) ).property('orders.@each.total') + + is_extended_version: -> + @get('extended_version') diff --git a/app/assets/javascripts/user/app/modules/pagination.js.coffee b/app/assets/javascripts/user/app/modules/pagination.js.coffee new file mode 100644 index 00000000..2e667626 --- /dev/null +++ b/app/assets/javascripts/user/app/modules/pagination.js.coffee @@ -0,0 +1,37 @@ +App.PaginationModule = Ember.Mixin.create + #modelClass: -> + #@modelInfo.class + modelNameForStore: -> + #@modelInfo.store + @get('content.type.typeKey') + storeMetadata: (k) -> + #res = @store.typeMapFor(@modelClass()).metadata + res = @store.metadataFor @get('content.type.typeKey') + res = res[k] if k + res + setStoreMetadata: (k,v) -> + #res = @store.typeMapFor(@modelClass()).metadata + res = @store.metadataFor @get('content.type.typeKey') + res[k] = v + + loadMore: -> + page = @storeMetadata('page') + 1 + @setStoreMetadata('page',page) + @store.findQuery(@modelNameForStore(),page: page) + page + + hasMoreFunc: -> + page = @storeMetadata('page') + total = @storeMetadata('total_pages') + #unfiltered = @storeMetadata('unfiltered_total_pages') + unfiltered = total + + #console.debug "page #{page} total #{total} unfiltered #{unfiltered}" + + !unfiltered || page < unfiltered + actions: + showMore: -> + page = @loadMore() + @set 'lastKnownPage', page + + hasMore: (-> @hasMoreFunc()).property('lastKnownPage','firstObject','@each','filtered.@each') diff --git a/app/assets/javascripts/user/app/router.js.coffee b/app/assets/javascripts/user/app/router.js.coffee index be535ba4..86d51f32 100644 --- a/app/assets/javascripts/user/app/router.js.coffee +++ b/app/assets/javascripts/user/app/router.js.coffee @@ -3,8 +3,6 @@ App.Router.reopen #location: 'history' rootURL: '/user' - activate: (a,b,c)-> - debugger App.Router.map -> @route 'select_qrcode' @@ -12,3 +10,5 @@ App.Router.map -> @route 'active_list' @route 'list_products' @route 'list_products_for_table' + @resource 'lists', -> + @resource 'list', path: ':list_id' diff --git a/app/assets/javascripts/user/app/routes/active_list_route.js.coffee b/app/assets/javascripts/user/app/routes/active_list_route.js.coffee index 33f1cd28..66972bda 100644 --- a/app/assets/javascripts/user/app/routes/active_list_route.js.coffee +++ b/app/assets/javascripts/user/app/routes/active_list_route.js.coffee @@ -1,3 +1 @@ -App.ActiveListRoute = Ember.Route.extend - model: -> - @controllerFor('application').get('list.orders') +App.ActiveListRoute = Ember.Route.extend {} diff --git a/app/assets/javascripts/user/app/routes/application_route.js.coffee b/app/assets/javascripts/user/app/routes/application_route.js.coffee index 57e369e6..7ef8b684 100644 --- a/app/assets/javascripts/user/app/routes/application_route.js.coffee +++ b/app/assets/javascripts/user/app/routes/application_route.js.coffee @@ -1,7 +1,7 @@ App.ApplicationRoute = Ember.Route.extend setupController: (controller)-> #@controllerFor('product_orders').set 'model', @store.filter('product_order', (po)-> !po.get('order')) # does not work (yet) - @controllerFor('product_orders').set 'model', @store.filter('product_order', (po)-> !po.get('placed')) + @controllerFor('product_orders').set 'model', @store.filter('product_order', (po)-> !po.get('id')) controller.secured -> faye = new Faye.Client(event_host) user_id = Qstorage.getItem('user_id') @@ -9,7 +9,16 @@ App.ApplicationRoute = Ember.Route.extend console.log e @events[e.event].call(@) if @events[e.event] @store.find('list', 'current').then (list)=> + #@store.find('list', 'current').deleteRecord() # gets not replaced, buty stays as dummy + + # A list record with id current and with the content of the returned list is created + # at the moment remove the dummy list, this should be resolved by Ember eventually + if error_list = @store.all('list').findBy('id', 'current') + error_list.rollback() + error_list.transitionTo 'loaded.saved' + error_list.unloadRecord() @set 'list', list + @controllerFor('active_list').set('model', list) unauthorized: -> Qstorage.setItem('auth_token', '') diff --git a/app/assets/javascripts/user/app/routes/list_route.js.coffee b/app/assets/javascripts/user/app/routes/list_route.js.coffee new file mode 100644 index 00000000..c0bf0904 --- /dev/null +++ b/app/assets/javascripts/user/app/routes/list_route.js.coffee @@ -0,0 +1,5 @@ +App.ListRoute = Ember.Route.extend + model: (options)-> + @store.find 'list', options.list_id + afterModel: (model)-> + model.reload() unless model.is_extended_version() diff --git a/app/assets/javascripts/user/app/routes/lists_route.js.coffee b/app/assets/javascripts/user/app/routes/lists_route.js.coffee new file mode 100644 index 00000000..7b8e864f --- /dev/null +++ b/app/assets/javascripts/user/app/routes/lists_route.js.coffee @@ -0,0 +1,5 @@ +App.ListsRoute = Ember.Route.extend + model: -> + @store.find 'list' + setupController: (controller, model)-> + controller.set('alreadyLoaded', true) diff --git a/app/assets/javascripts/user/app/store.js.coffee b/app/assets/javascripts/user/app/store.js.coffee index 8eded371..0be80275 100644 --- a/app/assets/javascripts/user/app/store.js.coffee +++ b/app/assets/javascripts/user/app/store.js.coffee @@ -2,6 +2,32 @@ DS.RESTAdapter.reopen namespace: 'user' +DS.ActiveModelSerializer.reopen + serializeBelongsTo: (record, json, relationship) -> + console.log "Serialize belongsTo #{record.toString()}" + key = relationship.key + belongsTo = Ember.get(record, key) + key = (if @keyForRelationship then @keyForRelationship(key, "belongsTo") else key) + if relationship.options.embedded is "always" + json[key] = belongsTo.serialize() + else + @_super record, json, relationship + + serializeHasMany: (record, json, relationship) -> + console.log "Serialize hasMany #{record.toString()}" + key = relationship.key + hasMany = Ember.get(record, key) + relationshipType = DS.RelationshipChange.determineRelationshipType(record.constructor, relationship) + if relationship.options.embedded is "always" + if hasMany and relationshipType is "manyToNone" or relationshipType is "manyToMany" or relationshipType is "manyToOne" + json[key] = [] + hasMany.forEach (item, index) -> + json[key].push item.serialize() + else + @_super record, json, relationship +DS.Model.reopen + created_at: DS.attr('date') + updated_at: DS.attr('date') App.ApplicationSerializer = DS.ActiveModelSerializer App.CustomAdapter = DS.RESTAdapter.extend diff --git a/app/assets/javascripts/user/app/templates/active_list.emblem b/app/assets/javascripts/user/app/templates/active_list.emblem index db54163c..a3976dce 100644 --- a/app/assets/javascripts/user/app/templates/active_list.emblem +++ b/app/assets/javascripts/user/app/templates/active_list.emblem @@ -1,15 +1,15 @@ .row h2=t 'active_list.title' - if list.orders + if orders ul.active_list-orders - each order in list.orders + each order in orders li class=order.state = order.display span.currency= currency order.total if displayTotal li.total = t 'total' - span.currency= currency list.total + span.currency= currency model.total else p span=t 'active_list.no_orders_explanation' diff --git a/app/assets/javascripts/user/app/templates/application.emblem b/app/assets/javascripts/user/app/templates/application.emblem index 9a1d9786..6ae3bdd6 100644 --- a/app/assets/javascripts/user/app/templates/application.emblem +++ b/app/assets/javascripts/user/app/templates/application.emblem @@ -31,6 +31,9 @@ li =link-to 'active_list' span= t 'active_list.title' + li + =link-to 'lists' + span= t 'models.plural.list' section.main-section if notice #notice.alert-box{action clearNotice} data-alert=true diff --git a/app/assets/javascripts/user/app/templates/list.emblem b/app/assets/javascripts/user/app/templates/list.emblem new file mode 100644 index 00000000..322db191 --- /dev/null +++ b/app/assets/javascripts/user/app/templates/list.emblem @@ -0,0 +1,10 @@ +.row + h2=t 'models.list' + p Hoi + = controller + if model.extended_version + h3 Extended + else + h3 Not extended + link-to 'lists' + span Go to lists diff --git a/app/assets/javascripts/user/app/templates/lists.emblem b/app/assets/javascripts/user/app/templates/lists.emblem new file mode 100644 index 00000000..26191258 --- /dev/null +++ b/app/assets/javascripts/user/app/templates/lists.emblem @@ -0,0 +1 @@ += outlet diff --git a/app/assets/javascripts/user/app/templates/lists/index.emblem b/app/assets/javascripts/user/app/templates/lists/index.emblem new file mode 100644 index 00000000..ac7864d3 --- /dev/null +++ b/app/assets/javascripts/user/app/templates/lists/index.emblem @@ -0,0 +1,11 @@ +.row + h2=t 'models.plural.list' + each list in controller + .lists-overview-entry{action showList list} + span.created_at=time list.created_at + span.price.currency= currency list.price + span.name= list.supplier_name + hr + .clearfix + if hasMore + button{action showMore} Show more diff --git a/app/assets/javascripts/user/flat/application.js.coffee.erb b/app/assets/javascripts/user/flat/application.js.coffee.erb index 28c1dc2a..ab62bdca 100644 --- a/app/assets/javascripts/user/flat/application.js.coffee.erb +++ b/app/assets/javascripts/user/flat/application.js.coffee.erb @@ -10,12 +10,15 @@ #= require js-routes #= require_directory . #= require_self - +(($) -> + origAppend = $.fn.append + $.fn.append = -> origAppend.apply(@, arguments).trigger("append") +)(jQuery) @Qstorage = localStorage $.extend($translations.en, <%= I18n.t('user', locale: :en).to_json %>); $.extend($translations.nl, <%= I18n.t('user', locale: :nl).to_json %>); $(document).foundation() -setLocale('en') +setLocale() $ -> $('.main-section').css 'min-height', ($(window).height() - $('.tab-bar:first').outerHeight()) diff --git a/app/assets/stylesheets/user/foundation/lists.css.sass b/app/assets/stylesheets/user/foundation/lists.css.sass new file mode 100644 index 00000000..54595b54 --- /dev/null +++ b/app/assets/stylesheets/user/foundation/lists.css.sass @@ -0,0 +1,13 @@ +@import foundation +.lists-overview-entry + +grid-column(12) + @media #{$medium-only} + +grid-column(6) + @media #{$large-up} + +grid-column(4) + .created_at + float: right + .price + float: right + clear: right + +panel($padding: 4px) diff --git a/app/controllers/users/lists_controller.rb b/app/controllers/users/lists_controller.rb index 9c7a3588..9da2752d 100644 --- a/app/controllers/users/lists_controller.rb +++ b/app/controllers/users/lists_controller.rb @@ -1,12 +1,25 @@ module Users class ListsController < Users::ApplicationController + + def index + #lists = current_user.lists.include_relation(:supplier, :table) + lists = List.for_user(current_user, page: params[:page], per_page: 2).include_relation(:supplier, :table) + #lists.include_relation(:supplier) + render json: lists, each_serializer: UserListSerializer, meta: {total_pages: lists.total_pages, page: lists.current_page} #, root: :lists + end + #EMBER def current list = current_user.active_list + [list].include_relation(supplier: {product_categories: :products}, orders: :product_orders) # table also when it is a real array :) render json: json_response(not_present: true) and return unless list.present? - render json: { - list: list.serialized_with_status_join_requests_and_supplier_counters, - }.merge(ActiveModel::ArraySerializer.new(list.supplier.product_categories, each_serializer: ProductCategorySerializer, root: :product_categories).as_json) + render json: list, serializer: UserExtendedListSerializer + end + + def show + list = List.find(params[:id]) + [list].include_relation(supplier: {product_categories: :products}, orders: :product_orders) # table also when it is a real array :) + render json: list, serializer: UserExtendedListSerializer end end end diff --git a/app/controllers/users/orders_controller.rb b/app/controllers/users/orders_controller.rb new file mode 100644 index 00000000..3510e43a --- /dev/null +++ b/app/controllers/users/orders_controller.rb @@ -0,0 +1,11 @@ +module Users + class OrdersController < Users::ApplicationController + respond_to :json + def create + converted_order = params[:order][:product_orders].each_with_object({}){|po, o| o[po[:product_id]] = po[:quantity] } + list = current_user.active_list + order = list.place_order converted_order + render json: order, serializer: OrderSerializer + end + end +end diff --git a/app/models/list.rb b/app/models/list.rb index 9ed27682..33a298a3 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -343,11 +343,19 @@ class List def supplier_counter_info { - supplier_orders_in_process_count: Qwaiter::Counter.get(Supplier.orders_in_process_counter_key(supplier_id)), - supplier_orders_placed_count: Qwaiter::Counter.get(Supplier.orders_placed_counter_key(supplier_id)) + supplier_orders_in_process_count: supplier_orders_in_process_count, + supplier_orders_placed_count: supplier_orders_placed_count } end + def supplier_orders_in_process_count + Qwaiter::Counter.get(Supplier.orders_in_process_counter_key(supplier_id)) + end + + def supplier_orders_placed_count + Qwaiter::Counter.get(Supplier.orders_placed_counter_key(supplier_id)) + end + def has_active_orders? Order.count_active_for_supplier_and_list(supplier_id, id) > 0 end @@ -370,6 +378,10 @@ class List @join_requests_as_json = h end + def product_categories + supplier.product_categories + end + def with_info_as_json return @with_info_as_json if @with_info_as_json.present? hl = as_json diff --git a/app/models/product_order.rb b/app/models/product_order.rb index aa2ac828..47fdb732 100644 --- a/app/models/product_order.rb +++ b/app/models/product_order.rb @@ -1,5 +1,6 @@ class ProductOrder include SimplyStored::Couch + include ActiveModel::SerializerSupport property :quantity, type: Fixnum property :price, type: Float diff --git a/app/serializers/list_serializer.rb b/app/serializers/list_serializer.rb index 1645a2d0..72783b97 100644 --- a/app/serializers/list_serializer.rb +++ b/app/serializers/list_serializer.rb @@ -1,9 +1,9 @@ class ListSerializer < Qwaiter::Serializer # user ids for facebook pictures - embed :ids + #embed :ids attributes :state, :needs_help, :needs_payment, :is_paid, :price, :table_id, :table_number, :section_id, :has_active_orders, :user_ids - def has_active_orders - object.has_active_orders? - end + #def has_active_orders + #object.has_active_orders? + #end end diff --git a/app/serializers/order_serializer.rb b/app/serializers/order_serializer.rb index 6846d9ac..0a46f5be 100644 --- a/app/serializers/order_serializer.rb +++ b/app/serializers/order_serializer.rb @@ -1,14 +1,18 @@ class OrderSerializer < Qwaiter::Serializer - embed :ids - attributes :state, :list_id, :section_id, :product_orders, :price + embed :ids, include: true + attributes :state, :list_id, :section_id #, :price - def product_orders - @product_orders ||= object.product_orders.include_relation(:product) .map do |product_order| - {product_name: product_order.product.name, id: product_order.id, quantity: product_order.quantity, price: product_order.price} - end - end + # todo, put this logic in Ember + #def product_orders + #@product_orders ||= object.product_orders.include_relation(:product) .map do |product_order| + #{product_name: product_order.product.name, id: product_order.id, quantity: product_order.quantity, price: product_order.price} + #end + #end - def price - product_orders.inject(0){|sum, po| sum + po[:quantity] * po[:price]}.round(2) - end + # todo, put this logic in Ember + #def price + #product_orders.inject(0){|sum, po| sum + po[:quantity] * po[:price]}.round(2) + #end + + has_many :product_orders end diff --git a/app/serializers/product_order_serializer.rb b/app/serializers/product_order_serializer.rb new file mode 100644 index 00000000..750d86b2 --- /dev/null +++ b/app/serializers/product_order_serializer.rb @@ -0,0 +1,5 @@ +# Used for user ember1 +class ProductOrderSerializer < Qwaiter::Serializer + embed :ids + attributes :order_id, :product_id, :quantity, :price +end diff --git a/app/serializers/user_extended_list_serializer.rb b/app/serializers/user_extended_list_serializer.rb new file mode 100644 index 00000000..2dd53479 --- /dev/null +++ b/app/serializers/user_extended_list_serializer.rb @@ -0,0 +1,25 @@ +#class ListExtendedSerializer + #render json: { + #list: list.serialized_with_status_join_requests_and_supplier_counters.merge(order_ids: list.orders.map(&:id)), + #} + #.merge(ActiveModel::ArraySerializer.new(list.supplier.product_categories, each_serializer: ProductCategorySerializer, root: :product_categories).as_json) + #.merge(ActiveModel::ArraySerializer.new(list.orders, each_serializer: OrderSerializer, root: :orders).as_json) +#end +class UserExtendedListSerializer < Qwaiter::Serializer + # user ids for facebook pictures + self.root = :list + embed :ids, include: true + attributes :extended_version, :state, :needs_help, :needs_payment, :is_paid, :price, + :table_id, :table_number, :section_id, :user_ids, + :supplier_orders_in_process_count, :supplier_orders_placed_count + + def has_active_orders + object.has_active_orders? + end + has_many :orders + has_many :product_categories + + def extended_version + true + end +end diff --git a/app/serializers/user_list_serializer.rb b/app/serializers/user_list_serializer.rb new file mode 100644 index 00000000..b12810a1 --- /dev/null +++ b/app/serializers/user_list_serializer.rb @@ -0,0 +1,12 @@ +class UserListSerializer < Qwaiter::Serializer + # user ids for facebook pictures + #embed :ids + attributes :state, :needs_help, :needs_payment, :is_paid, :price, :table_id, :table_number, :section_id, :user_ids, :supplier_name + + #def has_active_orders + #object.has_active_orders? + #end + def supplier_name + object.supplier.name + end +end diff --git a/config/routes.rb b/config/routes.rb index e4632674..98a92bc1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -71,11 +71,13 @@ Qwaiter::Application.routes.draw do namespace :users, path: '/user' do resources :product_categories, only: [:index] - resources :lists, only: [] do + resources :lists, only: [:index, :show] do collection do get :current end end + + resources :orders, only: [:create] end diff --git a/lib/qwaiter/serializer.rb b/lib/qwaiter/serializer.rb index e0227ca2..390e5b09 100644 --- a/lib/qwaiter/serializer.rb +++ b/lib/qwaiter/serializer.rb @@ -1,5 +1,6 @@ module Qwaiter class Serializer < ActiveModel::Serializer attribute :_id, key: :id + attributes :created_at, :updated_at end end