Commit 76e3cbd7ac101bbee44e6e9b6eaaf0b6a3a97386

Authored by Fabio Teixeira
Committed by Pedro de Lyra Pereira
1 parent 1e289d8d
Exists in temp_ratings

Remove star rate ajax and set commment as optional

Signed-off-by: Fábio Teixeira <fabio1079@gmail.com>
Signed-off-by: Pedro de Lyra Pereira <pedrodelyra@gmail.com>
plugins/communities_ratings/controllers/communities_ratings_plugin_profile_controller.rb
1 class CommunitiesRatingsPluginProfileController < ProfileController 1 class CommunitiesRatingsPluginProfileController < ProfileController
2 -  
3 before_filter :login_required 2 before_filter :login_required
4 - # Inside a community, receive a ajax from the current logged person with its  
5 - # rate for the community. If the user already rated this commnity, update its  
6 - # rate value or else, create a new one  
7 - def rate  
8 - # If its not a ajax request or the profile isn't a community  
9 - if (not request.xhr?) or (profile.type != "Community")  
10 - # Redirect to the profile home page  
11 - return redirect_to :controller => 'profile', :action => :index  
12 - end  
13 -  
14 - community_rating = get_community_rating(user, profile)  
15 - community_rating.value = params[:value].to_i  
16 -  
17 - if community_rating.save  
18 - render :json => {  
19 - success: true,  
20 - message: "Successfully rated community #{community_rating.community.name}"  
21 - }  
22 - else  
23 - render :json => {  
24 - success: false,  
25 - message: "Could not rate community #{community_rating.community.name}",  
26 - errors: community_rating.errors.full_messages  
27 - }  
28 - end  
29 - end  
30 3
31 def new_rating 4 def new_rating
32 @plugins = plugins 5 @plugins = plugins
33 - community_rating = get_community_rating(user, profile) 6 + community_rating = get_community_rating(user, profile, params[:community_rating_value])
34 @actual_rate_value = if community_rating.value 7 @actual_rate_value = if community_rating.value
35 community_rating.value 8 community_rating.value
36 else 9 else
@@ -38,17 +11,17 @@ class CommunitiesRatingsPluginProfileController &lt; ProfileController @@ -38,17 +11,17 @@ class CommunitiesRatingsPluginProfileController &lt; ProfileController
38 end 11 end
39 12
40 if request.post? 13 if request.post?
41 - unless params[:comments][:body].empty? 14 + if params[:comments] and not params[:comments][:body].empty?
42 comment = Comment.new(params[:comments]) 15 comment = Comment.new(params[:comments])
43 comment.author = current_user.person 16 comment.author = current_user.person
44 comment.community = profile 17 comment.community = profile
45 comment.save 18 comment.save
  19 +
46 community_rating.comment = comment 20 community_rating.comment = comment
47 - community_rating.save  
48 - else  
49 - session[:notice] = _("You need to provide a decription to make a comment")  
50 - redirect_to action: :new_rating  
51 end 21 end
  22 +
  23 + community_rating.save!
  24 + session[:notice] = _("#{profile.name} successfully rated")
52 end 25 end
53 end 26 end
54 27
@@ -56,7 +29,7 @@ class CommunitiesRatingsPluginProfileController &lt; ProfileController @@ -56,7 +29,7 @@ class CommunitiesRatingsPluginProfileController &lt; ProfileController
56 29
57 # If there is already a rate by the current logged user to this community 30 # If there is already a rate by the current logged user to this community
58 # return it or else, prepare a new one 31 # return it or else, prepare a new one
59 - def get_community_rating person, community 32 + def get_community_rating person, community, community_rating_value=nil
60 already_rated = CommunityRating.where( 33 already_rated = CommunityRating.where(
61 :community_id=>community.id, 34 :community_id=>community.id,
62 :person_id=>person.id 35 :person_id=>person.id
@@ -66,7 +39,8 @@ class CommunitiesRatingsPluginProfileController &lt; ProfileController @@ -66,7 +39,8 @@ class CommunitiesRatingsPluginProfileController &lt; ProfileController
66 already_rated.first 39 already_rated.first
67 else 40 else
68 CommunityRating.new :person => person, 41 CommunityRating.new :person => person,
69 - :community => community 42 + :community => community,
  43 + :value => community_rating_value
70 end 44 end
71 end 45 end
72 end 46 end
plugins/communities_ratings/public/rate.js
@@ -100,35 +100,17 @@ @@ -100,35 +100,17 @@
100 DATA.selected_rate = DATA.NOT_SELECTED_VALUE; 100 DATA.selected_rate = DATA.NOT_SELECTED_VALUE;
101 } 101 }
102 102
  103 + // Mark the selected_rate
  104 + $("#selected-star-rate").val(DATA.selected_rate);
  105 +
103 var star_notice = $(".star-notice"); 106 var star_notice = $(".star-notice");
  107 + star_notice.find("span").html(DATA.selected_rate);
104 star_notice.removeClass("star-hide"); 108 star_notice.removeClass("star-hide");
105 -  
106 - // Call selected_rate_action to send the rate data.  
107 - selected_rate_action(star_notice);  
108 }); 109 });
109 } 110 }
110 111
111 112
112 /* 113 /*
113 - * Send the user rate to the server when he/she clicks on the rate button  
114 - */  
115 - function selected_rate_action(notice_element) {  
116 - var profile = $("#community-profile").val();  
117 -  
118 - $.ajax({  
119 - url: "/profile/"+profile+"/plugin/communities_ratings/rate",  
120 - type: "POST",  
121 - data: {  
122 - value: DATA.selected_rate  
123 - },  
124 - success: function(response) {  
125 - notice_element.html(response.message);  
126 - }  
127 - });  
128 - }  
129 -  
130 -  
131 - /*  
132 * When the page DOM is ready, set all the stars events 114 * When the page DOM is ready, set all the stars events
133 */ 115 */
134 $(document).ready(function() { 116 $(document).ready(function() {
plugins/communities_ratings/views/communities_ratings_plugin_profile/new_rating.html.erb
1 -<input type="hidden" id="community-profile" value="<%= @profile.identifier %>">  
2 -<input type="hidden" id="selected-star-rate" value="<%= @actual_rate_value %>">  
3 -  
4 -<div class="star-notice star-hide"></div>  
5 -  
6 <div class="star-page-title"> 1 <div class="star-page-title">
7 -<%= @plugins.dispatch(:communities_ratings_title).collect { |content| instance_exec(&content) }.join("") %> 2 + <%= @plugins.dispatch(:communities_ratings_title).collect { |content| instance_exec(&content) }.join("") %>
8 </div> 3 </div>
9 4
10 <div class="star-rate-data"> 5 <div class="star-rate-data">
@@ -35,12 +30,15 @@ @@ -35,12 +30,15 @@
35 <% end %> 30 <% end %>
36 </div> 31 </div>
37 32
  33 + <div class="star-notice star-hide">
  34 + <%= _("Rated as") %> <span></span> <%= _("stars") %>
  35 + </div>
38 </div> 36 </div>
39 37
40 <div class="star-comment-container"> 38 <div class="star-comment-container">
41 <%= form_for :comments do |c| %> 39 <%= form_for :comments do |c| %>
42 <div class="formfieldline formfield type-text"> 40 <div class="formfieldline formfield type-text">
43 - <%= c.label :body, _('Comment:'), :class => "formlabel" %> 41 + <%= c.label :body, _('Comment (Optional):'), :class => "formlabel" %>
44 <%= c.text_area :body %> 42 <%= c.text_area :body %>
45 </div> 43 </div>
46 44
@@ -48,6 +46,8 @@ @@ -48,6 +46,8 @@
48 <div class="button-bar"> 46 <div class="button-bar">
49 <%= c.submit _("Send"), class: "button icon-save submit with-text" %> 47 <%= c.submit _("Send"), class: "button icon-save submit with-text" %>
50 </div> 48 </div>
  49 +
  50 + <input type="hidden" id="selected-star-rate" name="community_rating_value" value="<%= @actual_rate_value %>">
51 <% end %> 51 <% end %>
52 </div> 52 </div>
53 </div> 53 </div>