combined_errors 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in combine_errors.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mark Sim
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Combined Errors
2
+
3
+ Sometimes you want to use SimpleForm and have 2 or 3 inputs on the same line. Like with an address.
4
+
5
+ = simple_form_for @profile do |f|
6
+ .form-inputs
7
+ = f.input :name
8
+ = f.input :address_line_1
9
+ = f.input :address_line_2
10
+ = f.input :city_state_zip do
11
+ = f.input_field :city, class: 'input-medium'
12
+ = f.input_field :state, collection: State.all
13
+ = f.input_field :zip, class: 'input-small'
14
+
15
+
16
+ But it would be nice if the errors still stacked up nicely. That's where Combined Errors comes in:
17
+
18
+ class Profile < ActiveRecord::Base
19
+ # ...
20
+ validates :city, presence: true
21
+ validates :state, presence: true
22
+ validates :zip, presence: true
23
+
24
+ combine_errors :city_state_zip, [:city, :state, :zip]
25
+ end
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application's Gemfile:
30
+
31
+ gem 'combined_errors'
32
+
33
+ And then execute:
34
+
35
+ $ bundle
36
+
37
+ Or install it yourself as:
38
+
39
+ $ gem install combined_errors
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'combined_errors/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "combined_errors"
8
+ spec.version = CombinedErrors::VERSION
9
+ spec.authors = ["Mark Sim"]
10
+ spec.email = ["mark@blog.quarternotecoda.com"]
11
+ spec.description = %q{Combine Model errors into a new single property for use with gems like SimpleForm}
12
+ spec.summary = %q{Combine Model errors into a new single property}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,3 @@
1
+ module CombinedErrors
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ require "combined_errors/version"
2
+
3
+ module CombinedErrors
4
+ module Model
5
+ def combine_errors new_method, *methods_to_combine
6
+ define_method new_method do
7
+ methods_to_combine.flatten.each do |m|
8
+ if errors[m].any?
9
+ title = m.to_s.respond_to?(:titleize) ? m.titleize : m.capitalize
10
+ errors[m].each do |error|
11
+ errors.add(new_method, "#{title} #{error}")
12
+ end
13
+ end
14
+ end
15
+ nil
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ if defined?(ActiveRecord)
22
+ ActiveRecord::Base.extend CombinedErrors::Model
23
+ end
@@ -0,0 +1,35 @@
1
+ require 'rspec'
2
+ require './lib/combined_errors'
3
+
4
+ class TestModel
5
+ extend CombinedErrors::Model
6
+ attr_accessor :property1, :property2
7
+ attr_reader :errors
8
+ def initialize
9
+ @errors = { :property1 => ["is required"],
10
+ :property2 => ["is hard", "is empty"] }
11
+ def @errors.add(key, value)
12
+ self[key] ||= []
13
+ self[key] << value
14
+ end
15
+ end
16
+ end
17
+
18
+ describe CombinedErrors::Model do
19
+ context "combine_errors" do
20
+ class TestModel
21
+ combine_errors :two_properties, :property1, :property2
22
+ end
23
+ let(:model) { TestModel.new }
24
+ it "defines a new method on the model" do
25
+ model.should respond_to :two_properties
26
+ end
27
+
28
+ it "gets the errors from property1 and property2" do
29
+ model.two_properties
30
+ model.errors[:two_properties].should == [
31
+ "Property1 is required", "Property2 is hard", "Property2 is empty"
32
+ ]
33
+ end
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: combined_errors
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mark Sim
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Combine Model errors into a new single property for use with gems like
63
+ SimpleForm
64
+ email:
65
+ - mark@blog.quarternotecoda.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - combined_errors.gemspec
76
+ - lib/combined_errors.rb
77
+ - lib/combined_errors/version.rb
78
+ - spec/combined_errors_spec.rb
79
+ homepage: ''
80
+ licenses:
81
+ - MIT
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ segments:
93
+ - 0
94
+ hash: -2086557416837512409
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ segments:
102
+ - 0
103
+ hash: -2086557416837512409
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.25
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Combine Model errors into a new single property
110
+ test_files:
111
+ - spec/combined_errors_spec.rb