Skip to content

Instantly share code, notes, and snippets.

@alpaca-tc
alpaca-tc / missing_model_i18n_spec.rb
Created May 23, 2021 12:55
ActiveRecordのi18nの定義漏れをテストする
# frozen_string_literal: true
RSpec.describe 'ActiveRecord i18n' do
let(:table_names) { ActiveRecord::Base.connection.tables }
let(:models) do
table_names.map do |table_name|
table_to_model(table_name)
rescue NameError
nil
@alpaca-tc
alpaca-tc / db_seed_spec.rb
Created May 23, 2021 12:51
rake db:seed が例外を吐かないことをテストする
# frozen_string_literal: true
RSpec.describe 'rake db:seed' do
it 'works fine' do
expect { load('db/seeds.rb') }.to_not raise_error
end
end
@alpaca-tc
alpaca-tc / activerecord_duplicated_indexes_spec.rb
Created May 23, 2021 12:49
重複するインデックスを検知する
# frozen_string_literal: true
RSpec.describe 'ActiveRecord duplicated indexes' do
let(:connection) do
ActiveRecord::Base.connection
end
it 'is not found' do
tables = connection.tables
duplicated_indexes = {}
@alpaca-tc
alpaca-tc / missing_foreign_keys_spec.rb
Created May 23, 2021 12:45
定義漏れの外部キーを自動検知する
# frozen_string_literal: true
RSpec.describe 'ActiveRecord foreign key' do
let(:connection) { ActiveRecord::Base.connection }
let(:tables) { connection.tables }
def foreign_key_exists?(reflection, foreign_keys)
to_table_name = reflection.klass.table_name
reflection.foreign_key
# 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, ...] }
https://github.com/kufu/activerecord-bitemporal/pull/70/files#diff-77aac43b146505859dd74b1fd3e151c0e94174f5abc85475fe1dfba10a5c64bfL330
git clone https://github.com/kufu/activerecord-bitemporal
hub checkout https://github.com/kufu/activerecord-bitemporal/pull/70
# L330をObjectに戻す
vi lib/activerecord-bitemporal/bitemporal.rb
rbenv local 3.0.0
bundle i --path .bundle
# 指定したレコードの関連の探索処理
#
# @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
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", '~> 6.1.0'
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", '~> 6.0.0'
@alpaca-tc
alpaca-tc / activemodel-inspector.rb
Created September 20, 2020 08:01
こういうのが欲しいけれど、どうやったらAM::Modelに入りうるだろうか...
module ActiveModelAttributesInspector
# @return [String]
def inspect
values = attributes.map { |key, value| "#{key}: #{value.inspect}" }.join(', ')
%(<#{self.class.name} #{values}>)
end
# @param pp [PP]
#
# @return [void]