View wrapped_type.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage | |
# attribute :wrapped_type, klass: UserModel, array: true | |
class WrappedType < ActiveModel::Type::Value | |
KEYWORD_PARAMETERS = %i[keyreq key].freeze | |
private_constant(:KEYWORD_PARAMETERS) | |
# @param [Class, String] klass 変換したいクラス | |
# @param [Boolean] array 配列で変換するかどうか | |
def initialize(klass:, array: false) | |
super() |
View stripped_string.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StrippedString < ActiveModel::Type::String | |
# 左右の空白を除去した文字列クラス | |
# | |
# @param [Object] value | |
# | |
# @return [String, nil] | |
def cast(value) | |
super(value)&.strip&.presence | |
end |
View association_owner_validator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 使い方 | |
# validates_with( | |
# AssociationOwnerValidator, | |
# associations: { | |
# sub_item: :user | |
# }, | |
# owner: -> { profile_image.user }, | |
# if: %i[sub_item profile_image] | |
# ) | |
class AssociationOwnerValidator < ActiveModel::Validator |
View boolean_validator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
View ridgepole_unsigned_primary_id.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Schemafileで下記のように読み込む | |
# require_relative './lib/ridgepole_unsigned_primary_id' | |
# `create_table`, `t.references` で、常に `unsigned: true` を有効にするための拡張 | |
# 個別に指定することもできるが、可読性が悪くなるのでデフォルトとする | |
module UnsingedReferences | |
def references(*args) | |
options = args.extract_options! | |
options[:unsigned] = true | |
args.push(options) |
View ridgepole_hashed_index_name.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Schemafileから | |
# require_relative './lib/ridgepole_hashed_index_name' | |
# して使う | |
module RidgepoleHashedIndexName | |
def index_name(table_name, options) | |
if options.is_a?(Hash) && options[:column] | |
identifier = super | |
hashed_identifier = Digest::SHA256.hexdigest(identifier).first(10) | |
"index_rails_#{hashed_identifier}" |
View active_record_dowsing.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
dowsing = Module.new do | |
def execute(sql, *) | |
source_location = query_source_location(caller.lazy) | |
sql = "#{sql} /* #{source_location} */" if source_location | |
super | |
end | |
private |
View sidekiq_cron_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
View factory_bot_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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| |
View missing_model_i18n_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |