require 'much-plugin' require 'mr/record' module MR; end module MR::JsonField module Record include MuchPlugin plugin_included do include MR::Record include InstanceMethods before_save :json_field_sync_all_json_fields_on_save end module InstanceMethods private # this makes it so changes that are done to the JSON object are persisted # to the DB without having to re-write the entire field, for example if # the json field is a hash then a change to an individual key's value # needs to get re-encoded and stored in the source field, since the json # field writer wasn't used it won't get encoded, this callback fixes the # issue by ensuring all json fields get synced before we save the record def json_field_sync_all_json_fields_on_save self.model.class.json_field_accessors.each do |field_name| self.model.send("#{field_name}=", self.model.send(field_name)) end end end end end