errors_for 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/errors_for.rb +85 -0
  2. metadata +48 -0
data/lib/errors_for.rb ADDED
@@ -0,0 +1,85 @@
1
+ # Disclaimer - Code is copied as is from rails 2.3.10, and is only intended to act as a polyfill for error_messages_for() usage
2
+ # In projects migrating from old rails env
3
+ # --@avnerner (israbirding@gmail.com)
4
+ # Returns a string with a <tt>DIV</tt> containing all of the error messages for the objects located as instance variables by the names
5
+ # given. If more than one object is specified, the errors for the objects are displayed in the order that the object names are
6
+ # provided.
7
+ #
8
+ # This <tt>DIV</tt> can be tailored by the following options:
9
+ #
10
+ # * <tt>:header_tag</tt> - Used for the header of the error div (default: "h2").
11
+ # * <tt>:id</tt> - The id of the error div (default: "errorExplanation").
12
+ # * <tt>:class</tt> - The class of the error div (default: "errorExplanation").
13
+ # * <tt>:object</tt> - The object (or array of objects) for which to display errors,
14
+ # if you need to escape the instance variable convention.
15
+ # * <tt>:object_name</tt> - The object name to use in the header, or any text that you prefer.
16
+ # If <tt>:object_name</tt> is not set, the name of the first object will be used.
17
+ # * <tt>:header_message</tt> - The message in the header of the error div. Pass +nil+
18
+ # or an empty string to avoid the header message altogether. (Default: "X errors
19
+ # prohibited this object from being saved").
20
+ # * <tt>:message</tt> - The explanation message after the header message and before
21
+ # the error list. Pass +nil+ or an empty string to avoid the explanation message
22
+ # altogether. (Default: "There were problems with the following fields:").
23
+ #
24
+ # To specify the display for one object, you simply provide its name as a parameter.
25
+ # For example, for the <tt>@user</tt> model:
26
+ #
27
+ # error_messages_for 'user'
28
+ #
29
+ # To specify more than one object, you simply list them; optionally, you can add an extra <tt>:object_name</tt> parameter, which
30
+ # will be the name used in the header message:
31
+ #
32
+ # error_messages_for 'user_common', 'user', :object_name => 'user'
33
+ #
34
+ # If the objects cannot be located as instance variables, you can add an extra <tt>:object</tt> parameter which gives the actual
35
+ # object (or array of objects to use):
36
+ #
37
+ # error_messages_for 'user', :object => @question.user
38
+ #
39
+ # NOTE: This is a pre-packaged presentation of the errors with embedded strings and a certain HTML structure. If what
40
+ # you need is significantly different from the default presentation, it makes plenty of sense to access the <tt>object.errors</tt>
41
+ # instance yourself and set it up. View the source of this method to see how easy it is.
42
+ def error_messages_for(*params)
43
+ options = params.extract_options!.symbolize_keys
44
+
45
+ if object = options.delete(:object)
46
+ objects = Array.wrap(object)
47
+ else
48
+ objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact
49
+ end
50
+
51
+ count = objects.inject(0) {|sum, object| sum + object.errors.count }
52
+ unless count.zero?
53
+ html = {}
54
+ [:id, :class].each do |key|
55
+ if options.include?(key)
56
+ value = options[key]
57
+ html[key] = value unless value.blank?
58
+ else
59
+ html[key] = 'errorExplanation'
60
+ end
61
+ end
62
+ options[:object_name] ||= params.first
63
+
64
+ I18n.with_options :locale => options[:locale], :scope => [:activerecord, :errors, :template] do |locale|
65
+ header_message = if options.include?(:header_message)
66
+ options[:header_message]
67
+ else
68
+ object_name = options[:object_name].to_s
69
+ object_name = I18n.t(object_name, :default => object_name.gsub('_', ' '), :scope => [:activerecord, :models], :count => 1)
70
+ locale.t :header, :count => count, :model => object_name
71
+ end
72
+ message = options.include?(:message) ? options[:message] : locale.t(:body)
73
+ error_messages = objects.sum {|object| object.errors.full_messages.map {|msg| content_tag(:li, ERB::Util.html_escape(msg)) } }.join.html_safe
74
+
75
+ contents = ''
76
+ contents << content_tag(options[:header_tag] || :h2, header_message) unless header_message.blank?
77
+ contents << content_tag(:p, message) unless message.blank?
78
+ contents << content_tag(:ul, error_messages)
79
+
80
+ content_tag(:div, contents.html_safe, html)
81
+ end
82
+ else
83
+ ''
84
+ end
85
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: errors_for
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Avner Cohen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-14 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: This is used to aid on migrations of projects from rails projects 2.x
15
+ to 3+ versions by providing a polyfill to the error_messages_for ActiveRecord helper.
16
+ email:
17
+ - israbirding@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/errors_for.rb
23
+ homepage: https://github.com/AvnerCohen/errors_for
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Replacment for Rails 2.x error_messages_for helper
47
+ test_files: []
48
+ has_rdoc: