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 | 42 | |
43 | 43 | oauth_auth = person.oauth_auth |
44 | 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 | 46 | external_person_uid: auth.uid, external_person_image_url: auth.info.image } |
47 | 47 | oauth_auth = OauthClientPlugin::Auth.create_for_strategy(provider.strategy, auth_data) |
48 | 48 | end | ... | ... |
plugins/oauth_client/db/migrate/20160720165808_add_external_profile_to_oauth_auth.rb
1 | 1 | class AddExternalProfileToOauthAuth < ActiveRecord::Migration |
2 | 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 | 4 | add_column :oauth_client_plugin_auths, :external_person_uid, :string |
5 | 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 | 7 | end |
8 | 8 | |
9 | 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 | 12 | remove_column :oauth_client_plugin_auths, :external_person_uid |
13 | 13 | remove_column :oauth_client_plugin_auths, :external_person_image_url |
14 | 14 | end | ... | ... |
plugins/oauth_client/lib/ext/external_person.rb
... | ... | @@ -2,7 +2,7 @@ require_dependency 'external_person' |
2 | 2 | |
3 | 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 | 6 | has_one :oauth_provider, through: :oauth_auth, source: :provider |
7 | 7 | |
8 | 8 | def avatar | ... | ... |
plugins/oauth_client/lib/ext/profile.rb
... | ... | @@ -2,7 +2,7 @@ require_dependency 'profile' |
2 | 2 | |
3 | 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 | 6 | has_many :oauth_providers, through: :oauth_auths, source: :provider |
7 | 7 | |
8 | 8 | end | ... | ... |
plugins/oauth_client/models/oauth_client_plugin/auth.rb
1 | 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 | 8 | belongs_to :provider, class_name: 'OauthClientPlugin::Provider' |
10 | 9 | |
11 | 10 | validates_presence_of :provider |
11 | + validates_presence_of :profile | |
12 | 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 | 14 | acts_as_having_settings field: :data |
18 | 15 | |
... | ... | @@ -36,12 +33,6 @@ class OauthClientPlugin::Auth < ApplicationRecord |
36 | 33 | OauthClientPlugin::Auth.create!(args.merge(type: class_name)) |
37 | 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 | 36 | IMAGE_SIZES = { |
46 | 37 | :big => "150", |
47 | 38 | :thumb => "100", | ... | ... |
plugins/oauth_client/models/oauth_client_plugin/github_auth.rb
plugins/oauth_client/models/oauth_client_plugin/noosfero_oauth2_auth.rb
1 | 1 | class OauthClientPlugin::NoosferoOauth2Auth < OauthClientPlugin::Auth |
2 | 2 | |
3 | 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 | 5 | end |
6 | 6 | |
7 | 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 | 9 | end |
10 | 10 | |
11 | 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 | 13 | end |
14 | 14 | end | ... | ... |
plugins/oauth_client/models/oauth_client_plugin/twitter_auth.rb
plugins/oauth_client/test/unit/auth_test.rb
... | ... | @@ -19,7 +19,7 @@ class AuthTest < ActiveSupport::TestCase |
19 | 19 | end |
20 | 20 | |
21 | 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 | 23 | provider: @provider) |
24 | 24 | assert auth.id.present? |
25 | 25 | end |
... | ... | @@ -40,19 +40,19 @@ class AuthTest < ActiveSupport::TestCase |
40 | 40 | STRATEGIES.each do |strategy| |
41 | 41 | should "override the external person's image url for #{strategy} strategy" do |
42 | 42 | auth = OauthClientPlugin::Auth.create_for_strategy(strategy, provider: @provider, |
43 | - external_person: @external_person) | |
43 | + profile: @external_person) | |
44 | 44 | assert_not auth.image_url.nil? |
45 | 45 | end |
46 | 46 | |
47 | 47 | should "override the external person's profile url for #{strategy} strategy" do |
48 | 48 | auth = OauthClientPlugin::Auth.create_for_strategy(strategy, provider: @provider, |
49 | - external_person: @external_person) | |
49 | + profile: @external_person) | |
50 | 50 | assert_not auth.profile_url.nil? |
51 | 51 | end |
52 | 52 | |
53 | 53 | should "override the external person's profile settings url for #{strategy} strategy" do |
54 | 54 | auth = OauthClientPlugin::Auth.create_for_strategy(strategy, provider: @provider, |
55 | - external_person: @external_person) | |
55 | + profile: @external_person) | |
56 | 56 | assert_not auth.settings_url.nil? |
57 | 57 | end |
58 | 58 | end | ... | ... |