From 016d233aeadb9a496e3400348e38f34562d4ce09 Mon Sep 17 00:00:00 2001 From: Daniela Feitosa Date: Sat, 26 Jul 2014 21:09:16 +0000 Subject: [PATCH] profile-suggestions: added basic structure --- app/models/person.rb | 4 ++++ app/models/profile.rb | 2 ++ app/models/profile_suggestion.rb | 37 +++++++++++++++++++++++++++++++++++++ db/migrate/20140720144212_create_profile_suggestions.rb | 15 +++++++++++++++ db/schema.rb | 15 ++++++++++++++- test/unit/profile_suggestion_test.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 app/models/profile_suggestion.rb create mode 100644 db/migrate/20140720144212_create_profile_suggestions.rb create mode 100644 test/unit/profile_suggestion_test.rb diff --git a/app/models/person.rb b/app/models/person.rb index d0d0521..8eeddb0 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -59,6 +59,10 @@ class Person < Profile has_and_belongs_to_many :acepted_forums, :class_name => 'Forum', :join_table => 'terms_forum_people' has_and_belongs_to_many :articles_with_access, :class_name => 'Article', :join_table => 'article_privacy_exceptions' + has_many :profile_suggestions, :foreign_key => :person_id, :dependent => :destroy + has_many :suggested_people, :through => :profile_suggestions, :source => :suggestion, :conditions => ['profile_suggestions.suggestion_type = ? AND profile_suggestions.enabled = ?', 'Person', true] + has_many :suggested_communities, :through => :profile_suggestions, :source => :suggestion, :conditions => ['profile_suggestions.suggestion_type = ? AND profile_suggestions.enabled = ?', 'Community', true] + scope :more_popular, :order => 'friends_count DESC' scope :abusers, :joins => :abuse_complaints, :conditions => ['tasks.status = 3'], :select => 'DISTINCT profiles.*' diff --git a/app/models/profile.rb b/app/models/profile.rb index 1506d8d..c546163 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -217,6 +217,8 @@ class Profile < ActiveRecord::Base has_many :abuse_complaints, :foreign_key => 'requestor_id', :dependent => :destroy + has_many :profile_suggestions, :foreign_key => :suggestion_id, :dependent => :destroy + def top_level_categorization ret = {} self.profile_categorizations.each do |c| diff --git a/app/models/profile_suggestion.rb b/app/models/profile_suggestion.rb new file mode 100644 index 0000000..037de13 --- /dev/null +++ b/app/models/profile_suggestion.rb @@ -0,0 +1,37 @@ +class ProfileSuggestion < ActiveRecord::Base + belongs_to :person + belongs_to :suggestion, :class_name => 'Profile', :foreign_key => :suggestion_id + + attr_accessible :person, :suggestion, :suggestion_type, :categories, :similarity, :enabled + + before_create do |profile_suggestion| + profile_suggestion.suggestion_type = self.suggestion.class.to_s + end + + acts_as_having_settings :field => :categories + + validate :must_be_a_valid_category, :on => :create + def must_be_a_valid_category + if categories.keys.map { |cat| self.respond_to?(cat)}.include?(false) + errors.add(:categories, 'Category must be valid') + end + end + + validates_uniqueness_of :suggestion_id, :scope => [ :person_id ] + + CATEGORIES = { + :common_friends => _('Friends in common'), + :common_communities => _('Communities in common'), + :common_tags => _('Tags in common') + } + + CATEGORIES.keys.each do |category| + settings_items category.to_sym + attr_accessible category.to_sym + end + def disable + self.enabled = false + self.save + end + +end diff --git a/db/migrate/20140720144212_create_profile_suggestions.rb b/db/migrate/20140720144212_create_profile_suggestions.rb new file mode 100644 index 0000000..8a3b726 --- /dev/null +++ b/db/migrate/20140720144212_create_profile_suggestions.rb @@ -0,0 +1,15 @@ +class CreateProfileSuggestions < ActiveRecord::Migration + def change + create_table :profile_suggestions do |t| + t.references :person + t.references :suggestion + t.string :suggestion_type + t.text :categories + t.boolean :enabled, :default => true + + t.timestamps + end + add_index :profile_suggestions, :person_id + add_index :profile_suggestions, :suggestion_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 15598d2..d5eabfe 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20140605222753) do +ActiveRecord::Schema.define(:version => 20140720144212) do create_table "abuse_reports", :force => true do |t| t.integer "reporter_id" @@ -446,6 +446,19 @@ ActiveRecord::Schema.define(:version => 20140605222753) do add_index "products", ["product_category_id"], :name => "index_products_on_product_category_id" add_index "products", ["profile_id"], :name => "index_products_on_profile_id" + create_table "profile_suggestions", :force => true do |t| + t.integer "person_id" + t.integer "suggestion_id" + t.string "suggestion_type" + t.text "categories" + t.boolean "enabled", :default => true + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + add_index "profile_suggestions", ["person_id"], :name => "index_profile_suggestions_on_person_id" + add_index "profile_suggestions", ["suggestion_id"], :name => "index_profile_suggestions_on_suggestion_id" + create_table "profiles", :force => true do |t| t.string "name" t.string "type" diff --git a/test/unit/profile_suggestion_test.rb b/test/unit/profile_suggestion_test.rb new file mode 100644 index 0000000..fbe2d8f --- /dev/null +++ b/test/unit/profile_suggestion_test.rb @@ -0,0 +1,47 @@ +# encoding: UTF-8 +require File.dirname(__FILE__) + '/../test_helper' + +class ProfileSuggestionTest < ActiveSupport::TestCase + + should 'save the profile class' do + person = create_user('person').person + suggested_community = fast_create(Community) + + suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_community) + assert_equal 'Community', suggestion.suggestion_type + end + + should 'only accept pre-defined categories' do + person = create_user('person').person + suggested_community = fast_create(Community) + + suggestion = ProfileSuggestion.new(:person => person, :suggestion => suggested_community) + + suggestion.categories = {:unexistent => 1} + suggestion.valid? + assert suggestion.errors[:categories.to_s].present? + end + + should 'disable a suggestion' do + person = create_user('person').person + suggested_community = fast_create(Community) + + suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_community) + + suggestion.disable + assert_equal false, ProfileSuggestion.find(suggestion.id).enabled? + end + + should 'not suggest the same community twice' do + person = create_user('person').person + suggested_community = fast_create(Community) + + ProfileSuggestion.create(:person => person, :suggestion => suggested_community) + + repeated_suggestion = ProfileSuggestion.new(:person => person, :suggestion => suggested_community) + + repeated_suggestion.valid? + assert_equal true, repeated_suggestion.errors[:suggestion_id.to_s].present? + end + +end -- libgit2 0.21.2