Module: Rango::MessageMixin

Included in:
StackController
Defined in:
lib/rango/mixins/message.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(controller) ⇒ Object

The rails-style flash messages NOTE: it’s important to include this mixin after ImplicitRendering mixin

Since:

  • 0.0.2



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rango/mixins/message.rb', line 8

def self.included(controller)
  controller.class_eval do
    if self.method_defined?(:context)
      Rango.logger.debug("Extending #{self}#context by message")
      # This could be in the mixin itself, but we don't want
      # to define it if the context method doesn't exist, so
      # context.respond_to?(:context) as a detection of explicit
      # rendering mixin and similar can work. BTW the following
      # doesn't work with do/end syntax.
      include Module.new {
        def context
          @context ||= super.merge!(message: self.message)
        end
      }
    else
      Rango.logger.warn("Context #{self}#method isn't defined")
    end
  end
end

Instance Method Details

#messageObject Also known as: flash



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rango/mixins/message.rb', line 28

def message
  @message ||= begin
    messages = request.GET[:msg] || Hash.new
    if messages.is_a?(String)
      messages.force_encoding(Encoding.default_external)
    elsif messages.is_a?(Hash)
      messages.inject(Hash.new.extend(ParamsMixin)) do |result, pair| # TODO: here is the problem, Hash.new isn't params mixin
        result.merge(pair[0] => pair[1].force_encoding(Encoding.default_external))
      end
    end
  end
end

#redirect(uri, options = Hash.new, status = 303, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rango/mixins/message.rb', line 42

def redirect(uri, options = Hash.new, status = 303, &block)
  # status, options = 303, status if status.is_a?(Hash)
  if options.respond_to?(:inject)
    # redirect "/post", error: "Try again"
    # ?msg[error]="Try again"
    uri = options.inject(uri) do |uri, pair|
      type, message = pair
      uri + "?msg[#{type}]=#{message}"
    end
  else
    # redirect "/post", "Try again"
    # ?msg="Try again"
    uri.concat("?msg=#{options}")
  end
  super(uri, status)
end