<commit_before># encoding: utf-8

require "rango/mixins/chainable"

module Rango
  module MessageMixin
    # The rails-style flash messages
    # @since 0.0.2
    # NOTE: it's important to include this mixin after ImplicitRendering mixin
    def self.included(controller)
      unless controller.method_defined?(:request)
        raise "Rango::MessageMixin requires method request to be defined"
      end
      if controller.method_defined?(:context)
        Rango.logger.debug("Extending context by message")
        controller.class_eval do
          extend Chainable
          chainable do
            def context
              super.merge!(message: self.message)
            end

            def redirect(uri, status = 303, options = Hash.new, &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
          end
        end
      else
        Rango.logger.warn("Context method isn't defined")
      end
    end

    def message
      @message ||= begin
        messages = request.GET[:msg] || String.new
        if messages.is_a?(String)
          messages.force_encoding(Encoding.default_external)
        elsif messages.is_a?(Hash)
          messages.inject(Hash.new) do |result, pair|
            result.merge(pair[0] => pair[1].force_encoding(Encoding.default_external))
          end
        end
      end
    end
    alias_method :flash, :message
  end
end
<commit_msg>Fixed messages mixin, so now ... Rango specs are green again!<commit_after># encoding: utf-8

module Rango
  module MessageMixin
    # The rails-style flash messages
    # @since 0.0.2
    # NOTE: it's important to include this mixin after ImplicitRendering mixin
    def self.included(controller)
      unless controller.method_defined?(:request)
        raise "Rango::MessageMixin requires method request to be defined"
      end
      controller.class_eval do
        if self.method_defined?(:context)
          Rango.logger.debug("Extending context by message")
          def context
            super.merge!(message: self.message)
          end
        else
          Rango.logger.warn("Context method isn't defined")
        end

        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
      end
    end

    def message
      @message ||= begin
        messages = request.GET[:msg] || String.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
    alias_method :flash, :message
  end
end

{'fraction_non_alphanumeric': 0.10028878970858493, 'fraction_numerical': 0.006300866369125755, 'mean_word_length': 1.9106187929717342, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2199548', 'n_tokens_mistral': 1063, 'n_tokens_neox': 1037, 'n_words': 283}