Commit 2f7c368f7df1c3a0f99d9f87fe9265fc479cfb08
1 parent
f61d401e
Exists in
temp_ratings
Add ajax route to rate community
Signed-off-by: DylanGuedes <djmgguedes@gmail.com> Signed-off-by: Fabio Teixeira <fabio1079@gmail.com> Signed-off-by: Filipe Ribeiro <firibeiro77@live.com>
Showing
2 changed files
with
81 additions
and
0 deletions
Show diff stats
plugins/communities_ratings/controllers/communities_ratings_plugin_profile_controller.rb
0 → 100644
@@ -0,0 +1,47 @@ | @@ -0,0 +1,47 @@ | ||
1 | +class CommunitiesRatingsPluginProfileController < ProfileController | ||
2 | + | ||
3 | + # Inside a community, receive a ajax from the current logged person with its | ||
4 | + # rate for the community. If the user already rated this commnity, update its | ||
5 | + # rate value or else, create a new one | ||
6 | + def rate | ||
7 | + # If its not a ajax request or the profile isn't a community | ||
8 | + if (not request.xhr?) or (profile.type != "Community") | ||
9 | + # Redirect to the profile home page | ||
10 | + return redirect_to :controller => 'profile', :action => :index | ||
11 | + end | ||
12 | + | ||
13 | + community_rating = get_community_rating(user, profile) | ||
14 | + community_rating.value = params[:value].to_i | ||
15 | + | ||
16 | + if community_rating.save | ||
17 | + render :json => { | ||
18 | + success: true, | ||
19 | + message: "Successfully rated community #{community_rating.community.name}" | ||
20 | + } | ||
21 | + else | ||
22 | + render :json => { | ||
23 | + success: false, | ||
24 | + message: "Could not rate community #{community_rating.community.name}", | ||
25 | + errors: community_rating.errors.full_messages | ||
26 | + } | ||
27 | + end | ||
28 | + end | ||
29 | + | ||
30 | + private | ||
31 | + | ||
32 | + # If there is already a rate by the current logged user to this community | ||
33 | + # return it or else, prepare a new one | ||
34 | + def get_community_rating person, community | ||
35 | + already_rated = CommunityRating.where( | ||
36 | + :community_id=>community.id, | ||
37 | + :person_id=>person.id | ||
38 | + ) | ||
39 | + | ||
40 | + if already_rated.count != 0 | ||
41 | + already_rated.first | ||
42 | + else | ||
43 | + CommunityRating.new :person => person, | ||
44 | + :community => community | ||
45 | + end | ||
46 | + end | ||
47 | +end |
plugins/communities_ratings/test/functional/communities_ratings_plugin_profile_controller_test.rb
0 → 100644
@@ -0,0 +1,34 @@ | @@ -0,0 +1,34 @@ | ||
1 | +require File.dirname(__FILE__) + '/../../../../test/test_helper' | ||
2 | +require( | ||
3 | + File.dirname(__FILE__) + | ||
4 | + '/../../controllers/communities_ratings_plugin_profile_controller' | ||
5 | +) | ||
6 | + | ||
7 | +# Re-raise errors caught by the controller. | ||
8 | +class CommunitiesRatingsPluginProfileController; def rescue_action(e) raise e end; end | ||
9 | + | ||
10 | +class CommunitiesRatingsPluginProfileControllerTest < ActionController::TestCase | ||
11 | + | ||
12 | + def setup | ||
13 | + @controller = CommunitiesRatingsPluginProfileController.new | ||
14 | + @request = ActionController::TestRequest.new | ||
15 | + @response = ActionController::TestResponse.new | ||
16 | + | ||
17 | + @person = create_user('testuser').person | ||
18 | + @community = create_user('testcommunity').person | ||
19 | + | ||
20 | + login_as(@person.identifier) | ||
21 | + @controller.stubs(:logged_in?).returns(true) | ||
22 | + @controller.stubs(:current_user).returns(@person.user) | ||
23 | + end | ||
24 | + | ||
25 | + | ||
26 | + test "should logged person rate a community" do | ||
27 | + #xhr :post , :rate, :profile => @community, :value => 4 | ||
28 | + xhr :post , url_for(:action=>:rate), :profile => @community, :value => 4 | ||
29 | + | ||
30 | + puts "="*80, @response.body, "="*80 | ||
31 | + assert_equal true, true | ||
32 | + end | ||
33 | + | ||
34 | +end |