Module: Applicat::Mvc::Controller

Included in:
Voluntary::ApplicationController
Defined in:
lib/applicat/mvc/controller.rb,
lib/applicat/mvc/controller/error_handling.rb

Defined Under Namespace

Modules: ErrorHandling, Resource, TransitionActions Classes: Helper, JavaScriptHelper

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/applicat/mvc/controller.rb', line 5

def self.included(base)
  base.class_eval do
    include ErrorHandling
    include TransitionActions
    
    protect_from_forgery
       
    layout proc { |controller| controller.request.xhr? || !controller.params[:is_ajax_request].nil? ? false : 'application' }
     
    helper :all
    helper_method :controller_action?, :resource_exists?, :layout_presenter 

    def controller_action?(input_controller, input_action)
      if "#{input_controller}/#{input_action}" == "#{controller_name}/#{action_name}"
        return true
      else
        return false
      end
    end

    def resource_exists?(name)
      if eval("@#{name}.nil?") || (eval("@#{name}.class.name") != name.classify && eval("@#{name}.class.superclass.name") != name.classify) || eval("@#{name}.id.nil?")
        return false
      else
        return true
      end
    end
              
    def help
      helper = Helper.instance
      helper.controller = self 
      return helper
    end
    
    def layout_presenter
      layout_name = _layout
      
      if layout_name.inspect.match('.')
        # app/views/layouts/application.html.erb
        layout_name = layout_name.inspect.gsub('"', '').split('/').last.split('.').first
      end
      
      @layout_presenter ||= "Layout::#{layout_name.classify}Presenter".constantize.new(
        self.view_context, resource: self.respond_to?(:resource) ? resource : nil
      )
    end
    
    protected         
    
    def eager_load_polymorphs(list, includes)
      polymorphs = {}
      
      logger.debug "eager_load_polymorphs for list length: #{list.length}"
      
      list.each do |record|
        includes.each do |include|
          include = include.first if include.is_a?(Array)
          
          polymorphs[eval("record.#{include}_type")] ||= [] 
          polymorphs[eval("record.#{include}_type")] << eval("record.#{include}_id")
        end
      end
      
      logger.debug "Load polymorphs: #{polymorphs.keys.join(', ')}"
      
      polymorphs.each do |type,ids|
        polymorphs[type] = type.constantize.find(ids)
      end
      
      list_index = 0
      
      list.each do |record|
        includes.each do |include|
          polymorphs[eval("record.#{include}_type")].each do |polymorph|
            next unless polymorph.id == eval("record.#{include}_id")
            
            eval("list[list_index].#{include} = polymorph")
            
            break
          end
        end
        
        list_index += 1
      end
      
      # free memory
      polymorphs.each do |type,ids|
        polymorphs.delete(type)
      end
      
      return list
    end         
    
    def model_name
      self.class.name.gsub('Controller', '').classify
    end
    
    def set_i18n_locale_from_params
      if params[:locale]
        if I18n.available_locales.include?(params[:locale].to_sym)
          I18n.locale = params[:locale]
        else
          flash.now[:notice] = "#{params[:locale]} translation not available"
          logger.error flash.now[:notice]
        end
      end
    end
   
    def js_help
      js_helper = JavaScriptHelper.instance
      js_helper.controller = self
      return js_helper
    end
    
    def store_location
      session[:"user.return_to"] = request.fullpath
    end
    
    private
    
    def exception_action(exception)
      @exception_catched = true
      
      if Rails.env == 'production'
        notify_airbrake(exception)
        logger.error "INTERNAL SERVER ERROR FOR REQUEST: " + exception.message
        logger.error exception.backtrace.join("\n")
      else
        raise exception
      end
      
      if !request.env['REQUEST_URI'].match(/\/assets\//).nil?
        render :text => '', :layout => false
      elsif exception.is_a?(ActiveRecord::RecordNotFound)
        render 'pages/exception_404', :layout => 'application'
      else
        render 'pages/exception_500', :layout => 'application'
      end
    end
  end
end