rails/activemodel
Jonathan Hefner 0fb166e81c Improve performance for contextual validations
This change reduces retained memory when registering contextual
validations, and reduces overhead when running contextual validations.

Benchmark script:

```ruby
require "benchmark/memory"
require "benchmark/ips"

class Post
  include ActiveModel::Validations

  def foo; end
  def bar; end
end

class Comment
  include ActiveModel::Validations

  def foo; end
  def bar; end
end

Benchmark.memory do |x|
  x.report("warmup") do
    Post.validate :will_not_be_called, on: :warmup
    Comment.validate :will_not_be_called, on: :warmup
  end

  x.report("register validations") do
    Post.validate :foo, on: :create
    Post.validate :bar, on: :update
    Comment.validate :foo, on: :create
    Comment.validate :bar, on: :update
  end
end

post = Post.new

Benchmark.ips do |x|
  x.report("run validations") do
    post.valid?(:create)
  end
end
```

Before:

```
Calculating -------------------------------------
              warmup     3.320k memsize (     1.552k retained)
                        48.000  objects (    21.000  retained)
                         0.000  strings (     0.000  retained)
register validations     5.856k memsize (     2.456k retained)
                        92.000  objects (    33.000  retained)
                         0.000  strings (     0.000  retained)
Warming up --------------------------------------
     run validations     6.232k i/100ms
Calculating -------------------------------------
     run validations     61.826k (± 0.7%) i/s -    311.600k in   5.040176s
```

After:

```
Calculating -------------------------------------
              warmup     3.960k memsize (     1.400k retained)
                        51.000  objects (    17.000  retained)
                         0.000  strings (     0.000  retained)
register validations     6.368k memsize (     1.384k retained)
                        94.000  objects (    21.000  retained)
                         0.000  strings (     0.000  retained)
Warming up --------------------------------------
     run validations     6.998k i/100ms
Calculating -------------------------------------
     run validations     70.741k (± 0.6%) i/s -    356.898k in   5.045347s
```
2022-05-27 15:37:11 -05:00
..
bin Use frozen string literal in activemodel/ 2017-07-16 20:11:16 +03:00
lib Improve performance for contextual validations 2022-05-27 15:37:11 -05:00
test Dup options in validates_with 2022-05-26 15:08:00 -05:00
activemodel.gemspec Fix gemspec 2021-11-15 21:06:21 +00:00
CHANGELOG.md Support infinite ranges for LengthValidators :in/:within options 2022-05-20 20:41:18 +03:00
MIT-LICENSE Bump license years to 2022 [ci-skip] 2022-01-01 15:22:15 +09:00
Rakefile Load framework test files in deterministic order 2019-12-16 16:55:06 +00:00
README.rdoc Introduce ActiveModel::API 2021-09-15 18:24:47 +02:00

= Active Model -- model interfaces for Rails

Active Model provides a known set of interfaces for usage in model classes.
They allow for Action Pack helpers to interact with non-Active Record models,
for example. Active Model also helps with building custom ORMs for use outside of
the Rails framework.

