From f940a154f31460d781f68ba4af0bb4fc5b0e632a Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Thu, 1 Jun 2017 22:41:03 +0100 Subject: [PATCH] Replace alias_method_chain with Module#prepend --- config/initializers/abstract_adapter.rb | 18 +++---- config/initializers/i18n.rb | 27 +++++----- config/initializers/router.rb | 18 +++---- lib/migrate.rb | 72 ++++++++++++------------- 4 files changed, 63 insertions(+), 72 deletions(-) diff --git a/config/initializers/abstract_adapter.rb b/config/initializers/abstract_adapter.rb index 6bb5d10c0..290e2493d 100644 --- a/config/initializers/abstract_adapter.rb +++ b/config/initializers/abstract_adapter.rb @@ -1,21 +1,17 @@ if defined?(ActiveRecord::ConnectionAdaptors::AbstractAdapter) - module ActiveRecord - module ConnectionAdapters - class AbstractAdapter - protected - - alias old_log log - - def translate_exception_class_with_timeout(e, sql) + module OSM + module AbstractAdapter + module PropagateTimeouts + def translate_exception_class(e, sql) if e.is_a?(Timeout::Error) || e.is_a?(OSM::APITimeoutError) e else - translate_exception_class_without_timeout(e, sql) + super(e, sql) end end - - alias_method_chain :translate_exception_class, :timeout end end end + + ActiveRecord::ConnectionAdaptors::AbstractAdapter.prepend(OSM::AbstractAdapter::PropagateTimeouts) end diff --git a/config/initializers/i18n.rb b/config/initializers/i18n.rb index a94618c7e..fff69378b 100644 --- a/config/initializers/i18n.rb +++ b/config/initializers/i18n.rb @@ -8,31 +8,32 @@ module I18n ex.entry[:other] end end + end +end - class Simple - def store_translations_with_normalisation(locale, data, options = {}) - locale = I18n::Locale::Tag::Rfc4646.tag(locale).to_s +module OSM + module I18n + module NormaliseLocales + def store_translations(locale, data, options = {}) + locale = ::I18n::Locale::Tag::Rfc4646.tag(locale).to_s - store_translations_without_normalisation(locale, data, options) + super(locale, data, options) end - - alias_method_chain :store_translations, :normalisation end - end - module JS - class FallbackLocales - def default_fallbacks_with_validation - default_fallbacks_without_validation.select do |locale| + module ValidateLocales + def default_fallbacks + super.select do |locale| ::I18n.available_locales.include?(locale) end end - - alias_method_chain :default_fallbacks, :validation end end end +I18n::Backend::Simple.prepend(OSM::I18n::NormaliseLocales) +I18n::JS::FallbackLocales.prepend(OSM::I18n::ValidateLocales) + I18n::Backend::Simple.include(I18n::Backend::PluralizationFallback) I18n::Backend::Simple.include(I18n::Backend::Fallbacks) diff --git a/config/initializers/router.rb b/config/initializers/router.rb index 2987e424a..0987fa018 100644 --- a/config/initializers/router.rb +++ b/config/initializers/router.rb @@ -1,18 +1,14 @@ # Some versions of ruby seem to accidentally force the encoding # as part of normalize_path and some don't -module ActionDispatch - module Journey - class Router - class Utils - def self.normalize_path_with_encoding(path) - normalize_path_without_encoding(path).force_encoding("UTF-8") - end - - class << self - alias_method_chain :normalize_path, :encoding - end +module OSM + module Router + module ForceEncoding + def normalize_path(path) + super(path).force_encoding("UTF-8") end end end end + +ActionDispatch::Journey::Router::Utils.singleton_class.prepend(OSM::Router::ForceEncoding) diff --git a/lib/migrate.rb b/lib/migrate.rb index 21c1b2cf2..b9917df6d 100644 --- a/lib/migrate.rb +++ b/lib/migrate.rb @@ -1,50 +1,48 @@ -module ActiveRecord - module ConnectionAdapters - module SchemaStatements - def add_index_options_with_columns(table_name, column_name, options = {}) - columns = options.delete(:columns) - index_name, index_type, index_columns, index_options, algorithm, using = add_index_options_without_columns(table_name, column_name, options) - [index_name, index_type, columns || index_columns, index_options, algorithm, using] - end - - alias_method_chain :add_index_options, :columns +module OSM + module SchemaStatements + def add_index_options(table_name, column_name, options = {}) + columns = options.delete(:columns) + index_name, index_type, index_columns, index_options, algorithm, using = super(table_name, column_name, options) + [index_name, index_type, columns || index_columns, index_options, algorithm, using] end + end - module PostgreSQL - module Quoting - def quote_column_name_with_arrays(name) - Array(name).map { |n| quote_column_name_without_arrays(n) }.join(", ") - end - - alias_method_chain :quote_column_name, :arrays + module PostgreSQL + module Quoting + def quote_column_name(name) + Array(name).map { |n| super(n) }.join(", ") end + end - module SchemaStatements - def add_primary_key(table_name, column_name, _options = {}) - execute "ALTER TABLE #{quote_table_name(table_name)} ADD PRIMARY KEY (#{quote_column_name(column_name)})" - end + module SchemaStatements + def add_primary_key(table_name, column_name, _options = {}) + execute "ALTER TABLE #{quote_table_name(table_name)} ADD PRIMARY KEY (#{quote_column_name(column_name)})" + end - def remove_primary_key(table_name) - execute "ALTER TABLE #{quote_table_name(table_name)} DROP PRIMARY KEY" - end + def remove_primary_key(table_name) + execute "ALTER TABLE #{quote_table_name(table_name)} DROP PRIMARY KEY" + end - def alter_primary_key(table_name, new_columns) - execute "ALTER TABLE #{quote_table_name(table_name)} DROP CONSTRAINT #{quote_table_name(table_name + '_pkey')}" - execute "ALTER TABLE #{quote_table_name(table_name)} ADD PRIMARY KEY (#{quote_column_name(new_columns)})" - end + def alter_primary_key(table_name, new_columns) + execute "ALTER TABLE #{quote_table_name(table_name)} DROP CONSTRAINT #{quote_table_name(table_name + '_pkey')}" + execute "ALTER TABLE #{quote_table_name(table_name)} ADD PRIMARY KEY (#{quote_column_name(new_columns)})" + end - def create_enumeration(enumeration_name, values) - execute "CREATE TYPE #{enumeration_name} AS ENUM ('#{values.join '\',\''}')" - end + def create_enumeration(enumeration_name, values) + execute "CREATE TYPE #{enumeration_name} AS ENUM ('#{values.join '\',\''}')" + end - def drop_enumeration(enumeration_name) - execute "DROP TYPE #{enumeration_name}" - end + def drop_enumeration(enumeration_name) + execute "DROP TYPE #{enumeration_name}" + end - def rename_enumeration(old_name, new_name) - execute "ALTER TYPE #{quote_table_name(old_name)} RENAME TO #{quote_table_name(new_name)}" - end + def rename_enumeration(old_name, new_name) + execute "ALTER TYPE #{quote_table_name(old_name)} RENAME TO #{quote_table_name(new_name)}" end end end end + +ActiveRecord::ConnectionAdapters::SchemaStatements.extend(OSM::SchemaStatements) +ActiveRecord::ConnectionAdapters::PostgreSQL::Quoting.extend(OSM::PostgreSQL::Quoting) +ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaStatements.extend(OSM::PostgreSQL::SchemaStatements) -- 2.39.5