Coder Social home page Coder Social logo

default_limit_attributes's Introduction

Default Limit Attributes

Giới thiệu

Thông thường, chúng ta thường chỉ quan tâm đến một số validates nhất định cho các attributes như presence, uniqueness, ... Hay đơn giản chỉ là xác định max length cho một số attributes như Số điện thoại kiểu:

validates :phone_number, length: {in: 10..11}

Không ít trong chúng ta không để ý tới việc viết validate max length cho các attributes khác như name, email, ... dẫn đến việc khi tester test nhập quá số lượng ký tự mà MySQL (hoặc RDMS nào đó) cho phép trên field type nó sẽ dẫn đến lỗi 500 (Internal server error - ảnh hưởng đến trải nghiệm của người dùng và làm rối log khi chúng ta muốn điều tra). Mà việc viết toàn bộ cho các attributes của project cũng không phải vui vẻ gì cả :D! Nên mình mới viết một gem đơn giản này.

Mục đích của gem này rất đơn giản. Là trước khi model thực hiện validate, nó sẽ lấy toàn bộ attributes mà model này có. Rồi kiểm tra xem attribute nào có kiểu dữ liệu là string (trong MySQL) và không có một trong các validates sau: maximum, in, is, within (tức là các attributes bị bỏ quên, dễ dẫn đến quả lỗi nhập quá ký tự) thì thực hiện thêm validate maximum bằng đúng giới hạn của field type đó trong MySQL.

Cài đặt và sử dụng

Bạn thêm đoạn sau vào Gemfile

gem 'default_limit_attributes'

Rồi thực hiện lệnh

bundle install

Để thực hiện cài đặt gem. Sau khi cài đặt hoàn thành, bạn có thể sử dụng theo hai cách dưới đây:

Cho toàn bộ models trong project

Bạn thêm dòng sau vào file app/models/application_record.rb

class ApplicationRecord < ActiveRecord::Base
  # ...
  default_limit_attributes
  # ...
end

Cho từng model mà bạn muốn

Bạn thêm dòng sau vào các models mà bạn muốn:

# app/models/model_name.rb
class ModelName < ApplicationRecord
  # ...
  default_limit_attributes
  # ...
end

Báo lỗi và Đóng góp

Trong quá trình sử dụng gem có phát sinh bug hay có ý tưởng đóng góp thêm hoặc để cải thiện gem thì mọi người vui lòng tạo Issue giúp mình nhé 😃!

default_limit_attributes's People

Contributors

namnv609 avatar

Watchers

James Cloos avatar

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.