Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from joecorcoran/confirmation-validation
Move confirmation validation to confirmation field
- Loading branch information
Showing
7 changed files
with
118 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Judge | ||
class ConfirmationValidator | ||
|
||
include Judge::EachValidator | ||
|
||
attr_reader :object, :method, :amv | ||
|
||
def initialize(object, method) | ||
@object = object | ||
@method = method | ||
@amv = amv_from_original | ||
end | ||
|
||
def kind | ||
@amv.kind if @amv.present? | ||
end | ||
|
||
def options | ||
@amv.options if @amv.present? | ||
end | ||
|
||
private | ||
|
||
def amv_from_original | ||
original_amv = nil | ||
original_method = method.to_s.gsub('_confirmation', '').to_sym | ||
object.class.validators_on(original_method).each do |v| | ||
original_amv = v if v.class.name['ConfirmationValidator'] | ||
end | ||
|
||
original_amv | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require "spec_helper" | ||
|
||
describe Judge::ConfirmationValidator do | ||
|
||
let :password_confirmation do | ||
user = FactoryGirl.build(:user) | ||
Judge::ConfirmationValidator.new(user, :password_confirmation) | ||
end | ||
|
||
describe '#amv' do | ||
it "should return a ConfirmationValidator" do | ||
expect(password_confirmation.amv).to be_a(ActiveModel::Validations::ConfirmationValidator) | ||
end | ||
|
||
it "should be the ConfirmationValidator from :password" do | ||
expect(password_confirmation.amv.attributes).to include(:password) | ||
end | ||
end | ||
|
||
describe '#kind' do | ||
it "should return the the original amv's kind (:confiramtion)" do | ||
expect(password_confirmation.kind).to eq(:confirmation) | ||
expect(password_confirmation.kind).to eq(password_confirmation.amv.kind) | ||
end | ||
end | ||
|
||
describe '#options' do | ||
it "should return the original amv's options (an empty hash)" do | ||
expect(password_confirmation.options).to eq({}) | ||
expect(password_confirmation.options).to eq(password_confirmation.amv.options) | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters