Skip to content

Commit

Permalink
Merge pull request #111 from Code-the-Dream-School/104-add-display-co…
Browse files Browse the repository at this point in the history
…mment-page-stylings

added styling and got the comment buttons to work correctly
  • Loading branch information
Mariela-t authored Jan 21, 2025
2 parents e82834c + daed680 commit 7f31fb9
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 47 deletions.
86 changes: 86 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1017,3 +1017,89 @@ h1 {





//comments styles ------------------------------

.comments-show {
color: white;
background-color: rgba(126, 132, 128, 0.2);
border-radius: 10px;
padding: 30px;
width: 100%;
max-width: 600px;
display: block;
align-items: center;
margin: auto;
}

.comment-btn-group {
display: flex;
gap: 10px;
align-items: center;
}

.comment-btn {
padding: 5px;
width: 100%;
background-color: rgba(126, 132, 128, 0.2);
border: white solid 1px;
color: white;
border-radius: 10px;
font-size: 15px;
text-align: center;
text-decoration: none;
margin-top: 10px;
}

.delete-comment-btn {
padding: 5px 30px;
width: 100%;
background-color: rgba(212, 59, 59, 0.2);
border: white solid 1px;
color: white;
border-radius: 10px;
font-size: 15px;
text-align: center;
text-decoration: none;
margin-top: 10px;
}

.comments-new {
color: white;
background-color: rgba(126, 132, 128, 0.2);
border-radius: 10px;
padding: 30px;
width: 100%;
max-width: 600px;
display: block;
align-items: center;
margin: auto;
}

.comment-field-box {
width: 100%;
max-width: 600px;
background-color: rgba(126, 132, 128, 0.2);
border: 1px white solid;
border-radius: 10px;
color: white;
padding: 5px;
text-decoration: none;
}

.comment-field {
background: none;
border: none;
outline: none;
resize: none;
overflow: auto;
width: 100%;
height: 125px;
color: rgb(197, 191, 191);
}

.comment-field:focus {
outline: none;
}

13 changes: 12 additions & 1 deletion app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def create

def update
if @comment.update(comment_params)
redirect_to [@entry, @comment], notice: 'Comment was successfully updated.'
redirect_to friend_entry_show_path(@entry), notice: 'Comment was successfully updated.'
else
render :edit, alert: 'Unable to update comment.'
end
Expand All @@ -49,6 +49,17 @@ def destroy
end
end

def destroy_another
@comment = @entry.comments.find_by(id: params[:id])
if @comment.user == current_user || @entry.user == current_user
@comment.destroy
flash[notice:] = 'Comment was successfully deleted.'
else
flash[alert:] = 'You are not authorized to delete comment.'
end
redirect_to request.referer || entry_path(@entry)
end

private

def authorize_friend!
Expand Down
6 changes: 3 additions & 3 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
</div>
<% end %>

<div class="field">
<%= f.text_area :text %>
<div class="comment-field-box">
<%= f.text_area :text, class: "comment-field" %>
</div>


<div class="actions">
<%= f.submit 'Comment', class: "btn btn-primary btn-sm" %>
<%= f.submit 'Comment', class: "comment-btn" %>
</div>
<% end %>
14 changes: 6 additions & 8 deletions app/views/comments/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@

<h1>Edit comment</h1>

<div class="comments-new">
<%= form_with model: [@entry, @comment], local: true do |f| %>
<div>
<%= f.label :text, "Edit your comment:" %><br>
<%= f.text_area :text, value: @comment.text %>
<%= f.text_area :text, value: @comment.text, class: "comment-field" %>
</div>
<%= f.submit 'Update' %>

<br>
<div class="actions">
<%= f.submit 'Update', class: "comment-btn" %>
</div>

<% end %>
<div>
<%= link_to "Show this comment", entry_comments_path(@entry, @comment) %> |
<%= link_to "Back to comment", entry_comments_path(@entry) %>
</div>
2 changes: 1 addition & 1 deletion app/views/comments/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1> Comments for <%= @entry.text %> </h1>

<% @comments.each do |comment| %>
<div>
<div class="comments-show">
<p> <%= comment.user.username %> : </p>
<p>
<%= comment.text %>
Expand Down
5 changes: 1 addition & 4 deletions app/views/comments/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@

<h1>New comment</h1>

<div class="comments-new" >
<%= render "form", comment: @comment %>

<br>

<div>
<%= link_to 'Friends Entries', friend_entries_path, class: "btn btn-secondary btn-sm" %>
</div>
15 changes: 9 additions & 6 deletions app/views/comments/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<h1>Comment</h1>

<div class="comments-show">

<% if @comment.present? %>
<%= render @comment %>
<% else %>
<p>No comment found.</p>

<% end %>
<%= link_to 'Edit', edit_entry_comment_path(@entry, @comment), class: "btn btn-primary" %> |
<%= button_to entry_comment_path(@entry, @comment), method: :delete, class: "btn btn-danger btn-sm" do%>
<i class="bi bi-trash"></i>
<% end %>
<%= link_to 'My Entries', entries_path, class: "btn btn-primary" %>
<%= link_to 'Friends Entries', friend_entries_path, class: "btn btn-primary" %>

<div class="comment-btn-group">
<%= link_to 'Edit', edit_entry_comment_path(@entry, @comment), class: 'comment-btn' %>
<%= button_to entry_comment_path(@entry, @comment), method: :delete, class: "delete-comment-btn" do%>
<i class="bi bi-trash"></i>
<% end %>
</div>
</div>
11 changes: 6 additions & 5 deletions app/views/entries/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,18 @@
</div>

<div class="entries__show-comment-container-end">
<% if (comment.user = current_user) %>
<% if (comment.user == current_user ) %>

<%= link_to entry_comment_path(@entry, comment), class: "btn btn-primary btn-sm" do %>
<i class="bi bi-eye"></i>
<% end %>


<%= button_to entry_comment_path(@entry, comment), method: :delete, class: "btn btn-danger btn-sm" do %>
<% end %>
<% if comment.user == current_user || @entry.user == current_user %>
<%= button_to destroy_another_entry_comment_path(@entry, comment), method: :delete, class: "btn btn-danger btn-sm", data: { turbo: false } do %>
<i class="bi bi-trash"></i>
<% end %>
<% end %>
<% end %>

</div>
</div>
<% end %>
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
end

resources :entries do
resources :comments, only: [:index, :show, :new, :create, :edit, :update, :destroy]
resources :comments, only: [:index, :show, :new, :create, :edit, :update, :destroy] do
delete 'destroy_another', on: :member
end
end

resources :users, only: [:show, :edit, :update] do
Expand Down
7 changes: 0 additions & 7 deletions spec/views/comments/edit.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,5 @@
expect(rendered).to have_selector("input[type='submit'][value='Update']")
end

it "has a link to show the comment" do
expect(rendered).to have_link('Show this comment', href: entry_comments_path(entry, comment))
end

it "has a link to go back to the comments index" do
expect(rendered).to have_link('Back to comment', href: entry_comments_path(entry))
end
end
end
3 changes: 0 additions & 3 deletions spec/views/comments/new.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,5 @@
expect(rendered).to render_template(partial: "_form")
end

it "renders a link back to comments index" do
expect(rendered).to have_link("Friends Entries", href: friend_entries_path)
end
end
end
9 changes: 1 addition & 8 deletions spec/views/comments/show.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,8 @@
end

it "renders the delete button" do
expect(rendered).to have_selector("button.btn.btn-danger.btn-sm")
expect(rendered).to have_selector("button.delete-comment-btn")
end

it "displays the back to entry link" do
expect(rendered).to have_link("My Entries", href: entries_path)
end

it "displays my friends entries" do
expect(rendered).to have_link("Friends Entries", href: friend_entries_path)
end
end
end

0 comments on commit 7f31fb9

Please sign in to comment.