Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Commits on Jul 15, 2015
  1. @tgxworld

    Revert "Revert "Reduce allocations when running AR callbacks.""

    tgxworld authored
    This reverts commit bdc1d32.
    
    Before:
    Calculating -------------------------------------
                            22.000  i/100ms
    -------------------------------------------------
                            229.700  (± 0.4%) i/s -      1.166k
    Total Allocated Object: 9939
    
    After:
    Calculating -------------------------------------
                            24.000  i/100ms
    -------------------------------------------------
                            246.443  (± 0.8%) i/s -      1.248k
    Total Allocated Object: 7939
    
    ```
    begin
      require 'bundler/inline'
    rescue LoadError => e
      $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
      raise e
    end
    
    gemfile(true) do
      source 'https://rubygems.org';
      # gem 'rails', github: 'rails/rails', ref: 'bdc1d329d4eea823d07cf010064bd19c07099ff3'
      gem 'rails', github: 'rails/rails', ref: 'd2876141d08341ec67cf6a11a073d1acfb920de7'
      gem 'arel', github: 'rails/arel'
      gem 'sqlite3'
      gem 'benchmark-ips'
    end
    
    require 'active_record'
    require 'benchmark/ips'
    
    ActiveRecord::Base.establish_connection('sqlite3::memory:')
    
    ActiveRecord::Migration.verbose = false
    
    ActiveRecord::Schema.define do
      create_table :users, force: true do |t|
        t.string :name, :email
        t.boolean :admin
        t.timestamps null: false
      end
    end
    
    class User < ActiveRecord::Base
      default_scope { where(admin: true) }
    end
    
    admin = true
    
    1000.times do
      attributes = {
        name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        email: "foobar@email.com",
        admin: admin
      }
    
      User.create!(attributes)
    
      admin = !admin
    end
    
    GC.disable
    
    Benchmark.ips(5, 3) do |x|
      x.report { User.all.to_a }
    end
    
    key =
      if RUBY_VERSION < '2.2'
        :total_allocated_object
      else
        :total_allocated_objects
      end
    
    before = GC.stat[key]
    User.all.to_a
    after = GC.stat[key]
    puts "Total Allocated Object: #{after - before}"
    ```
Commits on Jul 7, 2015
  1. @senny

    docs, clarify the meanaing of return values from validation methods.

    senny authored
    [ci skip]
    
    Closes #20792.
    
    Custom validation methods are implemented in terms of
    callbacks. The `validate` callback chain can't be halted using return
    values of individual callbacks.
  2. @senny

    docs, remove accidental :nodoc: of ActiveModel::Validations::ClassMet…

    senny authored
    …hods methods.
    
    [ci skip]
    
    While this :nodoc: did hide the constant it also removed the following
    methods from the API docs:
    
    - #attribute_method?
    - #clear_validators!
    - #validate
    - #validators
    - #validators_on
    
    Those are public API and should be visible.
    
    Issue was caused by dee4fbc
    
    /cc @zzak
Commits on Apr 26, 2015
  1. @zzak
Commits on Mar 22, 2015
  1. @tgxworld

    Revert "Reduce allocations when running AR callbacks."

    tgxworld authored
    This reverts commit 796cab4.
Commits on Feb 20, 2015
  1. @lucasmazza
Commits on Dec 12, 2014
  1. @chancancode

    Pass through the `prepend` option to `AS::Callback`

    chancancode authored
    I'm not sure what's the use case for this, but apparently it broke some apps.
    Since it was not the intended result from #16210 I fixed it to not raise an
    exception anymore. However, I didn't add documentation for it because I don't
    know if this should be officially supported without knowing how it's meant to
    be used.
    
    In general, validations should be side-effect-free (other than adding to the
    error message to `@errors`). Order-dependent validations seems like a bad idea.
    
    Fixes #18002
Commits on Oct 26, 2014
  1. @rafaelfranca

    Prefix internal method with _

    rafaelfranca authored
    This will avoid naming clash with user defined methods
Commits on Oct 4, 2014
  1. @zzak

    Merge pull request #16409 from justinweiss/update_validation_context_…

    zzak authored
    …documentation
    
    Docs: Add a note on custom validation contexts. [ci skip]
