Sha256: ab6e5fb88cb9e0cee7ae6f0151be898f5911bce25cefc8111b1d06f133f7c6c0

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

require 'active_record'

ActiveRecord::Migration.verbose = false
ActiveRecord::Base.logger = Logger.new($STDOUT)
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')

module ActiveRecord
  class QueryCounter
    cattr_accessor :query_count do
      0
    end

    IGNORED_SQL = [/^PRAGMA (?!(table_info))/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /^SHOW max_identifier_length/]

    def call(name, start, finish, message_id, values)
      # FIXME: this seems bad. we should probably have a better way to indicate
      # the query was cached
      unless 'CACHE' == values[:name]
        self.class.query_count += 1 unless IGNORED_SQL.any? { |r| values[:sql] =~ r }
      end
    end
  end
end

ActiveSupport::Notifications.subscribe('sql.active_record', ActiveRecord::QueryCounter.new)

class CreateModelsForTest < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :name
      t.integer :age, :default => 12
    end
  end

  def self.down
    drop_table(:users)
  end
end

class User < ActiveRecord::Base
  include Cache::Object::ActiveRecord

  object_cache_include :shoe_size
  attr_accessor :shoe_size

  object_cache_on :name, :age

  after_save :asplode_if_name_is_asplode

  def asplode_if_name_is_asplode
    raise "WOAH" if name == "asplode"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cache-object-0.1.0 spec/support/models.rb