diff --git a/plugins/communities_ratings/controllers/communities_ratings_plugin_profile_controller.rb b/plugins/communities_ratings/controllers/communities_ratings_plugin_profile_controller.rb new file mode 100644 index 0000000..c626301 --- /dev/null +++ b/plugins/communities_ratings/controllers/communities_ratings_plugin_profile_controller.rb @@ -0,0 +1,47 @@ +class CommunitiesRatingsPluginProfileController < ProfileController + + # Inside a community, receive a ajax from the current logged person with its + # rate for the community. If the user already rated this commnity, update its + # rate value or else, create a new one + def rate + # If its not a ajax request or the profile isn't a community + if (not request.xhr?) or (profile.type != "Community") + # Redirect to the profile home page + return redirect_to :controller => 'profile', :action => :index + end + + community_rating = get_community_rating(user, profile) + community_rating.value = params[:value].to_i + + if community_rating.save + render :json => { + success: true, + message: "Successfully rated community #{community_rating.community.name}" + } + else + render :json => { + success: false, + message: "Could not rate community #{community_rating.community.name}", + errors: community_rating.errors.full_messages + } + end + end + + private + + # If there is already a rate by the current logged user to this community + # return it or else, prepare a new one + def get_community_rating person, community + already_rated = CommunityRating.where( + :community_id=>community.id, + :person_id=>person.id + ) + + if already_rated.count != 0 + already_rated.first + else + CommunityRating.new :person => person, + :community => community + end + end +end diff --git a/plugins/communities_ratings/test/functional/communities_ratings_plugin_profile_controller_test.rb b/plugins/communities_ratings/test/functional/communities_ratings_plugin_profile_controller_test.rb new file mode 100644 index 0000000..224552d --- /dev/null +++ b/plugins/communities_ratings/test/functional/communities_ratings_plugin_profile_controller_test.rb @@ -0,0 +1,34 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require( + File.dirname(__FILE__) + + '/../../controllers/communities_ratings_plugin_profile_controller' +) + +# Re-raise errors caught by the controller. +class CommunitiesRatingsPluginProfileController; def rescue_action(e) raise e end; end + +class CommunitiesRatingsPluginProfileControllerTest < ActionController::TestCase + + def setup + @controller = CommunitiesRatingsPluginProfileController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @person = create_user('testuser').person + @community = create_user('testcommunity').person + + login_as(@person.identifier) + @controller.stubs(:logged_in?).returns(true) + @controller.stubs(:current_user).returns(@person.user) + end + + + test "should logged person rate a community" do + #xhr :post , :rate, :profile => @community, :value => 4 + xhr :post , url_for(:action=>:rate), :profile => @community, :value => 4 + + puts "="*80, @response.body, "="*80 + assert_equal true, true + end + +end -- libgit2 0.21.2