Giter Club home page Giter Club logo

chilean-rutify's Introduction

Chilean::Rutify

reviewdog rspec

Chilean Rutify is a Ruby on Rails gem that makes rut validations and manipulation easy

Installation

Add this line to your application's Gemfile:

gem 'chilean-rutify'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install chilean-rutify

Usage

You can use the available utility methods directly form the module:

    # using an example rut '36.408.368-8'
    rut = '36408368-8'
    Chilean::Rutify.normalize_rut(rut)
    # => "364083688"
    Chilean::Rutify.format_rut(rut) # alias Chilean::Rutify.classic_rut(rut)
    # => "36.408.368-8"
    Chilean::Rutify.format_rut(rut, :dash_only) # alias Chilean::Rutify.dash_only_rut(rut)
    # => "36408368-8"
    Chilean::Rutify.format_rut(rut, :normalized) # alias Chilean::Rutify.normalize_rut(rut)
    # => "364083688"
    Chilean::Rutify.valid_rut?(rut)
    # => true
    Chilean::Rutify.valid_rut_verifier?(rut)
    # => true
    Chilean::Rutify.valid_rut_values?(rut)
    # => true
    rut = "36408368"
    Chilean::Rutify.get_verifier(rut)
    # => "8"

Validator

You can also use the available validator as any other custom Rails validator:

class Person < ActiveRecord::Base
    validates :rut, presence: true, rut: true
end

Concern

Or include the new Rutifiable concern (it asumes you created a rut column):

class Person < ActiveRecord::Base
    includes Chilean::Rutifiable
    self.rut_format = :classic #(optional) options: [:classic, :normalized, :dash_only, :nil] (nil = no override, default: :classic)
end

the last one is equivalent to:

class Person < ActiveRecord::Base
    validates :rut, presence: true, uniqueness: { case_sensitive: false }, rut: true

    def rut=(value)
        value = Chilean::Rutify.format_rut(value)
        super(value)
    end
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/mihailpozarski/chilean-rutify. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Chilean::Rutify project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

chilean-rutify's People

Contributors

mihailpozarski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

chilean-rutify's Issues

Validation fails after formatting

Describe the bug
The validation fails after formatting a rut using the rutifier.

To Reproduce
Steps to reproduce the behavior:
Let's say you have a model with the following attributes: name, rut, commune.

class Person < ApplicationRecord
  before_validation :format_rut, on: %i[create update]

  validates :name, presence: true
  validates :rut, presence: true, rut: true
  validates :commune, presence: true

  private

  def format_rut
    self.rut = Chilean::Rutify.format_rut(rut)
  end
end

Then I go to the console and type:

p = Person.new(name: 'Bob, rut: '45235220', commune: 'Pica')
p.save! 
ActiveRecord::RecordInvalid: La validación falló: RUT is not a valid rut
p.rut
=> "4.523.522-0"

So the rut has been formatted correctly, but the validation fails for some reason.

Expected behavior

A rut should be valid after is formatted.

Desktop (please complete the following information):

  • OS: iOS
  • Browser: Firefox (doesn't really matter though)

Additional context
Thanks for this gem! It's a very useful module except for this weird behavior.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.