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 | 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 | 67 | #### SEARCH |
| 55 | 68 | #################################################################### |
| 56 | 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 | 149 | # FIXME verify allowed values |
| 150 | 150 | render_api_error!('Vote value not allowed', 400) unless [-1, 1].include?(value) |
| 151 | 151 | article = find_article(environment.articles, params[:id]) |
| 152 | - | |
| 153 | 152 | ## If login with captcha |
| 154 | 153 | if @current_tmp_user |
| 154 | + # Vote allowed only if data does not include this article | |
| 155 | 155 | vote = (@current_tmp_user.data.include? article.id) ? false : true |
| 156 | 156 | if vote |
| 157 | 157 | @current_tmp_user.data << article.id |
| 158 | 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 | 160 | else |
| 167 | 161 | {:vote => false} |
| 168 | 162 | end |
| 169 | 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 | 165 | end |
| 178 | 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 | 3 | require File.expand_path(File.dirname(__FILE__) + "/../../../lib/noosfero/api/helpers") |
| 4 | 4 | |
| ... | ... | @@ -31,6 +31,7 @@ class APIHelpersTest < ActiveSupport::TestCase |
| 31 | 31 | user.generate_private_token! |
| 32 | 32 | user.private_token_generated_at = DateTime.now.prev_year |
| 33 | 33 | user.save |
| 34 | + binding.pry | |
| 34 | 35 | self.params = {:private_token => user.private_token} |
| 35 | 36 | assert_equal user, current_user |
| 36 | 37 | end | ... | ... |