-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edae528
commit d5f8751
Showing
24 changed files
with
283 additions
and
196 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require: rubocop-rspec | ||
|
||
Metrics/BlockLength: | ||
Enabled: false | ||
|
||
Metrics/LineLength: | ||
Max: 125 | ||
|
||
RSpec/DescribedClass: | ||
EnforcedStyle: explicit | ||
|
||
Style/Documentation: | ||
Enabled: false |
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 |
---|---|---|
@@ -1 +1 @@ | ||
1.9.3 | ||
2.2 |
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 |
---|---|---|
@@ -1,17 +1,11 @@ | ||
rvm: | ||
- 1.8.7 | ||
- 1.9.2 | ||
- 1.9.3 | ||
- jruby | ||
- ree | ||
- 2.2 | ||
- 2.3 | ||
- 2.4 | ||
- 2.5 | ||
gemfile: | ||
- gemfiles/Gemfile.rails3 | ||
- gemfiles/Gemfile.rails4 | ||
matrix: | ||
exclude: | ||
- rvm: 1.8.7 | ||
gemfile: gemfiles/Gemfile.rails4 | ||
- rvm: 1.9.2 | ||
gemfile: gemfiles/Gemfile.rails4 | ||
- rvm: ree | ||
gemfile: gemfiles/Gemfile.rails4 | ||
- gemfiles/Gemfile.rails5 | ||
before_script: | ||
- bundle exec rubocop |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
source "http://rubygems.org" | ||
# frozen_string_literal: true | ||
|
||
source 'http://rubygems.org' | ||
|
||
gemspec |
File renamed without changes.
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
# frozen_string_literal: true | ||
|
||
require 'bundler/gem_tasks' | ||
require 'rspec/core/rake_task' | ||
|
||
RSpec::Core::RakeTask.new | ||
|
||
desc "Default Task" | ||
task :default => [ :spec ] | ||
desc 'Default Task' | ||
task default: [:spec] |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
source :rubygems | ||
|
||
gem 'activesupport', '3.2.14' | ||
gem 'activesupport', '< 4.0.0' | ||
gem 'mime-types', '< 2.0' | ||
gem 'test-unit', '~> 3.0' | ||
|
||
gemspec :path => '../' |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
source :rubygems | ||
|
||
gem 'activesupport', '>= 4.0.0' | ||
gem 'activesupport', '>= 4.0.0', '< 5.0.0' | ||
|
||
gemspec :path => '../' |
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,5 @@ | ||
source :rubygems | ||
|
||
gem 'activesupport', '>= 5.0.0' | ||
|
||
gemspec :path => '../' |
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 |
---|---|---|
@@ -1,5 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
class CpfValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
record.errors.add(attribute, :invalid, options) unless ValidatesCpf::Cpf.new(value).valid? | ||
cpf = ValidatesCpf::Cpf.new(value) | ||
|
||
if cpf.valid? | ||
record.send("#{attribute}=", cpf.number) if options[:mask] | ||
else | ||
record.errors.add(attribute, :invalid, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module ValidatesCpf | ||
VERSION = '2.0.1' | ||
VERSION = '2.0.1'.freeze | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe CpfValidator do | ||
let(:user) { User.new } | ||
|
||
context 'with invalid cpf' do | ||
before do | ||
user.cpf = '12345' | ||
I18n.stub(:t).with(:'activerecord.errors.models.user.attributes.cpf.invalid', | ||
default: :'activerecord.errors.messages.invalid').and_return('is invalid') | ||
user.valid? | ||
end | ||
|
||
it 'invalidates the object' do | ||
expect(user).not_to be_valid | ||
end | ||
|
||
it 'sets an error message' do | ||
expect(user.errors[:cpf]).to match(['is invalid']) | ||
end | ||
end | ||
|
||
context 'with valid cpf' do | ||
before do | ||
user.cpf = '11144477735' | ||
user.valid? | ||
end | ||
|
||
it 'validates the object' do | ||
expect(user).to be_valid | ||
end | ||
|
||
it 'does not set an error message' do | ||
expect(user.errors[:cpf]).to be_blank | ||
end | ||
end | ||
|
||
context 'with nil value' do | ||
before { user.cpf = nil } | ||
|
||
it 'validates the object' do | ||
expect(user).to be_valid | ||
end | ||
|
||
it 'does not set an error message' do | ||
expect(user.errors[:cpf]).to be_blank | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,15 +1,10 @@ | ||
class User | ||
include ActiveModel::Validations | ||
include ActiveModel::Conversion | ||
extend ActiveModel::Naming | ||
# frozen_string_literal: true | ||
|
||
attr_accessor :cpf, :name | ||
class User | ||
include ActiveModel::Model | ||
|
||
validates :cpf, :cpf => true | ||
attr_accessor :cpf, :masked_cpf, :name | ||
|
||
def initialize(attributes = {}) | ||
attributes.each do |key, value| | ||
instance_variable_set("@#{key}", value) | ||
end | ||
end | ||
end | ||
validates :cpf, cpf: true | ||
validates :masked_cpf, cpf: { mask: true } | ||
end |
11 changes: 11 additions & 0 deletions
11
spec/shoulda/matchers/active_model/require_a_valid_cpf_matcher_spec.rb
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe Shoulda::Matchers::ActiveModel::RequireAValidCpfMatcher do | ||
subject { User.new } | ||
|
||
it { is_expected.to require_a_valid_cpf(:cpf) } | ||
it { is_expected.to require_a_valid_cpf } | ||
it { is_expected.not_to require_a_valid_cpf(:name) } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rspec' | ||
require 'active_model' | ||
require 'coveralls' | ||
require 'jazz_fingers' | ||
require 'shoulda-matchers' | ||
|
||
JazzFingers.configure do |config| | ||
config.colored_prompt = false | ||
config.awesome_print = false | ||
config.coolline = false | ||
config.application_name = ValidatesCpf | ||
end | ||
|
||
require 'jazz_fingers/setup' | ||
|
||
RSpec.configure do |config| | ||
config.include Shoulda::Matchers::ActiveModel | ||
end | ||
|
||
Coveralls.wear! | ||
|
||
Dir.glob(File.dirname(__FILE__) + '/../lib/**/*.rb').each { |file| require file } | ||
Dir.glob(File.dirname(__FILE__) + '/fake_app/*.rb').each { |file| require file } | ||
require File.expand_path('lib/validates_cpf') | ||
require File.expand_path('spec/fake_app/user') |
Oops, something went wrong.