Skip to content

Instantly share code, notes, and snippets.

@alpaca-tc
alpaca-tc / optimized_refine.patch
Created March 31, 2025 04:25
testing optimized_refine.patch for 3.3.7
diff --git a/vm.c b/vm.c
index a0fe0104b0..9e733f638d 100644
--- a/vm.c
+++ b/vm.c
@@ -2877,6 +2877,13 @@ rb_vm_each_stack_value(void *ptr, void (*cb)(VALUE, void*), void *ctx)
}
}
+static enum rb_id_table_iterator_result
+vm_mark_refined_cc(VALUE val, void *dmy)
@alpaca-tc
alpaca-tc / use_index.rb
Created May 23, 2021 14:07
ActiveRecordでUSE INDEXを使うための拡張
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 / enum.rb
Created May 23, 2021 13:44
ActiveModel用でEnum-likeな定義を使う拡張
# 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 / a.rb
Created January 19, 2024 07:40
Example of deadlocking by executing require in multithread
# ruby 3.2.3 (2024-01-18 revision 52bb2ac0a6) [arm64-darwin23]
$LOAD_PATH << "#{__dir__}/lib"
# # lib/first.rb
# module First
# require 'second'
# end
# # lib/second.rb
# module Second
@alpaca-tc
alpaca-tc / sidekiq_cron_spec.rb
Created May 23, 2021 12:59
sidekiq-cronのconfig/schedule.ymlがvalidであることをテスト
# frozen_string_literal: true
RSpec.describe 'sidekiq-cron' do
describe 'config/schedule.yml' do
let(:path) { Rails.root.join('config', 'schedule.yml') }
before do
Sidekiq::Cron::Job.load_from_array(YAML.load_file(path))
end
# 指定したレコードの関連の探索処理
#
# @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
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))
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 12:57
FactoryBotの定義漏れ、不正な定義をテストする
# 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 June 14, 2021 01:13
モデル名のlocaleを複数形用に変換して追加する
# 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 に変換する
#