module InactiveRecord module Fixtures def self.included(base) base.extend ClassMethods base.class_eval do alias_method :setup, :setup_with_inactive_fixtures end def base.method_added(method) if method.to_s == 'setup' unless method_defined?(:setup_without_inactive_fixtures) alias_method :setup_without_inactive_fixtures, :setup define_method(:setup) do setup_with_inactive_fixtures setup_without_inactive_fixtures end end end super end end module ClassMethods def inactive_fixtures(*names) write_inheritable_attribute :inactive_fixture_names, names end end def setup_with_inactive_fixtures setup_with_fixtures load_inactive_fixtures end def method_missing(method, *args) if inactive_fixture_names && inactive_fixture_names.include?(method) find_inactive_fixture(method, args[0]) else super end end private def inactive_fixture_names self.class.read_inheritable_attribute(:inactive_fixture_names) end def find_inactive_fixture(table, name) @@loaded_inactive_fixtures[table.to_s][name.to_s] end def load_inactive_fixtures return unless inactive_fixture_names @@loaded_inactive_fixtures = {} inactive_fixture_names.each do |name| klass = name.to_s.classify.constantize content = File.open(self.class.fixture_path + "/#{name}.yml") do |file| YAML.load ERB.new(file.read).result end @@loaded_inactive_fixtures[name.to_s] = {} content.each do |key, values| klass.finds values['id'], values @@loaded_inactive_fixtures[name.to_s][key] = klass.find(values['id']) end end end end end