Created
August 12, 2019 05:11
-
-
Save yuemori/064f4d36b64477c69b10572d179bfebb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
product_a = Product.fetch(:product_a).new(price: 0) | |
product_b = Product.fetch(:product_b).new(price: 0) | |
Product.fetch_class(:product_a).transaction do | |
Product.fetch_class(:product_b).transaction do | |
product_a.save! | |
product_b.save! | |
end | |
end |
This file contains hidden or 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
class Product < ApplicationRecord | |
self.abstract_class = true | |
def self.register_class(key, klass) | |
registry[key] = klass | |
end | |
def self.registry | |
@registry ||= Hash::HashWithIndifferentAccess | |
end | |
def self.fetch_class(key) | |
@registry.fetch(key) | |
end | |
end | |
require_dependency 'product_a' | |
require_dependency 'product_b' |
This file contains hidden or 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
class ProductA < Product | |
register_class :product_a, self | |
establish_connection :a | |
end |
This file contains hidden or 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
class ProductB < Product | |
register_class :product_b, self | |
establish_connection :b | |
end |
This file contains hidden or 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
RSpec.describe Product do | |
let(:product_a) { build :product_a } | |
let(:product_b) { build :product_b } | |
# write your test code by product_a, product_b | |
end |
This file contains hidden or 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
FactoryBot.define do | |
factory :product do | |
end | |
factory :product_a, class: 'ProductA' do | |
end | |
factory :product_a, class: 'ProductB' do | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment