Module: ConstantTableSaver::BaseMethods

Defined in:
lib/constant_table_saver.rb

Instance Method Summary collapse

Instance Method Details

#constant_table(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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