Class: FixtureFarm::FixtureRecorder

Inherits:
Object
  • Object
show all
Defined in:
lib/fixture_farm/fixture_recorder.rb

Constant Summary collapse

STORE_PATH =
Rails.root.join('tmp', 'fixture_farm_store.json')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fixture_name_prefix, new_models = []) ⇒ FixtureRecorder

Returns a new instance of FixtureRecorder.



7
8
9
10
11
# File 'lib/fixture_farm/fixture_recorder.rb', line 7

def initialize(fixture_name_prefix, new_models = [])
  @fixture_name_prefix = fixture_name_prefix
  @new_models = new_models
  @initial_now = Time.zone.now
end

Class Method Details

.recording_session_in_progress?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/fixture_farm/fixture_recorder.rb', line 36

def self.recording_session_in_progress?
  File.exist?(STORE_PATH)
end

.resume_recording_sessionObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fixture_farm/fixture_recorder.rb', line 13

def self.resume_recording_session
  start_recording_session! unless recording_session_in_progress?

  recording_session = JSON.load_file(STORE_PATH, permitted_classes: [ActiveSupport::HashWithIndifferentAccess])

  new_models = recording_session['new_models'].map do |(class_name, id)|
    class_name.constantize.find(id)
  end

  new(recording_session['fixture_name_prefix'], new_models)
end

.start_recording_session!(fixture_name_prefix) ⇒ Object



25
26
27
28
29
30
# File 'lib/fixture_farm/fixture_recorder.rb', line 25

def self.start_recording_session!(fixture_name_prefix)
  File.write(STORE_PATH, {
    fixture_name_prefix: fixture_name_prefix,
    new_models: []
  }.to_json)
end

.stop_recording_session!Object



32
33
34
# File 'lib/fixture_farm/fixture_recorder.rb', line 32

def self.stop_recording_session!
  FileUtils.rm_f(STORE_PATH)
end

Instance Method Details

#record_new_fixturesObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fixture_farm/fixture_recorder.rb', line 40

def record_new_fixtures
  stopped = false

  subscriber = ActiveSupport::Notifications.subscribe 'sql.active_record' do |event|
    payload = event.payload

    next unless payload[:name] =~ /([:\w]+) Create/

    new_fixture_class_name = Regexp.last_match(1)

    payload[:connection].transaction_manager.current_transaction.records.reject(&:persisted?).reject(&:destroyed?).each do |model_instance|
      next if new_fixture_class_name != model_instance.class.name

      @new_models << model_instance
    end
  end

  yield lambda {
    ActiveSupport::Notifications.unsubscribe(subscriber)
    stopped = true
    reload_models
    update_fixture_files(named_new_fixtures)
  }

  unless stopped
    reload_models
    update_fixture_files(named_new_fixtures)
  end
ensure
  ActiveSupport::Notifications.unsubscribe(subscriber)
end

#update_recording_sessionObject



72
73
74
75
76
77
78
79
# File 'lib/fixture_farm/fixture_recorder.rb', line 72

def update_recording_session
  return unless FixtureRecorder.recording_session_in_progress?

  File.write(STORE_PATH, {
    fixture_name_prefix: @fixture_name_prefix,
    new_models: @new_models.map { |model| [model.class.name, model.id] }
  }.to_json)
end