JackDanger/permanent_records

Select which models to become permanent records

mariochavez opened this issue · 4 comments

Hi,

I have a case where not all model in my app needs to become permanent_records but the issue is that Permanent Records adds itself to all Activerecords with:

ActiveSupport.on_load(:active_record) do
  ActiveRecord::Base.send :include, PermanentRecords::ActiveRecord
end

My proposal is to have a setting to:

  1. Decide if we want Permanent Records in all models - as it is right now -
  2. Add Permanent Records to only those that need it.

Please let me know if you are willing to accept a PR for this.

As long as it doesn't break the default functionality for existing users then a PR for this would be lovely!

@JackDanger sure by default it will work as it is.

joel commented

@mariochavez very good initiative 👍

I hope someone adds this, but in the meantime here is a workaround:

class MyModel < ApplicationRecord
  def self.dangerous_class_method?(method_name)
    # Let us define our own not_deleted scope,
    # overriding the one defined by permanent_records.
    # For more info see:
    # https://github.com/JackDanger/permanent_records/issues/92
    if method_name.to_s == 'not_deleted'
      false
    else
      super(method_name)
    end
  end
end

This approach to replacing class methods defined on ActiveRecord::Base seems to be endorsed by the Rails team.