Module: Odania::Controllers::Helpers

Defined in:
lib/odania/controllers/helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_helpers(config) ⇒ Object

Define authentication filters and accessor helpers.

Generated methods:
  authenticate_user!  # Signs user in or redirect
  user_signed_in?     # Checks whether there is a user signed in or not
  current_user        # Current signed in user

Use:
  before_filter :authenticate_user!


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/odania/controllers/helpers.rb', line 51

def self.define_helpers(config)
	# Define methods dynamically based on the configuration
	module_eval <<-METHODS, __FILE__, __LINE__ + 1
		def authenticate_user!(opts={})
			#{config.authenticate_user_function}(opts)
		end

		def user_signed_in?
			#{config.user_signed_in_function}
		end

		def current_user
			#{config.current_user_function}
		end
	METHODS

	# Register helpers automatically after loading of action_controller
	ActiveSupport.on_load(:action_controller) do
		helper Odania::Controllers::Helpers

		helper_method :user_signed_in?, :current_user
	end
end

Instance Method Details

#render_error(err_msg = nil) ⇒ Object

Raise an error



81
82
83
84
# File 'lib/odania/controllers/helpers.rb', line 81

def render_error(err_msg=nil)
	@error_msg = err_msg
	render template: 'odania/common/internal_server_error', layout: 'layouts/odania_core/error', status: :bad_request
end

#render_not_foundObject

Raise a not found exception



76
77
78
# File 'lib/odania/controllers/helpers.rb', line 76

def render_not_found
	render template: 'odania/common/not_found_error', layout: 'layouts/odania_core/error', status: :not_found
end

#require_admin_role!Object



34
35
36
37
38
39
# File 'lib/odania/controllers/helpers.rb', line 34

def require_admin_role!
	return false unless user_signed_in?

	role = current_user.roles.where(role: Odania::UserRole.roles[:admin]).first
	return redirect_to root_path, notice: t('Not allowed') if role.nil?
end

#set_layoutObject

Set layout depending on the current site



87
88
89
90
# File 'lib/odania/controllers/helpers.rb', line 87

def set_layout
	tpl = current_site.nil? ? nil : current_site.template
	tpl || 'odania_core/application'
end

#valid_menu!Object



25
26
27
28
29
30
31
32
# File 'lib/odania/controllers/helpers.rb', line 25

def valid_menu!
	if current_menu.nil?
		render template: 'odania/common/not_found_error', layout: 'layouts/odania_core/error'
		return false
	end

	return true
end

#valid_site!Object

before_filter to make sure that we have a valid site



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/odania/controllers/helpers.rb', line 8

def valid_site!
	if current_site.nil?
		site = Site.active.where(is_default: true).first
		return redirect_to "http://#{site.host}" unless site.nil?

		render :text => 'There is no (default)-site defined!', status: :service_unavailable
		return false
	end

	unless current_site.redirect_to.nil?
		redirect_to "http://#{current_site.redirect_to.host}"
		return false
	end

	return true
end