Module: PlaypathRails::Synchronizable

Extended by:
ActiveSupport::Concern
Defined in:
lib/playpath_rails/synchronizable.rb

Overview

Module to add synchronization callbacks and methods to ActiveRecord models

Instance Method Summary collapse

Instance Method Details

#playpath_delete!Object

Remove record from PlayPath.io



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/playpath_rails/synchronizable.rb', line 68

def playpath_delete!
  return true unless playpath_item_id && playpath_item_id != 0

  begin
    PlaypathRails.client.delete_item(playpath_item_id)
    true
  rescue PlaypathRails::NotFoundError
    # Item already deleted, consider this success
    true
  rescue PlaypathRails::Error
    # Log the error but don't raise it to avoid breaking the application
    # Rails.logger.error("PlayPath delete failed for #{self.class.name}##{id}: #{e.message}") if defined?(Rails)
    false
  end
end

#playpath_item_idObject

Get the PlayPath item ID for this record



90
91
92
93
94
# File 'lib/playpath_rails/synchronizable.rb', line 90

def playpath_item_id
  return nil unless respond_to?(:playpath_item_id)

  read_attribute(:playpath_item_id)
end

#playpath_sync!Object

Push current record state to PlayPath.io



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/playpath_rails/synchronizable.rb', line 43

def playpath_sync!
  return true unless should_sync?

  begin
    item_data = build_item_data

    if playpath_item_id && playpath_item_id != 0
      # Update existing item
      PlaypathRails.client.update_item(playpath_item_id, **item_data)
    else
      # Create new item
      response = PlaypathRails.client.create_item(**item_data)
      # Store the item ID if the model supports it
      update_column(:playpath_item_id, response['id']) if respond_to?(:playpath_item_id=) && response&.dig('id')
    end

    true
  rescue PlaypathRails::Error
    # Log the error but don't raise it to avoid breaking the application
    # Rails.logger.error("PlayPath sync failed for #{self.class.name}##{id}: #{e.message}") if defined?(Rails)
    false
  end
end

#sync_to_playpath!Object

Manually sync this record to PlayPath.io (bypasses callbacks)



85
86
87
# File 'lib/playpath_rails/synchronizable.rb', line 85

def sync_to_playpath!
  playpath_sync!
end