Skip to content

Commit

Permalink
Allow ActiveSnapshot.config to be called before ActiveRecord on_load …
Browse files Browse the repository at this point in the history
…hook has occurred (#53)

Prevents errors in application initializers using `ActiveSnapshot.config` like:

```
undefined method `config' for module ActiveSnapshot (NoMethodError)
```

Also resolves failures in the test suite regarding different storage_method configs
  • Loading branch information
westonganger authored Aug 21, 2024
1 parent 568d9f8 commit 367e974
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ CHANGELOG

- **Unreleased**
* [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.4.0...master)
* [#xx]((https://github.com/westonganger/active_snapshot/pull/xx) Remove :identifier argument as a positional argument
* [#53](https://github.com/westonganger/active_snapshot/pull/53) - Allow `ActiveSnapshot.config` to be called before ActiveRecord on_load hook has occurred
* [#52]((https://github.com/westonganger/active_snapshot/pull/52) Remove :identifier argument as a positional argument

- **v0.4.0** - July 23, 2024
* [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.3.2...v0.4.0)
Expand Down
24 changes: 13 additions & 11 deletions lib/active_snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@

require 'active_support/lazy_load_hooks'

module ActiveSnapshot
@@config = ActiveSnapshot::Config.new

def self.config(&block)
if block_given?
block.call(@@config)
else
return @@config
end
end
end

ActiveSupport.on_load(:active_record) do
require "active_snapshot/models/snapshot"
require "active_snapshot/models/snapshot_item"

require "active_snapshot/models/concerns/snapshots_concern"

module ActiveSnapshot
ActiveSnapshot.module_eval do
extend ActiveSupport::Concern

included do
include ActiveSnapshot::SnapshotsConcern
end

@@config = ActiveSnapshot::Config.new

def self.config(&block)
if block_given?
block.call(@@config)
else
return @@config
end
end
end
end
14 changes: 11 additions & 3 deletions test/models/snapshot_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_validations
snapshot = shared_post.snapshots.first

instance = @snapshot_klass.new

instance.valid?

[:item_id, :item_type].each do |attr|
Expand All @@ -67,7 +67,11 @@ def test_metadata

@snapshot.metadata = {foo: :bar}

assert_equal "bar", @snapshot.metadata['foo']
if ActiveSnapshot.config.storage_method_yaml?
assert_equal :bar, @snapshot.metadata.fetch(:foo)
else
assert_equal "bar", @snapshot.metadata.fetch("foo")
end
end

def test_build_snapshot_item
Expand Down Expand Up @@ -139,7 +143,11 @@ def test_single_model_snapshots_without_children
prev_time_attrs = prev_attrs.extract!("created_at","updated_at")
new_time_attrs = new_attrs.extract!("created_at","updated_at")

assert_equal new_time_attrs.values.map{|x| x.round(3)}, new_time_attrs.values
if ActiveSnapshot.config.storage_method_yaml?
assert_equal new_time_attrs.values.map{|x| x.round(6)}, new_time_attrs.values
else
assert_equal new_time_attrs.values.map{|x| x.round(3)}, new_time_attrs.values
end

### rounding to 3 sometimes fails due to millisecond precision so we just test for 2 decimal places here
assert_equal prev_time_attrs.values.map{|x| x.round(2)}, new_time_attrs.values.map{|x| x.round(2)}
Expand Down
9 changes: 7 additions & 2 deletions test/unit/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def teardown
end

def test_defaults_to_serialized_json
if ENV["ACTIVE_SNAPSHOT_STORAGE_METHOD"].present?
skip
end

assert_equal 'serialized_json', ActiveSnapshot.config.storage_method

assert_equal false, ActiveSnapshot.config.storage_method_yaml?
Expand Down Expand Up @@ -51,10 +55,11 @@ def test_accepts_native_json
end

def test_config_doesnt_accept_not_specified_storage_methods
assert ActiveSnapshot.config.storage_method.present?
assert_raise do
ActiveSnapshot.config.storage_method = 'foobar'
ActiveSnapshot.config.storage_method = "foobar"
end
assert_equal "serialized_json", ActiveSnapshot.config.storage_method
refute_equal "foobar", ActiveSnapshot.config.storage_method
end

def test_converts_symbol_to_string
Expand Down

0 comments on commit 367e974

Please sign in to comment.