diff --git a/app/models/article.rb b/app/models/article.rb index f2c4086..d2cc58f 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -8,7 +8,7 @@ class Article < ActiveRecord::Base :accept_comments, :feed, :published, :source, :source_name, :highlighted, :notify_comments, :display_hits, :slug, :external_feed_builder, :display_versions, :external_link, - :image_builder, :show_to_followers, :published_at + :image_builder, :show_to_followers, :published_at, :person_followers acts_as_having_image @@ -71,6 +71,10 @@ class Article < ActiveRecord::Base belongs_to :last_changed_by, :class_name => 'Person', :foreign_key => 'last_changed_by_id' belongs_to :created_by, :class_name => 'Person', :foreign_key => 'created_by_id' + #Article followers relation + has_many :article_followers, :dependent => :destroy + has_many :person_followers, :class_name => 'Person', :through => :article_followers, :source => :person + has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc' has_many :article_categorizations, :conditions => [ 'articles_categories.virtual = ?', false ] diff --git a/app/models/article_follower.rb b/app/models/article_follower.rb new file mode 100644 index 0000000..632f663 --- /dev/null +++ b/app/models/article_follower.rb @@ -0,0 +1,5 @@ +class ArticleFollower < ActiveRecord::Base + attr_accessible :article_id, :person_id, :since + belongs_to :article + belongs_to :person +end diff --git a/app/models/person.rb b/app/models/person.rb index 86f2872..68ed97c 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -1,7 +1,10 @@ # A person is the profile of an user holding all relationships with the rest of the system class Person < Profile - 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 + 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, :following_articiles SEARCH_FILTERS = { :order => %w[more_recent more_popular more_active], @@ -78,6 +81,10 @@ roles] } memberships.where('role_assignments.role_id = ?', role.id) end + #Article followers relation + has_many :article_followers, :dependent => :destroy + has_many :following_articiles, :class_name => 'Article', :through => :article_followers, :source => :article + has_many :friendships, :dependent => :destroy has_many :friends, :class_name => 'Person', :through => :friendships diff --git a/db/migrate/20150616200201_create_article_followers.rb b/db/migrate/20150616200201_create_article_followers.rb new file mode 100644 index 0000000..0819533 --- /dev/null +++ b/db/migrate/20150616200201_create_article_followers.rb @@ -0,0 +1,20 @@ +class CreateArticleFollowers < ActiveRecord::Migration + +def self.up + create_table :article_followers do |t| + t.integer :person_id, null: false + t.integer :article_id, null: false + t.datetime :since + + t.timestamps + end + add_index :article_followers, :person_id + add_index :article_followers, :article_id + add_index :article_followers, [:person_id, :article_id], :unique => true + end + + def self.down + drop_table :article_followers + end + +end diff --git a/test/fixtures/article_followers.yml b/test/fixtures/article_followers.yml new file mode 100644 index 0000000..1785b5f --- /dev/null +++ b/test/fixtures/article_followers.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +one: + person_id: 1 + article_id: 1 + since: 2015-06-16 17:02:01 + +two: + person_id: 1 + article_id: 1 + since: 2015-06-16 17:02:01 diff --git a/test/unit/article_follower_test.rb b/test/unit/article_follower_test.rb new file mode 100644 index 0000000..dcafd96 --- /dev/null +++ b/test/unit/article_follower_test.rb @@ -0,0 +1,79 @@ +require_relative "../test_helper" + +class ArticleFollowerTest < ActiveSupport::TestCase + + should 'create follower to article' do + p = create_user('testuser').person + article = p.articles.build(:name => 'test article'); article.save! + article_follower = ArticleFollower.new + article_follower.article = article + article_follower.person = p + article_follower.save! + assert_equal article, article_follower.article + end + + should 'not allow create Article Follower without an Article asssociated' do + person = create_user('one person').person + article_follower = ArticleFollower.new + article_follower.person = person + + assert_raises (ActiveRecord::StatementInvalid) { article_follower.save! } + end + + should 'not allow create duplicate Article Follower' do + person = create_user('one person').person + article = person.articles.build(:name => 'test article'); article.save! + article_follower = ArticleFollower.new + article_follower.article = article + article_follower.person = person + article_follower.save! + + article_follower = ArticleFollower.new + article_follower.article = article + article_follower.person = person + + assert_raises (ActiveRecord::RecordNotUnique) { article_follower.save! } + end + + should 'create many followers to article' do + p1 = create_user('testuser1').person + p2 = create_user('testuser2').person + p3 = create_user('testuser2').person + + article = p1.articles.build(:name => 'test article'); article.save! + + article_follower = ArticleFollower.new + article_follower.article = article + article_follower.person = p2 + article_follower.save! + + article_follower = ArticleFollower.new + article_follower.article = article + article_follower.person = p3 + article_follower.save! + + assert_equal article.person_followers.size, 2 + end + + should 'allow to follow many articles' do + p1 = create_user('testuser1').person + p2 = create_user('testuser2').person + + article1 = p2.articles.build(:name => 'test article 1'); article1.save! + article2 = p2.articles.build(:name => 'test article 2'); article2.save! + + article_follower = ArticleFollower.new + article_follower.article = article1 + article_follower.person = p1 + article_follower.save! + + article_follower = ArticleFollower.new + article_follower.article = article2 + article_follower.person = p1 + article_follower.save! + + assert_equal p1.following_articiles.size, 2 + end + + +end -- libgit2 0.21.2