Class: Lux::Controller

Inherits:
Object show all
Defined in:
lib/lux/controller/controller.rb

Overview

filters stack for call before, before_action, :action, after

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeController

Returns a new instance of Controller.



36
37
38
39
40
# File 'lib/lux/controller/controller.rb', line 36

def initialize
  # before and after should be exected only once
  @executed_filters = {}
  @base_template = self.class.to_s.include?('::') ? self.class.to_s.sub(/Controller$/,'').underscore : self.class.to_s.sub(/Controller$/,'').downcase
end

Instance Attribute Details

#controller_actionObject (readonly)

INSTANCE METHODS



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

def controller_action
  @controller_action
end

Class Method Details

.action(*args) ⇒ Object

simple shortcut allows direct call to action, bypasing call



19
20
21
# File 'lib/lux/controller/controller.rb', line 19

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

.mock(*args) ⇒ Object

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



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

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’])



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

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

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

  # dev console log
  Lux.log " #{self.class.to_s}##{method_name}".light_blue

  @controller_action = method_name.to_sym
  if @controller_format = current.nav.format
    current.nav.format = nil
  end

  # format error unless method found
  report_not_found_error unless respond_to? method_name

  catch :done do
    filter :before
    filter :before_action
    send method_name, *args
    render
  end

  filter :after

  throw :done
rescue => e
  response.body { nil }
  on_error(e)
end

#cache(*args, &block) ⇒ Object



42
43
44
# File 'lib/lux/controller/controller.rb', line 42

def cache *args, &block
  Lux.cache.fetch *args, &block
end

#error(*args) ⇒ Object



79
80
81
# File 'lib/lux/controller/controller.rb', line 79

def error *args
  args.first.nil? ? Lux::Error::AutoRaise : Lux::Error.report(*args)
end

#on_error(error) ⇒ Object



83
84
85
# File 'lib/lux/controller/controller.rb', line 83

def on_error error
  raise error
end

#render(name = nil, opts = {}) ⇒ Object

render :index render ‘main/root/index’ render text: ‘ok’



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

def render name=nil, opts={}
  on_error Lux::Error.new(404, '%s document Not Found' % @controller_format.to_s.upcase) if @controller_format

  if name.class == Hash
    opts.merge! name
  else
    opts[:template] = name
  end

  filter :before_render

  opts = opts.to_opts :text, :html, :json, :javascript, :cache, :template, :layout, :render_to_string, :data, :status, :ttl, :content_type

  response.status opts.status if opts.status
  response.content_type = opts.content_type if opts.content_type

  page =
  if opts.cache
    Lux.cache.fetch(opts.cache, opts.ttl || 3600) { render_resolve(opts) }
  else
    render_resolve(opts)
  end

  if opts.render_to_string
    page
  else
    response.body { page }
    throw :done
  end
end

#render_javascript(name = nil, opts = {}) ⇒ Object



130
131
132
133
134
# File 'lib/lux/controller/controller.rb', line 130

def render_javascript name=nil, opts={}
  opts[:content_type] = :javascript
  opts[:layout]       = false
  render name, opts
end

#render_to_string(name = nil, opts = {}) ⇒ Object



125
126
127
128
# File 'lib/lux/controller/controller.rb', line 125

def render_to_string name=nil, opts={}
  opts[:render_to_string] = true
  render name, opts
end

#send_file(file, opts = {}) ⇒ Object



87
88
89
# File 'lib/lux/controller/controller.rb', line 87

def send_file file, opts={}
  Lux::Response::File.send(file, opts)
end