Class Babylon::Base::Controller
In: lib/babylon/base/controller.rb
Parent: Object

Your application‘s controller should be descendant of this class.

Methods

Attributes

action_name  [RW] 
rendered  [RW] 
stanza  [RW] 

Public Class methods

Creates a new controller (you should not override this class) and assigns the stanza as well as any other value of the hash to instances named after the keys of the hash.

[Source]

    # File lib/babylon/base/controller.rb, line 12
12:       def initialize(stanza = nil)
13:         @stanza = stanza
14:         @view = nil
15:       end

Public Instance methods

Returns the list of variables assigned during the action.

[Source]

    # File lib/babylon/base/controller.rb, line 33
33:       def assigns
34:         vars = Hash.new
35:         instance_variables.each do |var|
36:           if !["@view", "@action_name", "@block"].include? var
37:             vars[var[1..-1]] = instance_variable_get(var)
38:           end
39:         end
40:         vars
41:       end

Actually evaluates the view

[Source]

    # File lib/babylon/base/controller.rb, line 70
70:       def evaluate
71:         @view.evaluate if @view
72:       end

Performs the action and calls back the optional block argument : you should not override this function

[Source]

    # File lib/babylon/base/controller.rb, line 19
19:       def perform(action)
20:         @action_name = action
21:         begin
22:           self.send(@action_name)
23:         rescue
24:           Babylon.logger.error {
25:             "#{$!}:\n#{$!.backtrace.join("\n")}"
26:           }
27:         end
28:         self.render
29:       end

Called by default after each action to "build" a XMPP stanza. By default, it will use the /controller_name/action.xml.builder You can use the following options :

  - :file : render a specific file (can be in a different controller)
  - :action : render another action of the current controller
  - :nothing : doesn't render anything

[Source]

    # File lib/babylon/base/controller.rb, line 49
49:       def render(options = {}) 
50:         return if @view and !options[:force] # Avoid double rendering, if we have already attached a view
51:         
52:         if options == {} # default rendering
53:           result = render(:file => default_template_name)
54:         elsif options[:file]
55:           file = options[:file]
56:           if file =~ /^\// # Render from view root
57:             result = render_for_file(File.join("app", "views", "#{file}.xml.builder"))
58:           else
59:             result = render_for_file(view_path(file)) 
60:           end
61:         elsif action_name = options[:action]
62:           result = render(:file => default_template_name(action_name.to_s))
63:         elsif options[:nothing]
64:           @view = Babylon::Base::View.new()
65:         end
66:       end

Protected Instance methods

Default template name used to build stanzas

[Source]

    # File lib/babylon/base/controller.rb, line 84
84:       def default_template_name(action_name = nil)
85:         "#{action_name || @action_name}.xml.builder"
86:       end

Creates the view and "evaluates" it to build the XML for the stanza

[Source]

    # File lib/babylon/base/controller.rb, line 90
90:       def render_for_file(file)
91:         Babylon.logger.info {
92:           "RENDERING : #{file}"
93:         }
94:         @view = Babylon::Base::View.new(file, assigns)
95:         Babylon.logger.info {
96:           " "
97:         }
98:       end

Builds the view path.

[Source]

    # File lib/babylon/base/controller.rb, line 78
78:       def view_path(file_name)
79:         File.join("app", "views", "#{self.class.name.gsub("Controller","").downcase}", file_name)
80:       end

[Validate]