destination_errors 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5280d933863a238d0409338078c1fa204a923bf8
4
+ data.tar.gz: d3691b9b111ce18e77369b01308a47abd6e60ec2
5
+ SHA512:
6
+ metadata.gz: 44f7f644acb1f91036a78c2df4255d2c6e140fc0dfec5de4afdae6964ee2640b7501ab891caf70933c6eecee59df0ee548206f2207379adf77633c12a88666e9
7
+ data.tar.gz: 88353011ba8bcd310f2ffdfaf7ff45bdcc86d3b280aa172041f315bd31a799795702be8e81caf78cde0c00d8a5834a303d103ecc52588f7ec709b06405615163
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ - 2.2.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in destination_errors.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Trumaker, Inc & Peter Boling
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,82 @@
1
+ # DestinationErrors
2
+
3
+ Allows you to create a class that has multiple error surfaces registered but stays within the familiar territory of `ActiveRecord::Validations`.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'destination_errors'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install destination_errors
20
+
21
+ ## Usage
22
+
23
+ Here is a contrived example.
24
+
25
+ You have three models, and a form that interacts with all of them:
26
+ ```
27
+ class User
28
+ has_one :profile
29
+ has_one :account
30
+ end
31
+
32
+ class Profile
33
+ belongs_to :user
34
+ end
35
+
36
+ class Account
37
+ belongs_to :user
38
+ end
39
+ ```
40
+
41
+ So you create an admin user form presenter class to handle everything, and you want it to be *railsy*.
42
+ ```
43
+ class AdminUserFormPresenter
44
+
45
+ include DestinationErrors
46
+
47
+ attr_accessor :user, :profile, :account
48
+ has_error_surfaces [nil, :user, :profile, :account]
49
+
50
+ def initialize(*args)
51
+ @surface_errors_on = nil # nil means errors will be moved onto this instance.
52
+ end
53
+
54
+ end
55
+ ```
56
+
57
+ For more example usage see the specs.
58
+
59
+
60
+ ## Development
61
+
62
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
63
+
64
+ To install this gem onto your local machine, run `bundle exec rake install`.
65
+
66
+ ## Maintenance
67
+
68
+ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it ( https://github.com/[my-github-username]/destination_errors/fork )
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Make sure to add tests!
77
+ 6. Create a new Pull Request
78
+
79
+ ## Contributors
80
+
81
+ See the [Network View](https://github.com/trumaker/destination_errors/network)
82
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "destination_errors"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'destination_errors/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "destination_errors"
8
+ spec.version = DestinationErrors::VERSION
9
+ spec.authors = ["Peter Boling"]
10
+ spec.email = ["peter.boling@gmail.com"]
11
+
12
+ spec.summary = %q{Mixin providing management of error surfaces within the familiar territory of ActiveModel}
13
+ spec.description = %q{Useful when a presenter deals with multiple objects that may enter into error states, and the errors need to be collected at a single point.}
14
+ spec.homepage = "https://github.com/trumaker/destination_errors"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "activemodel", "~> 3.1"
23
+ spec.add_development_dependency "bundler", "~> 1.9"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.2"
26
+ spec.add_development_dependency "pry", "~> 0.10"
27
+ end
@@ -0,0 +1,158 @@
1
+ require "destination_errors/version"
2
+
3
+ #
4
+ # There are three steps to implementing this module in a class:
5
+ #
6
+ # Setup 1: include DestinationErrors and set error_surfaces
7
+ #
8
+ # include DestinationErrors
9
+ # # Usage: set explicitly in each class
10
+ # # individual error surfaces can be nil, it's safe.
11
+ # has_error_surfaces [nil, :lead, :user]
12
+ #
13
+ # # a simple default with only one surface, nil, where the errors
14
+ # # accumulate on the object including this module would be:
15
+ # # has_error_surfaces [nil]
16
+ #
17
+ # Setup 2: (optional)
18
+ #
19
+ # def initialize
20
+ # # choose one of the surfaces to aggregate errors onto, with nil indicating self.
21
+ # @surface_errors_on = nil
22
+ # end
23
+ #
24
+ # Setup 3: call move_all_errors_to_destination after errors may exist on the error_surfaces
25
+ #
26
+ # def finalize
27
+ # move_all_errors_to_destination
28
+ # self # if you want chainability return self
29
+ # end
30
+ #
31
+
32
+ require "active_model"
33
+
34
+ module DestinationErrors
35
+
36
+ def self.included(base)
37
+ base.include(ActiveModel::Validations)
38
+ base.prepend(Initializer)
39
+ base.extend(ClassMethods)
40
+ base.class_eval do
41
+ attr_reader :errors
42
+ attr_reader :errors_finalized
43
+ attr_accessor :surface_errors_on
44
+ class_attribute :error_surfaces
45
+ end
46
+ end
47
+
48
+ module Initializer
49
+ def initialize(*args)
50
+ @errors = ActiveModel::Errors.new(self)
51
+ @surface_errors_on = nil
52
+ super
53
+ end
54
+ end
55
+
56
+ # Checks to see if any errors have been registered on any of the error surfaces but:
57
+ # 1. does not re-run validations
58
+ # 2. does not add or move errors
59
+ # returns true if any errors are found on any surface or false otherwise
60
+ def error_surfaces_clean?
61
+ return false if self.errors.any?
62
+ self.class.error_surfaces.compact.each do |surface|
63
+ return false if errors_on_surface?(surface)
64
+ end
65
+ return false if custom_error_destination_has_errors?
66
+ return true
67
+ end
68
+
69
+ # Required for ActiveModel::Validations
70
+ def read_attribute_for_validation(attr)
71
+ send(attr)
72
+ end
73
+
74
+ # dynamically access the surface where errors are being aggregated
75
+ def error_destination
76
+ @error_destination = error_destination_is_self? ?
77
+ self :
78
+ self.send(self.surface_errors_on)
79
+ end
80
+
81
+ module ClassMethods
82
+ # Implementation hook
83
+ def has_error_surfaces(value)
84
+ if value.length == 1 && value.first.nil?
85
+ warn "#{self}: error_surfaces might not be configured"
86
+ end
87
+ self.error_surfaces = value
88
+ end
89
+
90
+ # Required for ActiveModel::Validations
91
+ def human_attribute_name(attr, options = {})
92
+ attr
93
+ end
94
+
95
+ # Required for ActiveModel::Validations
96
+ def lookup_ancestors
97
+ [self]
98
+ end
99
+ end
100
+
101
+ protected
102
+
103
+ # The error destination is not one of error_surfaces, and is not self, and has errors...
104
+ def custom_error_destination_has_errors?
105
+ !self.class.error_surfaces.include?(surface_errors_on) &&
106
+ !error_destination_is_self? &&
107
+ errors_on_surface?(surface_errors_on)
108
+ end
109
+
110
+ def move_all_errors_to_destination
111
+ return false if self.errors_finalized
112
+ self.error_surfaces.each do |surface|
113
+ move_errors_from_surface_to_destination_if_needed(surface)
114
+ end
115
+ self.errors_finalized = true
116
+ end
117
+
118
+ def move_errors_from_surface_to_destination_if_needed(surface)
119
+ if move_errors_from_surface?(surface)
120
+ (
121
+ surface.nil? ?
122
+ errors.full_messages :
123
+ self.send(surface).errors.full_messages
124
+ ).each do |message|
125
+ move_error_to_destination(message)
126
+ end
127
+ end
128
+ end
129
+
130
+ def move_errors_from_surface?(surface)
131
+ if surface.nil?
132
+ !error_destination_is_self? && errors && errors.any?
133
+ else
134
+ (surface_errors_on.to_s != surface.to_s) && errors_on_surface?(surface)
135
+ end
136
+ end
137
+
138
+ def errors_on_surface?(surface)
139
+ self.send(surface) && self.send(surface).errors.any?
140
+ end
141
+
142
+ def error_destination
143
+ @error_destination = error_destination_is_self? ?
144
+ self :
145
+ self.send(surface_errors_on)
146
+ end
147
+
148
+ def error_destination_is_self?
149
+ surface_errors_on.nil? || !self.send(surface_errors_on)
150
+ end
151
+
152
+ def move_error_to_destination(message)
153
+ if error_destination
154
+ error_destination.errors.add(:base, message)
155
+ end
156
+ end
157
+
158
+ end
@@ -0,0 +1,3 @@
1
+ module DestinationErrors
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: destination_errors
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Boling
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ description: Useful when a presenter deals with multiple objects that may enter into
84
+ error states, and the errors need to be collected at a single point.
85
+ email:
86
+ - peter.boling@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - destination_errors.gemspec
102
+ - lib/destination_errors.rb
103
+ - lib/destination_errors/version.rb
104
+ homepage: https://github.com/trumaker/destination_errors
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.4.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Mixin providing management of error surfaces within the familiar territory
128
+ of ActiveModel
129
+ test_files: []