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

Initialize model callbacks on boot #37

Merged
merged 6 commits into from
Aug 16, 2022
Merged

Initialize model callbacks on boot #37

merged 6 commits into from
Aug 16, 2022

Conversation

duncanjbrown
Copy link
Contributor

See commits.

Manually mixing the behaviour in to ActiveRecord classes does not achieve what we want to achieve: it missed out HABTM relations. So hook up everything automatically — analytics.yml will determine what gets sent. As part of this piece of work we now refuse to boot if that file isn't correct, which should surface configuration issues as early as possible.

Depends on #36.

model = model_for_entity(entity)
if model.include?(DfE::Analytics::Entities) && !@shown_deprecation_warning
Rails.logger.info("DEPRECATION WARNING: DfE::Analytics::Entities was manually included in a model (#{model.name}), but it's included automatically since v1.4. You're running v#{DfE::Analytics::VERSION}. To silence this warning, remove the include from model definitions in app/models.")
@shown_deprecation_warning = true
Copy link
Contributor

Choose a reason for hiding this comment

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

It's tempting to include a deprecation warning for every model that does this. But, then, if it's only included in ApplicationRecord then that might be annoying.

I'd go for annoy and apologise later. 😛

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 agree, let's be nuisances

# not. To avoid this specific conflict with devise and possibly other
# gems/engines, proactively load the routes unless
# configuration.eager_load is set.
Rails.application.reload_routes! unless Rails.configuration.eager_load
Copy link
Contributor

Choose a reason for hiding this comment

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

Did we only need to do this in development env? Or do we not need to check that because in non-dev the routes will be loaded at this point anyway? I don't quite remember what we said, but might be worth explaining why this is ok to do in production if there's anything interesting to say.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will beef up this comment to explain what default configs there are in prod/dev

@misaka
Copy link
Contributor

misaka commented Aug 9, 2022

Might be worth baseing this PR onto the with_model PR, just to keep it's diff set clean?

@duncanjbrown duncanjbrown changed the base branch from main to with_model August 9, 2022 10:16
@duncanjbrown
Copy link
Contributor Author

OK: all updated

Base automatically changed from with_model to main August 12, 2022 09:40
We're going to use this on boot so we can be a bit more relaxed inside
the gem
Now that apps will panic on boot if there's invalid config, it's
necessary to put a correct analytics.yml file into the dummy app to make
the specs work
This means that we attach callbacks to many-to-many association tables
that don't have explicit models inheriting from ApplicationRecord

For some reason, presumably due to execution order, it's necessary to
move the route declarations from the around block to the before block in
analytics_spec.rb
@misaka misaka merged commit d6e181e into main Aug 16, 2022
@misaka misaka deleted the init-2 branch August 16, 2022 21:20
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.

2 participants