Class: TwoPercent::Syncable::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/two_percent/syncable.rb

Overview

Encapsulates type-specific SCIM synchronization logic

This object replaces case statements and conditionals in the Syncable concern by encapsulating all knowledge about User vs Group differences.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scim_model_class:, scim_id_column:, resource_type:, **options, &block) ⇒ Model

Returns a new instance of Model.



45
46
47
48
49
50
51
# File 'lib/two_percent/syncable.rb', line 45

def initialize(scim_model_class:, scim_id_column:, resource_type:, **options, &block)
  @scim_model_class = scim_model_class
  @scim_id_column = scim_id_column
  @resource_type = resource_type
  @options = options
  @attribute_mapper_block = block
end

Instance Attribute Details

#attribute_mapper_blockObject (readonly)

Returns the value of attribute attribute_mapper_block.



43
44
45
# File 'lib/two_percent/syncable.rb', line 43

def attribute_mapper_block
  @attribute_mapper_block
end

#optionsObject (readonly)

Returns the value of attribute options.



43
44
45
# File 'lib/two_percent/syncable.rb', line 43

def options
  @options
end

#resource_typeObject (readonly)

Returns the value of attribute resource_type.



43
44
45
# File 'lib/two_percent/syncable.rb', line 43

def resource_type
  @resource_type
end

#scim_id_columnObject (readonly)

Returns the value of attribute scim_id_column.



43
44
45
# File 'lib/two_percent/syncable.rb', line 43

def scim_id_column
  @scim_id_column
end

#scim_model_classObject (readonly)

Returns the value of attribute scim_model_class.



43
44
45
# File 'lib/two_percent/syncable.rb', line 43

def scim_model_class
  @scim_model_class
end

Instance Method Details

#setup_association(domain_model_class) ⇒ Object

Setup association and validations on the domain model class

Parameters:

  • domain_model_class (Class)

    The ActiveRecord model including Syncable



56
57
58
59
60
61
62
# File 'lib/two_percent/syncable.rb', line 56

def setup_association(domain_model_class)
  if scim_model_class == TwoPercent::ScimUser
    setup_user_syncable(domain_model_class)
  else
    setup_group_syncable(domain_model_class)
  end
end

#setup_group_syncable(domain_model_class) ⇒ Object

Setup ScimGroup association and validations

Parameters:

  • domain_model_class (Class)

    The ActiveRecord model including Syncable



80
81
82
83
84
85
86
87
88
# File 'lib/two_percent/syncable.rb', line 80

def setup_group_syncable(domain_model_class)
  domain_model_class.belongs_to :scim_group,
                                class_name: "TwoPercent::ScimGroup",
                                foreign_key: scim_id_column,
                                primary_key: "scim_id",
                                optional: true

  domain_model_class.validates scim_id_column, uniqueness: true, allow_nil: true
end

#setup_user_syncable(domain_model_class) ⇒ Object

Setup ScimUser association and validations

Parameters:

  • domain_model_class (Class)

    The ActiveRecord model including Syncable



67
68
69
70
71
72
73
74
75
# File 'lib/two_percent/syncable.rb', line 67

def setup_user_syncable(domain_model_class)
  domain_model_class.belongs_to :scim_user,
                                class_name: "TwoPercent::ScimUser",
                                foreign_key: scim_id_column,
                                primary_key: "scim_id",
                                optional: true

  domain_model_class.validates scim_id_column, uniqueness: true, allow_nil: true
end

#sync_created(attributes, domain_model_class) ⇒ ActiveRecord::Base

Sync created event to domain model

Parameters:

  • attributes (Hash)

    SCIM attributes from event

  • domain_model_class (Class)

    The domain model class

Returns:

  • (ActiveRecord::Base)

    The synced record



95
96
97
# File 'lib/two_percent/syncable.rb', line 95

def sync_created(attributes, domain_model_class)
  sync_upsert(attributes, domain_model_class)
end

#sync_deleted(scim_id, domain_model_class) ⇒ ActiveRecord::Base?

Sync deleted event to domain model

Parameters:

  • scim_id (String)

    SCIM ID of deleted resource

  • domain_model_class (Class)

    The domain model class

Returns:

  • (ActiveRecord::Base, nil)

    The destroyed record



113
114
115
116
# File 'lib/two_percent/syncable.rb', line 113

def sync_deleted(scim_id, domain_model_class)
  record = domain_model_class.find_by(scim_id_column => scim_id)
  record&.destroy
end

#sync_updated(attributes, domain_model_class) ⇒ ActiveRecord::Base

Sync updated event to domain model

Parameters:

  • attributes (Hash)

    SCIM attributes from event

  • domain_model_class (Class)

    The domain model class

Returns:

  • (ActiveRecord::Base)

    The synced record



104
105
106
# File 'lib/two_percent/syncable.rb', line 104

def sync_updated(attributes, domain_model_class)
  sync_upsert(attributes, domain_model_class)
end