なるようになるブログ

読書感想文かrailsについてかrubyについてか

rails commit log流し読み(2014/04/23)

2014/04/23分のコミットです。

has_many :throughを使用した際に、unscopeが正常に動作してなかったバグの修正が行われています。


Merge pull request #14801 from kuldeepaggarwal/fix-string-inflection

String#pluralizeの振る舞いを修正しています。

pluralizeに引数に1を設定した場合は、selfを、それ以外の数字を設定した場合は新しいStringのインスタンスを返すようになっていたのを、引数に1を設定した場合も新しいStringのインスタンスを返すように修正しています。処理の一貫性の統一のためですね。

pluralizeに引数があるのは知らなかったです。1を指定した場合は単数形の値が返るのですね。


Merge pull request #14573 from habermann24/has_many_through_fix

has_many :throughを使用した際に、unscopeが正常に動作してなかったのを修正しています。

issueに記載されていたサンプルは以下のような形です。

class Account < ActiveRecord::Base
  cattr_accessor :current

  has_many :people, dependent: :destroy
end

class Person < ActiveRecord::Base
  belongs_to :account
  belongs_to :user

  default_scope { where(account_id: Account.current) if Account.current }
  scope :across_all_accounts, -> { unscope(where: :account_id) }
end

class User < ActiveRecord::Base
  has_many :identities, ->{ across_all_accounts }, class_name: 'Person'
  has_many :accounts, through: :identities

end
# Account.current.id = 1  in this example

current_user.identities.pluck(:account_id)
  User Load (1.1ms)  SELECT  "users".* FROM "users"   ORDER BY "users"."id" DESC LIMIT 1
   (0.5ms)  SELECT "people"."account_id" FROM "people"  WHERE "people"."user_id" = $1  [["user_id", 1]]
=> [3, 1]

current_user.accounts.pluck(:id)
  User Load (1.1ms)  SELECT  "users".* FROM "users"   ORDER BY "users"."id" DESC LIMIT 1
   (0.6ms)  SELECT "accounts"."id" FROM "accounts" INNER JOIN "people" ON "accounts"."id" = "people"."account_id" WHERE "people"."account_id" = 1 AND "people"."user_id" = $1  [["user_id", 1]]
=> [1]

二つ目は、scope :across_all_accountsを指定しているのに、default_scopeが外れないというバグがあた為、正しくdefault_scopeが外れるよう修正しています。


Add CHANGELOG entry for #14757 [ci skip]

昨日あった、select!メソッドの名前の変更について、CHANGELOGに追記しています。


Fix syntax error

syntax errorの修正です。activerecord/test/cases/associations/has_many_through_associations_test.rbendが足りてなかったので追加。


Perfer to define methods instead of calling test

activerecord/test/cases/associations/has_many_through_associations_test.rbのテストの書き方を修正しています。

テストメソッドの書き方を、test xxxx -> def test_xxxxに修正しています。

activerecordの既存のテストが後者の書き方なので、書き方統一の為ですね。


Correct comment [ci skip]

ActiveSupport::Inflector#constantizeのコメントの修正を行っています。