This file contains hidden or 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
# 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' |
This file contains hidden or 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 :phone_number, :number_string | |
class NumberString < ActiveModel::Type::String | |
# 与えられた値を数値のみの文字列、またはnilに変換する | |
# | |
# @param value [Object] | |
# | |
# @return [String, NilClass] | |
def cast(value) | |
super(value)&.remove(/\D/) |
This file contains hidden or 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() |
This file contains hidden or 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 |
This file contains hidden or 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 |
This file contains hidden or 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 |
This file contains hidden or 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) |
This file contains hidden or 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}" |
This file contains hidden or 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 |
This file contains hidden or 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 |