ActiveModel::Lint
module for testing compatibility with the API - you just need to plug it in (to tackle) the test:Copy Source | Copy HTML<br/> class LintTest < ActiveModel::TestCase<br/> include ActiveModel::Lint::Tests<br/> <br/> class CompliantModel <br/> extend ActiveModel::Naming<br/> <br/> def to_model <br/> self <br/> end <br/> <br/> def valid ?() true end <br/> def new_record ?() true end <br/> def destroyed ?() true end <br/> <br/> def errors <br/> obj = Object .new<br/> def obj .[](key) [] end <br/> def obj .full_messages() [] end <br/> obj <br/> end <br/> end <br/> <br/> def setup <br/> @model = CompliantModel .new<br/> end <br/> end <br/>
ActiveModel::Lint::Tests
module check compatibility of the @model
object.Copy Source | Copy HTML<br/> class Person < ActiveRecord::Base <br/> validates_presence_of :first_name, :last_name<br/> end <br/> Ruby, :<br/> <br/> class Person <br/> include ActiveModel::Validations<br/> <br/> validates_presence_of :first_name, :last_name<br/> <br/> attr_accessor :first_name, :last_name<br/> def initialize (first_name, last_name)<br/> @first_name, @last_name = first_name, last_name<br/> end <br/> end <br/>
read_attribute_for_validation
to get the attribute, but by default it’s just an alias for send
that supports the standard Ruby attribute system via attr_accessor
.read_attribute_for_validation
:Copy Source | Copy HTML<br/> class Person <br/> include ActiveModel::Validations<br/> <br/> validates_presence_of :first_name, :last_name<br/> <br/> def initialize (attributes = {})<br/> @attributes = attributes<br/> end <br/> <br/> def read_attribute_for_validation (key)<br/> @attributes[key]<br/> end <br/> end <br/>
validates_presence_of
method:Copy Source | Copy HTML<br/> def validates_presence_of (*attr_names)<br/> validates_with PresenceValidator, _merge_attributes(attr_names)<br/> end <br/>
validates_presence_of
uses the more primitive validates_with
, passing it the class of the validator and adding the key {:attributes => attribute_names}
to attr_names
. Next is the validator class itself:Copy Source | Copy HTML<br/> class PresenceValidator < EachValidator<br/> def validate (record)<br/> record. errors .add_on_blank(attributes, options[:message])<br/> end <br/> end <br/>
validate
method in the EachValidator
class validates each attribute. In this case, it is redefined and adds an error message to the object only if the attribute is empty.add_on_blank
method calls add(attribute, :blank, :default => custom_message)
add_on_blank
add(attribute, :blank, :default => custom_message)
if value.blank?
(among other things), which adds a localized :blank
message to the object. The built-in localization file for the English locale/en.yml
as follows:Copy Source | Copy HTML<br/>en:<br/> errors:<br/> # . <br/> format : "{{attribute}} {{message}}" <br/> <br/> # :model, :attribute :value <br/> # :count . . <br/> messages:<br/> inclusion: "is not included in the list" <br/> exclusion: "is reserved" <br/> invalid: "is invalid" <br/> confirmation: "doesn't match confirmation" <br/> accepted: "must be accepted" <br/> empty: "can't be empty" <br/> blank: "can't be blank" <br/> too_long: "is too long (maximum is {{count}} characters)" <br/> too_short: "is too short (minimum is {{count}} characters)" <br/> wrong_length: "is the wrong length (should be {{count}} characters)" <br/> not_a_number: "is not a number" <br/> greater_than: "must be greater than {{count}}" <br/> greater_than_or_equal_to: "must be greater than or equal to {{count}}" <br/> equal_to: "must be equal to {{count}}" <br/> less_than: "must be less than {{count}}" <br/> less_than_or_equal_to: "must be less than or equal to {{count}}" <br/> odd: "must be odd" <br/> even: "must be even" <br/>
first_name can't be blank
.@person.to_json(:except => :comment)
.@person.to_xml(:except => :comment)
.attributes
method. See:Copy Source | Copy HTML<br/> class Person <br/> include ActiveModel::Serialization<br/> <br/> attr_accessor :attributes<br/> def initialize (attributes)<br/> @attributes = attributes<br/> end <br/> end <br/> <br/> p = Person . new (:first_name => "Yukihiro" , :last_name => "Matsumoto" )<br/> p .to_json #=> %|{"first_name": "Yukihiro", "last_name": "Matsumoto"}| <br/> p .to_json(:only => :first_name) #=> %|{"first_name": "Yukihiro"}| <br/>
:methods
; these methods will then be invoked dynamically.Copy Source | Copy HTML<br/> class Person <br/> include ActiveModel::Validations<br/> include ActiveModel::Serialization<br/> <br/> validates_presence_of :first_name, :last_name<br/> <br/> attr_accessor :attributes<br/> def initialize (attributes = {})<br/> @attributes = attributes<br/> end <br/> <br/> def read_attribute_for_validation (key)<br/> @attributes[key]<br/> end <br/> end <br/>
AttributeMethods
: Simplifies adding class methods to control attributes of the type table_name :foo
.Callbacks
: ActiveRecord style object life cycle callbacks.Dirty
: Support for dirty objects.Naming
: Default implementations of model.model_name
used by ActionPack (for example, for render :partial => model
).Observing
: ActiveRecord Observers (Observers).StateMachine
: A simple implementation of a state machine.Translation
: Basic support for translations into other languages (integration with the I18n internationalization framework).Source: https://habr.com/ru/post/86938/
All Articles