module ConstantTableSaver::BaseMethods

Public Instance Methods

constant_table(options = {}) click to toggle source
# File lib/constant_table_saver.rb, line 6
def constant_table(options = {})
  options.assert_valid_keys(:name, :name_prefix, :name_suffix)
  class_attribute :constant_table_options, :instance_writer => false
  self.constant_table_options = options

  @constant_record_methods = nil
  
  if ActiveRecord::VERSION::MAJOR == 4
    extend ActiveRecord4ClassMethods
  elsif ActiveRecord::VERSION::MAJOR == 5 && (ActiveRecord::VERSION::MINOR == 0 || ActiveRecord::VERSION::MINOR == 1)
    extend ActiveRecord5ClassMethods
  else
    extend ActiveRecord52ClassMethods
  end
  extend ClassMethods
  extend NameClassMethods if constant_table_options[:name]
  
  klass = defined?(ActiveRecord::FixtureSet) ? ActiveRecord::FixtureSet : ActiveRecord::Fixtures
  class <<klass
    # normally, create_fixtures method gets called exactly once - but unfortunately, it
    # loads the class and does a #respond_to?, which causes us to load and cache before
    # the new records are added, so we need to reset our cache afterwards.
    def create_fixtures_with_constant_tables(*args)
      create_fixtures_without_constant_tables(*args).tap { ConstantTableSaver.reset_all_caches }
    end
    def reset_cache_with_constant_tables(*args)
      reset_cache_without_constant_tables(*args).tap     { ConstantTableSaver.reset_all_caches }
    end
    alias :create_fixtures_without_constant_tables :create_fixtures
    alias :create_fixtures :create_fixtures_with_constant_tables
    alias :reset_cache_without_constant_tables :reset_cache
    alias :reset_cache :reset_cache_with_constant_tables
  end unless klass.respond_to?(:create_fixtures_with_constant_tables)
end