Commits on Sep 30, 2014
  1. @pabloh
Commits on Sep 28, 2014
  1. @phiggins

    Reduce allocations when running AR callbacks.

    phiggins authored
    Inspired by @tenderlove's work in
    c363fff, this reduces the number of
    strings allocated when running callbacks for ActiveRecord instances. I
    measured that using this script:
    
    ```
    require 'objspace'
    require 'active_record'
    require 'allocation_tracer'
    
    ActiveRecord::Base.establish_connection adapter: "sqlite3",
                                            database: ":memory:"
    
    ActiveRecord::Base.connection.instance_eval do
      create_table(:articles) { |t| t.string :name }
    end
    
    class Article < ActiveRecord::Base; end
    a = Article.create name: "foo"
    a = Article.find a.id
    
    N = 10
    result = ObjectSpace::AllocationTracer.trace do
      N.times { Article.find a.id }
    end
    
    result.sort.each do |k,v|
      p k => v
    end
    puts "total: #{result.values.map(&:first).inject(:+)}"
    ```
    
    When I run this against master and this branch I get this output:
    
    ```
    pete@balloon:~/projects/rails/activerecord$ git checkout master
    M Gemfile
    Switched to branch 'master'
    pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_before
    pete@balloon:~/projects/rails/activerecord$ git checkout remove-dynamic-send-on-built-in-callbacks
    M Gemfile
    Switched to branch 'remove-dynamic-send-on-built-in-callbacks'
    pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_after
    pete@balloon:~/projects/rails/activerecord$ diff allocations_before allocations_after
    39d38
    <
    {["/home/pete/projects/rails/activesupport/lib/active_support/callbacks.rb",
    81]=>[40, 0, 0, 0, 0, 0]}
    42c41
    < total: 630
    ---
    > total: 590
    
    ```
    
    In addition to this, there are two micro-optimizations present:
    
    * Using `block.call if block` vs `yield if block_given?` when the block was being captured already.
    
    ```
    pete@balloon:~/projects$ cat benchmark_block_call_vs_yield.rb
    require 'benchmark/ips'
    
    def block_capture_with_yield &block
      yield if block_given?
    end
    
    def block_capture_with_call &block
      block.call if block
    end
    
    def no_block_capture
      yield if block_given?
    end
    
    Benchmark.ips do |b|
      b.report("block_capture_with_yield") { block_capture_with_yield }
      b.report("block_capture_with_call") { block_capture_with_call }
      b.report("no_block_capture") { no_block_capture }
    end
    pete@balloon:~/projects$ ruby benchmark_block_call_vs_yield.rb
    Calculating -------------------------------------
    block_capture_with_yield
                            124979 i/100ms
    block_capture_with_call
                            138340 i/100ms
        no_block_capture    136827 i/100ms
    -------------------------------------------------
    block_capture_with_yield
                          5703108.9 (±2.4%) i/s -   28495212 in   4.999368s
    block_capture_with_call
                          6840730.5 (±3.6%) i/s -   34169980 in   5.002649s
        no_block_capture  5821141.4 (±2.8%) i/s -   29144151 in   5.010580s
    ```
    
    * Defining and calling methods instead of using send.
    
    ```
    pete@balloon:~/projects$ cat benchmark_method_call_vs_send.rb
    require 'benchmark/ips'
    
    class Foo
      def tacos
        nil
      end
    end
    
    my_foo = Foo.new
    
    Benchmark.ips do |b|
      b.report('send') { my_foo.send('tacos') }
      b.report('call') { my_foo.tacos }
    end
    pete@balloon:~/projects$ ruby benchmark_method_call_vs_send.rb
    Calculating -------------------------------------
                    send     97736 i/100ms
                    call    151142 i/100ms
    -------------------------------------------------
                    send  2683730.3 (±2.8%) i/s -   13487568 in   5.029763s
                    call  8005963.9 (±2.7%) i/s -   40052630 in   5.006604s
    ```
    
    The result of this is making typical ActiveRecord operations slightly faster:
    
    https://gist.github.com/phiggins/e46e51dcc7edb45b5f98
Commits on Sep 22, 2014
  1. @chancancode
