Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Audited.audit_class= to receive a string class name and return … #648

Merged
merged 1 commit into from
Jan 23, 2023

Conversation

rocket-turtle
Copy link
Contributor

…an class

@rocket-turtle
Copy link
Contributor Author

Would it be better to remove the String case and add that to the Readme

#609 (comment)

ActiveSupport.on_load :active_record do
  Audited.audit_class = CustomAudit
end

I do not understand why this should not work. maybe phortx can describe the problem.
#609 (comment)

@@ -13,12 +13,13 @@ class << self
attr_writer :audit_class
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you should replace attr_writer with a manual implementation to avoid the overhead in the getter:

def audit_class=(cls)
  @audit_class = cls.is_a?(String) ? cls.constantize : cls
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had it like this in my first draft but I think it would not help.
The constantize would be called directly on the setting (in the initializer).

I think it would be the same as writing: Audited.audit_class = "CustomAudit".constantize

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's an interesting thing. Perhaps a comment in the audit_class why it's there (like "it has to be lazy") could help future readers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to add a comment :)

@@ -13,12 +13,13 @@ class << self
attr_writer :audit_class

def audit_class
@audit_class ||= "Audited::Audit"
@audit_class = @audit_class.constantize if @audit_class.is_a?(String)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not like this case but it is better than the old unmemorized audit_class.constantize in audit_model

lib/audited.rb Outdated Show resolved Hide resolved
lib/audited.rb Outdated Show resolved Hide resolved
@danielmorrison danielmorrison marked this pull request as ready for review January 23, 2023 21:40
@danielmorrison danielmorrison merged commit 904cc69 into collectiveidea:main Jan 23, 2023
@ekohl
Copy link

ekohl commented Jan 24, 2023

Thanks for this. I've opened theforeman/foreman#9595 to unpin again. Let's see if CI agrees this works :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants