Skip to content

Instantly share code, notes, and snippets.

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 / config locales model_attributes.rb
Created June 14, 2021 01:13
モデル名の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 / stable_sort_by.rb
Created May 24, 2021 00:47
安定ソートのsort_by
View stable_sort_by.rb
module Enumerable
def stable_sort_by
sort_by.with_index { |item, index| [yield(item), index] }
end
end
@alpaca-tc
alpaca-tc / use_index.rb
Created May 23, 2021 14:07
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
@alpaca-tc
alpaca-tc / lightweight_active_model_attributes.rb
Created May 23, 2021 14:04
軽量なActiveModel::Attributes相当のもの。どうしても大量のオブジェクトを扱いたいときに使う。
View lightweight_active_model_attributes.rb
# ActiveModel::Attributesは不要な抽象化が多くてメモリを大量に消費するため、軽量なActiveModel::Attributes相当のmoduleを用意する
# このmoduleは、ActiveModel::Attributesと比べて多くの機能(dirty trackingなど)がないため、readonlyなオブジェクトに利用するとよい
module LightweightActiveModelAttributes
extend ActiveSupport::Concern
include ActiveModel::AttributeMethods
included do
class_attribute :_attribute_types, :_default_attributes, instance_accessor: false
self._attribute_types = {}
@alpaca-tc
alpaca-tc / force_attribute.rb
Last active May 23, 2021 14:02
特定の条件下でのカラムの値を宣言的に定義する
View force_attribute.rb
# app/models/concerns/forced_attribute.rb
module ForcedAttribute
extend ActiveSupport::Concern
CONDITIONAL_OPTIONS = %i[if unless on].freeze
class ForcedAttributeCallbacks
# @param attributes [Hash<symbol, any>]
def initialize(attributes = {})
@attributes = attributes.freeze
@alpaca-tc
alpaca-tc / slim_partial_view_spec.rb
Created May 23, 2021 13:46
partial viewでインスタンス変数を使っていないことをテスト(slim版)
View slim_partial_view_spec.rb
require 'parser/current'
RSpec.describe 'View spec' do
describe 'partial view' do
# partial viewの一覧
let(:partial_views) do
path = Rails.application.paths['app/views'].first
Dir.glob("#{path}/**/_*.html.slim")
end
@alpaca-tc
alpaca-tc / enum.rb
Created May 23, 2021 13:44
ActiveModel用でEnum-likeな定義を使う拡張
View enum.rb
# app/models/concerns/enum_attribute.rb
module EnumAttribute
extend ActiveSupport::Concern
class_methods do
# Example
# Model.enum(:state, [:pending, :complete])
# Model.states #=> { 'pending' => 'PENDING', 'complete' => 'COMPLETE' }
#
# Model.new(state: 'PENDING').state #=> 'pending'
@alpaca-tc
alpaca-tc / phone_number.rb
Created May 23, 2021 13:42
渡された値から数値以外を削除する型(主に電話番号用)
View phone_number.rb
# Usage
# attribute :phone_number, :number_string
class NumberString < ActiveModel::Type::String
# 与えられた値を数値のみの文字列、またはnilに変換する
#
# @param value [Object]
#
# @return [String, NilClass]
def cast(value)
super(value)&.remove(/\D/)