Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

New setting: require_login;

Renamed rate_helpfulness setting and improved its work
  • Loading branch information...
commit 4f20905fafdd8879ba21f72496b7b0985df5d186 1 parent 4558284
@fabien fabien authored committed
View
3  app/controllers/admin/review_settings_controller.rb
@@ -2,6 +2,9 @@ class Admin::ReviewSettingsController < Admin::BaseController
def update
# workaround for unset checkbox behaviour
params[:preferences][:include_unapproved_reviews] = false if params[:preferences][:include_unapproved_reviews].blank?
+ params[:preferences][:feedback_rating] = false if params[:preferences][:feedback_rating].blank?
+ params[:preferences][:show_email] = false if params[:preferences][:show_email].blank?
+ params[:preferences][:require_login] = false if params[:preferences][:require_login].blank?
Spree::Reviews::Config.set(params[:preferences])
respond_to do |format|
View
8 app/controllers/feedback_reviews_controller.rb
@@ -4,9 +4,11 @@ def create
params[:feedback_review][:rating].sub!(/\s*stars/,'') unless params[:feedback_review][:rating].blank?
@review = Review.find_by_id(params[:review_id])
- @review &&
- (@feedback_review = @review.feedback_reviews.new(params[:feedback_review])) && @feedback_review.save
-
+ if @review && @feedback_review = @review.feedback_reviews.new(params[:feedback_review])
+ authorize! :create, @feedback_review
+ @feedback_review.save
+ end
+
respond_to do |format|
format.html { redirect_to :back }
format.js { render :action => :create }
View
12 app/controllers/reviews_controller.rb
@@ -2,28 +2,31 @@ class ReviewsController < Spree::BaseController
helper Spree::BaseHelper
before_filter :load_product, :only => [:index, :new, :create]
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
-
+
def index
@approved_reviews = Review.approved.find_all_by_product_id(@product.id)
end
def new
@review = Review.new(:product => @product)
+ authorize! :new, @review
end
# save if all ok
def create
params[:review][:rating].sub!(/\s*stars/,'') unless params[:review][:rating].blank?
- @review = Review.new
+ @review = Review.new(params[:review])
@review.product = @product
@review.user = current_user if user_signed_in?
@review.ip_address = request.remote_ip
- if @review.update_attributes(params[:review])
+
+ authorize! :create, @review
+
+ if @review.save
flash[:notice] = t('review_successfully_submitted')
redirect_to (product_path(@product))
else
- # flash[:notice] = 'There was a problem in the submitted review'
render :action => "new"
end
end
@@ -36,4 +39,5 @@ def terms
def load_product
@product = Product.find_by_permalink!(params[:product_id])
end
+
end
View
5 app/models/reviews_configuration.rb
@@ -9,5 +9,8 @@ class ReviewsConfiguration < Configuration
preference :show_email, :boolean, :default => false
# show helpfullness rating form elements
- preference :rate_helpfulness, :boolean, :default => false
+ preference :feedback_rating, :boolean, :default => false
+
+ # require login to post reviews
+ preference :require_login, :boolean, :default => true
end
View
8 app/views/admin/review_settings/edit.html.erb
@@ -8,14 +8,18 @@
<%= check_box_tag('preferences[include_unapproved_reviews]', "1", Spree::Reviews::Config[:include_unapproved_reviews]) %>
</p>
<p>
- <label><%= t("spree_reviews.rate_helpfulness") %>:</label>
- <%= check_box_tag('preferences[rate_helpfulness]', "1", Spree::Reviews::Config[:rate_helpfulness]) %>
+ <label><%= t("spree_reviews.feedback_rating") %>:</label>
+ <%= check_box_tag('preferences[feedback_rating]', "1", Spree::Reviews::Config[:feedback_rating]) %>
</p>
<p>
<label><%= t("spree_reviews.show_email") %>:</label>
<%= check_box_tag('preferences[show_email]', "1", Spree::Reviews::Config[:show_email]) %>
</p>
<p>
+ <label><%= t("spree_reviews.require_login") %>:</label>
+ <%= check_box_tag('preferences[require_login]', "1", Spree::Reviews::Config[:require_login]) %>
+ </p>
+ <p>
<label><%= t('spree_reviews.preview_size') %>:</label>
<%= text_field_tag('preferences[preview_size]', Spree::Reviews::Config[:preview_size], :size => 3) %>
</p>
View
8 app/views/admin/review_settings/show.html.erb
@@ -8,14 +8,18 @@
<td><%= Spree::Reviews::Config[:include_unapproved_reviews] ? t('yes') : t('no') %></td>
</tr>
<tr>
- <th scope="row"><%= t("spree_reviews.rate_helpfulness") %>:</th>
- <td><%= Spree::Reviews::Config[:rate_helpfulness] ? t('yes') : t('no') %></td>
+ <th scope="row"><%= t("spree_reviews.feedback_rating") %>:</th>
+ <td><%= Spree::Reviews::Config[:feedback_rating] ? t('yes') : t('no') %></td>
</tr>
<tr>
<th scope="row"><%= t("spree_reviews.show_email") %>:</th>
<td><%= Spree::Reviews::Config[:show_email] ? t('yes') : t('no') %></td>
</tr>
<tr>
+ <th scope="row"><%= t("spree_reviews.require_login") %>:</th>
+ <td><%= Spree::Reviews::Config[:require_login] ? t('yes') : t('no') %></td>
+ </tr>
+ <tr>
<th scope="row"><%= t("spree_reviews.preview_size") %>:</th>
<td><%= Spree::Reviews::Config[:preview_size] %></td>
</tr>
View
15 app/views/feedback_reviews/_form.html.erb
@@ -4,13 +4,12 @@
<span class="error"><%= @feedback_review.errors[:rating] %></span>
<br />
<% end %>
-<% if Spree::Reviews::Config[:rate_helpfulness] %>
- <%= t(:was_this_review_helpful) %>
- <% for i in 1..NB_STARS %>
- <%= radio_button_tag "feedback_review[rating]", pluralize(i, 'stars'), false, :class => "star" %>
- <% end %>
- <%= hidden_field_tag "feedback_review[user_id]", current_user.try(:id) %>
- <button class="feedback-review"><span><%= t("yes") %></span></button>
- <% end %>
+<%= t(:was_this_review_helpful) %>
+<% for i in 1..NB_STARS %>
+<%= radio_button_tag "feedback_review[rating]", pluralize(i, 'stars'), false, :class => "star" %>
<% end %>
+<%= hidden_field_tag "feedback_review[user_id]", current_user.try(:id) %>
+<button class="feedback-review"><span><%= t("yes") %></span></button>
+<% end %>
+
View
3  app/views/shared/_review.html.erb
@@ -12,8 +12,9 @@
<span class="attribution"><%= t('submitted_on') %> <strong><%= l review.created_at.to_date %></strong></span>
<%= simple_format(review.review) %>
-
+ <% if Spree::Reviews::Config[:feedback_rating] && ((Spree::Reviews::Config[:require_login] && current_user) || !Spree::Reviews::Config[:require_login]) %>
<div class="feedback_review" id="feedback_review_<%= review.id %>">
<%= render :partial => "feedback_reviews/form", :locals => {:review => review} %>
</div>
+ <% end %>
</div>
View
5 app/views/shared/_review_summary.html.erb
@@ -3,7 +3,8 @@
<% for review in @product.reviews.approval_filter.preview %>
<%= render :partial => 'shared/review', :locals => {:review => review} %>
<% end %>
-
- <%= link_to t('write_your_own_review'), new_product_review_path(@product), :class => "button" %>
+ <% if Spree::Reviews::Config[:feedback_rating] && ((Spree::Reviews::Config[:require_login] && current_user) || !Spree::Reviews::Config[:require_login]) %>
+ <%= link_to t('write_your_own_review'), new_product_review_path(@product), :class => "button" %>
+ <% end %>
</div>
View
4 app/views/shared/_reviews.html.erb
@@ -3,7 +3,7 @@
<% for review in @product.reviews.approval_filter %>
<%= render :partial => 'shared/review', :locals => {:review => review} %>
<% end %>
-
+ <% if (Spree::Reviews::Config[:require_login] && current_user) || !Spree::Reviews::Config[:require_login] %>
<%= link_to t('write_your_own_review'), new_product_review_path(@product), :class => "button" %>
+ <% end %>
</div>
-
View
4 config/locales/en.yml
@@ -42,6 +42,7 @@ en:
review_management_description: Manage the user-submitted reviews
review_name: Your Name
review_successfully_submitted: Review was successfully submitted
+ review_not_submitted: Review was not submitted
review_title: Title
spree_reviews:
review_settings: Review Settings
@@ -49,7 +50,8 @@ en:
preview_size: Size of the review snippets
include_unapproved: Include unapproved reviews in listings
show_email: Show email addresses
- rate_helpfulness: Rate helpfullness
+ feedback_rating: Rate feedback
+ require_login: Require user to be logged in
your_name: Your name
your_location: Your location
star:
View
15 lib/spree_reviews.rb
@@ -2,6 +2,20 @@
require 'spree_reviews_hooks'
module SpreeReviews
+
+ class AbilityDecorator
+ include CanCan::Ability
+
+ def initialize(user)
+ can :create, Review do |review|
+ user.has_role?(:user) || !Spree::Reviews::Config[:require_login]
+ end
+ can :create, FeedbackReview do |review|
+ user.has_role?(:user) || !Spree::Reviews::Config[:require_login]
+ end
+ end
+ end
+
class Engine < Rails::Engine
config.autoload_paths += %W(#{config.root}/lib)
@@ -12,6 +26,7 @@ def self.activate
end
ProductsHelper.send(:include, ReviewsHelper)
Admin::ReviewsController.cache_sweeper :review_sweeper
+ Ability.register_ability(AbilityDecorator)
end
config.to_prepare &method(:activate).to_proc
Please sign in to comment.
Something went wrong with that request. Please try again.