Poltergeist in the green, and resourceful refactor

This commit is contained in:
2015-02-27 15:12:08 +01:00
parent d1015dcc88
commit 046058b5d2
31 changed files with 210 additions and 165 deletions
+6
View File
@@ -0,0 +1,6 @@
class Employees::Ability
include CanCan::Ability
def initialize(employee)
can :read, Supplier
end
end
@@ -1,15 +1,7 @@
class Ability
attr_reader :resource
class Suppliers::Ability
include CanCan::Ability
def initialize(resource)
@resource = resource
case resource
when Employee then supplier_authorization
end
end
def supplier_authorization
if resource.settings.manager?
def initialize(employee)
if employee.manager?
can :manage, :all
#cam :mark_as_open, Supplier
else
@@ -1,4 +1,7 @@
Ember.Handlebars.helper 't', (path, params..., options)->
toptions = options.hash
if params.length and typeof(params[0].serialize) is 'function'
$.extend toptions, params[0].serialize()
text = t(path, options.hash)
tag = if options.hash.bare then text else "<span data-t='#{path}' data-t-attributes='#{JSON.stringify(options.hash)}'>#{text}</span>"
tag.htmlSafe()
@@ -5,6 +5,8 @@ App.SettingsController = Ember.ObjectController.extend
countries: (-> window.countries ).property()
saving: false
editIensProfile: (-> @get('model.country') is 'Netherlands' ).property('model.country')
suppliers: (-> @store.all 'supplier').property()
other_suppliers: ( -> @get('suppliers').rejectBy 'id', @get('controllers.application.supplier.id')).property('suppliers.@each')
actions:
saveSettings: ->
@set 'saving', true
@@ -12,3 +14,5 @@ App.SettingsController = Ember.ObjectController.extend
@set 'saving', false
$('.top-menu .supplier-name').text @get('model.name')
@transitionToRoute 'index'
switchToSupplier: (supplier)->
window.location = Routes.switch_to_suppliers_supplier_path(supplier.get('id'))
@@ -52,5 +52,5 @@ App.List = DS.Model.extend
$.post Routes.mark_helped_suppliers_list_path(@id)
remove_needs_payment: ->
@set 'needs_payment', false
$.post Routes.supplier_remove_list_needs_payment_path(), list_id: @id
$.post Routes.remove_needs_payment_suppliers_list_path(@id)
sorted_orders: (-> @get('relevant_orders').sortBy('created_at').reverseObjects()).property('relevant_orders.@each.isLoaded')
@@ -20,9 +20,9 @@ App.Supplier = DS.Model.extend
orders_placed_count: attr('number')
close: ->
$.post Routes.supplier_mark_as_closed_path(), =>
$.post Routes.mark_as_closed_suppliers_supplier_path(@id), =>
@set 'open', false
open_the_place: ->
$.post Routes.supplier_mark_as_open_path(), =>
$.post Routes.mark_as_open_suppliers_supplier_path(@id), =>
@set 'open', true
@@ -1,4 +1,7 @@
App.SettingsRoute = Ember.Route.extend
beforeModel: ->
$.getJSON(Routes.employees_suppliers_path()).then (result) =>
@store.pushPayload result
model: -> @container.lookup('route:application').supplier
# setupController: (controller, model)->
# #controller.set 'model', controller.get('controllers.application.supplier')
@@ -44,3 +44,9 @@ if editIensProfile
span.fa.fa-lg.fa-spinner.fa-spin
else
button.button.submit-supplier-settings{action 'saveSettings'}=t 'settings.save'
if other_suppliers
.row: .small-12.columns: ul.button-group.stack-for-small.round
each supplier in other_suppliers
li: a.warning.button{action "switchToSupplier" supplier}= t 'settings.switch_to_supplier' supplier
@@ -25,7 +25,6 @@ String.prototype.capitalize = function() { return this.charAt(0).toUpperCase() +
window.time_zones = <%= ActiveSupport::TimeZone.all.map{|tz| {name: tz.name, formatted: "GMT#{tz.formatted_offset} #{tz.name}"}}.to_json.html_safe %>;
window.countries = <%= IsoCountryCodes.all.map{|cc| {name: cc.name}}.to_json.html_safe %>;
var path_mapping = {
user_root: '/user',
join_occupied_table: '/user/join_occupied_table',
@@ -21,7 +21,6 @@
@ttry = (path, vars={})->
@t(path, $.extend(vars, emptyWhenNotFound: true))
# return translation in the form
# <span data-t="models.table">Tafel</span>
@tspan = (path, vars={}) -> "<span data-t='#{path}' data-t-attributes='#{JSON.stringify(vars)}'>#{t(path, vars)}</span>"
@@ -19,6 +19,7 @@ Ember.Application.initializer
@App = Ember.Application.create
LOG_TRANSITIONS: true
rootElement: '#ember-app-container'
ready: -> window.ember_ready = true
App.deferReadiness()
@@ -1,4 +1,5 @@
class ApplicationController < ActionController::Base
include ControllerAfterAuthenticationHooks
before_action :set_locale
if Rails.env.development?
before_action :force_reloads
@@ -14,6 +15,7 @@ class ApplicationController < ActionController::Base
private
def allow_all_origins
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Request-Method'] = '*'
@@ -0,0 +1,21 @@
module ControllerAfterAuthenticationHooks
extend ActiveSupport::Concern
included do
class_attribute :after_authentication_hooks
end
def run_after_authentication_hooks!
Array.wrap(after_authentication_hooks).each do |hook|
next if hook[:options][:only].present? && !Array.wrap(hook[:options][:only]).include?(action_name.to_sym)
instance_eval &hook[:block]
end
end
module ClassMethods
def after_authentication(options, &blk)
self.after_authentication_hooks ||= []
after_authentication_hooks << {options: options, block: blk}
end
end
end
@@ -0,0 +1,17 @@
module Employees
class ApplicationController < ::ApplicationController
before_action :setup_employee!
load_and_authorize_resource
def setup_employee!
authenticate_employee!
@current_ability = Employees::Ability.new( current_employee )
run_after_authentication_hooks!
end
rescue_from CanCan::AccessDenied do |exception|
respond_to do |format|
format.html { redirect_to root_path, alert: 'Action forbidden'}
format.json { render json: {}, status: :forbidden }
end
end
end
end
@@ -0,0 +1,8 @@
module Employees
class SuppliersController < Employees::ApplicationController
def index
@suppliers = current_employee.suppliers
render json: @suppliers, each_serializer: Employees::SupplierSerializer
end
end
end
+2 -74
View File
@@ -1,46 +1,7 @@
class SupplierController < Suppliers::ApplicationController
def home
end
def menu
end
# GET /supplier/settings
def edit
@supplier = current_supplier
end
def current
[current_supplier].include_relations(sections: :tables, product_categories: :products)
render json: Suppliers::SupplierSerializer.new(current_supplier).as_json
end
# POST /supplier/settings
def update
@supplier = current_supplier
respond_to do |format|
format.html do
if current_supplier.update_attributes(supplier_params)
redirect_to supplier_root_path
else
render action: :edit
end
end
format.json do
current_supplier.update_attributes(supplier_params)
render json: Suppliers::SupplierSerializer.new(current_supplier).as_json
end
end
end
def mark_as_open
current_supplier.mark_as_open!
head :ok
end
def mark_as_closed
current_supplier.mark_as_closed!
head :ok
end
=begin
# GET /suppliers/1/active_orders
# GET /suppliers/1/active_orders.json
def active_orders
@@ -95,38 +56,5 @@ class SupplierController < Suppliers::ApplicationController
end
#POST /supplier/remove_list_needs_payment
def remove_list_needs_payment
@list = List.find_by_supplier_id_and_id(current_supplier.id, params[:list_id])
@list.remove_needs_payment!
render nothing: true
end
private
def supplier_params
params.require(:supplier).permit(
:name,
:email,
:open,
:time_zone,
:night_offset,
:location,
:lat,
:lng,
:offer_wifi,
:wifi_ssid,
:wifi_type,
:wifi_password,
:iens_profile,
:address,
:house_number,
:house_number_addition,
:postal_code,
:city,
:country,
:facebook_promotion_url,
:created_at,
:updated_at
)
end
=end
end
@@ -5,7 +5,6 @@ module Suppliers
attr_reader :current_supplier
helper_method :current_supplier
layout 'supplier/app'
class_attribute :after_authentication_hooks
rescue_from 'RestClient::Conflict' do |e|
#binding.pry
@@ -17,22 +16,14 @@ module Suppliers
end
end
def self.after_authentication(options, &blk)
self.after_authentication_hooks ||= []
after_authentication_hooks << {options: options, block: blk}
end
def setup_employee_and_supplier!
authenticate_employee!
find_current_supplier!
return unless current_supplier.present?
current_employee.enrich_with_settings current_supplier.settings_for(current_employee)
raise CanCan::AccessDenied unless current_employee.active?
@current_ability = ::Ability.new( current_employee )
Array.wrap(after_authentication_hooks).each do |hook|
next if hook[:options][:only].present? && !Array.wrap(hook[:options][:only]).include?(action_name.to_sym)
instance_eval &hook[:block]
end
@current_ability = Suppliers::Ability.new( current_employee )
run_after_authentication_hooks!
end
@@ -1,6 +1,9 @@
module Suppliers
class ProductsController < Suppliers::ApplicationController
layout 'tablet'
after_authentication only: [:show, :edit, :update, :destroy] do
@product = Product.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
end
# GET /products
# GET /products.json
@@ -16,7 +19,6 @@ module Suppliers
# GET /products/1
# GET /products/1.json
def show
@product = Product.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
respond_to do |format|
format.html # show.html.erb
@@ -38,13 +40,13 @@ module Suppliers
# GET /products/1/edit
def edit
@product = Product.find(params[:id])
#@product = Product.find(params[:id])
end
# POST /products
# POST /products.json
def create
@product = Product.new(product_params)
#@product = Product.new(product_params)
@product.supplier = current_supplier
respond_to do |format|
@@ -61,7 +63,7 @@ module Suppliers
# PUT /products/1
# PUT /products/1.json
def update
@product = Product.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
#@product = Product.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
respond_to do |format|
if @product.update_attributes(product_params)
@@ -77,12 +79,12 @@ module Suppliers
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product = Product.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
#@product = Product.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
@product.destroy
respond_to do |format|
format.html { redirect_to suppliers_products_url, notice: t('action.destroy.successfull', model: Product.model_name.human) }
format.json { head :no_content }
format.json { head :ok }
end
end
@@ -0,0 +1,67 @@
module Suppliers
class SuppliersController < Suppliers::ApplicationController
def index
end
def show
[current_supplier].include_relations(sections: :tables, product_categories: :products)
render json: Suppliers::SupplierSerializer.new(current_supplier).as_json
end
def update
@supplier = current_supplier
current_supplier.update_attributes(supplier_params)
render json: Suppliers::SupplierSerializer.new(current_supplier).as_json
end
def switch_to
@switch_supplier = Supplier.find(params[:id])
session[:supplier_id] = params[:id] if @switch_supplier.employee_ids.include? current_employee.id
redirect_to supplier_root_path(anchor: '/settings')
end
def mark_as_open
current_supplier.mark_as_open!
head :ok
end
def mark_as_closed
current_supplier.mark_as_closed!
head :ok
end
def remove_needs_payment
@list = List.find_by_supplier_id_and_id(current_supplier.id, params[:id])
@list.remove_needs_payment!
head :ok
end
private
def supplier_params
params.require(:supplier).permit(
:name,
:email,
:open,
:time_zone,
:night_offset,
:location,
:lat,
:lng,
:offer_wifi,
:wifi_ssid,
:wifi_type,
:wifi_password,
:iens_profile,
:address,
:house_number,
:house_number_addition,
:postal_code,
:city,
:country,
:facebook_promotion_url
)
end
end
end
@@ -0,0 +1,6 @@
class Employees::SupplierSerializer < Qwaiter::Serializer
self.root = :supplier
embed :ids, include: true
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
@@ -54,6 +54,7 @@ html lang="en"
= javascript_include_tag "user/flat/application"
- unless Rails.env.user_app?
javascript:
window.ember_ready = false
App.advanceReadiness()
body
#ember-app-container
@@ -24,13 +24,6 @@ header.top-menu
aside.side-menu
ul
li.title: h3 Menu
- if current_supplier.open?
li
= form_tag supplier_mark_as_closed_path do
a.supplier-close-shop href="javascript:void(0)" onclick="$(this).parents('form').submit()"
span data-t="supplier.close_for_orders"
li= link_to content_tag(:span, '', data: {t: 'supplier.settings'}), supplier_settings_path, class: 'supplier-settings-link'
li= link_to content_tag(:span, '', data: {t: 'supplier.sign_out'}), destroy_employee_session_path, method: :delete, class: 'supplier-sign-out-link'
/.navbar.navbar-fixed-top.navbar-inverse
.navbar-inner
.container