palkan / logidze

Database changes log for Rails
MIT License
1.6k stars 76 forks source link

Responsible from different sources #224

Closed dixpac closed 1 year ago

dixpac commented 1 year ago

What if responsible_id can be from a different models.

For example for my use case, specific record can be changed by User or a Device? Would it make a sense to store also stringified object type for responsible for example:

 "m": {
   "_r": 42 
    "_r_type": "User"
 }

Or maybe gid could also work


 "m": {
   "_r":  "gid://app/User/42"
 }
palkan commented 1 year ago

Logidze doesn't care about what do you want to store in this reserved meta field; it's up to the developer to decide. how to serialize a responsible object.

I like the idea of using GlobalID though. Maybe, we can automatically use it in case an object responding to #to_global_id is passed. And when reading responsible data, we can check if the value looks like a GlobalID and restore it.

dixpac commented 1 year ago

Logidze doesn't care about what do you want to store in this reserved meta field; it's up to the developer to decide. how to serialize a responsible object.

Ahh cool, didn't catch that, yeah then I could do following, and it works 👍

 product = Product.new.tap do |p|
    p.name = "Test"
 end

Logidze.with_responsible(user.to_gid) do
  product.save
end

Logidze.with_responsible(device.to_gid) do
  product.update(name: "New name")
end

I like the idea of using GlobalID though. Maybe, we can automatically use it in case an object responding to #to_global_id is passed. And when reading responsible data, we can check if the value looks like a GlobalID and restore it.

Yup, that is exactly what I was thinking, then this style of method would be unnecessary

def whodunnit
    id = log_data.responsible_id
    User.find(id) if id.present?
 end
palkan commented 1 year ago

Closing in favor of https://github.com/palkan/logidze/issues/233