Commit a0944909011e1bdb9bac474f06a4871ead049939
Committed by
Victor Costa
1 parent
64f9b09c
Exists in
theme-brasil-digital-from-staging
and in
4 other branches
Merged login-captcha with staging
Showing
3 changed files
with
18 additions
and
16 deletions
Show diff stats
lib/noosfero/api/helpers.rb
| @@ -51,6 +51,19 @@ require 'grape' | @@ -51,6 +51,19 @@ require 'grape' | ||
| 51 | end | 51 | end |
| 52 | 52 | ||
| 53 | #################################################################### | 53 | #################################################################### |
| 54 | + #### VOTE | ||
| 55 | + #################################################################### | ||
| 56 | + def do_vote(article, current_person, value) | ||
| 57 | + begin | ||
| 58 | + vote = Vote.new(:voteable => article, :voter => current_person, :vote => value) | ||
| 59 | + return vote.save! | ||
| 60 | + rescue ActiveRecord::RecordInvalid => e | ||
| 61 | + render_api_error!(e.message, 400) | ||
| 62 | + return false | ||
| 63 | + end | ||
| 64 | + end | ||
| 65 | + | ||
| 66 | + #################################################################### | ||
| 54 | #### SEARCH | 67 | #### SEARCH |
| 55 | #################################################################### | 68 | #################################################################### |
| 56 | def find_by_contents(asset, context, scope, query, paginate_options={:page => 1}, options={}) | 69 | def find_by_contents(asset, context, scope, query, paginate_options={:page => 1}, options={}) |
lib/noosfero/api/v1/articles.rb
| @@ -149,31 +149,19 @@ module Noosfero | @@ -149,31 +149,19 @@ module Noosfero | ||
| 149 | # FIXME verify allowed values | 149 | # FIXME verify allowed values |
| 150 | render_api_error!('Vote value not allowed', 400) unless [-1, 1].include?(value) | 150 | render_api_error!('Vote value not allowed', 400) unless [-1, 1].include?(value) |
| 151 | article = find_article(environment.articles, params[:id]) | 151 | article = find_article(environment.articles, params[:id]) |
| 152 | - | ||
| 153 | ## If login with captcha | 152 | ## If login with captcha |
| 154 | if @current_tmp_user | 153 | if @current_tmp_user |
| 154 | + # Vote allowed only if data does not include this article | ||
| 155 | vote = (@current_tmp_user.data.include? article.id) ? false : true | 155 | vote = (@current_tmp_user.data.include? article.id) ? false : true |
| 156 | if vote | 156 | if vote |
| 157 | @current_tmp_user.data << article.id | 157 | @current_tmp_user.data << article.id |
| 158 | @current_tmp_user.store | 158 | @current_tmp_user.store |
| 159 | - begin | ||
| 160 | - vote = Vote.new(:voteable => article, :voter => current_person, :vote => value) | ||
| 161 | - saved = vote.save! | ||
| 162 | - {:vote => saved} | ||
| 163 | - rescue ActiveRecord::RecordInvalid => e | ||
| 164 | - render_api_error!(e.message, 400) | ||
| 165 | - end | 159 | + {:vote => do_vote(article, current_person, value)} |
| 166 | else | 160 | else |
| 167 | {:vote => false} | 161 | {:vote => false} |
| 168 | end | 162 | end |
| 169 | else | 163 | else |
| 170 | - begin | ||
| 171 | - vote = Vote.new(:voteable => article, :voter => current_person, :vote => value) | ||
| 172 | - saved = vote.save! | ||
| 173 | - {:vote => saved} | ||
| 174 | - rescue ActiveRecord::RecordInvalid => e | ||
| 175 | - render_api_error!(e.message, 400) | ||
| 176 | - end | 164 | + {:vote => do_vote(article, current_person, value)} |
| 177 | end | 165 | end |
| 178 | end | 166 | end |
| 179 | 167 |
test/unit/api/helpers_test.rb
| 1 | -require File.dirname(__FILE__) + '/test_helper'; | 1 | + require File.dirname(__FILE__) + '/test_helper'; |
| 2 | 2 | ||
| 3 | require File.expand_path(File.dirname(__FILE__) + "/../../../lib/noosfero/api/helpers") | 3 | require File.expand_path(File.dirname(__FILE__) + "/../../../lib/noosfero/api/helpers") |
| 4 | 4 | ||
| @@ -31,6 +31,7 @@ class APIHelpersTest < ActiveSupport::TestCase | @@ -31,6 +31,7 @@ class APIHelpersTest < ActiveSupport::TestCase | ||
| 31 | user.generate_private_token! | 31 | user.generate_private_token! |
| 32 | user.private_token_generated_at = DateTime.now.prev_year | 32 | user.private_token_generated_at = DateTime.now.prev_year |
| 33 | user.save | 33 | user.save |
| 34 | + binding.pry | ||
| 34 | self.params = {:private_token => user.private_token} | 35 | self.params = {:private_token => user.private_token} |
| 35 | assert_equal user, current_user | 36 | assert_equal user, current_user |
| 36 | end | 37 | end |