Class: TwoPercent::Syncable::Model
- Inherits:
-
Object
- Object
- TwoPercent::Syncable::Model
- 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
-
#attribute_mapper_block ⇒ Object
readonly
Returns the value of attribute attribute_mapper_block.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#resource_type ⇒ Object
readonly
Returns the value of attribute resource_type.
-
#scim_id_column ⇒ Object
readonly
Returns the value of attribute scim_id_column.
-
#scim_model_class ⇒ Object
readonly
Returns the value of attribute scim_model_class.
Instance Method Summary collapse
-
#initialize(scim_model_class:, scim_id_column:, resource_type:, **options, &block) ⇒ Model
constructor
A new instance of Model.
-
#setup_association(domain_model_class) ⇒ Object
Setup association and validations on the domain model class.
-
#setup_group_syncable(domain_model_class) ⇒ Object
Setup ScimGroup association and validations.
-
#setup_user_syncable(domain_model_class) ⇒ Object
Setup ScimUser association and validations.
-
#sync_created(attributes, domain_model_class) ⇒ ActiveRecord::Base
Sync created event to domain model.
-
#sync_deleted(scim_id, domain_model_class) ⇒ ActiveRecord::Base?
Sync deleted event to domain model.
-
#sync_updated(attributes, domain_model_class) ⇒ ActiveRecord::Base
Sync updated event to domain model.
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:, **, &block) @scim_model_class = scim_model_class @scim_id_column = scim_id_column @resource_type = resource_type @options = @attribute_mapper_block = block end |
Instance Attribute Details
#attribute_mapper_block ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
43 44 45 |
# File 'lib/two_percent/syncable.rb', line 43 def @options end |
#resource_type ⇒ Object (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_column ⇒ Object (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_class ⇒ Object (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
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
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
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
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
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
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 |