also_validates 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,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'rspec'
7
+ gem 'debugger'
8
+ gem 'shoulda-matchers'
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Noah Davis
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,53 @@
1
+ # AlsoValidates
2
+
3
+ An ActiveModel validator that validates associated models, adding any errors on those models back onto the "primary" model.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'validation_aggregator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install validation_aggregator
18
+
19
+ ## Usage
20
+
21
+ class Beer
22
+ include ActiveModel::Validations
23
+
24
+ validate_presence_of :hops
25
+ end
26
+
27
+ class Belly
28
+ include ActiveModel::Validations
29
+
30
+ validate_presence_of :button
31
+ end
32
+
33
+ class MonsterTruckRally
34
+ include ActiveModel::Validations
35
+
36
+ attr_accessor :beer, :belly
37
+
38
+ validate_presence_of :truck_count
39
+ also_validate :beer, :belly
40
+
41
+ def initialize(beer, belly)
42
+ @beer = beer
43
+ @belly = belly
44
+ end
45
+ end
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new do |t|
6
+ t.rspec_opts == ["--color"]
7
+ end
8
+
9
+ desc "Run the specs"
10
+ task :default => ["spec"]
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/also_validates/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Noah Davis"]
6
+ gem.email = ["noahd1@yahoo.com"]
7
+ gem.description = %q{Validate associated models, and aggregate their errors onto the primary model}
8
+ gem.summary = %q{Aggregate errors onto primary model from associated models}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "also_validates"
15
+ gem.require_paths = ["lib"]
16
+ gem.add_dependency(%q<activemodel>, [">= 3.0.0"])
17
+ gem.version = AlsoValidates::VERSION
18
+ end
@@ -0,0 +1,8 @@
1
+ require "active_model"
2
+ require "also_validates/version"
3
+ require "also_validates/validation"
4
+ require "also_validates/helper"
5
+
6
+ ActiveModel::Validations::HelperMethods.module_eval do
7
+ include AlsoValidates::Helper
8
+ end
@@ -0,0 +1,7 @@
1
+ module AlsoValidates
2
+ module Helper
3
+ def also_validates(*models)
4
+ validates_with AlsoValidates::Validation, models: models
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ module AlsoValidates
2
+
3
+ class Validation < ActiveModel::Validator
4
+ def initialize(args)
5
+ options = args[:models].extract_options!
6
+ @options = options.merge(:models => args[:models])
7
+ end
8
+
9
+ def validate(record)
10
+ options[:models].each do |model|
11
+ model_instance = record.send(model)
12
+ next if options[:allow_nil] && model_instance.nil?
13
+ if !options[:allow_nil] && model_instance.nil?
14
+ record.errors.add(:base, model.to_s.classify.humanize + " can't be blank")
15
+ else
16
+ unless model_instance.valid?
17
+ model_instance.errors.full_messages.each do |error_message|
18
+ record.errors.add(:base, error_message)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,3 @@
1
+ module AlsoValidates
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ class Beer
2
+ include ActiveModel::Validations
3
+ attr_accessor :hops
4
+ validates_presence_of :hops
5
+
6
+ def initialize(attrs = {})
7
+ @hops = attrs[:hops]
8
+ end
9
+ end
10
+
11
+ class Belly
12
+ include ActiveModel::Validations
13
+ attr_accessor :button
14
+ validates_presence_of :button
15
+
16
+ def initialize(attrs = {})
17
+ @button = attrs[:button]
18
+ end
19
+ end
@@ -0,0 +1,67 @@
1
+ require "spec_helper"
2
+
3
+ class MonsterTruckRally
4
+ include ActiveModel::Validations
5
+
6
+ attr_accessor :beer, :belly, :truck_count
7
+
8
+ validates_presence_of :truck_count
9
+ also_validates :beer, :belly
10
+
11
+ def initialize(attrs = {})
12
+ @beer = attrs[:beer]
13
+ @belly = attrs[:belly]
14
+ @truck_count = attrs[:truck_count]
15
+ end
16
+ end
17
+
18
+ class NascarRace
19
+ include ActiveModel::Validations
20
+ attr_accessor :beer, :belly
21
+ also_validates :beer, :allow_nil => true
22
+ also_validates :belly
23
+
24
+ def initialize(attrs = {})
25
+ @beer = attrs[:beer]
26
+ @belly = attrs[:belly]
27
+ end
28
+ end
29
+
30
+ describe MonsterTruckRally do
31
+
32
+ it "adds all the errors together from other models" do
33
+ monster = MonsterTruckRally.new(beer: Beer.new, belly: Belly.new)
34
+ monster.valid?
35
+ expect(monster).to have(3).errors
36
+ expect(monster.errors.full_messages).to include(
37
+ %q(Truck count can't be blank)
38
+ )
39
+ expect(monster.errors.full_messages).to include(
40
+ %q(Hops can't be blank)
41
+ )
42
+ expect(monster.errors.full_messages).to include(
43
+ %q(Button can't be blank)
44
+ )
45
+ end
46
+
47
+ it "is valid if all the attributes are valid" do
48
+ beer = Beer.new(hops: true)
49
+ belly = Belly.new(button: "inny")
50
+ monster = MonsterTruckRally.new(beer: beer, belly: belly, truck_count: 4)
51
+ expect(monster).to be_valid
52
+ end
53
+
54
+ it "complains if the associated model is nil" do
55
+ race = NascarRace.new(beer: Beer.new(hops: true))
56
+ race.valid?
57
+ expect(race.errors.full_messages).to eq(["Belly can't be blank"])
58
+ end
59
+
60
+ it "does not complain if the associated model is allowed to be nil" do
61
+ race = NascarRace.new(belly: Belly.new(button: "outy"))
62
+ race.valid?
63
+ expect(race).to have(:no).errors
64
+ end
65
+
66
+
67
+ end
@@ -0,0 +1,12 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require "shoulda-matchers"
4
+
5
+ require "also_validates"
6
+ require "fixtures/object_fixtures"
7
+
8
+ RSpec.configure do |config|
9
+ config.expect_with :rspec do |c|
10
+ c.syntax = :expect
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: also_validates
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Noah Davis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ description: Validate associated models, and aggregate their errors onto the primary
31
+ model
32
+ email:
33
+ - noahd1@yahoo.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - Gemfile
40
+ - LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - also_validates.gemspec
44
+ - lib/also_validates.rb
45
+ - lib/also_validates/helper.rb
46
+ - lib/also_validates/validation.rb
47
+ - lib/also_validates/version.rb
48
+ - spec/fixtures/object_fixtures.rb
49
+ - spec/lib/also_validates_spec.rb
50
+ - spec/spec_helper.rb
51
+ homepage: ''
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.23
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Aggregate errors onto primary model from associated models
75
+ test_files:
76
+ - spec/fixtures/object_fixtures.rb
77
+ - spec/lib/also_validates_spec.rb
78
+ - spec/spec_helper.rb