-
Notifications
You must be signed in to change notification settings - Fork 438
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
Refactor Event module #4191
Refactor Event module #4191
Conversation
Have every `Event::Build` subclass in a different file, as it is normally done in Rails.
Have every `Event::Package` subclass in a different file, as it is normally done in Rails.
Have every `Event::Project` subclass in a different file, as it is normally done in Rails.
Have every `Event::Request` subclass in a different file, as it is normally done in Rails.
Now that all the classes follow Rails name convention, we do not need to include the files one by one. Remove also the `require_dependency 'event/all'` in `request_controller`.
As Event module is now structure in a Rails way the requires are not longer needed.
When moving the code into different classes the exclusion in the `rubocop_todo.yml` doesn't have effect any more. Also, we had an inclussion of a helper outside a class/module. Rubocop was not complainning before due to a bug, that is already fixed in master and will be in the next release. Regenerating the todo file solve this as well. As we need to solve this in more places, it seems to be fine to exclude it by now.
To allow that the classes which includes it can find it, as now we do things in the Rails way.
Using `Event::Class` oblies us to include `CommentEvent` with an explicit namespace qualifier (`Event::CommentEvent`), because Ruby will not look inside `Event` for this name.
For consistency with the rest classes of the `Event` module.
And now that we are following Rails way, they are correct!
`Event::Package` is a class with almost nothing that is used as superclass for: - `Event::BranchCommand` - `Event::Build` - `Event::CommentForPackage` - `Event::Commit` - `Event::CreatePackage` - `Event::DeletePackage` - `Event::ServiceFail` - `Event::ServiceSuccess` - `Event::UndeletePackage` - `Event::UpdatePackage` - `Event::Upload` - `Event::VersionChange` The `Event::Package` causes problem as we also have a `Package` model in the root level. So once one of the two packages models is loaded, Rails doens't look for the second one, which lead to uses of the wrong model. To avoid that Rails uses the `Package` in the root level in `Event::Package` subclasses we can call the Package specifying the module. But for the other case there is nothing we can do. This is all caused because we shouldn't have models with repeated names even if they are in different levels. As the `Event::Package` is useless, we can just remove it and add its data directly to the subclasses.
`Event::Package` is a class with almost nothing that is used as superclass for: - `Event::CommentForProject` - `Event::CreateProject` - `Event::DeleteProject` - `Event::UndeleteProject` - `Event::UpdateProjectConfig` - `Event::UpdateProject` The `Event::Project` causes problem as we also have a `Project` model in the root level. So once one of the two projects models is loaded, Rails doens't look for the second one, which lead to uses of the wrong model. To avoid that Rails uses the `Project` in the root level in `Event::Project` subclasses we can call the Project specifying the module. But for the other case there is nothing we can do. This is all caused because we shouldn't have models with repeated names even if they are in different levels. As the `Event::Project` is useless, we can just remove it and add its data directly to the subclasses.
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.
Nice work! Couple small comments but nothing major. Thanks
@@ -1 +1,2 @@ | |||
require_relative 'event/all' | |||
module Event |
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'm pretty sure this file isn't necessary?
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.
If not Event
is not defined. You'll get something like:
LoadError: Unable to autoload constant Event, expected /obs/src/api/app/models/event.rb to define it
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.
What line does that error come from?
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.
That's what you get if you try to access to Event
from the console.
I created another branch exactly like this one, but deleting this two lines and that's the Travis result: https://travis-ci.org/Ana06/open-build-service/builds/312423246
As you can see it fails as Event definition is not where Rails expected it to be (see rspec for example). So it can not be removed. 😉
"Event::DeleteProject", | ||
"Event::UndeleteProject", | ||
"Event::UpdateProjectConfig", | ||
"Event::UpdateProject"] |
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.
Wouldn't the best thing here be to list all subclasses of Event
? Otherwise we have to update this file if we add a new Event class.
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.
But it is not all of the subclasses of Event. This is only the subclasses of the old Event::Package
and Event::Project
. So now, the classes in Event
related to Project
and Package
. I think the only way is to list them directly.
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.
@Ana06 Please add a comment, mentioning those are only the subclasses related to Project and Package
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.
@bgeuken ok! 👍
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.
Looks good. Well done!
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.
Only nitpicks from my side. Haven't had time to fully review it.
PS: I know that you sometimes copied just lines but it would by very cool if you could refactor my nitpicks if you have time.
"#{Configuration.amqp_namespace}.package.build_unchanged" | ||
def custom_headers | ||
mid = my_message_id | ||
h = super |
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.
Please don't use single-character variables. Make it more verbose, please.
end | ||
|
||
def custom_headers | ||
h = super |
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.
Please don't use single-character variables. Make it more verbose, please.
end | ||
|
||
def expanded_payload | ||
p = payload.dup |
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.
Please don't use single-character variables. Make it more verbose, please.
end | ||
|
||
def custom_headers | ||
h = super |
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.
Please don't use single-character variables. Make it more verbose, please.
end | ||
def custom_headers | ||
mid = my_message_number | ||
h = super |
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.
Please don't use single-character variables. Make it more verbose, please.
end | ||
|
||
def custom_headers | ||
h = super |
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.
Please don't use single-character variables. Make it more verbose, please.
end | ||
|
||
def custom_headers | ||
h = super |
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.
Please don't use single-character variables. Make it more verbose, please.
end | ||
|
||
def custom_headers | ||
h = super |
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.
Please don't use single-character variables. Make it more verbose, please.
this is about refactoring how the |
I could have assigned the classes array to event_classes, but as the `flat_map(&:descendants)` is no longer needed in ``` event_classes.flat_map(&:descendants).map(&:name) ``` having an array with the name Strings directly seems to be better.
He said those comments were nitpicks though so if you dont want to do them you dont have to.. |
Refactor
More specifically:
Event
module, to make things in a more Rails way, so we don't need to do strange things to use this moduleEvent
in a different file, as it is normally done in Rails.CommentEvent
in theEvent
module.Event::Package
.There are more details in the commit messages.