pundit: convert favorites to pundit.
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
class FavoritesController < ApplicationController
|
class FavoritesController < ApplicationController
|
||||||
before_action :member_only, except: [:index]
|
|
||||||
respond_to :html, :xml, :json, :js
|
respond_to :html, :xml, :json, :js
|
||||||
skip_before_action :api_check
|
skip_before_action :api_check
|
||||||
rescue_with Favorite::Error, status: 422
|
rescue_with Favorite::Error, status: 422
|
||||||
|
|
||||||
def index
|
def index
|
||||||
|
authorize Favorite
|
||||||
if !request.format.html?
|
if !request.format.html?
|
||||||
@favorites = Favorite.visible(CurrentUser.user).paginated_search(params)
|
@favorites = Favorite.visible(CurrentUser.user).paginated_search(params)
|
||||||
respond_with(@favorites)
|
respond_with(@favorites)
|
||||||
@@ -19,6 +19,7 @@ class FavoritesController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
authorize Favorite
|
||||||
@post = Post.find(params[:post_id])
|
@post = Post.find(params[:post_id])
|
||||||
@post.add_favorite!(CurrentUser.user)
|
@post.add_favorite!(CurrentUser.user)
|
||||||
flash.now[:notice] = "You have favorited this post"
|
flash.now[:notice] = "You have favorited this post"
|
||||||
@@ -27,6 +28,7 @@ class FavoritesController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
authorize Favorite
|
||||||
@post = Post.find_by_id(params[:id])
|
@post = Post.find_by_id(params[:id])
|
||||||
|
|
||||||
if @post
|
if @post
|
||||||
|
|||||||
9
app/policies/favorite_policy.rb
Normal file
9
app/policies/favorite_policy.rb
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
class FavoritePolicy < ApplicationPolicy
|
||||||
|
def create?
|
||||||
|
user.is_member?
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy?
|
||||||
|
user.is_member?
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -56,7 +56,7 @@
|
|||||||
<%= render "posts/partials/show/embedded", post: @post %>
|
<%= render "posts/partials/show/embedded", post: @post %>
|
||||||
<% end -%>
|
<% end -%>
|
||||||
|
|
||||||
<% if CurrentUser.is_member? %>
|
<% if policy(Favorite).create? %>
|
||||||
<%= content_tag(:div, class: "fav-buttons fav-buttons-#{@post.is_favorited?}") do %>
|
<%= content_tag(:div, class: "fav-buttons fav-buttons-#{@post.is_favorited?}") do %>
|
||||||
<%= form_tag(favorites_path(post_id: @post.id), method: "post", id: "add-fav-button", "data-remote": true) do %>
|
<%= form_tag(favorites_path(post_id: @post.id), method: "post", id: "add-fav-button", "data-remote": true) do %>
|
||||||
<%= button_tag tag.i(class: "far fa-heart"), class: "ui-button ui-widget ui-corner-all", "data-disable-with": tag.i(class: "fas fa-spinner fa-spin") %>
|
<%= button_tag tag.i(class: "far fa-heart"), class: "ui-button ui-widget ui-corner-all", "data-disable-with": tag.i(class: "fas fa-spinner fa-spin") %>
|
||||||
|
|||||||
@@ -4,51 +4,56 @@ class FavoritesControllerTest < ActionDispatch::IntegrationTest
|
|||||||
context "The favorites controller" do
|
context "The favorites controller" do
|
||||||
setup do
|
setup do
|
||||||
@user = create(:user)
|
@user = create(:user)
|
||||||
|
@post = create(:post)
|
||||||
|
@faved_post = create(:post)
|
||||||
|
@faved_post.add_favorite!(@user)
|
||||||
end
|
end
|
||||||
|
|
||||||
context "index action" do
|
context "index action" do
|
||||||
setup do
|
|
||||||
@post = create(:post)
|
|
||||||
@post.add_favorite!(@user)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "redirect the user_id param to an ordfav: search" do
|
should "redirect the user_id param to an ordfav: search" do
|
||||||
get favorites_path(user_id: @user.id)
|
get favorites_path(user_id: @user.id)
|
||||||
assert_redirected_to posts_path(tags: "ordfav:#{@user.name}")
|
assert_redirected_to posts_path(tags: "ordfav:#{@user.name}", format: "html")
|
||||||
end
|
end
|
||||||
|
|
||||||
should "redirect members to an ordfav: search" do
|
should "redirect members to an ordfav: search" do
|
||||||
get_auth favorites_path, @user
|
get_auth favorites_path, @user
|
||||||
assert_redirected_to posts_path(tags: "ordfav:#{@user.name}")
|
assert_redirected_to posts_path(tags: "ordfav:#{@user.name}", format: "html")
|
||||||
end
|
end
|
||||||
|
|
||||||
should "redirect anonymous users to the posts index" do
|
should "redirect anonymous users to the posts index" do
|
||||||
get favorites_path
|
get favorites_path
|
||||||
assert_redirected_to posts_path
|
assert_redirected_to posts_path(format: "html")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "create action" do
|
context "create action" do
|
||||||
setup do
|
|
||||||
@post = create(:post)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "create a favorite for the current user" do
|
should "create a favorite for the current user" do
|
||||||
assert_difference("Favorite.count", 1) do
|
assert_difference("Favorite.count", 1) do
|
||||||
post_auth favorites_path, @user, params: {:format => "js", :post_id => @post.id}
|
post_auth favorites_path(post_id: @post.id), @user, as: :javascript
|
||||||
|
assert_response :redirect
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
should "allow banned users to create favorites" do
|
||||||
|
assert_difference("Favorite.count", 1) do
|
||||||
|
post_auth favorites_path(post_id: @post.id), create(:banned_user), as: :javascript
|
||||||
|
assert_response :redirect
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "destroy action" do
|
context "destroy action" do
|
||||||
setup do
|
|
||||||
@post = create(:post)
|
|
||||||
@post.add_favorite!(@user)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "remove the favorite from the current user" do
|
should "remove the favorite from the current user" do
|
||||||
assert_difference("Favorite.count", -1) do
|
assert_difference("Favorite.count", -1) do
|
||||||
delete_auth favorite_path(@post.id), @user, params: {:format => "js"}
|
delete_auth favorite_path(@faved_post.id), @user, as: :javascript
|
||||||
|
assert_response :redirect
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
should "allow banned users to destroy favorites" do
|
||||||
|
assert_difference("Favorite.count", -1) do
|
||||||
|
delete_auth favorite_path(@faved_post.id), @user, as: :javascript
|
||||||
|
assert_response :redirect
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user