Commit 21adb295142f4ec0435eb5f1336da8cfe90cb85b

Authored by Victor Costa
2 parents d91cb9dd 9a7a82c2

Merge branch 'api' into production

app/models/article.rb
1   -
2 1 class Article < ActiveRecord::Base
3 2  
4 3 attr_accessible :name, :body, :abstract, :profile, :tag_list, :parent,
... ... @@ -8,7 +7,8 @@ class Article &lt; ActiveRecord::Base
8 7 :accept_comments, :feed, :published, :source, :source_name,
9 8 :highlighted, :notify_comments, :display_hits, :slug,
10 9 :external_feed_builder, :display_versions, :external_link,
11   - :image_builder, :show_to_followers, :published_at
  10 + :image_builder, :show_to_followers, :published_at, :person_followers
  11 +
12 12  
13 13 acts_as_having_image
14 14  
... ... @@ -71,6 +71,10 @@ class Article &lt; ActiveRecord::Base
71 71 belongs_to :last_changed_by, :class_name => 'Person', :foreign_key => 'last_changed_by_id'
72 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 78 has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc'
75 79  
76 80 has_many :article_categorizations, :conditions => [ 'articles_categories.virtual = ?', false ]
... ...
app/models/article_follower.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +class ArticleFollower < ActiveRecord::Base
  2 + attr_accessible :article_id, :person_id, :since
  3 + belongs_to :article
  4 + belongs_to :person
  5 +end
... ...
app/models/person.rb
1 1 # A person is the profile of an user holding all relationships with the rest of the system
2 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 9 SEARCH_FILTERS = {
7 10 :order => %w[more_recent more_popular more_active],
... ... @@ -78,6 +81,10 @@ roles] }
78 81 memberships.where('role_assignments.role_id = ?', role.id)
79 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 88 has_many :friendships, :dependent => :destroy
82 89 has_many :friends, :class_name => 'Person', :through => :friendships
83 90  
... ...
db/migrate/20150616200201_create_article_followers.rb 0 → 100644
... ... @@ -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
... ...
lib/noosfero/api/v1/articles.rb
... ... @@ -28,6 +28,23 @@ module Noosfero
28 28 article = find_article(environment.articles, params[:id])
29 29 present article, :with => Entities::Article, :fields => params[:fields]
30 30 end
  31 +
  32 + desc "Returns the total followers for the article"
  33 + get ':id/followers' do
  34 + article = find_article(environment.articles, params[:id])
  35 + total = article.person_followers.size
  36 + {:total_followers => total}
  37 + end
  38 +
  39 + desc "Add a follower for the article"
  40 + get ':id/follow' do
  41 + article = find_article(environment.articles, params[:id])
  42 + article_follower = ArticleFollower.new
  43 + article_follower.article = article
  44 + article_follower.person = current_person
  45 + article_follower.save!
  46 + end
  47 +
31 48  
32 49 post ':id/vote' do
33 50 value = (params[:value] || 1).to_i
... ...
test/fixtures/article_followers.yml 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
  2 +
  3 +one:
  4 + person_id: 1
  5 + article_id: 1
  6 + since: 2015-06-16 17:02:01
  7 +
  8 +two:
  9 + person_id: 1
  10 + article_id: 1
  11 + since: 2015-06-16 17:02:01
... ...
test/unit/article_follower_test.rb 0 → 100644
... ... @@ -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
... ...