OpenFarm icon indicating copy to clipboard operation
OpenFarm copied to clipboard

Create a cache for non-draft guides

Open simonv3 opened this issue 9 years ago • 4 comments

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

simonv3 avatar Sep 24 '16 19:09 simonv3

Okay! I will get to work on this. I am simultaneously looking at #809 - this issue has been bugging me for weeks now.

TheChirpyWitch avatar Oct 11 '16 18:10 TheChirpyWitch

What exactly do you mean by this : "whenever a guide gets updated we recalculate the counter on the crop for that guide."?

gargi-gupta avatar Mar 06 '17 17:03 gargi-gupta

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

  1. Update a guide.
  2. Check whether the guide is a draft or not
  3. If the guide is a draft - do nothing
  4. If the guide is not a draft, we need to set the crop.guides_counter value to be Guide.filter(draft: false).count
  5. 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?

simonv3 avatar Mar 06 '17 18:03 simonv3

Yeah! I now understand what this issue is all about. I, along with my teammate @Rupal-IIITD will work on this.

gargi-gupta avatar Mar 07 '17 06:03 gargi-gupta