controller_validator 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: 5dfefb1a267f528e15e8f6569b77762f121b7f55
4
+ data.tar.gz: 9543cc2bf48569c383cb052b964d501edabc3779
5
+ SHA512:
6
+ metadata.gz: 326974179e23e24d9111defd871b5121bd5d48be6462e47f63ba78e261857411815b27e8393965f1ac0988b7b8966d55ef44ec5fc68dad3938229a9d864bc84c
7
+ data.tar.gz: 942e414823657d5ff40141fb7c23d162ad6c6b86505ee66fa82572d97b00c21a8fade932549f531754a11fe4950cdc4f4a1ac56c430ed0b6736cbc9fe85ba3f8
@@ -0,0 +1,18 @@
1
+ /.idea
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
16
+ *.sw?
17
+ *.gem
18
+ *.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in controller_validator.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Peter Boling
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.
@@ -0,0 +1,79 @@
1
+ # ControllerValidator
2
+
3
+ Simple Validations in the Controller!
4
+ (Re)Use the familiar ActiveModel::Errors pattern for controller validations, so you already know how this works.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'controller_validator'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install controller_validator
21
+
22
+ ## Usage
23
+
24
+ See the integration specs!
25
+
26
+ ```ruby
27
+ class ThingController < ApplicationController
28
+
29
+ def create
30
+
31
+ @thing = Thing.new
32
+
33
+ validator = ValidateThingOnCreate.new()
34
+ validator.validate_and_push_errors_to(instance: @thing)
35
+
36
+ # So far save has not been called on @thing, no callbacks have been run, the model is not involved at all
37
+
38
+ if @thing.errors.any?
39
+ render :new
40
+ else
41
+ if @thing.save
42
+ redirect_to @thing_path(id: @thing.id)
43
+ else
44
+ render :new
45
+ end
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ class ValidateThingOnCreate < ControllerValidator::Validator
52
+
53
+ # Rules for creating a new thing
54
+ # 1. There must not be too many things already
55
+ # 2. There must be no Bobs
56
+
57
+ attr_accessor :too_many, :no_bobs
58
+
59
+ def initialize()
60
+ @too_many = Thing.count > 100
61
+ @no_bobs = Bob.count == 0
62
+ end
63
+
64
+ def validate!
65
+ errors.add(:too_many, "must not be true") if !self.too_many
66
+ errors.add(:no_bobs, "must be true") if !self.no_bobs
67
+ errors.blank?
68
+ end
69
+
70
+ end
71
+ ```
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it ( https://github.com/[my-github-username]/controller_validator/fork )
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'controller_validator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "controller_validator"
8
+ spec.version = ControllerValidator::VERSION
9
+ spec.authors = ["Peter Boling"]
10
+ spec.email = ["peter.boling@gmail.com"]
11
+ spec.summary = %q{Simple Validations in the Controller}
12
+ spec.description = %q{Use the familiar ActiveModel::Errors pattern for controller validations}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3"
24
+ spec.add_development_dependency "reek"
25
+ spec.add_development_dependency "roodi"
26
+
27
+ spec.add_dependency "activemodel", ">= 3"
28
+ end
@@ -0,0 +1,6 @@
1
+ require 'controller_validator/version'
2
+ require 'controller_validator/validator'
3
+
4
+ module ControllerValidator
5
+
6
+ end
@@ -0,0 +1,42 @@
1
+ require 'active_model'
2
+
3
+ # Nearly entirely lifted from the example code in the Rails Documentation.
4
+ # See: http://www.rubydoc.info/docs/rails/ActiveModel/Errors
5
+ class ControllerValidator::Validator
6
+
7
+ include ::ActiveModel::Validations
8
+
9
+ attr_reader :errors
10
+
11
+ def initialize(*args)
12
+ @errors = ::ActiveModel::Errors.new(self)
13
+ end
14
+
15
+ def validate_and_push_errors_to(instance:)
16
+ unless self.validate!
17
+ self.errors.full_messages.each do |message|
18
+ instance.errors.add(:base, message)
19
+ end
20
+ end
21
+ end
22
+
23
+ def validate
24
+ raise RuntimeError, 'Must be defined in subclass of ControllerValidator'
25
+ end
26
+
27
+ # The following methods are needed to be minimally implemented
28
+
29
+ def read_attribute_for_validation(attr)
30
+ send(attr)
31
+ end
32
+
33
+ def self.human_attribute_name(attr, options = {})
34
+ attr.to_s.humanize
35
+ end
36
+
37
+ def self.lookup_ancestors
38
+ [self]
39
+ end
40
+
41
+ end
42
+
@@ -0,0 +1,3 @@
1
+ module ControllerValidator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe ControllerValidator::Validator, :classes do
4
+ describe 'class methods' do
5
+
6
+ describe '.human_attribute_name' do
7
+ describe 'with required params' do
8
+ it 'should not raise error' do
9
+ expect { ControllerValidator::Validator.human_attribute_name(:thing) }.to_not raise_error
10
+ end
11
+ it 'should be empty string' do
12
+ expect( ControllerValidator::Validator.human_attribute_name(:thing) ).to eq 'Thing'
13
+ end
14
+ end
15
+
16
+ describe 'with optional params' do
17
+ it 'should not raise error' do
18
+ expect { ControllerValidator::Validator.human_attribute_name(:thing, {}) }.to_not raise_error
19
+ end
20
+ it 'should be empty string' do
21
+ expect( ControllerValidator::Validator.human_attribute_name(:thing, {}) ).to eq 'Thing'
22
+ end
23
+ end
24
+ end
25
+
26
+ describe '.lookup_ancestors' do
27
+ it 'should not raise error' do
28
+ expect { ControllerValidator::Validator.lookup_ancestors }.to_not raise_error
29
+ end
30
+ it 'should be empty string' do
31
+ expect( ControllerValidator::Validator.lookup_ancestors ).to eq [ControllerValidator::Validator]
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ describe 'instance methods' do
38
+ let(:controller_validator) { ControllerValidator::Validator.new }
39
+
40
+ describe '#validate_and_push_errors_to' do
41
+ let(:active_model_validation_actual) { ControllerValidator::Validator.new }
42
+ it 'should raise error' do
43
+ expect { controller_validator.validate_and_push_errors_to(instance: active_model_validation_actual) }.to raise_error
44
+ end
45
+ end
46
+
47
+ describe '#validate!' do
48
+ it 'should raise an error' do
49
+ expect { controller_validator.validate! }.to raise_error
50
+ end
51
+ end
52
+
53
+ describe '#read_attribute_for_validation' do
54
+ it 'should raise error for missing attribute' do
55
+ expect { controller_validator.read_attribute_for_validation(:thing) }.to raise_error
56
+ end
57
+ it 'should not raise error for actual attribute' do
58
+ expect { controller_validator.read_attribute_for_validation(:errors) }.to_not raise_error
59
+ end
60
+ it 'should be nil' do
61
+ expect( controller_validator.read_attribute_for_validation(:errors) ).to be_a ::ActiveModel::Errors
62
+ end
63
+ end
64
+ end
65
+
66
+ end
@@ -0,0 +1,56 @@
1
+ # Integration Spec
2
+
3
+ class MyControllerValidator < ControllerValidator::Validator
4
+ attr_accessor :thing
5
+ def initialize(thing:)
6
+ @thing = thing
7
+ super
8
+ end
9
+ def validate!
10
+ self.errors.add(:thing, 'monkeys are flying')
11
+ self.errors.add(:base, 'cheese so good')
12
+ self.errors.blank?
13
+ end
14
+ end
15
+
16
+ describe MyControllerValidator, :classes do
17
+
18
+ describe 'instance methods' do
19
+ let(:controller_validator) { MyControllerValidator.new(thing: 'hey') }
20
+
21
+ describe '.push_errors_to' do
22
+ let(:active_model_validation_actual) { ControllerValidator::Validator.new }
23
+ it 'should not raise error' do
24
+ # instance just needs to be something that implements ActiveModel::Validations
25
+ expect { controller_validator.validate_and_push_errors_to(instance: active_model_validation_actual) }.to_not raise_error
26
+ end
27
+ it 'should be empty string' do
28
+ expect( controller_validator.validate_and_push_errors_to(instance: active_model_validation_actual) ).to eq ['Thing monkeys are flying','cheese so good']
29
+ end
30
+ it 'should add errors to the instance' do
31
+ controller_validator.validate_and_push_errors_to(instance: active_model_validation_actual)
32
+ expect( active_model_validation_actual.errors.full_messages ).to eq ['Thing monkeys are flying','cheese so good']
33
+ end
34
+ end
35
+
36
+ describe '#validate!' do
37
+ it 'should not raise an error' do
38
+ expect { controller_validator.validate! }.to_not raise_error
39
+ end
40
+ it 'should be false' do
41
+ expect( controller_validator.validate! ).to eq false
42
+ end
43
+ end
44
+
45
+ describe '#read_attribute_for_validation' do
46
+ it 'should not raise error' do
47
+ expect { controller_validator.read_attribute_for_validation(:thing) }.to_not raise_error
48
+ end
49
+ it 'should be nil' do
50
+ expect( controller_validator.read_attribute_for_validation(:thing) ).to eq 'hey'
51
+ end
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1 @@
1
+ -x gems,spec\/
@@ -0,0 +1,95 @@
1
+ require 'controller_validator'
2
+
3
+ # TODO: Why this deprecation warning?
4
+ # [deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
5
+ I18n.enforce_available_locales = false
6
+
7
+ # This file was generated by the `rspec --init` command. Conventionally, all
8
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
9
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
10
+ # file to always be loaded, without a need to explicitly require it in any files.
11
+ #
12
+ # Given that it is always loaded, you are encouraged to keep this file as
13
+ # light-weight as possible. Requiring heavyweight dependencies from this file
14
+ # will add to the boot time of your test suite on EVERY test run, even for an
15
+ # individual file that may not need all of that loaded. Instead, consider making
16
+ # a separate helper file that requires the additional dependencies and performs
17
+ # the additional setup, and require it from the spec files that actually need it.
18
+ #
19
+ # The `.rspec` file also contains a few flags that are not defaults but that
20
+ # users commonly want.
21
+ #
22
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
23
+ RSpec.configure do |config|
24
+ # rspec-expectations config goes here. You can use an alternate
25
+ # assertion/expectation library such as wrong or the stdlib/minitest
26
+ # assertions if you prefer.
27
+ config.expect_with :rspec do |expectations|
28
+ # This option will default to `true` in RSpec 4. It makes the `description`
29
+ # and `failure_message` of custom matchers include text for helper methods
30
+ # defined using `chain`, e.g.:
31
+ # be_bigger_than(2).and_smaller_than(4).description
32
+ # # => "be bigger than 2 and smaller than 4"
33
+ # ...rather than:
34
+ # # => "be bigger than 2"
35
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
36
+ end
37
+
38
+ # rspec-mocks config goes here. You can use an alternate test double
39
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
40
+ config.mock_with :rspec do |mocks|
41
+ # Prevents you from mocking or stubbing a method that does not exist on
42
+ # a real object. This is generally recommended, and will default to
43
+ # `true` in RSpec 4.
44
+ mocks.verify_partial_doubles = true
45
+ end
46
+
47
+ # The settings below are suggested to provide a good initial experience
48
+ # with RSpec, but feel free to customize to your heart's content.
49
+ =begin
50
+ # These two settings work together to allow you to limit a spec run
51
+ # to individual examples or groups you care about by tagging them with
52
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
53
+ # get run.
54
+ config.filter_run :focus
55
+ config.run_all_when_everything_filtered = true
56
+
57
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
58
+ # For more details, see:
59
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
60
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
61
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
62
+ config.disable_monkey_patching!
63
+
64
+ # This setting enables warnings. It's recommended, but in some cases may
65
+ # be too noisy due to issues in dependencies.
66
+ config.warnings = true
67
+
68
+ # Many RSpec users commonly either run the entire suite or an individual
69
+ # file, and it's useful to allow more verbose output when running an
70
+ # individual spec file.
71
+ if config.files_to_run.one?
72
+ # Use the documentation formatter for detailed output,
73
+ # unless a formatter has already been configured
74
+ # (e.g. via a command-line flag).
75
+ config.default_formatter = 'doc'
76
+ end
77
+
78
+ # Print the 10 slowest examples and example groups at the
79
+ # end of the spec run, to help surface which specs are running
80
+ # particularly slow.
81
+ config.profile_examples = 10
82
+
83
+ # Run specs in random order to surface order dependencies. If you find an
84
+ # order dependency and want to debug it, you can fix the order by providing
85
+ # the seed, which is printed after each run.
86
+ # --seed 1234
87
+ config.order = :random
88
+
89
+ # Seed global randomization in this process using the `--seed` CLI option.
90
+ # Setting this allows you to use `--seed` to deterministically reproduce
91
+ # test failures related to randomization by passing the same `--seed` value
92
+ # as the one that triggered the failure.
93
+ Kernel.srand config.seed
94
+ =end
95
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: controller_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Boling
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: reek
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: roodi
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activemodel
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '3'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '3'
97
+ description: Use the familiar ActiveModel::Errors pattern for controller validations
98
+ email:
99
+ - peter.boling@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - controller_validator.gemspec
111
+ - lib/controller_validator.rb
112
+ - lib/controller_validator/validator.rb
113
+ - lib/controller_validator/version.rb
114
+ - spec/controller_validator/validator_spec.rb
115
+ - spec/integration/my_controller_validator_spec.rb
116
+ - spec/rcov.opts
117
+ - spec/spec_helper.rb
118
+ homepage: ''
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.4.2
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Simple Validations in the Controller
142
+ test_files:
143
+ - spec/controller_validator/validator_spec.rb
144
+ - spec/integration/my_controller_validator_spec.rb
145
+ - spec/rcov.opts
146
+ - spec/spec_helper.rb