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

Store country band details in database #10476

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

danieljames-dj
Copy link
Member

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.

@danieljames-dj danieljames-dj force-pushed the country_band_details branch 15 times, most recently from d02c82a to 9c50e1a Compare January 4, 2025 10:05
@@ -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)
Copy link
Member

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

Suggested change
unless CountryBandDetail.distinct.pluck(:number).include?(@number)
unless CountryBandDetail.exists?(number: @number)

Copy link
Member

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

Copy link
Member Author

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
Copy link
Member

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.

Copy link
Member Author

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.

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
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

# 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
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member Author

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?.

@danieljames-dj danieljames-dj force-pushed the country_band_details branch 2 times, most recently from bf747ae to b4fe1d2 Compare January 8, 2025 04:25
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