Skip to content

Instantly share code, notes, and snippets.

View active_record_depth_query.rb
# 指定したレコードの関連の探索処理
#
# @example
# depth_query = ActiveRecordDepthQuery.new(employee, [attendance: :attendance_records])
# depth_query.each do |relation|
# relation.to_a #=> 1度目は従業員に紐づくAttendanceの一覧、2度目はAttendanceRecordの一覧が返ってくる
# end
class ActiveRecordDepthQuery
include Enumerable
View rails#43878
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index d1b285101d..9ca4677c6f 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -352,9 +352,9 @@ def execute_grouped_calculation(operation, column_name, distinct) # :nodoc:
select_values.concat group_columns.map { |aliaz, field|
if field.respond_to?(:as)
- field.as(aliaz)
+ Arel::Nodes::As.new(field, Arel::Table.new(aliaz))
View changes.diff
diff --git a/Gemfile b/Gemfile
index 8d3bc88..6272179 100644
--- a/Gemfile
+++ b/Gemfile
@@ -23,6 +23,11 @@ group :development, :test do
gem "debug", ">= 1.0.0", platforms: %i[ mri mingw x64_mingw ]
end
+group :test do
+ gem 'rspec'
@alpaca-tc
alpaca-tc / factory_bot_spec.rb
Created May 23, 2021
FactoryBotの定義漏れ、不正な定義をテストする
View factory_bot_spec.rb
# frozen_string_literal: true
RSpec.describe FactoryBot do
shared_examples_for 'valid definition' do
it 'is valid' do
is_expected.to be_valid, (-> { factory.errors.full_messages.join("\n") })
end
end
FactoryBot.factories.map(&:name).each do |factory_name|
@alpaca-tc
alpaca-tc / config locales model_attributes.rb
Created Jun 14, 2021
モデル名のlocaleを複数形用に変換して追加する
View config locales model_attributes.rb
# frozen_string_literal: true
paths = Dir[Rails.root.join('config/locales/models/defaults/*.yml')]
all_locales = paths.map { |path| YAML.load_file(path) }.inject(&:deep_merge)
# has_many用のデフォルトのlocaleを追加する
# config/locales/models/defaults/ja.ymlにモデル名が定義されている前提。
#
# activerecord/models にある値を複数形にして attributes に変換する
#
@alpaca-tc
alpaca-tc / stripped_string.rb
Last active May 31, 2021
空白を除去するString型
View stripped_string.rb
class StrippedString < ActiveModel::Type::String
# 左右の空白を除去した文字列クラス
#
# @param [Object] value
#
# @return [String, nil]
def cast(value)
super(value)&.strip&.presence
end
@alpaca-tc
alpaca-tc / boolean_validator.rb
Created May 23, 2021
値がtrue/falseのいずれかであることをテスト
View boolean_validator.rb
# validates :column_name, boolean: true のように使う
class BooleanValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value.is_a?(TrueClass) || value.is_a?(FalseClass)
record.errors.add(attribute, :not_boolean)
end
end
@alpaca-tc
alpaca-tc / use_index.rb
Created May 23, 2021
ActiveRecordでUSE INDEXを使うための拡張
View use_index.rb
module WithIndex
extend ActiveSupport::Concern
class IndexNotFound < StandardError; end
class_methods do
# インデックスを指定する
#
# @example
# User.where(...).use_index([:group_id, :created_at]).load
View missing_association_spec.rb
# frozen_string_literal: true
RSpec.describe 'ActiveRecord associations' do
let(:connection) { ActiveRecord::Base.connection }
let(:table_names) { connection.tables }
let(:models) { table_names.map { table_to_model(_1) } }
# テスト対象から除外する関連を定義
let(:ignored_reflections) do
# { table_names => [association_name, ...] }
@alpaca-tc
alpaca-tc / stable_sort_by.rb
Created May 24, 2021
安定ソートのsort_by
View stable_sort_by.rb
module Enumerable
def stable_sort_by
sort_by.with_index { |item, index| [yield(item), index] }
end
end