Class: Lux::Controller

Inherits:
Object show all
Includes:
ClassCallbacks, Application::Shared
Defined in:
lib/lux/controller/controller.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Application::Shared

#body?, #current, #nav, #params, #redirect_to, #request, #response, #session, #user

Constructor Details

#initializeController

Returns a new instance of Controller.



46
47
48
49
50
# File 'lib/lux/controller/controller.rb', line 46

def initialize
  # before and after should be exected only once
  @lux = IVARS.new
  @lux.template_sufix = self.class.to_s.sub(/Controller$/,'').underscore.downcase.split('/').first
end

Instance Attribute Details

#controller_actionObject (readonly)

Returns the value of attribute controller_action.



44
45
46
# File 'lib/lux/controller/controller.rb', line 44

def controller_action
  @controller_action
end

Class Method Details

.action(*args) ⇒ Object

simple shortcut allows direct call to action, bypasing call



26
27
28
# File 'lib/lux/controller/controller.rb', line 26

def action *args
  new.action(*args)
end

.mock(*args) ⇒ Object

create mock function, to enable template rendering mock :index, :login



32
33
34
35
36
# File 'lib/lux/controller/controller.rb', line 32

def mock *args
  args.each do |el|
    define_method(el) { true }
  end
end

Instance Method Details

#action(method_name, *args) ⇒ Object

action(:show) action(:select’, [‘users’])



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
# File 'lib/lux/controller/controller.rb', line 54

def action method_name, *args
  if method_name.blank?
    raise ArgumentError.new('Controller action called with blank action name argument')
  end

  if method_name.is_a?(Symbol)
    raise Lux.error.internal_server_error('Forbiden action name :%s' % method_name) if [:action, :error].include?(method_name)
  else
    return controller_action_call(method_name, *args)
  end

  method_name = method_name.to_s.gsub('-', '_').gsub(/[^\w]/, '')

  # dev console log
  Lux.log { ' %s#%s (action)'.light_blue % [self.class, method_name] }
  # Lux.log { ' %s' % self.class.source_location }

  @lux.action = method_name.to_sym

  run_callback :before, @lux.action

  catch :done do
    unless response.body?
      run_callback :before_action, @lux.action

      # if action not found
      if respond_to?(method_name)
        send method_name, *args
      else
        action_missing method_name
      end

      render
    end
  end
rescue => error
  rescue_from error
end

#flashObject



97
98
99
# File 'lib/lux/controller/controller.rb', line 97

def flash
  response.flash
end

#rescue_from(error) ⇒ Object



101
102
103
104
105
# File 'lib/lux/controller/controller.rb', line 101

def rescue_from error
  Lux::Error.log error
  data = Lux.env.show_errors? ? Lux::Error.inline(error) : 'Server error: %s (%s)' % [error.message, error.class]
  render html: data, status: 400
end

#timeout(seconds) ⇒ Object



93
94
95
# File 'lib/lux/controller/controller.rb', line 93

def timeout seconds
  Lux.current.var[:app_timeout] = seconds
end