Skip to content

Implementation of feature 1 and 2#1

Open
ahteshamtariq wants to merge 4 commits intomainfrom
develop
Open

Implementation of feature 1 and 2#1
ahteshamtariq wants to merge 4 commits intomainfrom
develop

Conversation

@ahteshamtariq
Copy link
Owner

No description provided.

- associate municipality with price
- associate municipality with package
- add test cases for Municipality model
- add DB seed for municipality
- adding some test cases for feature 1
- add test cases for feature 2
@ahteshamtariq ahteshamtariq changed the title #1 Implementation of feature 1 and 2 Apr 20, 2023
@@ -0,0 +1,5 @@
---

Choose a reason for hiding this comment

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

I don't know why we need --- here since we have only one document in this file. But I did notice that's how the other imports were set up. So 👍 for following the pre-existing convention.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yes, there was no need to add this.
Yes only reason to add this is follow the previous convention.

"Göteborg" => [100_00],
"Stockholm" => [30_00, 40_00],
)
"Göteborg" => [100_00],

Choose a reason for hiding this comment

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

Is this change an intentional formatting update?

Copy link
Owner Author

Choose a reason for hiding this comment

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

@ziadsawalha Yes, As for the new test cases, I've used my preset formatting so I've changed this one to match the file in the same format. Otherwise there is no need to do this.

@ziadsawalha
Copy link

I very much appreciate the commit messages and how you kept each change in a separate commit!

net-protocol
timeout
nio4r (2.5.8)
nokogiri (1.13.10-arm64-darwin)
Copy link

Choose a reason for hiding this comment

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

What was the intention to include this gem?

Copy link
Owner Author

Choose a reason for hiding this comment

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

@romangt This is my platform dependency. It's the same gem that is at line 109. I'm using Mac M1 so this gem is used to operate on the platform.

Choose a reason for hiding this comment

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

Yeah. I have the same pop up for me since I have an M1 as well. Apple Silicon uses an arm64 architecture and some gems need to be compiled to it.

@@ -2,6 +2,7 @@

class Package < ApplicationRecord
has_many :prices, dependent: :destroy
Copy link

Choose a reason for hiding this comment

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

Could you please explain your intention of leaving prices directly related to package? Do you think this a good approach? This will force us to always do if/else depending on municipalities. Maybe some kind of "global" municipality can act as direct package prices?

Copy link
Owner Author

Choose a reason for hiding this comment

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

I've left this price directly related to the package keeping in mind the backward compatibility. If we remove this the old data in the application may not be able to fully functional. As in Feature 1 it is stated In other words, a package should be able to have different prices depending on a municipality. and We still want to have our pricing log, but now with the added municipalities. and there is no statement of ignoring the backward compatibility so I assumed it should be backward compatible. Also, as stated in Feature 2 requirements the municipality is an optional filter.
Putting if/else is not good in general, if we ignore backward compatibility then I think what you have suggested is the more reasonable and best fitted.

Choose a reason for hiding this comment

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

Is there any statement in the docs about handling pre-existing prices in the code? Should they be migrated (in the db), supported, or assumed to not exist?

Copy link
Owner Author

Choose a reason for hiding this comment

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

@ziadsawalha please see my comments above.

has_many :prices

validates :name, presence: true, uniqueness: true
validates :price_cents, presence: true
Copy link

Choose a reason for hiding this comment

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

We have has_many: prices and we have price_cents. Why do we need both?

Copy link
Owner Author

Choose a reason for hiding this comment

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

We can eliminate the price_cents, but I've assumed the same structure as for the Package that Municipality will store the current price in price_cents and has_many: prices will store the history of price.


# Update the current price
package.update!(price_cents: new_price_cents)
price.save!
Copy link

Choose a reason for hiding this comment

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

Is it correct that running this method we will get a price attached to package and municipality?

Copy link
Owner Author

Choose a reason for hiding this comment

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

If we see the backward compatibility then it can generate a price with the only package and with new requirements with the municipality and package also.

# the existing one.

xit "supports adding a price for a specific municipality" do
it "supports adding a price for a specific municipality if municipality not exists" do
Copy link

Choose a reason for hiding this comment

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

You should use context to avoid conditions in spec names

Copy link
Owner Author

Choose a reason for hiding this comment

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

I agree it's good to use context. I just tried it to match the already used convention.

expect(package.municipalities.first.name).to eq("Göteborg")
end

it "supports adding a price for a specific municipality if municipality exists" do
Copy link

Choose a reason for hiding this comment

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

generally it is a good practice to have one expect per spec.
Here you can have 3 separate specs:

  • it creates municipality
  • it sets correct price
  • it sets correct name
    It is much easier to read specs in that case

Also, Municipality.create!(name: 'Göteborg', price_cents: 100_00, package: package) looks like a preparation step which can be moved to before hook. It makes specs clearer

# Price history records including their municipality associations
price_history = Price.includes(:municipality)
# Apply additional filters to the query based on the given options.
price_history = price_history.where(package: options[:package]) if options[:package].present?
Copy link

Choose a reason for hiding this comment

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

According to conditions of test task, package and year will be always provided

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yes in the task description, both will be present.
I've updated it by considering that we can extend the functionality to this also.


raise NotImplementedError, "Implement this to pass the assignment"
# Group the results by the name of the municipality and extract the price values as an array
puts price_history.group_by { |price| price.municipality&.name }.map { |key, value| [key, value.map(&:price_cents)] }.to_h
Copy link

Choose a reason for hiding this comment

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

Redundant puts

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thank you, for highlighting this.

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.

3 participants