Commit 9e014471cd8c2f0bec17fbbee545c2623923831f
1 parent
ef9afcda
Refactors Auth relation to polymorphic
Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
Showing
9 changed files
with
21 additions
and
30 deletions
Show diff stats
plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb
@@ -42,7 +42,7 @@ class OauthClientPluginPublicController < PublicController | @@ -42,7 +42,7 @@ class OauthClientPluginPublicController < PublicController | ||
42 | 42 | ||
43 | oauth_auth = person.oauth_auth | 43 | oauth_auth = person.oauth_auth |
44 | if oauth_auth.nil? | 44 | if oauth_auth.nil? |
45 | - auth_data = { external_person: person, provider: provider, enabled: true, | 45 | + auth_data = { profile: person, provider: provider, enabled: true, |
46 | external_person_uid: auth.uid, external_person_image_url: auth.info.image } | 46 | external_person_uid: auth.uid, external_person_image_url: auth.info.image } |
47 | oauth_auth = OauthClientPlugin::Auth.create_for_strategy(provider.strategy, auth_data) | 47 | oauth_auth = OauthClientPlugin::Auth.create_for_strategy(provider.strategy, auth_data) |
48 | end | 48 | end |
plugins/oauth_client/db/migrate/20160720165808_add_external_profile_to_oauth_auth.rb
1 | class AddExternalProfileToOauthAuth < ActiveRecord::Migration | 1 | class AddExternalProfileToOauthAuth < ActiveRecord::Migration |
2 | def up | 2 | def up |
3 | - add_column :oauth_client_plugin_auths, :external_person_id, :integer | 3 | + add_column :oauth_client_plugin_auths, :profile_type, :string |
4 | add_column :oauth_client_plugin_auths, :external_person_uid, :string | 4 | add_column :oauth_client_plugin_auths, :external_person_uid, :string |
5 | add_column :oauth_client_plugin_auths, :external_person_image_url, :string | 5 | add_column :oauth_client_plugin_auths, :external_person_image_url, :string |
6 | - add_index :oauth_client_plugin_auths, :external_person_id | 6 | + add_index :oauth_client_plugin_auths, :profile_type |
7 | end | 7 | end |
8 | 8 | ||
9 | def down | 9 | def down |
10 | - remove_index :oauth_client_plugin_auths, :external_person_id | ||
11 | - remove_column :oauth_client_plugin_auths, :external_person_id | 10 | + remove_index :oauth_client_plugin_auths, :profile_type |
11 | + remove_column :oauth_client_plugin_auths, :profile_type | ||
12 | remove_column :oauth_client_plugin_auths, :external_person_uid | 12 | remove_column :oauth_client_plugin_auths, :external_person_uid |
13 | remove_column :oauth_client_plugin_auths, :external_person_image_url | 13 | remove_column :oauth_client_plugin_auths, :external_person_image_url |
14 | end | 14 | end |
plugins/oauth_client/lib/ext/external_person.rb
@@ -2,7 +2,7 @@ require_dependency 'external_person' | @@ -2,7 +2,7 @@ require_dependency 'external_person' | ||
2 | 2 | ||
3 | class ExternalPerson | 3 | class ExternalPerson |
4 | 4 | ||
5 | - has_one :oauth_auth, foreign_key: :external_person_id, class_name: 'OauthClientPlugin::Auth', dependent: :destroy | 5 | + has_one :oauth_auth, as: :profile, class_name: 'OauthClientPlugin::Auth', dependent: :destroy |
6 | has_one :oauth_provider, through: :oauth_auth, source: :provider | 6 | has_one :oauth_provider, through: :oauth_auth, source: :provider |
7 | 7 | ||
8 | def avatar | 8 | def avatar |
plugins/oauth_client/lib/ext/profile.rb
@@ -2,7 +2,7 @@ require_dependency 'profile' | @@ -2,7 +2,7 @@ require_dependency 'profile' | ||
2 | 2 | ||
3 | class Profile | 3 | class Profile |
4 | 4 | ||
5 | - has_many :oauth_auths, foreign_key: :profile_id, class_name: 'OauthClientPlugin::Auth', dependent: :destroy | 5 | + has_many :oauth_auths, as: :profile, class_name: 'OauthClientPlugin::Auth', dependent: :destroy |
6 | has_many :oauth_providers, through: :oauth_auths, source: :provider | 6 | has_many :oauth_providers, through: :oauth_auths, source: :provider |
7 | 7 | ||
8 | end | 8 | end |
plugins/oauth_client/models/oauth_client_plugin/auth.rb
1 | class OauthClientPlugin::Auth < ApplicationRecord | 1 | class OauthClientPlugin::Auth < ApplicationRecord |
2 | 2 | ||
3 | - attr_accessible :profile, :external_person, :provider, | ||
4 | - :enabled, :access_token, :expires_in, :type, | ||
5 | - :external_person_uid, :external_person_image_url | 3 | + attr_accessible :profile, :provider, :enabled, :access_token, |
4 | + :expires_in, :type, :external_person_uid, | ||
5 | + :external_person_image_url | ||
6 | 6 | ||
7 | - belongs_to :profile, class_name: 'Profile' | ||
8 | - belongs_to :external_person, class_name: 'ExternalPerson' | 7 | + belongs_to :profile, polymorphic: true |
9 | belongs_to :provider, class_name: 'OauthClientPlugin::Provider' | 8 | belongs_to :provider, class_name: 'OauthClientPlugin::Provider' |
10 | 9 | ||
11 | validates_presence_of :provider | 10 | validates_presence_of :provider |
11 | + validates_presence_of :profile | ||
12 | validates_uniqueness_of :profile_id, scope: :provider_id | 12 | validates_uniqueness_of :profile_id, scope: :provider_id |
13 | - validates_uniqueness_of :external_person_id, scope: :provider_id | ||
14 | - | ||
15 | - validate :must_be_related_to_profile | ||
16 | 13 | ||
17 | acts_as_having_settings field: :data | 14 | acts_as_having_settings field: :data |
18 | 15 | ||
@@ -36,12 +33,6 @@ class OauthClientPlugin::Auth < ApplicationRecord | @@ -36,12 +33,6 @@ class OauthClientPlugin::Auth < ApplicationRecord | ||
36 | OauthClientPlugin::Auth.create!(args.merge(type: class_name)) | 33 | OauthClientPlugin::Auth.create!(args.merge(type: class_name)) |
37 | end | 34 | end |
38 | 35 | ||
39 | - def must_be_related_to_profile | ||
40 | - if self.profile.nil? && self.external_person.nil? | ||
41 | - self.errors.add(:base, "Must be related to a profile or an external person") | ||
42 | - end | ||
43 | - end | ||
44 | - | ||
45 | IMAGE_SIZES = { | 36 | IMAGE_SIZES = { |
46 | :big => "150", | 37 | :big => "150", |
47 | :thumb => "100", | 38 | :thumb => "100", |
plugins/oauth_client/models/oauth_client_plugin/github_auth.rb
@@ -6,7 +6,7 @@ class OauthClientPlugin::GithubAuth < OauthClientPlugin::Auth | @@ -6,7 +6,7 @@ class OauthClientPlugin::GithubAuth < OauthClientPlugin::Auth | ||
6 | end | 6 | end |
7 | 7 | ||
8 | def profile_url | 8 | def profile_url |
9 | - "https://www.github.com/#{self.external_person.identifier}" | 9 | + "https://www.github.com/#{self.profile.identifier}" |
10 | end | 10 | end |
11 | 11 | ||
12 | def settings_url | 12 | def settings_url |
plugins/oauth_client/models/oauth_client_plugin/noosfero_oauth2_auth.rb
1 | class OauthClientPlugin::NoosferoOauth2Auth < OauthClientPlugin::Auth | 1 | class OauthClientPlugin::NoosferoOauth2Auth < OauthClientPlugin::Auth |
2 | 2 | ||
3 | def image_url(size = "") | 3 | def image_url(size = "") |
4 | - URI.join("http://#{self.provider.client_options[:site]}/profile/#{self.external_person.identifier}/icon/", size) | 4 | + URI.join("http://#{self.provider.client_options[:site]}/profile/#{self.profile.identifier}/icon/", size) |
5 | end | 5 | end |
6 | 6 | ||
7 | def profile_url | 7 | def profile_url |
8 | - "http://#{self.external_person.source}/profile/#{self.external_person.identifier}" | 8 | + "http://#{self.profile.source}/profile/#{self.profile.identifier}" |
9 | end | 9 | end |
10 | 10 | ||
11 | def settings_url | 11 | def settings_url |
12 | - "http://#{self.external_person.source}/myprofile/#{self.external_person.identifier}" | 12 | + "http://#{self.profile.source}/myprofile/#{self.profile.identifier}" |
13 | end | 13 | end |
14 | end | 14 | end |
plugins/oauth_client/models/oauth_client_plugin/twitter_auth.rb
@@ -14,7 +14,7 @@ class OauthClientPlugin::TwitterAuth < OauthClientPlugin::Auth | @@ -14,7 +14,7 @@ class OauthClientPlugin::TwitterAuth < OauthClientPlugin::Auth | ||
14 | end | 14 | end |
15 | 15 | ||
16 | def profile_url | 16 | def profile_url |
17 | - "https://twitter.com/#{self.external_person.identifier}" | 17 | + "https://twitter.com/#{self.profile.identifier}" |
18 | end | 18 | end |
19 | 19 | ||
20 | def settings_url | 20 | def settings_url |
plugins/oauth_client/test/unit/auth_test.rb
@@ -19,7 +19,7 @@ class AuthTest < ActiveSupport::TestCase | @@ -19,7 +19,7 @@ class AuthTest < ActiveSupport::TestCase | ||
19 | end | 19 | end |
20 | 20 | ||
21 | should "create an auth with an external person" do | 21 | should "create an auth with an external person" do |
22 | - auth = OauthClientPlugin::Auth.create!(external_person: @external_person, | 22 | + auth = OauthClientPlugin::Auth.create!(profile: @external_person, |
23 | provider: @provider) | 23 | provider: @provider) |
24 | assert auth.id.present? | 24 | assert auth.id.present? |
25 | end | 25 | end |
@@ -40,19 +40,19 @@ class AuthTest < ActiveSupport::TestCase | @@ -40,19 +40,19 @@ class AuthTest < ActiveSupport::TestCase | ||
40 | STRATEGIES.each do |strategy| | 40 | STRATEGIES.each do |strategy| |
41 | should "override the external person's image url for #{strategy} strategy" do | 41 | should "override the external person's image url for #{strategy} strategy" do |
42 | auth = OauthClientPlugin::Auth.create_for_strategy(strategy, provider: @provider, | 42 | auth = OauthClientPlugin::Auth.create_for_strategy(strategy, provider: @provider, |
43 | - external_person: @external_person) | 43 | + profile: @external_person) |
44 | assert_not auth.image_url.nil? | 44 | assert_not auth.image_url.nil? |
45 | end | 45 | end |
46 | 46 | ||
47 | should "override the external person's profile url for #{strategy} strategy" do | 47 | should "override the external person's profile url for #{strategy} strategy" do |
48 | auth = OauthClientPlugin::Auth.create_for_strategy(strategy, provider: @provider, | 48 | auth = OauthClientPlugin::Auth.create_for_strategy(strategy, provider: @provider, |
49 | - external_person: @external_person) | 49 | + profile: @external_person) |
50 | assert_not auth.profile_url.nil? | 50 | assert_not auth.profile_url.nil? |
51 | end | 51 | end |
52 | 52 | ||
53 | should "override the external person's profile settings url for #{strategy} strategy" do | 53 | should "override the external person's profile settings url for #{strategy} strategy" do |
54 | auth = OauthClientPlugin::Auth.create_for_strategy(strategy, provider: @provider, | 54 | auth = OauthClientPlugin::Auth.create_for_strategy(strategy, provider: @provider, |
55 | - external_person: @external_person) | 55 | + profile: @external_person) |
56 | assert_not auth.settings_url.nil? | 56 | assert_not auth.settings_url.nil? |
57 | end | 57 | end |
58 | end | 58 | end |