Skip to content

Commit

Permalink
Support custom self.table_name
Browse files Browse the repository at this point in the history
  • Loading branch information
artemave committed Mar 1, 2022
1 parent 94caf8d commit a9d2482
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
20 changes: 10 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
fixture_farm (0.1.0)
fixture_farm (0.1.3)
rails (~> 6.1.4, >= 6.1.4.1)

GEM
Expand Down Expand Up @@ -70,21 +70,21 @@ GEM
concurrent-ruby (1.1.9)
crass (1.0.6)
erubi (1.10.0)
globalid (0.5.2)
globalid (1.0.0)
activesupport (>= 5.0)
i18n (1.8.11)
concurrent-ruby (~> 1.0)
loofah (2.12.0)
loofah (2.13.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (1.0.2)
method_source (1.0.0)
mini_mime (1.1.2)
minitest (5.14.4)
minitest (5.15.0)
nio4r (2.5.8)
nokogiri (1.12.5-x86_64-linux)
nokogiri (1.13.1-x86_64-linux)
racc (~> 1.4)
racc (1.6.0)
rack (2.2.3)
Expand Down Expand Up @@ -120,18 +120,18 @@ GEM
sprockets (4.0.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.2)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets-rails (3.4.2)
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
sqlite3 (1.4.2)
thor (1.1.0)
thor (1.2.1)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.5.1)
zeitwerk (2.5.3)

PLATFORMS
x86_64-linux
Expand Down
20 changes: 17 additions & 3 deletions lib/fixture_farm/active_record_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,29 @@ module ActiveRecordExtension
def fixture_name
require 'active_record/fixtures'

return nil unless fixture_file_path
return nil unless File.exist?(fixtures_file_path)

fixtures = YAML.load_file(fixture_file_path)
fixtures = YAML.load_file(fixtures_file_path)
fixtures.keys.find do |key|
ActiveRecord::FixtureSet.identify(key) == id
end
end

def fixture_file_path
def fixtures_file_path
existing_fixtures_file_path || candidate_fixtures_file_path
end

def candidate_fixtures_file_path
klass = self.class
loop do
path = Rails.root.join('test', 'fixtures', "#{klass.to_s.underscore.pluralize}.yml")
return path if klass >= ActiveRecord::Base || !klass.columns.map(&:name).include?(klass.inheritance_column)

klass = klass.superclass
end
end

def existing_fixtures_file_path
klass = self.class

while klass < ActiveRecord::Base
Expand Down
8 changes: 5 additions & 3 deletions lib/fixture_farm/fixture_recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,14 @@ def update_fixture_files(named_new_fixtures)
end
end.to_h

fixture_file_path = model_instance.fixture_file_path || Rails.root.join('test', 'fixtures', "#{model_instance.class.table_name}.yml")
fixtures_file_path = model_instance.fixtures_file_path

fixtures = File.exist?(fixture_file_path) ? YAML.load_file(fixture_file_path) : {}
fixtures = File.exist?(fixtures_file_path) ? YAML.load_file(fixtures_file_path) : {}
fixtures[new_fixture_name] = yaml_attributes

File.open(fixture_file_path, 'w') do |file|
FileUtils.mkdir_p(fixtures_file_path.dirname)

File.open(fixtures_file_path, 'w') do |file|
yaml = YAML.dump(fixtures).gsub(/\n(?=[^\s])/, "\n\n").delete_prefix("---\n\n")
file.write(yaml)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fixture_farm/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module FixtureFarm
VERSION = '0.1.2'
VERSION = '0.1.3'
end

0 comments on commit a9d2482

Please sign in to comment.