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!


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

def self.define_helpers(config)
  # Define methods dynamically based on the configuration
  module_eval "    def authenticate_user!(opts={})\n      \#{config.authenticate_user_function}(opts)\n    end\n\n    def user_signed_in?\n      \#{config.user_signed_in_function}\n    end\n\n    def current_user\n      \#{config.current_user_function}\n    end\n  METHODS\n\n  # Register helpers automatically after loading of action_controller\n  ActiveSupport.on_load(:action_controller) do\n    helper Odania::Controllers::Helpers\n\n    helper_method :user_signed_in?, :current_user\n  end\nend\n", __FILE__, __LINE__ + 1

Instance Method Details

#render_error(err_msg = nil) ⇒ Object

Raise an error



79
80
81
82
# File 'lib/odania/controllers/helpers.rb', line 79

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



74
75
76
# File 'lib/odania/controllers/helpers.rb', line 74

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
# File 'lib/odania/controllers/helpers.rb', line 34

def require_admin_role!
  return false unless user_signed_in?
  return redirect_to root_path, notice: t('Not allowed') unless current_user.admin?
end

#set_layoutObject

Set layout depending on the current site



85
86
87
88
# File 'lib/odania/controllers/helpers.rb', line 85

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