Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRY up PostsController with before_action #24

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions app/assets/stylesheets/partials/_posts.scss
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,9 @@
.confirmation-text {
text-align: center;
}

.delete-button input {
background-color: red;
margin: 0.5em;
}
}
42 changes: 16 additions & 26 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
class PostsController < ApplicationController
before_action :assign_post_by_validation_and_id, only: [:edit,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this really the indentation rubocop likes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¯ \ _ (ツ) _ / ¯

:update,
:destroy,
:confirm]

def index
if check_for_search_params
@posts = post_search
Expand All @@ -23,20 +28,13 @@ def create
end

def edit
redirect_to(validation_error_url) && return unless check_validation

@post = Post.find(params[:id])
end

def show
@post = Post.active.find(params[:id])
end

def update
redirect_to(validation_error_url) && return unless check_validation

@post = Post.find_by_validation(params[:validation])

if @post.update_attributes(post_params)
redirect_to @post
else
Expand All @@ -45,40 +43,28 @@ def update
end

def destroy
redirect_to(validation_error_url) && return unless check_validation
@post.toggle_show(false)

post = Post.find_by_validation(params[:validation])
redirect_to(validation_error_url) && return if post.nil?

post.toggle_show(false)

ConfirmationMailer.deleted_email(post).deliver_now
ConfirmationMailer.deleted_email(@post).deliver_now

flash[:notice] = "Post successfully deleted"
end

def confirm
redirect_to(validation_error_url) && return unless check_validation

@post = Post.find_by_validation(params[:validation])
redirect_to(validation_error_url) && return if @post.nil?

@post.toggle_show(true)
ConfirmationMailer.posted_email(@post).deliver_now
end

# Get wrapper/confirmation for destroy
def delete
redirect_to(validation_error_url) && return unless check_validation && params.key?(:id)
end

def validation_error
end

private

def check_validation
params.key?(:validation) && params[:validation].match(/^.{32}$/)
def assign_post_by_validation_and_id
@post = Post.where(id: find_post_params[:id],
validation: find_post_params[:validation]).first

redirect_to(validation_error_url) && return if @post.nil?
end

def post_params
Expand All @@ -99,6 +85,10 @@ def post_params
)
end

def find_post_params
params.permit(:id, :validation)
end

def check_for_search_params
[:type, :location, :query].any? { |k| params.key?(k) }
end
Expand Down
6 changes: 1 addition & 5 deletions app/helpers/posts_helper.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
module PostsHelper
def delete_post_link(post)
link_to_url(posts_delete_url(post, validation: post.validation, id: post.id))
end

def edit_post_link(post)
link_to_url(edit_post_url(post, validation: post.validation))
end

def confirm_post_link(post)
link_to_url(posts_confirm_url(post, validation: post.validation, id: post.id))
link_to_url(confirm_post_url(post, post.validation))
end

def post_link(post)
Expand Down
3 changes: 1 addition & 2 deletions app/views/confirmation_mailer/posted_email.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
<body>
<p>Your post is confirmed!</p>
<p>To view your post, visit: <%= post_link(@post) %></p>
<p>To edit your post, visit: <%= edit_post_link(@post) %></p>
<p>To delete your post, visit: <%= delete_post_link(@post) %></p>
<p>To edit (or delete) your post, visit: <%= edit_post_link(@post) %></p>
<p>Your post will expire in two weeks, on <%= @post.expiration.strftime("%B %d, %Y") %>. </p>
<p>Thanks for posting on <%= app_name %>!</p>
</body>
Expand Down
6 changes: 4 additions & 2 deletions app/views/posts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<% url = post_path(:id => params[:id], validation: params[:validation]) if params[:validation] %>
<%= form_for(@post, url: url) do |f| %>
<%= form_for(@post) do |f| %>
<%= hidden_field_tag :validation, @post.validation %>
<%= hidden_field_tag :id, @post.id %>

<%= render "posts/form_error", :target => @post %>

<div class="button-group">
Expand Down
4 changes: 0 additions & 4 deletions app/views/posts/delete.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
<p class="confirmation-text">
Are you sure you want to delete your post?
</p>
<%= button_to 'Delete', post_path(id: params[:id], validation: params[:validation]), method: :delete %>
13 changes: 13 additions & 0 deletions app/views/posts/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,17 @@
Editing Post
</h1>

<div class="delete-button">
<%=
button_to(
"Delete this post",
post_path(id: @post.id, validation: @post.validation),
method: :delete,
data: {
confirm: "Are you sure you want to delete this post?"
}
)
%>
</div>

<%= render "form" %>
16 changes: 12 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
ActiveAdmin.routes(self)
root "posts#index"

get "posts/delete", to: "posts#delete"
get "posts/confirm", to: "posts#confirm"
# TODO: Can we remove these in favor of using the flash, and redirecting?
get "posts/success", to: "posts#success"
resources :posts

get "validation_error", to: "posts#validation_error"

resources :posts, except: [:edit] do
member do
constraints validation: /[[:alnum:]]{32}/ do
get "edit/:validation", to: "posts#edit", as: :edit
get "confirm/:validation", to: "posts#confirm", as: :confirm
end

get "confirm/:validation", to: "posts#validation_error" # if ^^^^ fails
end
end

get "about", controller: "about"

get "contact", to: "contacts#new"
Expand Down
11 changes: 2 additions & 9 deletions spec/controllers/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
describe "GET #confirm" do
it "activates a post" do
post = create(:post)
get :confirm, validation: post.validation
get :confirm, id: post.id, validation: post.validation

expect(Post.active).to match_array([post])
end
Expand All @@ -105,14 +105,7 @@
id: 1,
validation: "0f21473d03145662d38ce4ea1ebac790"
)
get :confirm, validation: "0f21473d03145662d38ce4ea1ebac791"

expect(response).to redirect_to validation_error_url
end

it "redirects to error page without validation" do
create(:post)
get :confirm
get :confirm, id: 1, validation: "0f21473d03145662d38ce4ea1ebac791"

expect(response).to redirect_to validation_error_url
end
Expand Down
6 changes: 3 additions & 3 deletions spec/features/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,17 @@
it "confirms a post from given link" do
post = create(:post)

visit posts_confirm_url(id: post.id, validation: post.validation)
visit confirm_post_url(post.id, post.validation)

expect(page).to have_content("confirmed")
end
end

describe "the post deletion process" do
it "deletes a post from given link" do
it "deletes a post from edit link" do
post = create(:post)

visit posts_delete_url(id: post.id, validation: post.validation)
visit edit_post_url(post.id, validation: post.validation)
click_button "Delete"

expect(page).to have_content("successfully")
Expand Down