Commit 21adb295142f4ec0435eb5f1336da8cfe90cb85b
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Merge branch 'api' into production
Showing
7 changed files
with
146 additions
and
3 deletions
Show diff stats
app/models/article.rb
1 | - | ||
2 | class Article < ActiveRecord::Base | 1 | class Article < ActiveRecord::Base |
3 | 2 | ||
4 | attr_accessible :name, :body, :abstract, :profile, :tag_list, :parent, | 3 | attr_accessible :name, :body, :abstract, :profile, :tag_list, :parent, |
@@ -8,7 +7,8 @@ class Article < ActiveRecord::Base | @@ -8,7 +7,8 @@ class Article < ActiveRecord::Base | ||
8 | :accept_comments, :feed, :published, :source, :source_name, | 7 | :accept_comments, :feed, :published, :source, :source_name, |
9 | :highlighted, :notify_comments, :display_hits, :slug, | 8 | :highlighted, :notify_comments, :display_hits, :slug, |
10 | :external_feed_builder, :display_versions, :external_link, | 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 | 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 |
lib/noosfero/api/v1/articles.rb
@@ -28,6 +28,23 @@ module Noosfero | @@ -28,6 +28,23 @@ module Noosfero | ||
28 | article = find_article(environment.articles, params[:id]) | 28 | article = find_article(environment.articles, params[:id]) |
29 | present article, :with => Entities::Article, :fields => params[:fields] | 29 | present article, :with => Entities::Article, :fields => params[:fields] |
30 | end | 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 | post ':id/vote' do | 49 | post ':id/vote' do |
33 | value = (params[:value] || 1).to_i | 50 | value = (params[:value] || 1).to_i |
@@ -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 |