2012-04-15 19:59:39 +02:00
|
|
|
# encoding: utf-8
|
2009-01-06 11:49:19 +01:00
|
|
|
class ApplicationController < ActionController::Base
|
2018-10-13 20:16:35 +02:00
|
|
|
include Concerns::FoodcoopScope
|
|
|
|
include Concerns::Auth
|
|
|
|
include Concerns::Locale
|
|
|
|
helper_method :current_user
|
2013-05-31 18:42:28 +02:00
|
|
|
helper_method :available_locales
|
2009-03-24 17:01:10 +01:00
|
|
|
|
2011-05-11 13:38:46 +02:00
|
|
|
protect_from_forgery
|
2018-10-13 20:16:35 +02:00
|
|
|
before_filter :authenticate, :set_user_last_activity, :store_controller, :items_per_page
|
2009-03-09 13:02:43 +01:00
|
|
|
after_filter :remove_controller
|
2014-06-27 11:45:34 +02:00
|
|
|
around_filter :set_time_zone, :set_currency
|
2011-05-11 13:38:46 +02:00
|
|
|
|
2018-10-13 20:16:35 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Returns the controller handling the current request.
|
|
|
|
def self.current
|
|
|
|
Thread.current[:application_controller]
|
|
|
|
end
|
|
|
|
|
2013-05-31 18:42:28 +02:00
|
|
|
private
|
2013-06-15 02:04:44 +02:00
|
|
|
|
2015-03-03 14:55:30 +01:00
|
|
|
def set_user_last_activity
|
|
|
|
if current_user && (session[:last_activity] == nil || session[:last_activity] < 1.minutes.ago)
|
|
|
|
current_user.update_attribute(:last_activity, Time.now)
|
|
|
|
session[:last_activity] = Time.now
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-05 18:47:16 +02:00
|
|
|
# Many plugins can be turned on and off on the fly with a `use_` configuration option.
|
|
|
|
# To disable a controller in the plugin, you can use this as a `before_action`:
|
|
|
|
#
|
|
|
|
# class MypluginController < ApplicationController
|
|
|
|
# before_filter -> { require_plugin_enabled FoodsoftMyplugin }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
def require_plugin_enabled(plugin)
|
2017-09-24 17:27:23 +02:00
|
|
|
redirect_to_root_with_feature_disabled_alert unless plugin.enabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def require_config_enabled(config)
|
|
|
|
redirect_to_root_with_feature_disabled_alert unless FoodsoftConfig[config]
|
2016-03-06 13:56:33 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def require_config_disabled(config)
|
2017-09-24 17:27:23 +02:00
|
|
|
redirect_to_root_with_feature_disabled_alert if FoodsoftConfig[config]
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_to_root_with_feature_disabled_alert
|
|
|
|
redirect_to root_path, alert: I18n.t('application.controller.error_feature_disabled')
|
2014-04-05 18:47:16 +02:00
|
|
|
end
|
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
# Stores this controller instance as a thread local varibale to be accessible from outside ActionController/ActionView.
|
|
|
|
def store_controller
|
|
|
|
Thread.current[:application_controller] = self
|
|
|
|
end
|
2018-10-13 20:16:35 +02:00
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
# Sets the thread local variable that holds a reference to the current controller to nil.
|
|
|
|
def remove_controller
|
|
|
|
Thread.current[:application_controller] = nil
|
|
|
|
end
|
2009-01-08 16:33:27 +01:00
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
# Get supplier in nested resources
|
|
|
|
def find_supplier
|
|
|
|
@supplier = Supplier.find(params[:supplier_id]) if params[:supplier_id]
|
|
|
|
end
|
2009-03-24 17:01:10 +01:00
|
|
|
|
2011-06-10 12:18:55 +02:00
|
|
|
def items_per_page
|
2013-08-12 14:49:23 +02:00
|
|
|
if params[:per_page] && params[:per_page].to_i > 0 && params[:per_page].to_i <= 500
|
2011-06-10 12:18:55 +02:00
|
|
|
@per_page = params[:per_page].to_i
|
|
|
|
else
|
|
|
|
@per_page = 20
|
|
|
|
end
|
|
|
|
end
|
2012-08-06 12:00:40 +02:00
|
|
|
|
2014-06-25 17:32:26 +02:00
|
|
|
# Set timezone according to foodcoop preference.
|
|
|
|
# @see http://stackoverflow.com/questions/4362663/timezone-with-rails-3
|
|
|
|
# @see http://archives.ryandaigle.com/articles/2008/1/25/what-s-new-in-edge-rails-easier-timezones
|
|
|
|
def set_time_zone
|
|
|
|
old_time_zone = Time.zone
|
|
|
|
Time.zone = FoodsoftConfig[:time_zone] if FoodsoftConfig[:time_zone]
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Time.zone = old_time_zone
|
|
|
|
end
|
|
|
|
|
2014-06-27 11:45:34 +02:00
|
|
|
# Set currency according to foodcoop preference.
|
|
|
|
# @see #set_time_zone
|
|
|
|
def set_currency
|
|
|
|
old_currency = ::I18n.t('number.currency.format.unit')
|
|
|
|
new_currency = FoodsoftConfig[:currency_unit] || ''
|
|
|
|
new_currency += "\u202f" if FoodsoftConfig[:currency_space]
|
|
|
|
::I18n.backend.store_translations(::I18n.locale, number: {currency: {format: {unit: new_currency}}})
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
::I18n.backend.store_translations(::I18n.locale, number: {currency: {format: {unit: old_currency}}})
|
|
|
|
end
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|