Created
August 6, 2014 07:43
-
-
Save BugRoger/9fb697269925079331ce to your computer and use it in GitHub Desktop.
ActiveRecord Playground in a Single File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unless File.exist?('Gemfile') | |
File.write('Gemfile', <<-GEMFILE) | |
source 'https://rubygems.org' | |
gem 'rails' | |
gem 'sqlite3' | |
GEMFILE | |
system 'bundle' | |
end | |
require 'bundler' | |
Bundler.setup(:default) | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
# Define a DB schema for the test we would run | |
ActiveRecord::Schema.define do | |
create_table :quotas do |t| | |
t.string :organization | |
t.integer :instances | |
end | |
create_table :usages do |t| | |
t.string :organization | |
t.integer :bucket_id | |
t.integer :instances | |
t.string :type | |
end | |
create_table :buckets do |t| | |
t.integer :quota_id | |
t.string :organization | |
t.string :project | |
t.integer :instances, default: 0 | |
end | |
end | |
class Quota < ActiveRecord::Base | |
self.table_name = "quotas" | |
end | |
class Usage < Quota | |
def self.recalculate_organization(id) | |
buckets = Bucket.where(organization: id).all | |
usage = OrganizationUsage.find_or_create_by(organization: id) | |
instances = buckets.map(&:usage).map(&:instances).compact.inject(&:+) | |
usage.update(instances: instances) | |
end | |
def self.recalculate_bucket(id) | |
bucket = Bucket.find(id) | |
usage = BucketUsage.find_or_create_by(bucket: id) | |
usage.update(instances: bucket.instances) | |
end | |
end | |
class OrganizationUsage < Usage | |
end | |
class BucketUsage < Usage | |
belongs_to :bucket | |
end | |
class Bucket < ActiveRecord::Base | |
has_one :usage, class_name: "BucketUsage", dependent: :destroy | |
after_save :update_usages | |
def update_usages | |
Usage.recalculate_bucket(self) | |
Usage.recalculate_organization(organization) | |
end | |
end | |
describe 'The tests' do | |
before do | |
@bucket0 = Bucket.create(organization: "a", project: "a") | |
@bucket1 = Bucket.create(organization: "a", project: "b") | |
end | |
after do | |
Bucket.destroy_all | |
Quota.destroy_all | |
BucketUsage.destroy_all | |
OrganizationUsage.destroy_all | |
end | |
it "updates bucket usages" do | |
@bucket0.update(instances: 5) | |
@bucket0.usage.instances.must_equal 5 | |
end | |
it "updates organization usage" do | |
@bucket0.update(instances: 3) | |
@bucket1.update(instances: 4) | |
OrganizationUsage.find_by_organization("a").instances.must_equal 7 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment