-
Notifications
You must be signed in to change notification settings - Fork 184
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
Store country band details in database #10476
base: main
Are you sure you want to change the base?
Conversation
d02c82a
to
9c50e1a
Compare
@@ -10,7 +10,7 @@ def index | |||
|
|||
def edit | |||
@number = id_from_params | |||
unless CountryBand::BANDS.keys.include?(@number) | |||
unless CountryBandDetail.distinct.pluck(:number).include?(@number) |
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.
In case of CountryBand, where there's only like 5 or so entries the whole time, loading all values from the DB into memory won't be a big problem. But in general, loading the whole column into memory just for a simple include?
check is a big problem, so let me show you the cool SQL way
unless CountryBandDetail.distinct.pluck(:number).include?(@number) | |
unless CountryBandDetail.exists?(number: @number) |
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.
This (roughly, not sure how Rails does internal optimizations exactly) translates to a SELECT 1 FROM country_bands WHERE number = 3
query
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.
Oh ya this is a cool way. I didn't notice it as I just tried to translate old code.
# frozen_string_literal: true | ||
|
||
class PopulateCountryBandDetails < ActiveRecord::Migration[7.2] | ||
def change |
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.
This should be def up
. Then you can also create a def down
which does CountryBandDetail.delete_all
or similar.
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.
Okay done. I knew about def up
and def down
but since def change
is used everywhere, I just thought of continuing with it.
lib/dues_calculator.rb
Outdated
country_band_detail = CountryBandDetail.find_by(number: country_band) | ||
return nil unless country_band_detail | ||
|
||
registration_fee_dues_us_dollars = input_money_us_dollars * country_band_detail.due_percent_registration_fee.to_f/100 |
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.
Spaces around the /
division sign please
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.
Done
lib/dues_calculator.rb
Outdated
# times 100 because Money require lowest currency subunit, which is cents for USD | ||
country_band_dues_us_dollars_money = Money.new(country_band_dues_us_dollars * 100, "USD") | ||
country_band_detail = CountryBandDetail.find_by(number: country_band) | ||
return nil unless country_band_detail |
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.
Is our code prepared to handle a nil
return value? The previous implementation was returning 0
If the country_band.present?
check failed.
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.
Also, you can check country_band_detail.present?
. It's a bit more readable than this "hacky" null 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.
Oh yes, returning 0 was the previous behavior. I missed that. Changed now, also added .present?
.
bf747ae
to
b4fe1d2
Compare
b4fe1d2
to
91c0294
Compare
91c0294
to
e28d6cc
Compare
This PR aims at removing some hard-coding related to dues details. This will help to make the changes quickly in case we need to make some change in dues in future.