Sha256: 8fc7f4bc6765645c991723a7316e2caa34fe52d75231bd5b6be410841f895ed7

Contents?: true

Size: 1.39 KB

Versions: 7

Compression:

Stored size: 1.39 KB

Contents

module ActiveType
  module Util

    def cast(object, klass)
      if object.is_a?(ActiveRecord::Relation)
        cast_relation(object, klass)
      elsif object.is_a?(ActiveRecord::Base)
        cast_record(object, klass)
      else
        raise ArgumentError, "Don't know how to cast #{object.inspect}"
      end
    end

    def scoped(klass_or_relation)
      klass_or_relation.where(nil)
    end

    private

    def cast_record(record, klass)
      # record.becomes(klass).dup
      klass.new do |casted|
        # Rails 3.2, 4.2
        casted.instance_variable_set(:@attributes, record.instance_variable_get(:@attributes))
        # Rails 3.2
        record.instance_variable_set(:@attributes_cache, record.instance_variable_get(:@attributes_cache))
        # Rails 4.2
        casted.instance_variable_set(:@changed_attributes, record.instance_variable_get(:@changed_attributes))
        # Rails 3.2, 4.2
        casted.instance_variable_set(:@new_record, record.new_record?)
        # Rails 3.2, 4.2
        casted.instance_variable_set(:@destroyed, record.destroyed?)
        # Rails 3.2, 4.2
        casted.instance_variable_set(:@errors, record.errors)
      end
    end

    def cast_relation(relation, klass)
      scoped(klass).merge(scoped(relation))
    end

    extend self

  end

  # Make Util methods available under the `ActiveType` namespace
  # like `ActiveType.cast(...)`
  extend Util
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
active_type-0.5.0 lib/active_type/util.rb
active_type-0.4.5 lib/active_type/util.rb
active_type-0.4.4 lib/active_type/util.rb
active_type-0.4.3 lib/active_type/util.rb
active_type-0.4.2 lib/active_type/util.rb
active_type-0.4.1 lib/active_type/util.rb
active_type-0.4.0 lib/active_type/util.rb