Module: TwoPercent::Syncable

Extended by:
ActiveSupport::Concern
Defined in:
lib/two_percent/syncable.rb

Overview

Syncable concern for syncing SCIM data to domain models

This concern provides one-way synchronization from SCIM to your domain models, ensuring SCIM remains the source of truth for identity data.

Usage:

class User < ApplicationRecord
  include TwoPercent::Syncable

  syncable_as :user, scim_id_column: :scim_id do |scim_attrs|
    {
      first_name: scim_attrs.dig(:name, :givenName),
      last_name: scim_attrs.dig(:name, :familyName),
      email: scim_attrs[:email],
      active: scim_attrs[:active]
    }
  end
end

class Group < ApplicationRecord
  include TwoPercent::Syncable

  syncable_as :group, scim_id_column: :scim_id do |scim_attrs|
    { name: scim_attrs[:display_name], active: scim_attrs[:active] }
  end
end

This provides:

  • user.scim_user => linked ScimUser record

  • user.refresh_from_scim => pull latest data from SCIM

  • User.sync_from_scim_event(event) => sync from SCIM domain events

Defined Under Namespace

Classes: Model

Instance Method Summary collapse

Instance Method Details

#refresh_from_scimObject

Refresh this record from SCIM data



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/two_percent/syncable.rb', line 181

def refresh_from_scim
  model = self.class.syncable_model
  association_name = model.scim_model_class == TwoPercent::ScimUser ? :scim_user : :scim_group
  scim_record = public_send(association_name)

  return unless scim_record

  unless model.attribute_mapper_block
    raise ArgumentError, "No attribute mapper block provided. Define one in syncable_as."
  end

  attrs = scim_record.to_domain_attributes
  mapped_attrs = model.attribute_mapper_block.call(attrs)
  assign_attributes(mapped_attrs)
  save! if changed?
end