westonganger / active_snapshot

Simplified snapshots and restoration for ActiveRecord models and associations with a transparent white-box implementation
MIT License
103 stars 16 forks source link

Use ActiveRecord lazy loading #34

Closed AmShaegar13 closed 1 year ago

AmShaegar13 commented 1 year ago

Currently, the gem is loaded right away and requires active_record. When this happens, the database configuration is required even to run basic tasks like assets:precompile on a build host, which is suboptimal. There are two possible ways to prevent that:

  1. Set require: false for the active_snapshot gem in Gemfile and add an initializer that lazy loads it:

    gem 'active_snapshot', '~> 0.3.0', require: false

ActiveSupport.on_load(:active_record) do
  require 'active_snapshot'
end

However, this will make the user required for a snapshot. I don't know why exactly but it looks like this is a bug anyway. In Rails 7, belongs_to will always add a presence validation. When lazy loading the gem, this actually works so the user is not optional anymore.

  1. Merge this pull request. It will mark the user optional and lazy load all the code. Therefor, I had to move the requirement of active_record' totest_helper.rb`.
westonganger commented 1 year ago

Yes :user should be optional. Lets do that separate from this PR and add a test for it.

AmShaegar13 commented 1 year ago

Done.

westonganger commented 1 year ago

Were you planning to create a separate PR to add optional: true to :user association?

westonganger commented 1 year ago

Closing this as I've merged #36 which modifies this approach slightly.