Class: FixtureBuilder::Configuration

Inherits:
Object
  • Object
show all
Includes:
Delegations::Namer
Defined in:
lib/fixture_builder/configuration.rb

Constant Summary collapse

ACCESSIBLE_ATTRIBUTES =
[:select_sql, :delete_sql, :skip_tables, :files_to_check, :record_name_fields,
:fixture_builder_file, :fixture_directory, :after_build, :legacy_fixtures, :model_name_procs,
:write_empty_files]
SCHEMA_FILES =
['db/schema.rb', 'db/development_structure.sql', 'db/test_structure.sql', 'db/production_structure.sql']

Instance Method Summary collapse

Methods included from Delegations::Namer

included

Constructor Details

#initialize(opts = {}) ⇒ Configuration

Returns a new instance of Configuration.



25
26
27
28
29
# File 'lib/fixture_builder/configuration.rb', line 25

def initialize(opts={})
  @namer = Namer.new(self)
  @file_hashes = file_hashes
  @write_empty_files = true
end

Instance Method Details

#delete_sqlObject



58
59
60
# File 'lib/fixture_builder/configuration.rb', line 58

def delete_sql
  @delete_sql ||= "DELETE FROM %{table}"
end

#delete_sql=(sql) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/fixture_builder/configuration.rb', line 62

def delete_sql=(sql)
  if sql =~ /%s/
    ActiveSupport::Deprecation.warn("Passing '%s' into delete_sql is deprecated. Please use '%{table}' instead.", caller)
    sql = sql.sub(/%s/, '%{table}')
  end
  @delete_sql = sql
end

#factory(&block) ⇒ Object



39
40
41
42
43
44
# File 'lib/fixture_builder/configuration.rb', line 39

def factory(&block)
  self.files_to_check += @legacy_fixtures.to_a
  return unless rebuild_fixtures?
  @builder = Builder.new(self, @namer, block).generate!
  write_config
end

#files_to_checkObject



74
75
76
# File 'lib/fixture_builder/configuration.rb', line 74

def files_to_check
  @files_to_check ||= schema_definition_files
end

#files_to_check=(files) ⇒ Object



85
86
87
88
89
# File 'lib/fixture_builder/configuration.rb', line 85

def files_to_check=(files)
  @files_to_check = files
  @file_hashes = file_hashes
  @files_to_check
end

#fixture_builder_fileObject



95
96
97
# File 'lib/fixture_builder/configuration.rb', line 95

def fixture_builder_file
  @fixture_builder_file ||= ::Rails.root.join('tmp', 'fixture_builder.yml')
end

#fixture_directoryObject



107
108
109
# File 'lib/fixture_builder/configuration.rb', line 107

def fixture_directory
  @fixture_directory ||= FixturesPath.absolute_rails_fixtures_path
end

#fixtures_dir(path = '') ⇒ Object



111
112
113
# File 'lib/fixture_builder/configuration.rb', line 111

def fixtures_dir(path = '')
  File.expand_path(File.join(fixture_directory, path))
end

#include(*args) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/fixture_builder/configuration.rb', line 31

def include(*args)
  class_eval do
    args.each do |arg|
      include arg
    end
  end
end

#name_model_with(model_class, &block) ⇒ Object



99
100
101
# File 'lib/fixture_builder/configuration.rb', line 99

def name_model_with(model_class, &block)
  @namer.name_model_with(model_class, &block)
end

#record_name_fieldsObject



91
92
93
# File 'lib/fixture_builder/configuration.rb', line 91

def record_name_fields
  @record_name_fields ||= %w{ unique_name display_name name title username login }
end

#schema_definition_filesObject



78
79
80
81
82
83
# File 'lib/fixture_builder/configuration.rb', line 78

def schema_definition_files
  Dir['db/*'].inject([]) do |result, file|
    result << file if SCHEMA_FILES.include?(file)
    result
  end
end

#select_sqlObject



46
47
48
# File 'lib/fixture_builder/configuration.rb', line 46

def select_sql
  @select_sql ||= "SELECT * FROM %{table}"
end

#select_sql=(sql) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/fixture_builder/configuration.rb', line 50

def select_sql=(sql)
  if sql =~ /%s/
    ActiveSupport::Deprecation.warn("Passing '%s' into select_sql is deprecated. Please use '%{table}' instead.", caller)
    sql = sql.sub(/%s/, '%{table}')
  end
  @select_sql = sql
end

#skip_tablesObject



70
71
72
# File 'lib/fixture_builder/configuration.rb', line 70

def skip_tables
  @skip_tables ||= %w{ schema_migrations }
end

#tablesObject



103
104
105
# File 'lib/fixture_builder/configuration.rb', line 103

def tables
  ActiveRecord::Base.connection.tables - skip_tables
end