Module: GoSecure::SerializeInstanceMethods

Defined in:
lib/go_secure.rb

Instance Method Summary collapse

Instance Method Details

#load_secure_objectObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/go_secure.rb', line 175

def load_secure_object
  @secure_object_json = nil.to_json
  if self.id
    attr = read_attribute(self.class.secure_column) || (!self.respond_to?(:secure_column_value) && self.send(self.class.secure_column)) || (@secure_object.is_a?(String) && @secure_object) || nil
    if attr && attr.match(/\s*^{/)
      @secure_object = JSON.parse(attr)
    else
      @secure_object = GoSecure::SecureJson.load(attr)
    end
    @secure_object_json = @secure_object.to_json
    @loaded_secure_object = true
  end
  true
end

#mark_changed_secure_object_hashObject

If the serialized data has changed since initialize and paper_trail is configured, then we need to manually mark the column as dirty to make sure a proper paper_trail is maintained



193
194
195
196
197
198
199
200
201
# File 'lib/go_secure.rb', line 193

def mark_changed_secure_object_hash
  if !send("#{self.class.secure_column}_changed?")
    json = @secure_object.to_json
    if json != @secure_object_json
      send("#{self.class.secure_column}_will_change!")
    end
  end
  true
end

#persist_secure_objectObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/go_secure.rb', line 203

def persist_secure_object
  self.class.more_before_saves ||= []
  self.class.more_before_saves.each do |method|
    res = send(method)
    return false if res == false
  end
  mark_changed_secure_object_hash
  if send("#{self.class.secure_column}_changed?")
    secure = GoSecure::SecureJson.dump(@secure_object)
    @secure_object = GoSecure::SecureJson.load(secure)
    write_attribute(self.class.secure_column, secure)
  end
  true
end