Sha256: b85ad45286d54b2deb5578a06414c30d0b428ca17fd4e4a62b0e84ed1e12e13c

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

module EnumerateBy
  module Extensions #:nodoc:
    module XmlSerializer #:nodoc:
      # Adds support for xml serialization of enumeration associations as
      # attributes
      module Attribute
        def self.included(base) #:nodoc:
          base.class_eval do
            alias_method_chain :compute_type, :enumerations
            alias_method_chain :compute_value, :enumerations
          end
        end
        
        protected
          # Enumerator types are always strings
          def compute_type_with_enumerations
            if enumeration_association?
              klass = @record.class.reflections[name.to_sym].klass
              type = klass.columns_hash[klass.enumerator_attribute.to_s].type
              
              case type
                when :text
                  :string
                when :time
                  :datetime
                else
                  type
              end
            else
              compute_type_without_enumerations
            end
          end
          
          # Gets the real value representing the enumerator
          def compute_value_with_enumerations
            if enumeration_association?
              association = @record.send(name)
              association.enumerator if association
            else
              compute_value_without_enumerations
            end
          end
          
          # Is this attribute defined by an enumeration association?
          def enumeration_association?
            @enumeration_association ||= @record.enumeration_associations.value?(name)
          end
      end
    end
  end
end

ActiveRecord::XmlSerializer::Attribute.class_eval do
  include EnumerateBy::Extensions::XmlSerializer::Attribute
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
pluginaweek-enumerate_by-0.4.3 lib/enumerate_by/extensions/xml_serializer.rb
enumerate_by-0.4.4 lib/enumerate_by/extensions/xml_serializer.rb
enumerate_by-0.4.3 lib/enumerate_by/extensions/xml_serializer.rb