Create a cache for non-draft guides
Right now the guides_count for a crop is the total amount of guides, whereas what we really want to show is the total amount of draft: false guides.
Ideally what we would do here is that whenever a guide gets updated we recalculate the counter on the crop for that guide. Mongoid's counter_cache functionality won't work for us because it doesn't accept conditional counting.
I imagine something like
after_save :increment_crop_guides_count
def increment_crop_guides_count
# increments or sets the crop guides count of the crop
end
in models/guide.rb
Okay! I will get to work on this. I am simultaneously looking at #809 - this issue has been bugging me for weeks now.
What exactly do you mean by this : "whenever a guide gets updated we recalculate the counter on the crop for that guide."?
@gargi-gupta when you search for crops it shows how many guides are linked to the crop, but at the moment that count of guides includes guides that are drafts and aren't publicly viewable yet, so that number is technically wrong.
Mongoid provides a counter method for keeping track of how many links there are between two objects, but in this case it wouldn't work, because the link we're looking for is dependent on a setting of the crop (draft: false).
So instead what we have to do is that every time a guide gets updated, we'll have to set - on the linked crops of the guide (which could be multiple crops in the future) - how many non-draft guides are linked to the guide.
- Update a guide.
- Check whether the guide is a draft or not
- If the guide is a draft - do nothing
- If the guide is not a draft, we need to set the
crop.guides_countervalue to beGuide.filter(draft: false).count - save crop, finish saving guide.
I would use the rails automated after_save hook to do this, as described in the ticket description:
after_save :increment_crop_guides_count
def increment_crop_guides_count
# increments or sets the crop guides count of the crop
end
Does that help?
Yeah! I now understand what this issue is all about. I, along with my teammate @Rupal-IIITD will work on this.