You can read more about Active Model in the {Active Model Basics}[https://edgeguides.rubyonrails.org/active_model_basics.html] guide.

Prior to Rails 3.0, if a plugin or gem developer wanted to have an object
interact with Action Pack helpers, it was required to either copy chunks of
code from Rails, or monkey patch entire helpers to make them handle objects
that did not exactly conform to the Active Record interface. This would result
in code duplication and fragile applications that broke on upgrades. Active
Model solves this by defining an explicit API. You can read more about the
API in <tt>ActiveModel::Lint::Tests</tt>.

Active Model provides a default module that implements the basic API required
to integrate with Action Pack out of the box: <tt>ActiveModel::API</tt>.

    class Person
      include ActiveModel::API

      attr_accessor :name, :age
      validates_presence_of :name
    end

    person = Person.new(name: 'bob', age: '18')
    person.name   # => 'bob'
    person.age    # => '18'
    person.valid? # => true

It includes model name introspections, conversions, translations and
validations, resulting in a class suitable to be used with Action Pack.
See <tt>ActiveModel::API</tt> for more examples.

Active Model also provides the following functionality to have ORM-like
behavior out of the box:

* Add attribute magic to objects

    class Person
      include ActiveModel::AttributeMethods

      attribute_method_prefix 'clear_'
      define_attribute_methods :name, :age

      attr_accessor :name, :age

      def clear_attribute(attr)
        send("#{attr}=", nil)
      end
    end

    person = Person.new
    person.clear_name
    person.clear_age

  {Learn more}[link:classes/ActiveModel/AttributeMethods.html]

* Callbacks for certain operations

    class Person
      extend ActiveModel::Callbacks
      define_model_callbacks :create

      def create
        run_callbacks :create do
          # Your create action methods here
        end
      end
    end

  This generates +before_create+, +around_create+ and +after_create+
  class methods that wrap your create method.

  {Learn more}[link:classes/ActiveModel/Callbacks.html]

* Tracking value changes

    class Person
      include ActiveModel::Dirty

      define_attribute_methods :name

      def name
        @name
      end

      def name=(val)
        name_will_change! unless val == @name
        @name = val
      end

      def save
        # do persistence work
        changes_applied
      end
    end

    person = Person.new
    person.name             # => nil
    person.changed?         # => false
    person.name = 'bob'
    person.changed?         # => true
    person.changed          # => ['name']
    person.changes          # => { 'name' => [nil, 'bob'] }
    person.save
    person.name = 'robert'
    person.save
    person.previous_changes # => {'name' => ['bob, 'robert']}

  {Learn more}[link:classes/ActiveModel/Dirty.html]

* Adding +errors+ interface to objects

  Exposing error messages allows objects to interact with Action Pack
  helpers seamlessly.

    class Person

      def initialize
        @errors = ActiveModel::Errors.new(self)
      end

      attr_accessor :name
      attr_reader   :errors

      def validate!
        errors.add(:name, "cannot be nil") if name.nil?
      end

      def self.human_attribute_name(attr, options = {})
        "Name"
      end
    end

    person = Person.new
    person.name = nil
    person.validate!
    person.errors.full_messages
    # => ["Name cannot be nil"]

  {Learn more}[link:classes/ActiveModel/Errors.html]

* Model name introspection

    class NamedPerson
      extend ActiveModel::Naming
    end

    NamedPerson.model_name.name   # => "NamedPerson"
    NamedPerson.model_name.human  # => "Named person"

  {Learn more}[link:classes/ActiveModel/Naming.html]

* Making objects serializable

  <tt>ActiveModel::Serialization</tt> provides a standard interface for your object
  to provide +to_json+ serialization.

    class SerialPerson
      include ActiveModel::Serialization

      attr_accessor :name

      def attributes
        {'name' => name}
      end
    end

    s = SerialPerson.new
    s.serializable_hash   # => {"name"=>nil}

    class SerialPerson
      include ActiveModel::Serializers::JSON
    end

    s = SerialPerson.new
    s.to_json             # => "{\"name\":null}"

  {Learn more}[link:classes/ActiveModel/Serialization.html]

* Internationalization (i18n) support

    class Person
      extend ActiveModel::Translation
    end

    Person.human_attribute_name('my_attribute')
    # => "My attribute"

  {Learn more}[link:classes/ActiveModel/Translation.html]

* Validation support

    class Person
      include ActiveModel::Validations

      attr_accessor :first_name, :last_name

      validates_each :first_name, :last_name do |record, attr, value|
        record.errors.add attr, "starts with z." if value.start_with?("z")
      end
    end

    person = Person.new
    person.first_name = 'zoolander'
    person.valid?  # => false

  {Learn more}[link:classes/ActiveModel/Validations.html]

* Custom validators

    class HasNameValidator < ActiveModel::Validator
      def validate(record)
        record.errors.add(:name, "must exist") if record.name.blank?
      end
    end

    class ValidatorPerson
      include ActiveModel::Validations
      validates_with HasNameValidator
      attr_accessor :name
    end

    p = ValidatorPerson.new
    p.valid?                  # =>  false
    p.errors.full_messages    # => ["Name must exist"]
    p.name = "Bob"
    p.valid?                  # =>  true

  {Learn more}[link:classes/ActiveModel/Validator.html]


== Download and installation

The latest version of Active Model can be installed with RubyGems:

  $ gem install activemodel

Source code can be downloaded as part of the Rails project on GitHub

* https://github.com/rails/rails/tree/main/activemodel


== License

Active Model is released under the MIT license:

* https://opensource.org/licenses/MIT


== Support

API documentation is at:

* https://api.rubyonrails.org

Bug reports for the Ruby on Rails project can be filed here:

* https://github.com/rails/rails/issues

Feature requests should be discussed on the rails-core mailing list here:

* https://discuss.rubyonrails.org/c/rubyonrails-core