Commit 016d233aeadb9a496e3400348e38f34562d4ce09

Authored by Daniela Feitosa
1 parent bc1632a9

profile-suggestions: added basic structure

(ActionItem3234)
app/models/person.rb
... ... @@ -59,6 +59,10 @@ class Person < Profile
59 59 has_and_belongs_to_many :acepted_forums, :class_name => 'Forum', :join_table => 'terms_forum_people'
60 60 has_and_belongs_to_many :articles_with_access, :class_name => 'Article', :join_table => 'article_privacy_exceptions'
61 61  
  62 + has_many :profile_suggestions, :foreign_key => :person_id, :dependent => :destroy
  63 + has_many :suggested_people, :through => :profile_suggestions, :source => :suggestion, :conditions => ['profile_suggestions.suggestion_type = ? AND profile_suggestions.enabled = ?', 'Person', true]
  64 + has_many :suggested_communities, :through => :profile_suggestions, :source => :suggestion, :conditions => ['profile_suggestions.suggestion_type = ? AND profile_suggestions.enabled = ?', 'Community', true]
  65 +
62 66 scope :more_popular, :order => 'friends_count DESC'
63 67  
64 68 scope :abusers, :joins => :abuse_complaints, :conditions => ['tasks.status = 3'], :select => 'DISTINCT profiles.*'
... ...
app/models/profile.rb
... ... @@ -217,6 +217,8 @@ class Profile < ActiveRecord::Base
217 217  
218 218 has_many :abuse_complaints, :foreign_key => 'requestor_id', :dependent => :destroy
219 219  
  220 + has_many :profile_suggestions, :foreign_key => :suggestion_id, :dependent => :destroy
  221 +
220 222 def top_level_categorization
221 223 ret = {}
222 224 self.profile_categorizations.each do |c|
... ...
app/models/profile_suggestion.rb 0 → 100644
... ... @@ -0,0 +1,37 @@
  1 +class ProfileSuggestion < ActiveRecord::Base
  2 + belongs_to :person
  3 + belongs_to :suggestion, :class_name => 'Profile', :foreign_key => :suggestion_id
  4 +
  5 + attr_accessible :person, :suggestion, :suggestion_type, :categories, :similarity, :enabled
  6 +
  7 + before_create do |profile_suggestion|
  8 + profile_suggestion.suggestion_type = self.suggestion.class.to_s
  9 + end
  10 +
  11 + acts_as_having_settings :field => :categories
  12 +
  13 + validate :must_be_a_valid_category, :on => :create
  14 + def must_be_a_valid_category
  15 + if categories.keys.map { |cat| self.respond_to?(cat)}.include?(false)
  16 + errors.add(:categories, 'Category must be valid')
  17 + end
  18 + end
  19 +
  20 + validates_uniqueness_of :suggestion_id, :scope => [ :person_id ]
  21 +
  22 + CATEGORIES = {
  23 + :common_friends => _('Friends in common'),
  24 + :common_communities => _('Communities in common'),
  25 + :common_tags => _('Tags in common')
  26 + }
  27 +
  28 + CATEGORIES.keys.each do |category|
  29 + settings_items category.to_sym
  30 + attr_accessible category.to_sym
  31 + end
  32 + def disable
  33 + self.enabled = false
  34 + self.save
  35 + end
  36 +
  37 +end
... ...
db/migrate/20140720144212_create_profile_suggestions.rb 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +class CreateProfileSuggestions < ActiveRecord::Migration
  2 + def change
  3 + create_table :profile_suggestions do |t|
  4 + t.references :person
  5 + t.references :suggestion
  6 + t.string :suggestion_type
  7 + t.text :categories
  8 + t.boolean :enabled, :default => true
  9 +
  10 + t.timestamps
  11 + end
  12 + add_index :profile_suggestions, :person_id
  13 + add_index :profile_suggestions, :suggestion_id
  14 + end
  15 +end
... ...
db/schema.rb
... ... @@ -11,7 +11,7 @@
11 11 #
12 12 # It's strongly recommended to check this file into your version control system.
13 13  
14   -ActiveRecord::Schema.define(:version => 20140605222753) do
  14 +ActiveRecord::Schema.define(:version => 20140720144212) do
15 15  
16 16 create_table "abuse_reports", :force => true do |t|
17 17 t.integer "reporter_id"
... ... @@ -446,6 +446,19 @@ ActiveRecord::Schema.define(:version =&gt; 20140605222753) do
446 446 add_index "products", ["product_category_id"], :name => "index_products_on_product_category_id"
447 447 add_index "products", ["profile_id"], :name => "index_products_on_profile_id"
448 448  
  449 + create_table "profile_suggestions", :force => true do |t|
  450 + t.integer "person_id"
  451 + t.integer "suggestion_id"
  452 + t.string "suggestion_type"
  453 + t.text "categories"
  454 + t.boolean "enabled", :default => true
  455 + t.datetime "created_at", :null => false
  456 + t.datetime "updated_at", :null => false
  457 + end
  458 +
  459 + add_index "profile_suggestions", ["person_id"], :name => "index_profile_suggestions_on_person_id"
  460 + add_index "profile_suggestions", ["suggestion_id"], :name => "index_profile_suggestions_on_suggestion_id"
  461 +
449 462 create_table "profiles", :force => true do |t|
450 463 t.string "name"
451 464 t.string "type"
... ...
test/unit/profile_suggestion_test.rb 0 → 100644
... ... @@ -0,0 +1,47 @@
  1 +# encoding: UTF-8
  2 +require File.dirname(__FILE__) + '/../test_helper'
  3 +
  4 +class ProfileSuggestionTest < ActiveSupport::TestCase
  5 +
  6 + should 'save the profile class' do
  7 + person = create_user('person').person
  8 + suggested_community = fast_create(Community)
  9 +
  10 + suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_community)
  11 + assert_equal 'Community', suggestion.suggestion_type
  12 + end
  13 +
  14 + should 'only accept pre-defined categories' do
  15 + person = create_user('person').person
  16 + suggested_community = fast_create(Community)
  17 +
  18 + suggestion = ProfileSuggestion.new(:person => person, :suggestion => suggested_community)
  19 +
  20 + suggestion.categories = {:unexistent => 1}
  21 + suggestion.valid?
  22 + assert suggestion.errors[:categories.to_s].present?
  23 + end
  24 +
  25 + should 'disable a suggestion' do
  26 + person = create_user('person').person
  27 + suggested_community = fast_create(Community)
  28 +
  29 + suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_community)
  30 +
  31 + suggestion.disable
  32 + assert_equal false, ProfileSuggestion.find(suggestion.id).enabled?
  33 + end
  34 +
  35 + should 'not suggest the same community twice' do
  36 + person = create_user('person').person
  37 + suggested_community = fast_create(Community)
  38 +
  39 + ProfileSuggestion.create(:person => person, :suggestion => suggested_community)
  40 +
  41 + repeated_suggestion = ProfileSuggestion.new(:person => person, :suggestion => suggested_community)
  42 +
  43 + repeated_suggestion.valid?
  44 + assert_equal true, repeated_suggestion.errors[:suggestion_id.to_s].present?
  45 + end
  46 +
  47 +end
... ...