Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Register the Active Record and Action View integrations when the gem is required, instead of from the railtie initializer
- Add a top-level `null` option to the `add_monetize`/`monetize` migration helpers (#775)

## 3.0.0
Expand Down
14 changes: 14 additions & 0 deletions lib/money-rails.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "active_support/lazy_load_hooks"
require "money"
require "monetize"
require "monetize/core_extensions"
Expand All @@ -15,6 +16,19 @@ module MoneyRails
extend Configuration
end

# Registering the hooks here, rather than in the railtie initializer, keeps the
# integration points visible without a full Rails boot. Registration is lazy: the
# blocks only run once the framework in question is loaded.
ActiveSupport.on_load(:active_record) do
require "money-rails/active_model/validator"
require "money-rails/active_record/monetizable"
ActiveRecord::Base.include(MoneyRails::ActiveRecord::Monetizable)
end

ActiveSupport.on_load(:action_view) do
ActionView::Base.include MoneyRails::ActionViewExtension
end

if defined? Rails::Railtie
require "money-rails/railtie"
require "money-rails/engine"
Expand Down
14 changes: 4 additions & 10 deletions lib/money-rails/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ class Hooks
PG_ADAPTERS = %w[activerecord-jdbcpostgresql-adapter postgresql postgis].freeze

def self.init
# For Active Record
# For Active Record migrations
#
# These stay here, and not in money-rails.rb, because picking the migration
# extensions requires a configured database connection.
ActiveSupport.on_load(:active_record) do
require "money-rails/active_model/validator"
require "money-rails/active_record/monetizable"
::ActiveRecord::Base.include(MoneyRails::ActiveRecord::Monetizable)

current_adapter = ::ActiveRecord::Base.connection_db_config.configuration_hash[:adapter]
postgresql_with_money = PG_ADAPTERS.include?(current_adapter)

Expand Down Expand Up @@ -38,11 +37,6 @@ def self.init
require "money-rails/mongoid/money"
end

# For ActionView
ActiveSupport.on_load(:action_view) do
::ActionView::Base.include MoneyRails::ActionViewExtension
end

# For ActiveSupport
ActiveSupport.on_load(:active_job) do
if defined?(::ActiveJob::Serializers)
Expand Down
23 changes: 23 additions & 0 deletions spec/money_rails/load_hooks_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "spec_helper"

if defined? ActiveRecord
describe MoneyRails do
describe "load hooks" do
it "monetizes Active Record without a Rails initializer" do
script = <<~RUBY
require "money-rails"
raise "requiring money-rails eagerly loaded Active Record" if defined?(::ActiveRecord)

require "active_record"
raise "monetize is missing" unless ActiveRecord::Base.respond_to?(:monetize)
RUBY

lib_path = File.expand_path("../../lib", __dir__)

expect(system(RbConfig.ruby, "-I", lib_path, "-e", script)).to be(true)
end
end
end
end
Loading