Module: Russian::RailsIntegration
- Defined in:
- lib/russian/russian_rails.rb
Overview
Installs Rails-specific patches exposed by the gem.
The integration is lazy: outside Rails it does nothing, and inside Rails it wires itself through ActiveSupport.on_load hooks and also patches framework components that are already loaded.
Устанавливает Rails-специфичные патчи, поставляемые gem’ом.
Интеграция сделана “ленивой”: вне Rails она ничего не делает, а внутри Rails подключается через ActiveSupport.on_load и дополнительно патчит уже загруженные части фреймворка.
Class Method Summary collapse
- .define_railtie! ⇒ Object
-
.install! ⇒ void
Installs Rails hooks and applies patches to already loaded components.
- .install_hooks! ⇒ Object
- .on_load(hook, patcher) ⇒ Object
- .patch!(target, extension) ⇒ Object
- .patch_action_view! ⇒ Object
- .patch_active_model! ⇒ Object
- .rails? ⇒ Boolean
Class Method Details
.define_railtie! ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/russian/russian_rails.rb', line 86 def define_railtie! return unless defined?(Rails::Railtie) return if defined?(Russian::Railtie) Russian.const_set(:Railtie, Class.new(Rails::Railtie) do initializer "russian.install" do Russian::RailsIntegration.install! end end) end |
.install! ⇒ void
This method returns an undefined value.
Installs Rails hooks and applies patches to already loaded components.
The method is idempotent and safe to call more than once.
Устанавливает Rails-хуки и применяет патчи к уже загруженным компонентам.
Метод идемпотентен и безопасен для повторных вызовов.
39 40 41 42 43 44 45 46 47 |
# File 'lib/russian/russian_rails.rb', line 39 def install! return unless rails? define_railtie! install_hooks! patch_active_model! if defined?(ActiveModel::Error) patch_action_view! if defined?(ActionView::Helpers::DateTimeSelector) end |
.install_hooks! ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/russian/russian_rails.rb', line 50 def install_hooks! return if @hooks_installed %i[active_model active_model_error].each { |hook| on_load(hook, :patch_active_model!) } on_load(:action_view, :patch_action_view!) @hooks_installed = true end |
.on_load(hook, patcher) ⇒ Object
60 61 62 |
# File 'lib/russian/russian_rails.rb', line 60 def on_load(hook, patcher) ActiveSupport.on_load(hook) { Russian::RailsIntegration.public_send(patcher) } end |
.patch!(target, extension) ⇒ Object
76 77 78 |
# File 'lib/russian/russian_rails.rb', line 76 def patch!(target, extension) target.prepend(extension) unless target < extension end |
.patch_action_view! ⇒ Object
70 71 72 73 |
# File 'lib/russian/russian_rails.rb', line 70 def patch_action_view! patch!(ActionView::Helpers::DateHelper, Russian::ActionViewExt::Helpers::DateHelperPatch) patch!(ActionView::Helpers::DateTimeSelector, Russian::ActionViewExt::Helpers::DateTimeSelectorPatch) end |
.patch_active_model! ⇒ Object
65 66 67 |
# File 'lib/russian/russian_rails.rb', line 65 def patch_active_model! patch!(ActiveModel::Error.singleton_class, Russian::ActiveModelExt::ErrorPatch) end |
.rails? ⇒ Boolean
81 82 83 |
# File 'lib/russian/russian_rails.rb', line 81 def rails? defined?(Rails::Railtie) && defined?(ActiveSupport) && ActiveSupport.respond_to?(:on_load) end |