Commit 404d762080be1abed80b40f3e57fa36cb7c7990b
Committed by
Evandro Jr
1 parent
d3b995c2
Exists in
staging
and in
4 other branches
Article Following - initial version
Conflicts: app/models/article.rb
Showing
6 changed files
with
128 additions
and
2 deletions
Show diff stats
app/models/article.rb
| @@ -8,7 +8,7 @@ class Article < ActiveRecord::Base | @@ -8,7 +8,7 @@ class Article < ActiveRecord::Base | ||
| 8 | :accept_comments, :feed, :published, :source, :source_name, | 8 | :accept_comments, :feed, :published, :source, :source_name, |
| 9 | :highlighted, :notify_comments, :display_hits, :slug, | 9 | :highlighted, :notify_comments, :display_hits, :slug, |
| 10 | :external_feed_builder, :display_versions, :external_link, | 10 | :external_feed_builder, :display_versions, :external_link, |
| 11 | - :image_builder, :show_to_followers, :published_at | 11 | + :image_builder, :show_to_followers, :published_at, :person_followers |
| 12 | 12 | ||
| 13 | acts_as_having_image | 13 | acts_as_having_image |
| 14 | 14 | ||
| @@ -71,6 +71,10 @@ class Article < ActiveRecord::Base | @@ -71,6 +71,10 @@ class Article < ActiveRecord::Base | ||
| 71 | belongs_to :last_changed_by, :class_name => 'Person', :foreign_key => 'last_changed_by_id' | 71 | belongs_to :last_changed_by, :class_name => 'Person', :foreign_key => 'last_changed_by_id' |
| 72 | belongs_to :created_by, :class_name => 'Person', :foreign_key => 'created_by_id' | 72 | belongs_to :created_by, :class_name => 'Person', :foreign_key => 'created_by_id' |
| 73 | 73 | ||
| 74 | + #Article followers relation | ||
| 75 | + has_many :article_followers, :dependent => :destroy | ||
| 76 | + has_many :person_followers, :class_name => 'Person', :through => :article_followers, :source => :person | ||
| 77 | + | ||
| 74 | has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc' | 78 | has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc' |
| 75 | 79 | ||
| 76 | has_many :article_categorizations, :conditions => [ 'articles_categories.virtual = ?', false ] | 80 | has_many :article_categorizations, :conditions => [ 'articles_categories.virtual = ?', false ] |
app/models/person.rb
| 1 | # A person is the profile of an user holding all relationships with the rest of the system | 1 | # A person is the profile of an user holding all relationships with the rest of the system |
| 2 | class Person < Profile | 2 | class Person < Profile |
| 3 | 3 | ||
| 4 | - attr_accessible :organization, :contact_information, :sex, :birth_date, :cell_phone, :comercial_phone, :jabber_id, :personal_website, :nationality, :address_reference, :district, :schooling, :schooling_status, :formation, :custom_formation, :area_of_study, :custom_area_of_study, :professional_activity, :organization_website | 4 | + attr_accessible :organization, :contact_information, :sex, :birth_date, :cell_phone, |
| 5 | + :comercial_phone, :jabber_id, :personal_website, :nationality, :address_reference, | ||
| 6 | + :district, :schooling, :schooling_status, :formation, :custom_formation, :area_of_study, | ||
| 7 | + :custom_area_of_study, :professional_activity, :organization_website, :following_articiles | ||
| 5 | 8 | ||
| 6 | SEARCH_FILTERS = { | 9 | SEARCH_FILTERS = { |
| 7 | :order => %w[more_recent more_popular more_active], | 10 | :order => %w[more_recent more_popular more_active], |
| @@ -78,6 +81,10 @@ roles] } | @@ -78,6 +81,10 @@ roles] } | ||
| 78 | memberships.where('role_assignments.role_id = ?', role.id) | 81 | memberships.where('role_assignments.role_id = ?', role.id) |
| 79 | end | 82 | end |
| 80 | 83 | ||
| 84 | + #Article followers relation | ||
| 85 | + has_many :article_followers, :dependent => :destroy | ||
| 86 | + has_many :following_articiles, :class_name => 'Article', :through => :article_followers, :source => :article | ||
| 87 | + | ||
| 81 | has_many :friendships, :dependent => :destroy | 88 | has_many :friendships, :dependent => :destroy |
| 82 | has_many :friends, :class_name => 'Person', :through => :friendships | 89 | has_many :friends, :class_name => 'Person', :through => :friendships |
| 83 | 90 |
| @@ -0,0 +1,20 @@ | @@ -0,0 +1,20 @@ | ||
| 1 | +class CreateArticleFollowers < ActiveRecord::Migration | ||
| 2 | + | ||
| 3 | +def self.up | ||
| 4 | + create_table :article_followers do |t| | ||
| 5 | + t.integer :person_id, null: false | ||
| 6 | + t.integer :article_id, null: false | ||
| 7 | + t.datetime :since | ||
| 8 | + | ||
| 9 | + t.timestamps | ||
| 10 | + end | ||
| 11 | + add_index :article_followers, :person_id | ||
| 12 | + add_index :article_followers, :article_id | ||
| 13 | + add_index :article_followers, [:person_id, :article_id], :unique => true | ||
| 14 | + end | ||
| 15 | + | ||
| 16 | + def self.down | ||
| 17 | + drop_table :article_followers | ||
| 18 | + end | ||
| 19 | + | ||
| 20 | +end |
| @@ -0,0 +1,79 @@ | @@ -0,0 +1,79 @@ | ||
| 1 | +require_relative "../test_helper" | ||
| 2 | + | ||
| 3 | +class ArticleFollowerTest < ActiveSupport::TestCase | ||
| 4 | + | ||
| 5 | + should 'create follower to article' do | ||
| 6 | + p = create_user('testuser').person | ||
| 7 | + article = p.articles.build(:name => 'test article'); article.save! | ||
| 8 | + article_follower = ArticleFollower.new | ||
| 9 | + article_follower.article = article | ||
| 10 | + article_follower.person = p | ||
| 11 | + article_follower.save! | ||
| 12 | + assert_equal article, article_follower.article | ||
| 13 | + end | ||
| 14 | + | ||
| 15 | + should 'not allow create Article Follower without an Article asssociated' do | ||
| 16 | + person = create_user('one person').person | ||
| 17 | + article_follower = ArticleFollower.new | ||
| 18 | + article_follower.person = person | ||
| 19 | + | ||
| 20 | + assert_raises (ActiveRecord::StatementInvalid) { article_follower.save! } | ||
| 21 | + end | ||
| 22 | + | ||
| 23 | + should 'not allow create duplicate Article Follower' do | ||
| 24 | + person = create_user('one person').person | ||
| 25 | + article = person.articles.build(:name => 'test article'); article.save! | ||
| 26 | + article_follower = ArticleFollower.new | ||
| 27 | + article_follower.article = article | ||
| 28 | + article_follower.person = person | ||
| 29 | + article_follower.save! | ||
| 30 | + | ||
| 31 | + article_follower = ArticleFollower.new | ||
| 32 | + article_follower.article = article | ||
| 33 | + article_follower.person = person | ||
| 34 | + | ||
| 35 | + assert_raises (ActiveRecord::RecordNotUnique) { article_follower.save! } | ||
| 36 | + end | ||
| 37 | + | ||
| 38 | + should 'create many followers to article' do | ||
| 39 | + p1 = create_user('testuser1').person | ||
| 40 | + p2 = create_user('testuser2').person | ||
| 41 | + p3 = create_user('testuser2').person | ||
| 42 | + | ||
| 43 | + article = p1.articles.build(:name => 'test article'); article.save! | ||
| 44 | + | ||
| 45 | + article_follower = ArticleFollower.new | ||
| 46 | + article_follower.article = article | ||
| 47 | + article_follower.person = p2 | ||
| 48 | + article_follower.save! | ||
| 49 | + | ||
| 50 | + article_follower = ArticleFollower.new | ||
| 51 | + article_follower.article = article | ||
| 52 | + article_follower.person = p3 | ||
| 53 | + article_follower.save! | ||
| 54 | + | ||
| 55 | + assert_equal article.person_followers.size, 2 | ||
| 56 | + end | ||
| 57 | + | ||
| 58 | + should 'allow to follow many articles' do | ||
| 59 | + p1 = create_user('testuser1').person | ||
| 60 | + p2 = create_user('testuser2').person | ||
| 61 | + | ||
| 62 | + article1 = p2.articles.build(:name => 'test article 1'); article1.save! | ||
| 63 | + article2 = p2.articles.build(:name => 'test article 2'); article2.save! | ||
| 64 | + | ||
| 65 | + article_follower = ArticleFollower.new | ||
| 66 | + article_follower.article = article1 | ||
| 67 | + article_follower.person = p1 | ||
| 68 | + article_follower.save! | ||
| 69 | + | ||
| 70 | + article_follower = ArticleFollower.new | ||
| 71 | + article_follower.article = article2 | ||
| 72 | + article_follower.person = p1 | ||
| 73 | + article_follower.save! | ||
| 74 | + | ||
| 75 | + assert_equal p1.following_articiles.size, 2 | ||
| 76 | + end | ||
| 77 | + | ||
| 78 | + | ||
| 79 | +end |