Module: Amalgam::Authorities::Controllers::Helpers

Extended by:
ActiveSupport::Concern
Included in:
Amalgam::ApplicationController
Defined in:
lib/amalgam/authorities/controllers/helpers.rb

Class Method Summary collapse

Class Method Details

.amalgam_define_helpers(mapping, options = {}) ⇒ Object

:nodoc:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/amalgam/authorities/controllers/helpers.rb', line 12

def self.amalgam_define_helpers(mapping,options = {}) #:nodoc:

  as = options[:as].to_s if options[:as]

  class_eval <<-METHODS, __FILE__, __LINE__ + 1
    def authenticate_#{mapping}!
      if !current_#{mapping}
        store_location
        redirect_to #{mapping}_signin_url, :alert => I18n.t('amalgam.sessions.fail.need_to_be_#{mapping}')
      end
    end

    def #{mapping}_signed_in?
      !!current_#{mapping}
    end

    def current_#{mapping}
      @current_#{mapping} ||= #{mapping.classify.constantize}.find(session[:#{mapping}_id]) if session[:#{mapping}_id]
    end

  METHODS

  if options[:as] && options[:as].to_s != mapping
    class_eval <<-METHODS, __FILE__, __LINE__ + 1
      def authenticate_#{as}!
        if !current_#{as}
          store_location
          redirect_to #{mapping}_signin_url, :alert => I18n.t('amalgam.sessions.fail.need_to_be_#{as}')
        end
      end

      def current_#{as}
        current_#{mapping}
      end

      def #{as}_signed_in?
        !!current_#{as}
      end
    METHODS
  end

  ActiveSupport.on_load(:action_controller) do
    helper_method "current_#{mapping}", "#{mapping}_signed_in?"
    helper_method "current_#{as}", "#{as}_signed_in?" if options[:as] && options[:as].to_s != mapping
  end
end