-
Notifications
You must be signed in to change notification settings - Fork 57
Refactor Reservation Scopes / Validations #1288
Comments
Resolves #1288 - switch ReservationScopes module over to concern format - switch ReservationValidations module over as well - move both files to app/models/concerns
Ok, this is done and appears not to break anything. I'll check and see if that helped with the CodeClimate score and open a PR if it does. |
Damn that didn't help... one option I've seen is to refactor scopes into multiple files (see here), but that seems overly complex. I'll do a bit more research to see if there are any other ways we can fix the scope clutter... |
I don't know if this helps us, necessarily, but this is a great article with some fairly advanced techniques for improving Models. EDIT Nvm, look at |
+1 to query objects! :D |
If it would help, I'm happy to talk through the architecture of this :) |
Ok, back on this issue. I wrote my first query object but am having trouble with loading it (since I put them in |
Nope, it was just a typo in my class name 😞 |
Alright, it looks like this improves our CodeClimate score for the |
I've discovered that our upcoming checkin e-mail was potentially including reservations that had not been checked out (e.g. a 1-day reservation) and or reservations that had already been checked in... looks like we messed up our scope at some point. I'll fix that in this issue, increasing priority. |
Seems like our tests are insufficient if we broke this... we need to add some edge cases to our mailer / rake task specs (or add specs for the query objects themselves)? I'm opening this up a separate issue and I'll move over the current scopes as they are. |
Also note that we'll be removing the |
@caseywatts: let's shift discussion of this issue over here (re: your Slack comments). Based on what you wrote I'm going to move multipurpose scopes to the model file and then use Query Objects for single-use scopes. I'll also be writing tests for our scopes as well - where/how does one generally test Query Objects (since it seems somewhat coupled to the resource / model itself)? Do you just rely on the tests for the use case itself (kinda like an integration tests)? |
Started working on getting rid of the delegation stuff, might have broken some stuff. Also read a bunch about how people generally handle scopes, will keep working on cleaning this up this week. |
More cleanup... still not 100% sure on how best to structure these scopes and where to add specs. |
After starting to rearrange the scopes (and moving them back into the Reservation model for clarity), we're understandably seeing a decrease in the score for the Reservation model but benefiting from the removal of the separate scopes module. I'll still keep complex scopes in separate Query Objects but any scope based on a single parameter seems as though it belongs inside the model rather than creating a separate PORO for it. Hoping to wrap this up today and making sure that we haven't broken anything 😄. |
Gotta run to a meeting, but it seems as though changing the |
Fixed up the rubocop errors, now trying to debug the failing spec. |
Weird, according to this post we should be getting chainable queries with Query Objects due to the way that Rails handles things. That doesn't seem to be the case, so I'll have to investigate further. |
all specs pass, now it's just time to figure out why chaining broke... |
Ok, after investigating further I think I figured this out. If we define the scope as follows:
then Rails instantiates a copy of the
then we're instantiating a new copy of @caseywatts can you review this and make sure it makes sense / I haven't missed anything? In the meantime, I'm going to set this branch up with the latter formulation since that makes more sense to me at the moment. Thanks! |
Resolves #1288 - add a set of POROs for Query Objects where appropriate - move reservation scopes back into the model file - update code where necessary
Resolves #1288 - add a set of POROs for Query Objects where appropriate - move reservation scopes back into the model file - update code where necessary
Let's refactor the ReservationScopes module to use
ConcernsQuery Objects; this will hopefully help address #1097. We'll also try to use Form Objects to clean up the validations and finally rid ourselves of the ReservationScopes and ReservationValidations modules!The text was updated successfully, but these errors were encountered: