What type of language construct does Rails "test"?

I'm just starting to smash Ruby. My background is in .NET and PHP. In Rails, and I'm sure of other frameworks, I see things like this on classes:

class Person < ActiveRecord::Base validates :terms_of_service, :acceptance => true end 

What exactly does “check”? Is this a function? If this is a function, how does validation work, since you did not specify a validates function, which model are you testing?

Where can I find out more about how this works behind the scenes?

+4
source share
2 answers

This ... is a bit complicated, but the short answer is that validates is a method of the Person class inherited from ActiveRecord::Base . This line would also be written validates(:terms_of_service, :acceptance => true) .

Ruby, like many interpreted languages, effectively “executes” class definitions, so when it encounters a validates string, it sees it as a method call, where the current self object is an instance of the Class class, which represents the Person class that inherits from ActiveRecord::Base . It calls a method that connects the validator to the Person class.

You can read about the method here - but note that this adds more confusion, since it lists the method as an instance method of ActiveModel::Validations::ClassMethods . A? Well, Ruby has two ways to use the functionality from another Module and put it in its own class - you can either include module (in this case, its instance methods become instance methods of your class) or extend module (in this case its instance methods become class methods your class).

So to summarize: validates declared as an instance method of ActiveModel::Validations::ClassMethods , which extend ed in ActiveRecord::Base . Therefore, validates is a method of the ActiveRecord::Base class and, by inheritance, Person . A line in a code snippet is just a method call.

However, having said all this, most rubists and Railsists will largely ignore these facts; validates is what is called a “decorator,” and most people will simply read it as a statement about Person .

+9
source

This is a special callback method that is called to validate data before it is stored in the database.

A callback, more generally, is a method that “intercepts” an object or module of a module / library in such a way that the method is automatically called automatically when certain events occur. In this case, the event that invokes the set of validation methods is an attempt to save something in the database. This is useful because before you write new or updated information to your database, you want to make sure it is valid in order to prevent bad data from entering your application.

The following methods start checks and store the object in the database only if all checks pass (literally, this means that all check methods must return a value that is not false , or nil , etc., otherwise if verification is considered unsuccessful):

  • create
  • create!
  • save
  • save!
  • update
  • update_attributes
  • update_attributes!

Ruby's “methods” are very similar to the concept of functions in other languages. In many cases, you can think of them interchangeably - they claim some functionality, accept parameters as input, and return the result as output.

More about checks: http://guides.rubyonrails.org/active_record_validations_callbacks.html

Ruby (and Rails) use callbacks in many different situations — almost anytime you want a method to be called as a result of an event.

Additional information on callbacks: http://www.khelll.com/blog/ruby/ruby-callbacks/

+2
source

Source: https://habr.com/ru/post/1384356/


All Articles