Created
August 28, 2018 05:22
-
-
Save equivalent/cb5666f62b6e377f3f15853ec5eb49e5 to your computer and use it in GitHub Desktop.
Request model with validation on error model
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
require 'active_model' | |
class RequestModel | |
include ActiveModel::Validations | |
attr_reader :params, :ero | |
def initialize(params, ero) | |
@params = params | |
@ero = ero | |
end | |
validates :title, | |
length: { maximum: 255 }, | |
presence: true | |
def title | |
params[:title] | |
end | |
def valid? | |
result = super | |
unless result | |
ero << errors | |
end | |
result | |
end | |
end | |
puts 'without error' | |
ero = [] | |
puts RequestModel.new({title: 'a'}, ero).valid? | |
puts ero.inspect | |
puts 'with error' | |
ero = [] | |
x = RequestModel.new({x: 'a'},ero) | |
puts x.valid? | |
puts ero.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment