call custom callback before indexing
francescob opened this issue · 4 comments
Hi, i need to execute a specific callback before indexes are created, is that possible at all? or to execute some code on the record before indexing?
Thanks
Hey @francescob, I believe the best way right now is to use Module#prepend
to override the compute_attribute_bidx
method (replace attribute
with your attribute name). Can you explain your use case a bit more?
module BlindIndexCallback
def compute_attribute_bidx
# callback code
# ...
super
end
end
Model.prepend(BlindIndexCallback)
Basically, it's an application where only users with specific roles can decrypt encrypted attributes, to every other user the value is hidden ( ******* is shown). that is managed by setting a virtual attribute with result of can? :decrypt, record ( cancancan ability) in a controller before action. In that way the value of encrypted attributes is hidden by default, and so when I do BlindIndex.backfill every index get filled with ******
I managed to workaround by setting the can_decrypt attribute to true with an after initialize, but I would like not to do that because I prefer to keep it hidden by default
Anyhow, the proposed solution works, thank you very much for the tip (and all the great gems)
Great, glad it worked! compute_attribute_bidx
is a public API, so this should be stable.