Sha256: 9666e9fd5d9ab2b9237e316bf19002a54a73c3acccd962ec8ca25ec7efd6e6e7

Contents?: true

Size: 1.48 KB

Versions: 21

Compression:

Stored size: 1.48 KB

Contents

module SugarCRM; module AttributeSerializers
  
  protected
    
  # Serializes the id
  def serialize_id
    {:name => "id", :value => id.to_s}
  end

  # Converts the attributes hash into format recognizable by Sugar
  # { :last_name => "Smith"} 
  # becomes
  # { :last_name => {:name => "last_name", :value => "Smith"}}
  def serialize_attributes
    attr_hash = {}
    @attributes.each_pair do |name,value|
      attr_hash[name] = serialize_attribute(name,value)
    end
    attr_hash[:id] = serialize_id unless new?
    attr_hash
  end

  # Converts the modified_attributes hash into format recognizable by Sugar
  # {:last_name => {:old => "Smit", :new => "Smith"}} 
  # becomes
  # {:last_name => {:name => "last_name", :value => "Smith"}}  
  def serialize_modified_attributes
    attr_hash = {}
    @modified_attributes.each_pair do |name,hash|
      attr_hash[name] = serialize_attribute(name,hash[:new])
    end
    attr_hash[:id] = serialize_id unless new?
    attr_hash
  end
  
  # Un-typecasts the attribute - false becomes "0", 5234 becomes "5234", etc.
  def serialize_attribute(name,value)
    attr_value = value
    attr_type  = attr_type_for(name) 
    case attr_type
    when :bool
      attr_value = 0
      attr_value = 1 if value
    when :datetime, :datetimecombo
      begin
        attr_value = value.strftime("%Y-%m-%d %H:%M:%S")
      rescue
        attr_value = value
      end
    when :int
      attr_value = value.to_s
    end
    {:name => name, :value => attr_value}
  end
end; end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
sugarcrm-0.9.1 lib/sugarcrm/attributes/attribute_serializers.rb