Commits on Sep 20, 2014
  1. @prathamesh-sonpatki

    Update error message for validate method

    prathamesh-sonpatki authored
    - Improve the error message by suggesting that the user may have
      intended to call validates instead of validate method.
Commits on Aug 18, 2014
  1. @rafaelfranca

    Merge pull request #15889 from carnesmedia/model-name

    rafaelfranca authored
    Use #model_name on instances instead of classes
Commits on Aug 5, 2014
  1. @justinweiss

    Add a note on custom validation contexts.

    justinweiss authored
    The documentation on `:on` for validations was inconsistent, and most
    only referenced the `:create` and `:update` contexts. I fixed those to
    be consistent with the documentation on `AM::Validations.validates`,
    which seemed to have the best docs.
    
    [ci skip]
Commits on Jul 17, 2014
  1. @tenderlove

    %i doesn't work on 1.9

    tenderlove authored
  2. @sonnym

    check for valid options in validate method

    sonnym authored
    This change prevents a certain class of user error which results when
    mistakenly using the `validate` class method instead of the `validates`
    class method.
    
    Only apply when all arguments are symbols, because some validations use
    the `validate` method and pass in additional options, namely the
    `LenghValidator` via the `ActiveMode::Validations::validates_with`
    method.
Commits on Jun 25, 2014
  1. @amiel

    Use #model_name on instances instead of classes

    amiel authored
    This allows rails code to be more confdent when asking for a model name, instead of having to ask for the class.
    
    Rails core discussion here: https://groups.google.com/forum/#!topic/rubyonrails-core/ThSaXw9y1F8
Commits on Mar 27, 2014
  1. @henrik

    ActiveRecord/ActiveModel '#validate' alias for 'valid?'

    henrik authored
    It's unintuitive to call '#valid?' when you want to run validations but
    don't care about the return value.
    
    The alias in ActiveRecord isn't strictly necessary (the ActiveModel
    alias is still in effect), but it clarifies.
Commits on Jan 27, 2014
  1. @carlosantoniodasilva
  2. @vpuzzella

    Ability to specify multiple contexts when defining a validation.

    vpuzzella authored
    Example:
    
    validates_presence_of :name, on: [:update, :custom_validation_context]
Commits on Jan 26, 2014
  1. @fxn

    revises references to :allow_(nil|blank) in some docs [ci skip] [Stev…

    fxn authored
    …en Yang & Xavier Noria]
    
    Closes #11247.
Commits on Dec 27, 2013
  1. @aditya-kapoor
Commits on Jun 11, 2013
  1. @tenderlove
Commits on Jun 10, 2013
  1. @darai2k
Commits on May 2, 2013
  1. @patricksrobertson

    Convert ActiveModel to 1.9 hash syntax.

    patricksrobertson authored
    I also attempted to fix other styleguide violations such as
    { a: :b } over {a: :b} and foo(b: 'bar') over foo( b: 'bar' ).
Commits on Mar 20, 2013
  1. @Intrepidd

    Tidying up some require : removing useless sort and homogenizing with…

    Intrepidd authored
    … the rest of the code the wat the includes are done
Commits on Mar 19, 2013
  1. @wangjohn

    The repair_validations helper was not working correctly before because

    wangjohn authored
    it only cleared the validations that created :validate callbacks. This
    didn't include the validates created by validates_with, so I've added a
    method to clear all validations.
Commits on Nov 25, 2012
  1. @senny
Commits on Oct 22, 2012
  1. @AvnerCohen
Commits on Oct 21, 2012
  1. @vijaydev

    Merge branch 'master' of github.com:lifo/docrails

    vijaydev authored
    Conflicts:
    	activesupport/lib/active_support/core_ext/hash/slice.rb
    	guides/source/active_support_core_extensions.md
  2. minor edits in AM documentation [ci skip]

    Francesco Rodriguez authored
Commits on Oct 18, 2012
  1. @senny
  2. @senny
Commits on Oct 3, 2012
  1. @rafaelfranca

    Use the `flat_map` method.

    rafaelfranca authored
    Thanks to @jeremy to teach me this one.
Something went wrong with that request. Please try again.