foodsoft/app/controllers/application_controller.rb

97 lines
3.2 KiB
Ruby
Raw Normal View History

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
include PathHelper
2018-10-13 20:16:35 +02:00
helper_method :current_user
helper_method :available_locales
protect_from_forgery
before_action :authenticate, :set_user_last_activity, :store_controller, :items_per_page
after_action :remove_controller
around_action :set_time_zone, :set_currency
2009-01-06 11:49:19 +01:00
# Returns the controller handling the current request.
def self.current
Thread.current[:application_controller]
end
private
2015-03-03 14:55:30 +01:00
def set_user_last_activity
return unless current_user && (session[:last_activity].nil? || session[:last_activity] < 1.minute.ago)
current_user.update_attribute(:last_activity, Time.now)
session[:last_activity] = Time.now
2015-03-03 14:55:30 +01:00
end
# 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_action -> { require_plugin_enabled FoodsoftMyplugin }
# end
#
def require_plugin_enabled(plugin)
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]
end
def require_config_disabled(config)
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')
end
# 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
# Sets the thread local variable that holds a reference to the current controller to nil.
def remove_controller
Thread.current[:application_controller] = nil
end
# Get supplier in nested resources
def find_supplier
@supplier = Supplier.find(params[:supplier_id]) if params[:supplier_id]
end
def items_per_page
@per_page = if params[:per_page] && params[:per_page].to_i > 0 && params[:per_page].to_i <= 500
params[:per_page].to_i
else
20
end
end
2012-08-06 12:00:40 +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
# 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