model_error_messages 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b9c90e564f39abb63f5c6832858e7f6962457a3c
4
- data.tar.gz: c8b6625297972e405092859761cfb85df5a84a33
3
+ metadata.gz: 3346cf1d3cc91ec5fa3a8befc871931b1087b33a
4
+ data.tar.gz: cf709e9fad5263088ae0e3ffba6bac1a8fde6439
5
5
  SHA512:
6
- metadata.gz: f11e4261a820cb5b2bb25062ab731637b4e2b11b206faae1b2deaddd35d02be8066d8ede47b7b0dea1d92fa9e9c4ba57bdd03ac65a5f1999b02f55cc7b4dfde2
7
- data.tar.gz: c3834ad68db48d76afc1d1234aed750fd90304b97986065a649d95dda4e2f060bb63dd5808762b6fda9da378165234b7dbf79d5c3df2ac96b21763bfb67e3e5c
6
+ metadata.gz: 0d33d97a77439dd700d9afdf0e4ab471dd9f7f4c1ee20bf3c00cc9ae880a488d648c68bb37dd3fd0fc308126ae83415e7c40e9d953c3c3eb30320fb3aecbbbf5
7
+ data.tar.gz: 1ad227a9061979297a003d05ec3a8de746b5e000a41a887c7cf062b732117a4431811558a05ba1e1532715f7eb2ce39cfbc51d8223e9716c11f50b9f714742d2
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- model_error_messages (1.0.1)
4
+ model_error_messages (1.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  [![Build Status](https://secure.travis-ci.org/christophemaximin/model_error_messages.png)](https://travis-ci.org/christophemaximin/model_error_messages)
4
4
  [![Gem version](https://badge.fury.io/rb/model_error_messages.png)](https://rubygems.org/gems/model_error_messages)
5
+ [![Code Climate](https://codeclimate.com/github/christophemaximin/model_error_messages/badges/gpa.svg)](https://codeclimate.com/github/christophemaximin/model_error_messages)
5
6
 
6
7
  A simple Rails helper which displays a HTML div with the validation errors attached to a model.
7
8
 
@@ -16,6 +17,7 @@ gem 'model_error_messages'
16
17
  ## What this gem does / How to use
17
18
 
18
19
  This gem allows you to use a helper called `model_error_messages`.
20
+ By default, the wrapper `div` has the [Bootstrap](http://getbootstrap.com)-friendly classes `alert alert-danger`.
19
21
  If you have a typical form, you would want to display `model_error_messages(@model)` next to it, which will render something like:
20
22
 
21
23
  ```html
data/Rakefile CHANGED
@@ -2,4 +2,4 @@ require 'rspec/core/rake_task'
2
2
  RSpec::Core::RakeTask.new('spec')
3
3
 
4
4
  desc 'Run tests'
5
- task :default => [:spec]
5
+ task default: [:spec]
@@ -1,29 +1,31 @@
1
+ require 'cgi'
2
+
1
3
  module ModelErrorMessages
2
4
  module Helpers
3
- include ActionView::Helpers::TagHelper
4
-
5
5
  def model_error_messages(model, options = {})
6
6
  return '' if model.nil?
7
7
  return '' if model.errors.full_messages.empty?
8
- config = local_config(options)
9
- errors_wrapper(model, config)
10
- end
11
8
 
12
- private
9
+ private_helpers = ModelErrorMessages::PrivateHelpers
10
+ config = private_helpers.local_config(options)
11
+ private_helpers.errors_wrapper(model, config)
12
+ end
13
+ end
13
14
 
14
- def errors_wrapper(model, config)
15
+ module PrivateHelpers
16
+ def self.errors_wrapper(model, config)
15
17
  class_attr = wrapper_class_attr(model, config)
16
18
 
17
- content_tag(:div, class: class_attr) do
18
- [
19
- config.prepend_html,
20
- errors_list(model, config),
21
- config.append_html
22
- ].join.html_safe
23
- end
19
+ div_string = [
20
+ config.prepend_html,
21
+ errors_list(model, config),
22
+ config.append_html
23
+ ].join.html_safe
24
+
25
+ tag(:div, div_string, class: class_attr)
24
26
  end
25
27
 
26
- def wrapper_class_attr(model, config)
28
+ def self.wrapper_class_attr(model, config)
27
29
  if config.classes.is_a?(Proc)
28
30
  config.classes.call(model)
29
31
  else
@@ -31,26 +33,36 @@ module ModelErrorMessages
31
33
  end
32
34
  end
33
35
 
34
- def errors_list(model, config)
36
+ def self.errors_list(model, config)
35
37
  messages = model.errors.full_messages
36
38
 
37
39
  if messages.count == 1 && config.single_error_in_paragraph
38
- return content_tag(:p, messages.first)
40
+ return tag(:p, messages.first)
39
41
  end
40
42
 
41
- content_tag(:ul) do
42
- messages.map do |message|
43
- content_tag(:li, message)
44
- end.join.html_safe
45
- end
43
+ ul_string = messages.map do |message|
44
+ tag(:li, message)
45
+ end.join.html_safe
46
+
47
+ tag(:ul, ul_string)
46
48
  end
47
49
 
48
- def local_config(options)
50
+ def self.local_config(options)
49
51
  config = ModelErrorMessages.configuration.clone
50
52
  options.each_pair do |k, v|
51
53
  config.send(k.to_s + '=', v)
52
54
  end
53
55
  config
54
56
  end
57
+
58
+ def self.tag(name, value, attributes = {})
59
+ string_attributes = attributes.inject('') do |attrs, pair|
60
+ unless pair.last.nil?
61
+ attrs << %( #{pair.first}="#{CGI.escapeHTML(pair.last.to_s)}")
62
+ end
63
+ attrs
64
+ end
65
+ "<#{name}#{string_attributes}>#{value}</#{name}>"
66
+ end
55
67
  end
56
68
  end
@@ -2,7 +2,7 @@ module ModelErrorMessages
2
2
  module VERSION
3
3
  MAJOR = 1
4
4
  MINOR = 0
5
- TINY = 1
5
+ TINY = 2
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].compact.join('.')
8
8
  end
@@ -69,8 +69,8 @@ describe 'Helpers' do
69
69
  let(:result) { model_error_messages(model, options) }
70
70
 
71
71
  before do
72
- configuration.prepend_html = content_tag(:h1, 'An error occurred')
73
- configuration.append_html = content_tag(:p, 'Please try again.')
72
+ configuration.prepend_html = '<h1>An error occurred</h1>'
73
+ configuration.append_html = '<p>Please try again.</p>'
74
74
  configuration.classes = model.class.model_name.param_key + '-foo bar'
75
75
  end
76
76
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_error_messages
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christophe Maximin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-25 00:00:00.000000000 Z
11
+ date: 2016-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake