Guest User

Untitled

a guest
Aug 20th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. # Back to Basics: Validations in Rails
  2.  
  3. ```bash
  4. rails new basics --skip-bootsnap --skip-bundle --skip-action-cable --skip-active-storage --skip-action-mailer --skip-yarn --skip-sprockets --skip-turbolinks --skip-coffee --skip-javascript --database=postgresql
  5. ```
  6.  
  7. ```bash
  8. rails db:setup db:migrate
  9. ```
  10.  
  11. ## Models
  12.  
  13. ```bash
  14. rails g model User name email bio:text age:integer states_visited:array
  15. ```
  16.  
  17. ## Basics of rails validations
  18.  
  19. ### presence
  20.  
  21. ```ruby
  22. validates :name, presence: true
  23. validates_presence_of :name
  24. ```
  25.  
  26.  
  27. ### chaining
  28. ```ruby
  29. validates :first_name, :last_name, presence: true
  30. ```
  31.  
  32. ### multiple
  33. ```ruby
  34. validates :name, presence: true, length: { minimum: 2 }
  35. validates :name, presence: true, length: { minimum: 2, maximum: 255 }
  36. validates :name, presence: true, length: { in: 2..255 }
  37. ```
  38.  
  39. ### format
  40. ```ruby
  41. EMAIL_REGEX = /\A\S+@.+\.\S+\z/
  42. validates :email, format: { with: EMAIL_REGEX }
  43. ```
  44.  
  45. ### numericality
  46. ```ruby
  47. validates :age, numericality: { only_integer: true }
  48. validates :age, numericality: { only_integer: true, greater_than: 0, less_than: 1000 }
  49. validates :age, numericality: { only_integer: true, greater_than: 0, less_than: 1000, other_than: 69 }
  50. ```
  51.  
  52. ### uniqueness
  53. ```ruby
  54. validates :email, uniqueness: true
  55. validates :email, uniqueness: { case_sensitive: false }
  56. validates :email, uniqueness: { scope: :name, case_sensitive: false }
  57. ```
  58.  
  59. ### options
  60. ```ruby
  61. validates :age, numericality: { only_integer: true }, allow_nil: true
  62. validates :name, length: { in: 2..255 }, allow_blank: true
  63. validates :name, length: { in: 2..255 }, allow_blank: true, on: :update
  64. validates :email, uniqueness: true, if: :email_changed?
  65. validates :email, uniqueness: true, unless: -> { Rails.env.test? }
  66. ```
  67.  
  68. ## Custom validations
  69.  
  70. ### validate
  71. ```ruby
  72. validate :states_visited_has_no_empty_elements
  73.  
  74. def states_visited_has_no_empty_elements
  75. return unless states_visited.include?(nil) || states_visited.include?('')
  76. errors.add(:states_visited, 'cannot contain nil or an empty string')
  77. end
  78. ```
  79.  
  80. ### ActiveModel::Validator
  81. ```ruby
  82. class ReadOnlyValidator < ActiveModel::Validtor
  83. def validate(record)
  84. record.errors.add(:base, 'The system is read only') if ENV['READ_ONLY']
  85. end
  86. end
  87.  
  88. class User < ApplicationRecord
  89. validates_with ReadOnlyValidator
  90. end
  91. ```
  92.  
  93. ### ActiveModel::EachValidator
  94. ```ruby
  95. class EmailValidator < ActiveModel::EachValidator
  96. EMAIL_REGEX = /\A\S+@.+\.\S+\z/
  97. def validate_each(record, attribute, value)
  98. return if value.match? EMAIL_REGEX
  99. record.errors[attribute] << (options[:message] || 'format is invalid')
  100. end
  101. end
  102.  
  103. validates :email, email: true
  104. validates :email, email: true, message: 'needs to be a valid email address'
  105. ```
  106.  
  107. ## Validation Contexts
  108.  
  109. ### on:
  110. ```ruby
  111. validates :bio, length: { minimum: 1 }, on: :admin
  112. validates :bio, length: { minimum: 3 }, on: :user
  113. validates :bio, length: { minimum: 3 }, on: [:user, :admin]
  114.  
  115. u = User.new(bio: 'a')
  116. u.valid?(:admin)
  117. u.valid?(:user)
  118. ```
  119. Can be used with `valid?`, `invalid?`, and `save`.
  120.  
  121. ## ActiveModel::Errors
  122.  
  123. ### .errors
  124.  
  125. ```ruby
  126. u = User.new(bio: 'hello!', name: 'Bob')
  127. u.errors
  128. u.valid?
  129. u.errors
  130. ```
  131.  
  132. ### .errors[]
  133.  
  134. ### .errors[:attr].add / .errors[:attr] <<
  135.  
  136. ### .errors[:base]
  137.  
  138. ### .errors.messages
  139.  
  140. ### .errors.details
  141.  
  142. ### .errors.full_messages
  143.  
  144. ### .errors.clear
Add Comment
Please, Sign In to add comment