Class: Authorization
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Authorization
- Defined in:
- app/models/authorization.rb
Class Method Summary collapse
- .create_from_hash(hash, existing_user = nil) ⇒ Object
- .find_from_hash(hash) ⇒ Object
- .find_or_create_from_hash(hash, existing_user = nil) ⇒ Object
Instance Method Summary collapse
- #allow_destroy? ⇒ Boolean
- #assign_account_info(auth_hash) ⇒ Object
- #find_or_create_or_associate_user(existing_user = nil) ⇒ Object
Class Method Details
.create_from_hash(hash, existing_user = nil) ⇒ Object
18 19 20 21 22 23 |
# File 'app/models/authorization.rb', line 18 def self.create_from_hash(hash, existing_user = nil) create do || .assign_account_info(hash) .find_or_create_or_associate_user(existing_user) end end |
.find_from_hash(hash) ⇒ Object
25 26 27 |
# File 'app/models/authorization.rb', line 25 def self.find_from_hash(hash) find_by_provider_and_uid(hash['provider'], hash['uid']) end |
.find_or_create_from_hash(hash, existing_user = nil) ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'app/models/authorization.rb', line 8 def self.find_or_create_from_hash(hash, existing_user = nil) if (auth = find_from_hash(hash)) auth.assign_account_info(hash) auth.save auth else create_from_hash(hash, existing_user) end end |
Instance Method Details
#allow_destroy? ⇒ Boolean
40 41 42 43 44 45 |
# File 'app/models/authorization.rb', line 40 def allow_destroy? if user..count.eql?(1) errors.add(:base, "You must have at least one authorization provider.") raise ActiveRecord::Rollback end end |
#assign_account_info(auth_hash) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'app/models/authorization.rb', line 47 def assign_account_info(auth_hash) self.uid = auth_hash['uid'] self.provider = auth_hash['provider'] self.nickname = auth_hash['info']['nickname'] self.email = auth_hash['info']['email'] self.picture_url = auth_hash['info']['image'] self.name = auth_hash['info']['name'] if auth_hash['credentials'] self.access_token = auth_hash['credentials']['token'] self.access_token_secret = auth_hash['credentials']['secret'] end end |
#find_or_create_or_associate_user(existing_user = nil) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'app/models/authorization.rb', line 29 def find_or_create_or_associate_user(existing_user = nil) if existing_user self.user = existing_user elsif self.user self.user else self.user = User.(self) end end |