From 9e014471cd8c2f0bec17fbbee545c2623923831f Mon Sep 17 00:00:00 2001 From: Gabriel Silva Date: Mon, 8 Aug 2016 16:55:50 +0000 Subject: [PATCH] Refactors Auth relation to polymorphic --- plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb | 2 +- plugins/oauth_client/db/migrate/20160720165808_add_external_profile_to_oauth_auth.rb | 8 ++++---- plugins/oauth_client/lib/ext/external_person.rb | 2 +- plugins/oauth_client/lib/ext/profile.rb | 2 +- plugins/oauth_client/models/oauth_client_plugin/auth.rb | 19 +++++-------------- plugins/oauth_client/models/oauth_client_plugin/github_auth.rb | 2 +- plugins/oauth_client/models/oauth_client_plugin/noosfero_oauth2_auth.rb | 6 +++--- plugins/oauth_client/models/oauth_client_plugin/twitter_auth.rb | 2 +- plugins/oauth_client/test/unit/auth_test.rb | 8 ++++---- 9 files changed, 21 insertions(+), 30 deletions(-) diff --git a/plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb b/plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb index 72a7e4d..81d0d20 100644 --- a/plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb +++ b/plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb @@ -42,7 +42,7 @@ class OauthClientPluginPublicController < PublicController oauth_auth = person.oauth_auth if oauth_auth.nil? - auth_data = { external_person: person, provider: provider, enabled: true, + auth_data = { profile: person, provider: provider, enabled: true, external_person_uid: auth.uid, external_person_image_url: auth.info.image } oauth_auth = OauthClientPlugin::Auth.create_for_strategy(provider.strategy, auth_data) end diff --git a/plugins/oauth_client/db/migrate/20160720165808_add_external_profile_to_oauth_auth.rb b/plugins/oauth_client/db/migrate/20160720165808_add_external_profile_to_oauth_auth.rb index 855e2ce..c803855 100644 --- a/plugins/oauth_client/db/migrate/20160720165808_add_external_profile_to_oauth_auth.rb +++ b/plugins/oauth_client/db/migrate/20160720165808_add_external_profile_to_oauth_auth.rb @@ -1,14 +1,14 @@ class AddExternalProfileToOauthAuth < ActiveRecord::Migration def up - add_column :oauth_client_plugin_auths, :external_person_id, :integer + add_column :oauth_client_plugin_auths, :profile_type, :string add_column :oauth_client_plugin_auths, :external_person_uid, :string add_column :oauth_client_plugin_auths, :external_person_image_url, :string - add_index :oauth_client_plugin_auths, :external_person_id + add_index :oauth_client_plugin_auths, :profile_type end def down - remove_index :oauth_client_plugin_auths, :external_person_id - remove_column :oauth_client_plugin_auths, :external_person_id + remove_index :oauth_client_plugin_auths, :profile_type + remove_column :oauth_client_plugin_auths, :profile_type remove_column :oauth_client_plugin_auths, :external_person_uid remove_column :oauth_client_plugin_auths, :external_person_image_url end diff --git a/plugins/oauth_client/lib/ext/external_person.rb b/plugins/oauth_client/lib/ext/external_person.rb index 14639e7..469e736 100644 --- a/plugins/oauth_client/lib/ext/external_person.rb +++ b/plugins/oauth_client/lib/ext/external_person.rb @@ -2,7 +2,7 @@ require_dependency 'external_person' class ExternalPerson - has_one :oauth_auth, foreign_key: :external_person_id, class_name: 'OauthClientPlugin::Auth', dependent: :destroy + has_one :oauth_auth, as: :profile, class_name: 'OauthClientPlugin::Auth', dependent: :destroy has_one :oauth_provider, through: :oauth_auth, source: :provider def avatar diff --git a/plugins/oauth_client/lib/ext/profile.rb b/plugins/oauth_client/lib/ext/profile.rb index f0c1c65..d5eb948 100644 --- a/plugins/oauth_client/lib/ext/profile.rb +++ b/plugins/oauth_client/lib/ext/profile.rb @@ -2,7 +2,7 @@ require_dependency 'profile' class Profile - has_many :oauth_auths, foreign_key: :profile_id, class_name: 'OauthClientPlugin::Auth', dependent: :destroy + has_many :oauth_auths, as: :profile, class_name: 'OauthClientPlugin::Auth', dependent: :destroy has_many :oauth_providers, through: :oauth_auths, source: :provider end diff --git a/plugins/oauth_client/models/oauth_client_plugin/auth.rb b/plugins/oauth_client/models/oauth_client_plugin/auth.rb index bf99049..fdee8a8 100644 --- a/plugins/oauth_client/models/oauth_client_plugin/auth.rb +++ b/plugins/oauth_client/models/oauth_client_plugin/auth.rb @@ -1,18 +1,15 @@ class OauthClientPlugin::Auth < ApplicationRecord - attr_accessible :profile, :external_person, :provider, - :enabled, :access_token, :expires_in, :type, - :external_person_uid, :external_person_image_url + attr_accessible :profile, :provider, :enabled, :access_token, + :expires_in, :type, :external_person_uid, + :external_person_image_url - belongs_to :profile, class_name: 'Profile' - belongs_to :external_person, class_name: 'ExternalPerson' + belongs_to :profile, polymorphic: true belongs_to :provider, class_name: 'OauthClientPlugin::Provider' validates_presence_of :provider + validates_presence_of :profile validates_uniqueness_of :profile_id, scope: :provider_id - validates_uniqueness_of :external_person_id, scope: :provider_id - - validate :must_be_related_to_profile acts_as_having_settings field: :data @@ -36,12 +33,6 @@ class OauthClientPlugin::Auth < ApplicationRecord OauthClientPlugin::Auth.create!(args.merge(type: class_name)) end - def must_be_related_to_profile - if self.profile.nil? && self.external_person.nil? - self.errors.add(:base, "Must be related to a profile or an external person") - end - end - IMAGE_SIZES = { :big => "150", :thumb => "100", diff --git a/plugins/oauth_client/models/oauth_client_plugin/github_auth.rb b/plugins/oauth_client/models/oauth_client_plugin/github_auth.rb index f7891ad..758d223 100644 --- a/plugins/oauth_client/models/oauth_client_plugin/github_auth.rb +++ b/plugins/oauth_client/models/oauth_client_plugin/github_auth.rb @@ -6,7 +6,7 @@ class OauthClientPlugin::GithubAuth < OauthClientPlugin::Auth end def profile_url - "https://www.github.com/#{self.external_person.identifier}" + "https://www.github.com/#{self.profile.identifier}" end def settings_url diff --git a/plugins/oauth_client/models/oauth_client_plugin/noosfero_oauth2_auth.rb b/plugins/oauth_client/models/oauth_client_plugin/noosfero_oauth2_auth.rb index 8dc79f3..dddf676 100644 --- a/plugins/oauth_client/models/oauth_client_plugin/noosfero_oauth2_auth.rb +++ b/plugins/oauth_client/models/oauth_client_plugin/noosfero_oauth2_auth.rb @@ -1,14 +1,14 @@ class OauthClientPlugin::NoosferoOauth2Auth < OauthClientPlugin::Auth def image_url(size = "") - URI.join("http://#{self.provider.client_options[:site]}/profile/#{self.external_person.identifier}/icon/", size) + URI.join("http://#{self.provider.client_options[:site]}/profile/#{self.profile.identifier}/icon/", size) end def profile_url - "http://#{self.external_person.source}/profile/#{self.external_person.identifier}" + "http://#{self.profile.source}/profile/#{self.profile.identifier}" end def settings_url - "http://#{self.external_person.source}/myprofile/#{self.external_person.identifier}" + "http://#{self.profile.source}/myprofile/#{self.profile.identifier}" end end diff --git a/plugins/oauth_client/models/oauth_client_plugin/twitter_auth.rb b/plugins/oauth_client/models/oauth_client_plugin/twitter_auth.rb index 53a0acc..14d08fa 100644 --- a/plugins/oauth_client/models/oauth_client_plugin/twitter_auth.rb +++ b/plugins/oauth_client/models/oauth_client_plugin/twitter_auth.rb @@ -14,7 +14,7 @@ class OauthClientPlugin::TwitterAuth < OauthClientPlugin::Auth end def profile_url - "https://twitter.com/#{self.external_person.identifier}" + "https://twitter.com/#{self.profile.identifier}" end def settings_url diff --git a/plugins/oauth_client/test/unit/auth_test.rb b/plugins/oauth_client/test/unit/auth_test.rb index 18aa653..5e71608 100644 --- a/plugins/oauth_client/test/unit/auth_test.rb +++ b/plugins/oauth_client/test/unit/auth_test.rb @@ -19,7 +19,7 @@ class AuthTest < ActiveSupport::TestCase end should "create an auth with an external person" do - auth = OauthClientPlugin::Auth.create!(external_person: @external_person, + auth = OauthClientPlugin::Auth.create!(profile: @external_person, provider: @provider) assert auth.id.present? end @@ -40,19 +40,19 @@ class AuthTest < ActiveSupport::TestCase STRATEGIES.each do |strategy| should "override the external person's image url for #{strategy} strategy" do auth = OauthClientPlugin::Auth.create_for_strategy(strategy, provider: @provider, - external_person: @external_person) + profile: @external_person) assert_not auth.image_url.nil? end should "override the external person's profile url for #{strategy} strategy" do auth = OauthClientPlugin::Auth.create_for_strategy(strategy, provider: @provider, - external_person: @external_person) + profile: @external_person) assert_not auth.profile_url.nil? end should "override the external person's profile settings url for #{strategy} strategy" do auth = OauthClientPlugin::Auth.create_for_strategy(strategy, provider: @provider, - external_person: @external_person) + profile: @external_person) assert_not auth.settings_url.nil? end end -- libgit2 0.21.2