1. 24 12月, 2016 1 次提交
  2. 27 10月, 2016 1 次提交
  3. 14 9月, 2016 1 次提交
  4. 08 8月, 2016 1 次提交
    • X
      code gardening: removes redundant selfs · a9dc4545
      Xavier Noria 提交于
      A few have been left for aesthetic reasons, but have made a pass
      and removed most of them.
      
      Note that if the method `foo` returns an array, `foo << 1`
      is a regular push, nothing to do with assignments, so
      no self required.
      a9dc4545
  5. 07 8月, 2016 2 次提交
  6. 03 8月, 2016 1 次提交
  7. 08 3月, 2016 1 次提交
  8. 28 1月, 2016 1 次提交
    • A
      Revert "Remove valid_scope_name? check - use ruby" · dc925119
      Akira Matsuda 提交于
      This reverts commit f6db31ec.
      
      Reason:
      Scope names can very easily conflict, particularly when sharing Concerns
      within the team, or using multiple gems that extend AR models.
      
      It is true that Ruby has the ability to detect this with the -w option, but the
      reality is that we are depending on too many gems that do not care about Ruby
      warnings, therefore it might not be a realistic solution to turn this switch on
      in our real-world apps.
      dc925119
  9. 23 1月, 2016 1 次提交
    • A
      Eliminate instance level writers for class accessors · 4642d68d
      Aaron Patterson 提交于
      Instance level writers can have an impact on how the Active Model /
      Record objects are saved.  Specifically, they can be used to bypass
      validations.  This is a problem if mass assignment protection is
      disabled and specific attributes are passed to the constructor.
      
      CVE-2016-0753
      4642d68d
  10. 12 1月, 2016 1 次提交
    • M
      Skip the STI condition when evaluating a default scope · 5c6d3653
      Matthew Draper 提交于
      Given a default_scope on a parent of the current class, where that
      parent is not the base class, the parent's STI condition would become
      attached to the evaluated default scope, and then override the child's
      own STI condition.
      
      Instead, we can treat the STI condition as though it is a default scope,
      and skip it in this situation: the scope will be merged into the base
      relation, which already contains the correct STI condition.
      
      Fixes #22426.
      5c6d3653
  11. 25 10月, 2015 1 次提交
  12. 14 10月, 2015 1 次提交
    • Y
      applies new doc guidelines to Active Record. · 428d47ad
      Yves Senn 提交于
      The focus of this change is to make the API more accessible.
      References to method and classes should be linked to make it easy to
      navigate around.
      
      This patch makes exzessiv use of `rdoc-ref:` to provide more readable
      docs. This makes it possible to document `ActiveRecord::Base#save` even
      though the method is within a separate module
      `ActiveRecord::Persistence`. The goal here is to bring the API closer to
      the actual code that you would write.
      
      This commit only deals with Active Record. The other gems will be
      updated accordingly but in different commits. The pass through Active
      Record is not completely finished yet. A follow up commit will change
      the spots I haven't yet had the time to update.
      
      /cc @fxn
      428d47ad
  13. 08 10月, 2015 1 次提交
    • T
      Modify the scope method documentation · e2a42243
      Tommaso Visconti 提交于
      Adds a paragraph to the documentation of the `ActiveRecord::Scoping::Named.scope` method,
      explaining that the method is intended to return an ActiveRecord::Relation object to be
      composable with other scopes.
      
      In the case that in the case that `nil` or `false` are returned, the method returns
      an `all` relation instead.
      This unexpected behaviour is mentioned in #19249 #14256 #21465 and #21882 and wasn't
      documented at all. This commit adds this documentation.
      e2a42243
  14. 07 9月, 2015 2 次提交
    • G
      PERF: Don't create a Relation when it is not needed. · 69de09c5
      Guo Xiang Tan 提交于
      Benchmark script used:
      ```
      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', path: '~/rails' # master against ref "f1f0a3f8"
        gem 'arel', github: 'rails/arel', branch: 'master'
        gem 'rack', github: 'rack/rack', branch: 'master'
        gem 'sass'
        gem 'sprockets-rails', github: 'rails/sprockets-rails', branch: 'master'
        gem 'sprockets', github: 'rails/sprockets', branch: 'master'
        gem 'pg'
        gem 'benchmark-ips'
      end
      
      require 'active_record'
      require 'benchmark/ips'
      
      ActiveRecord::Base.establish_connection('postgres://postgres@localhost:5432/rubybench')
      
      ActiveRecord::Migration.verbose = false
      
      ActiveRecord::Schema.define do
        create_table :users, force: true do |t|
          t.string :name, :email
          t.timestamps null: false
        end
      end
      
      class User < ActiveRecord::Base; end
      
      attributes = {
        name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        email: "foobar@email.com",
      }
      
      1000.times { User.create!(attributes) }
      
      Benchmark.ips(5, 3) do |x|
        x.report('all') { User.all }
      end
      
      key =
        if RUBY_VERSION < '2.2'
          :total_allocated_object
        else
          :total_allocated_objects
        end
      
      before = GC.stat[key]
      User.where(name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
      after = GC.stat[key]
      puts "Total Allocated Object: #{after - before}"
      
      ```
      
      Before:
      ```
      Calculating -------------------------------------
                       all    17.569k i/100ms
      -------------------------------------------------
                       all    190.854k (± 3.3%) i/s -    966.295k
      Total Allocated Object: 85
      ```
      
      After:
      ```
      Calculating -------------------------------------
                       all    22.237k i/100ms
      -------------------------------------------------
                       all    262.715k (± 5.5%) i/s -      1.312M
      Total Allocated Object: 80
      ```
      69de09c5
    • G
      Cache check if `default_scope` has been overridden. · 52b2ab9e
      Guo Xiang Tan 提交于
      Benchmark Script:
      ```
      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: 'f1f0a3f8'
        gem 'rails', path: '~/rails'
        gem 'arel', github: 'rails/arel', branch: 'master'
        gem 'rack', github: 'rack/rack', branch: 'master'
        gem 'sass'
        gem 'sprockets-rails', github: 'rails/sprockets-rails', branch: 'master'
        gem 'sprockets', github: 'rails/sprockets', branch: 'master'
        gem 'pg'
        gem 'benchmark-ips'
      end
      
      require 'active_record'
      require 'benchmark/ips'
      
      ActiveRecord::Base.establish_connection('postgres://postgres@localhost:5432/rubybench')
      
      ActiveRecord::Migration.verbose = false
      
      ActiveRecord::Schema.define do
        create_table :users, force: true do |t|
          t.string :name, :email
          t.timestamps null: false
        end
      end
      
      class User < ActiveRecord::Base; end
      
      attributes = {
        name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        email: "foobar@email.com",
      }
      
      1000.times { User.create!(attributes) }
      
      Benchmark.ips(5, 3) do |x|
        x.report('where with hash') { User.where(name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.") }
        x.report('where with string') { User.where("users.name = ?", "Lorem ipsum dolor sit amet, consectetur adipiscing elit.") }
        x.compare!
      end
      
      key =
        if RUBY_VERSION < '2.2'
          :total_allocated_object
        else
          :total_allocated_objects
        end
      
      before = GC.stat[key]
      User.where(name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
      after = GC.stat[key]
      puts "Total Allocated Object: #{after - before}"
      ```
      
      Stackprof output truncated.
      ```
      TOTAL    (pct)     SAMPLES    (pct)     FRAME
         52  (10.6%)          10   (2.0%)     ActiveRecord::Scoping::Default::ClassMethods#build_default_scope
      ```
      
      Before:
      ```
      Calculating -------------------------------------
           where with hash     2.789k i/100ms
         where with string     4.407k i/100ms
      -------------------------------------------------
           where with hash     29.170k (± 1.9%) i/s -    147.817k
         where with string     46.954k (± 2.7%) i/s -    237.978k
      
      Comparison:
         where with string:    46954.3 i/s
           where with hash:    29169.9 i/s - 1.61x slower
      
      Total Allocated Object: 85
      Calculating -------------------------------------
                       all    16.773k i/100ms
      -------------------------------------------------
                       all    186.102k (± 3.6%) i/s -    939.288k
      ```
      
      After:
      ```
      Calculating -------------------------------------
           where with hash     3.014k i/100ms
         where with string     4.623k i/100ms
      -------------------------------------------------
           where with hash     31.524k (± 1.3%) i/s -    159.742k
         where with string     49.948k (± 2.3%) i/s -    249.642k
      
      Comparison:
         where with string:    49948.3 i/s
           where with hash:    31524.3 i/s - 1.58x slower
      
      Total Allocated Object: 84
      Calculating -------------------------------------
                       all    20.139k i/100ms
      -------------------------------------------------
                       all    227.860k (± 2.5%) i/s -      1.148M
      ```
      52b2ab9e
  15. 16 4月, 2015 1 次提交
  16. 01 4月, 2015 1 次提交
  17. 12 3月, 2015 1 次提交
    • B
      Isolate access to .default_scopes in ActiveRecord::Scoping::Default · c1deb81c
      Ben Woosley 提交于
      Instead use .scope_attributes? consistently in ActiveRecord to check whether
      there are attributes currently associated with the scope.
      
      Move the implementation of .scope_attributes? and .scope_attributes to
      ActiveRecord::Scoping because they don't particularly have to do specifically
      with Named scopes and their only dependency, in the case of
      .scope_attributes?, and only caller, in the case of .scope_attributes is
      contained in Scoping.
      c1deb81c
  18. 12 2月, 2015 1 次提交
    • S
      `current_scope` shouldn't pollute sibling STI classes · 5e0b555b
      Sean Griffin 提交于
      It looks like the only reason `current_scope` was thread local on
      `base_class` instead of `self` is to ensure that when we call a named
      scope created with a proc on the parent class, it correctly uses the
      default scope of the subclass. The reason this wasn't happening was
      because the proc captured `self` as the parent class, and we're not
      actually defining a real method. Using `instance_exec` fixes the
      problem.
      
      Fixes #18806
      5e0b555b
  19. 07 2月, 2015 1 次提交
  20. 12 11月, 2014 1 次提交
  21. 24 10月, 2014 1 次提交
  22. 21 5月, 2014 1 次提交
  23. 24 4月, 2014 1 次提交
    • J
      Fixes Issue #13466. · 9c3afdc3
      Jefferson Lai 提交于
      Changed the call to a scope block to be evaluated with instance_eval.
      The result is that ScopeRegistry can use the actual class instead of base_class when
      caching scopes so queries made by classes with a common ancestor won't leak scopes.
      9c3afdc3
  24. 07 3月, 2014 2 次提交
  25. 22 2月, 2014 1 次提交
  26. 30 1月, 2014 1 次提交
    • G
      `scope` now raises on "dangerous" name conflicts · 7e8e91c4
      Godfrey Chan 提交于
      Similar to dangerous attribute methods, a scope name conflict is
      dangerous if it conflicts with an existing class method defined within
      `ActiveRecord::Base` but not its ancestors.
      
      See also #13389.
      
      *Godfrey Chan*, *Philippe Creux*
      7e8e91c4
  27. 01 9月, 2013 1 次提交
  28. 06 8月, 2013 1 次提交
  29. 04 7月, 2013 1 次提交
  30. 02 7月, 2013 2 次提交
  31. 28 6月, 2013 1 次提交
    • J
      Simplify/fix implementation of default scopes · 94924dc3
      Jon Leighton 提交于
      The previous implementation was necessary in order to support stuff
      like:
      
          class Post < ActiveRecord::Base
            default_scope where(published: true)
            scope :ordered, order("created_at")
          end
      
      If we didn't evaluate the default scope at the last possible moment
      before sending the SQL to the database, it would become impossible to
      do:
      
          Post.unscoped.ordered
      
      This is because the default scope would already be bound up in the
      "ordered" scope, and therefore wouldn't be removed by the
      "Post.unscoped" part.
      
      In 4.0, we have deprecated all "eager" forms of scopes. So now you must
      write:
      
          class Post < ActiveRecord::Base
            default_scope { where(published: true) }
            scope :ordered, -> { order("created_at") }
          end
      
      This prevents the default scope getting bound up inside the "ordered"
      scope, which means we can now have a simpler/better/more natural
      implementation of default scoping.
      
      A knock on effect is that some things that didn't work properly now do.
      For example it was previously impossible to use #except to remove a part
      of the default scope, since the default scope was evaluated after the
      call to #except.
      94924dc3
  32. 09 4月, 2013 2 次提交
  33. 07 4月, 2013 1 次提交
  34. 05 4月, 2013 1 次提交
    • J
      Fix scope chaining + STI · 8606a7fb
      Jon Leighton 提交于
      See #9869 and #9929.
      
      The problem arises from the following example:
      
          class Project < ActiveRecord::Base
            scope :completed, -> { where completed: true }
          end
      
          class MajorProject < Project
          end
      
      When calling:
      
          MajorProject.where(tasks_count: 10).completed
      
      This expands to:
      
          MajorProject.where(tasks_count: 10).scoping {
            MajorProject.completed
          }
      
      However the lambda for the `completed` scope is defined on Project. This
      means that when it is called, `self` is Project rather than
      MajorProject. So it expands to:
      
          MajorProject.where(tasks_count: 10).scoping {
            Project.where(completed: true)
          }
      
      Since the scoping was applied on MajorProject, and not Project, this
      fails to apply the tasks_count condition.
      
      The solution is to make scoping apply across STI classes. I am slightly
      concerned about the possible side-effects of this, but no tests fail and
      it seems ok. I guess we'll see.
      8606a7fb
  35. 24 3月, 2013 1 次提交