-
Notifications
You must be signed in to change notification settings - Fork 898
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
Replace "some" default_value_for usage #22901
Conversation
0aa80d1
to
39c3e5f
Compare
after_create :audit_creation | ||
after_destroy :reload_all_server_settings, :audit_deletion | ||
after_save :reload_all_server_settings |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the cop wanted these sorted by their order... it's only the after_validation that's new
@@ -75,7 +73,7 @@ def tz_or_default(default_tz = DEFAULT_TZ) | |||
end | |||
|
|||
def days | |||
profile[:days] | |||
profile.fetch(:days, ALL_DAYS) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
attribute :days, :default => ALL_DAYS
Is there a reason to not just set the default on this attribute?
the default_value
overriding what comes back from the database (vs creating the object) is a little suspect. Glad we are getting rid of this.
Do we want to db migrate profiles and set ALL_DAYS
for time profiles with no :days
in the hash?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe I tried that and existing tests failed because it's expecting this hash structure on the getter since it's done on the setter. I 100% agree with the idea of migrating this out and simplifying it. I don't know why it was done this way.
the default_value overriding what comes back from the database (vs creating the object) is a little suspect. Glad we are getting rid of this.
Good point. Let me check into it. most of the defaults were to set it if it's not set. We probably don't have good tests if I was able to ignore the value in the database. I think you found a bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I took a look. I think this is fine for now. I agree, we should stop storing the days/hours in the profile column.
Still think using But keeping this in ruby does have its advantages |
def enabled=(value) | ||
return if enabled == value | ||
|
||
super | ||
$audit_log.info("Blacklisted event [#{event_name}] for provider [#{provider_model}] with ID [#{ems_id}] had enabled changed to #{value} by user [#{self.class.current_userid}]") | ||
def log_enabling | ||
$audit_log.info("Blacklisted event [#{event_name}] for provider [#{provider_model}] with ID [#{ems_id}] had enabled changed to #{enabled} by user [#{self.class.current_userid}]") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I really like the new solution here
- Do we need this audit trail?
- Can you add
new_record?
here? (maybe put abyebug
in this method and create a new record - see if it goes in here. I worry seeding will produce a slew of these)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I'll check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, it has the same exact behavior as before.
Seed with empty db... messages showing it was created, see audit_creation
, then, became enabled. I'll add a check to change the behavior to only do it unless new_record?
. I agree, it was a bug before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pushed change to skip if new_record? and added tests
@@ -3,7 +3,7 @@ | |||
class Endpoint < ApplicationRecord | |||
belongs_to :resource, :polymorphic => true | |||
|
|||
default_value_for :verify_ssl, OpenSSL::SSL::VERIFY_PEER | |||
attribute :verify_ssl, :default => OpenSSL::SSL::VERIFY_PEER |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: OpenSSL::SSL::VERIFY_PEER == 1
3ece4e3
to
e3efcc0
Compare
Note, defaults applied to new objects count as "changed", so the test needs to be changed to do 2 queries.
e3efcc0
to
48e1183
Compare
Checked commits jrafanie/manageiq@5b5e927~...48e1183 with ruby 2.7.8, rubocop 1.56.3, haml-lint 0.51.0, and yamllint spec/lib/extensions/ar_yaml_spec.rb
|
@kbrock pushed minor changes. I think you raise good questions for followup. I think the changes here are minimized to avoid changing behavior without using default_value_for. |
@@ -3,7 +3,7 @@ | |||
describe "#uniqueness_when_changed" do | |||
it "queries for a new record" do | |||
alert = FactoryBot.build(:miq_alert) | |||
expect { alert.valid? }.to make_database_queries(:count => 1) | |||
expect { alert.valid? }.to make_database_queries(:count => 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sigh
Not thrilled about getting a changed event for a default value.
I had thought that this changed the base metadata (so it acted as if the database specified the default value), but this suggests that is not true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
haha, yup... default_value_for masked changes... we can do the same but I was hoping we didn't need to.
Revert "Merge pull request #22901 from jrafanie/drop_default_value_for"
tldr; I think these are mostly safe changes that gets us closer to not depending on
default_value_for
. There's more to do but we can tackle that later if we really want to not use it anymore.From research in upgrading default_value_for, it seems reasonable to replace it and not have to deal with getting this gem to work with rails as we upgrade. Currently, the gem's master branch supports rails 7 but hasn't been released so we need to ride master to work with rails 7 or fork it.
I've found the attributes API is mostly a replacement but it is different than default_value_for in the following ways:
super
method's return isnil
({}
serialize as hash attributes) explicitly or coming from the defaults from rails. I suspect there's a way to do it correctly but it's less obvious.allow_nil: false
so if anil
is provided as a value for an attribute, the default value will not be used. NOTE: I don't see much usage of this.EDIT:
This PR avoids the more complicated changes where we used a block and the corresponding instance in the block. The remaining ones in core should probably be done in a followup PR. Either way, we can choose to keep default_value_for, but I thought it made sense to switch to vanilla rails for the more basic cases. This is what remains in core (note, there's also usage in providers/plugins):