module Errors2Html

Constants

Version

Public Class Methods

dependencies() click to toggle source
# File lib/rails_errors2html/_lib.rb, line 13
def dependencies
  {
    'fattr'      => [ 'fattr'         , ' ~> 2' ],
    'map'        => [ 'map'           , ' ~> 6' ],
    'rails_view' => [ 'rails_view'    , ' ~> 1' ]
  }
end
flatten(hashlike) click to toggle source
# File lib/rails_errors2html.rb, line 53
def Errors2Html.flatten(hashlike)
  case hashlike
    when Map
      hash = Hash.new
      hashlike.depth_first_each do |key, value|
        index = key.pop if key.last.is_a?(Integer)
        (hash[key] ||= []).push(value)
      end
      hash
    else
      hashlike.respond_to?(:to_hash) ? hashlike.to_hash : hashlike
  end
end
libdir(*args, &block) click to toggle source
# File lib/rails_errors2html/_lib.rb, line 34
def libdir(*args, &block)
  @libdir ||= File.dirname(File.expand_path(__FILE__).sub(/\.rb$/,''))
  args.empty? ? @libdir : File.join(@libdir, *args)
ensure
  if block
    begin
      $LOAD_PATH.unshift(@libdir)
      block.call()
    ensure
      $LOAD_PATH.shift()
    end
  end
end
load(*libs) click to toggle source
# File lib/rails_errors2html/_lib.rb, line 48
def load(*libs)
  libs = libs.join(' ').scan(/[^\s+]+/)
  libdir{ libs.each{|lib| Kernel.load(lib) } }
end
load_dependencies!() click to toggle source
# File lib/rails_errors2html/_lib.rb, line 21
def load_dependencies!
  begin 
    require 'rubygems'
  rescue LoadError
    nil
  end

  dependencies.each do |lib, dependency|
    gem(*dependency) if defined?(gem)
    require(lib)
  end
end
summary() click to toggle source
# File lib/rails_errors2html/_lib.rb, line 9
def summary
  "tiny, KISS html rendering of active_model errors"
end
to_html(*args) click to toggle source
# File lib/rails_errors2html.rb, line 5
def Errors2Html.to_html(*args)
  if args.size == 1
    case args.first
      when Array, String, Symbol
        messages = Array(args.first)
        args = [{:base => messages}]
    end
  end

  args.flatten!
  args.compact!

  at_least_one_error = false

  errors = Map.new
  errors[:global] = []
  errors[:fields] = {} 

  args.each do |e|
    flatten(e).each do |key, messages|
      Array(messages).each do |message|
        at_least_one_error = true
        message = message.to_s.html_safe

        if Array(key).join =~ /\A(?:[*]|base)\Z/iomx
          errors.global.push(message).uniq!
        else
          (errors.fields[key] ||= []).push(message).uniq!
        end
      end
    end
  end

  return "" unless at_least_one_error

  locals = {
    :errors => errors,
    :global_errors => errors.global,
    :fields_errors => errors.fields
  }

  if template
    View.render(:template => template, :locals => locals, :layout => false)
  else
    View.render(:inline => inline, :locals => locals, :layout => false)
  end
end
version() click to toggle source
# File lib/rails_errors2html/_lib.rb, line 5
def version
  Version
end