User foundation setup

This commit is contained in:
2014-03-26 13:17:34 +01:00
parent 6c2427e082
commit 4e75c72097
87 changed files with 861 additions and 120 deletions
+5 -2
View File
@@ -8,8 +8,11 @@ class DashboardController < ApplicationController
# Testing action
def select_qrcode
#@tables = Table.all.sample(2) | List.active.map(&:table)
@tables = Supplier.first.tables.sample(2) | List.active.map(&:table)
render layout: 'phone'
@tables = Supplier.first.tables.sample(5) | List.active.map(&:table)
respond_to do |format|
format.html { render layout: 'phone' }
format.json { render json: @tables.to_json }
end
end
+11 -54
View File
@@ -1,41 +1,7 @@
class UserController < ApplicationController
class UserController < Users::ApplicationController
before_filter :allow_mobile
before_filter :user_authentication, :unless => ->(c){ %w(obtain_token index).include?(c.action_name) || c.request.format.symbol == :html } # , except: [:obtain_token, :index]
layout 'phone'
def user_authentication
if params[:auth_token].present?
user = User.find_by_authentication_token(params[:auth_token])
sign_in user if user
sign_out current_user if current_user && !user # Other token attempt of logged in user
else
authenticate_user!
end
unless current_user.present?
respond_to do |format|
format.html {redirect_to new_user_session_path}
format.json {render json: json_response(ok: false, status: 401)}
end
end
end
def obtain_token
redirect_to user_omniauth_authorize_path('facebook') and return unless current_user.present?
respond_to do |format|
format.html
format.json do
render json: json_response(ok: false, status: 401) and return unless params[:user].present? && params[:user][:email].present? && params[:user][:password].present?
user = User.find_by_email(params[:user][:email])
render json: json_response(ok: false, status: 401) and return unless user
render json: json_response(ok: false, status: 401) and return unless user.valid_password?(params[:user][:password])
user.ensure_authentication_token
sign_in user
render json: json_response(ok: true, auth_token: user.authentication_token, user_id: user.id)
end
end
end
layout 'user/foundation'
#layout 'phone'
alias :list :active_list
@@ -91,13 +57,17 @@ class UserController < ApplicationController
# GET /suppliers/1/product_list
# GET /suppliers/1/product_list.json
def list_products
redirect_to(user_root_path(message: 'the_list_has_been_closed')) and return unless list
@supplier = list.supplier
respond_to do |format|
format.html do
redirect_to(user_root_path(message: 'the_list_has_been_closed')) and return unless list
@supplier = list.supplier
handle_message_params
end
format.json do
unless list
render json: {not_present: true} and return
end
@supplier = list.supplier
h = ProductCategory.for_user(current_user, table: list.table, list: list, supplier: @supplier) # list is performance parameter
render json: h
#products = list.supplier.products
@@ -291,7 +261,7 @@ class UserController < ApplicationController
end
format.json do
render json: json_alert('messages.cannot_order_on_non_active_list') and return unless @list.active?
@list.place_order params[:products], user: current_user
@list.place_order params[:order] || params[:products], user: current_user
render json: json_notice('messages.order_is_placed', location: :active_list)
end
end
@@ -299,6 +269,7 @@ class UserController < ApplicationController
def move_table
render json: json_alert('messages.no_active_list', list_active: false) and return unless list.present?
render json: json_alert('messages.table_not_found') and return unless params[:table_id].present?
@table = Table.find(params[:table_id])
if @table.occupied?
render json: {occupied: true}
@@ -307,18 +278,4 @@ class UserController < ApplicationController
render json: {occupied: false}
end
end
private
def handle_message_params
flash.now[:notice] = t('messages.the_list_has_been_closed', list: List.model_name.human) if params[:list_closed].present?
flash.now[:notice] = t("messages.#{params[:message]}", list: List.model_name.human, supplier: Supplier.model_name.human) if params[:message].present? && params[:message] =~ /^\w+$/
end
# General handler of json responses. Will be able to set some additional communication data.
# By default a response is ok.
def json_response(obj = {})
obj[:ok] = true unless obj.has_key?(:ok)
obj
end
end
@@ -0,0 +1,52 @@
module Users
class ApplicationController < ::ApplicationController
before_action :user_authentication, :unless => ->(c){ %w(obtain_token).include?(c.action_name) || c.request.format.symbol == :html } # , except: [:obtain_token, :index]
private
def user_authentication
if params[:auth_token].present?
user = User.find_by_authentication_token(params[:auth_token])
sign_in user if user
sign_out current_user if current_user && !user # Other token attempt of logged in user
else
authenticate_user!
end
unless current_user.present?
respond_to do |format|
format.html {redirect_to new_user_session_path}
format.json {render json: json_response(ok: false, status: 401), status: :unauthorized}
end
end
end
def obtain_token
redirect_to user_omniauth_authorize_path('facebook') and return unless current_user.present?
respond_to do |format|
format.html
format.json do
render json: json_response(ok: false, status: 401) and return unless params[:user].present? && params[:user][:email].present? && params[:user][:password].present?
user = User.find_by_email(params[:user][:email])
render json: json_response(ok: false, status: 401) and return unless user
render json: json_response(ok: false, status: 401) and return unless user.valid_password?(params[:user][:password])
user.ensure_authentication_token
sign_in user
render json: json_response(ok: true, auth_token: user.authentication_token, user_id: user.id)
end
end
end
def handle_message_params
flash.now[:notice] = t('messages.the_list_has_been_closed', list: List.model_name.human) if params[:list_closed].present?
flash.now[:notice] = t("messages.#{params[:message]}", list: List.model_name.human, supplier: Supplier.model_name.human) if params[:message].present? && params[:message] =~ /^\w+$/
end
# General handler of json responses. Will be able to set some additional communication data.
# By default a response is ok.
def json_response(obj = {})
obj[:ok] = true unless obj.has_key?(:ok)
obj
end
end
end
@@ -0,0 +1,15 @@
module Users
class ProductCategoriesController < Users::ApplicationController
#EMBER
def index
respond_to do |format|
format.json do
render json: {} and return unless params[:table_id].present?
table = Table.find(params[:table_id])
product_categories = table.supplier.product_categories.include_relation('products') # not yet implemented for many to many
render json: product_categories #, serializer: ProductCategorySerializer
end
end
end
end
end