Class: CIA::AttributeChange

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
SourceValidation
Defined in:
lib/cia/attribute_change.rb

Class Method Summary collapse

Methods included from SourceValidation

included

Class Method Details

.max_value_sizeObject



25
26
27
# File 'lib/cia/attribute_change.rb', line 25

def self.max_value_size
  @max_value_size ||= columns.detect { |c| c.name == "old_value" }.limit
end

.on_attribute(attribute) ⇒ Object



21
22
23
# File 'lib/cia/attribute_change.rb', line 21

def self.on_attribute(attribute)
  scoped(:conditions => {:attribute_name => attribute})
end

.serialize_for_storage(item, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cia/attribute_change.rb', line 29

def self.serialize_for_storage(item, &block)
  raise "Pass me a block to reduce size" unless block_given?
  before, json = nil

  loop do
    json = JSON.dump(item)
    raise "The block did not reduce the size" if before && json.bytesize >= before
    before = json.bytesize
    break if max_value_size >= before
    item = yield(item)
  end

  json
end