Sha256: 4c7997a2d7377c8fb3f89b85ca769d22812033e2523c7be586cb9f04f8c81512

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

require 'active_support/core_ext/string/output_safety'

module UnobtrusiveFlash
  module ControllerMixin
    protected

    def prepare_unobtrusive_flash
      if flash.any?
        cookie_flash = []
        if cookies['flash']
          cookie_flash = JSON.parse(cookies['flash']) rescue nil
          cookie_flash=[] unless cookie_flash.is_a? Array
        end

        cookie_flash += UnobtrusiveFlash::ControllerMixin.sanitize_flash(flash)
        cookies[:flash] = {:value => cookie_flash.to_json, :domain => unobtrusive_flash_domain}
        flash.discard
      end
    end

    # Setting cookies for :all domains is broken for Heroku apps, read this article for details
    # https://devcenter.heroku.com/articles/cookies-and-herokuapp-com
    # You can also override this method in your controller if you need to customize the cookie domain
    def unobtrusive_flash_domain
      if request.host =~ /\.herokuapp\.com$/
        request.host
      else
        :all
      end
    end

    class << self
      def sanitize_flash(flash)
        flash.to_a.map do |key, value|
          html_safe_value = value.html_safe? ? value : ERB::Util.html_escape(value)
          [key, html_safe_value]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
unobtrusive_flash-3.2.0 lib/unobtrusive_flash/controller_mixin.rb
unobtrusive_flash-3.1.0 lib/unobtrusive_flash/controller_mixin.rb