Module: Billfold::Identity::ClassMethods
- Included in:
- ActiveRecordIdentity::ClassMethods
- Defined in:
- lib/billfold/identity.rb
Instance Method Summary collapse
-
#update_or_create!(attributes = {}) ⇒ Object
### Billfold::Identity.update_or_create!.
-
#with_provider_and_value(provider, value) ⇒ Object
### Billfold::Identity.with_provider_and_value.
Instance Method Details
#update_or_create!(attributes = {}) ⇒ Object
### Billfold::Identity.update_or_create!
Updates or creates a ‘Billfold::Identity`.
### Parameters: a single ‘Hash` with the following keys:
* `:provider` -- the name of an OmniAuth provider (e.g. "twitter")
* `:user` -- if nil, creates a new `User` for the `Identity`
* `:value` -- the unique identifier
* `:data` -- extra data for the Identity
### Behavior
If ‘:provider` or `:value` is `nil`, this method raises an `ArgumentError`.
If ‘:user` is `nil`, this method creates a new `User` for the `Identity`.
If ‘:user` exists and there is no other `Identity` with the same `:value`, this method adds a new `Identity` to the `User`.
If there exists another ‘Identity` with the same `:value` and that `Identity` is owned by the given `User`, this method updates that `Identity`.
If ‘:user` exists and there exists another `Identity` with the same `:value` and that `Identity` is owned by a different `User`, this method merges that User into `:user` and updates the `Identity`.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/billfold/identity.rb', line 63 def update_or_create!(attributes = {}) raise ArgumentError.new("provider must not be blank") if attributes[:provider].blank? raise ArgumentError.new("value must not be blank") if attributes[:value].blank? identity = with_provider_and_value(attributes[:provider], attributes[:value]) if identity old_owner, new_owner = identity.user, attributes[:user] transaction do identity.update_attributes!(attributes) old_owner.merge_into!(new_owner) if old_owner != new_owner end else identity = new(attributes) identity.user = attributes[:user] || ::Billfold.user_class.new(:name => identity.name_for_user) identity.save! end identity end |
#with_provider_and_value(provider, value) ⇒ Object
### Billfold::Identity.with_provider_and_value
Return the identity with the given ‘provider` and `value`, or `nil`, if no such identity exists. Including classes must redefine this method.
32 33 34 |
# File 'lib/billfold/identity.rb', line 32 def with_provider_and_value(provider, value) raise NotImplementedError.new('classes including Billfold::Identity MUST redefine class method with_provider_and_value') end |