Sha256: fe1406a8736e37b352f4d791dc72b65ec7975432bc40ba3dd5186bd56249e554

Contents?: true

Size: 1.97 KB

Versions: 3

Compression:

Stored size: 1.97 KB

Contents

module SwitchNamespace

  module ClassMethods
    def rails_cache_key_namespace
      "#{self.namespace}:#{super}"
    end
  end

  def self.included(base)
    base.extend ClassMethods
    base.class_eval do
      class_attribute :namespace
      self.namespace = 'ns'
    end
  end
end

module ActiveRecordObjects

  def setup_models(base = ActiveRecord::Base)
    Object.send :const_set, 'DeeplyAssociatedRecord', Class.new(base) {
      include IdentityCache
      belongs_to :associated_record
      default_scope { order('name DESC') }
    }

    Object.send :const_set, 'AssociatedRecord', Class.new(base) {
      include IdentityCache
      belongs_to :item
      has_many :deeply_associated_records
      default_scope { order('id DESC') }
    }

    Object.send :const_set, 'NormalizedAssociatedRecord', Class.new(base) {
      include IdentityCache
      belongs_to :item
      default_scope { order('id DESC') }
    }

    Object.send :const_set, 'NotCachedRecord', Class.new(base) {
      belongs_to :item, :touch => true
      default_scope { order('id DESC') }
    }

    Object.send :const_set, 'PolymorphicRecord', Class.new(base) {
      belongs_to :owner, :polymorphic => true
    }

    Object.send :const_set, 'Item', Class.new(base) {
      include IdentityCache
      belongs_to :item
      has_many :associated_records
      has_many :normalized_associated_records
      has_many :not_cached_records
      has_many :polymorphic_records, :as => 'owner'
      has_one :polymorphic_record, :as => 'owner'
      has_one :associated, :class_name => 'AssociatedRecord'
    }
  end

  def teardown_models
    ActiveSupport::DescendantsTracker.clear
    ActiveSupport::Dependencies.clear
    Object.send :remove_const, 'DeeplyAssociatedRecord'
    Object.send :remove_const, 'PolymorphicRecord'
    Object.send :remove_const, 'NormalizedAssociatedRecord'
    Object.send :remove_const, 'AssociatedRecord'
    Object.send :remove_const, 'NotCachedRecord'
    Object.send :remove_const, 'Item'
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
identity_cache-0.0.7 test/helpers/active_record_objects.rb
identity_cache-0.0.6 test/helpers/active_record_objects.rb
identity_cache-0.0.5 test/helpers/active_record_objects.rb