Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b3275b6
Created model for comments
Nakushi64 Dec 4, 2020
27a6699
Added commenting functionality
Nakushi64 Dec 4, 2020
53dc56b
Added deleting functionality
Nakushi64 Dec 4, 2020
96b30b6
Moderators can delete others comments
Nakushi64 Dec 4, 2020
e593f39
Fixed rspec issue
Nakushi64 Dec 4, 2020
e5aa5ac
Reworked comments with nesting resources
Nakushi64 Dec 7, 2020
721f423
Rubocop changes
Nakushi64 Dec 7, 2020
4c78d7b
More Rubocop
Nakushi64 Dec 7, 2020
f049346
Delete device_story_comment_decorator.rb
Nakushi64 Dec 8, 2020
3c74b3f
Added Akismet spam protection
Nakushi64 Dec 8, 2020
253b3b5
Rubocop
Nakushi64 Dec 8, 2020
d2db661
Fixed status messages and page refreshing after comment actions
Nakushi64 Dec 9, 2020
7fcc2fe
Improved error message
Nakushi64 Dec 9, 2020
4ec037f
Update show.html.erb
Nakushi64 Dec 9, 2020
e5f4aac
Added testing spec for device story comments
Nakushi64 Dec 15, 2020
4d3a04c
Applied suggested changes
Nakushi64 Jan 5, 2021
e52d11b
Rubocop changes
Nakushi64 Jan 6, 2021
62d7cfd
Update app/models/device_story_comment.rb
Nakushi64 Jan 6, 2021
a95a2bd
Update spec/requests/device_story_comments_controller_spec.rb
Nakushi64 Jan 6, 2021
199c3be
Update config/initializers/rakismet.rb
Nakushi64 Jan 6, 2021
d9c5ba8
Update config/initializers/rakismet.rb
Nakushi64 Jan 6, 2021
026a675
Applied recommended spec changes
Nakushi64 Jan 6, 2021
727977a
Rubocop changes
Nakushi64 Jan 6, 2021
6172638
Fixed rspec issue
Nakushi64 Jan 7, 2021
ca500b4
Fixed "Rakismet.key is not defined"
Nakushi64 Jan 8, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ gem 'newrelic_rpm'
gem 'pg'
gem 'puma'
gem 'rails_admin', '~>1.4.2'
gem 'rakismet'
gem 'responders'
gem 'sass-rails', '~>5.0'
gem 'simple_form'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ GEM
thor (>= 0.19.0, < 2.0)
rainbow (3.0.0)
rake (13.0.1)
rakismet (1.5.4)
rb-fsevent (0.10.4)
rb-inotify (0.10.1)
ffi (~> 1.0)
Expand Down Expand Up @@ -503,6 +504,7 @@ DEPENDENCIES
rails (~> 5.2.4.3)
rails-controller-testing
rails_admin (~> 1.4.2)
rakismet
responders
rspec-its
rspec-rails
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/device_stories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

class DeviceStoriesController < ApplicationController
has_scope :order

before_action :authenticate_user!, only: %i(new create edit update destroy)

layout :current_layout

def index
@device_stories = if params[:search].blank?
full_table
Expand Down
74 changes: 74 additions & 0 deletions app/controllers/device_story_comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

class DeviceStoryCommentsController < ApplicationController
before_action :set_device_story
before_action :set_device_story_comment, only: %i(show edit update destroy)

def index
@device_story_comments = @device_story.device_story_comments
end

def new
@device_story_comment = @device_story.device_story_comments.build
end

def create
return unless user_signed_in?

@device_story_comment = @device_story.device_story_comments.build(device_story_comment_params)
@device_story_comment.user_id = current_user.id
responder_create
end

def update
return unless user_signed_in? && ((@device_story_comment.user_id == current_user.id) || moderator?(current_user))

responder_update
end

def responder_create
respond_to do |format|
if @device_story_comment.save && !@device_story_comment.spam?
format.html { redirect_to device_story_path(@device_story), notice: 'Comment successfully submitted!' }
else
flash[:error] = @device_story_comment.errors.full_messages.join(' ')
format.html { redirect_to device_story_path(@device_story) }
end
end
end

def responder_update
respond_to do |format|
if @device_story_comment.update(device_story_comment_params)
format.html { redirect_to device_story_path(@device_story), notice: 'device_story_comment was successfully updated.' }
format.json { render :show, status: :ok, location: @device_story_comment }
else
format.html { render :edit }
format.json { render json: @device_story_comment.errors, status: :unprocessable_entity }
end
end
end

def destroy
return unless user_signed_in? && ((@device_story_comment.user_id == current_user.id) || moderator?(current_user))

@device_story_comment.destroy

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pretty sure at the moment this allows any user to delete (or update) any comment, we should set it so only moderator or comment author can do that.

respond_to do |format|
format.html { redirect_to device_story_path(@device_story), notice: 'Comment deleted!' }
end
end

private

def set_device_story_comment
@device_story_comment = @device_story.device_story_comments.find(params[:id])
end

def device_story_comment_params
params.require(:device_story_comment).permit(:content, :device_story_id, :user_id)
end

def set_device_story
@device_story = DeviceStory.find(params[:device_story_id])
end
end
1 change: 1 addition & 0 deletions app/models/device_story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
class DeviceStory < ApplicationRecord
validates :device_urn, uniqueness: true
validates :device_urn, :custodian_name, presence: true
has_many :device_story_comments, dependent: :destroy
end
9 changes: 9 additions & 0 deletions app/models/device_story_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class DeviceStoryComment < ApplicationRecord
include Rakismet::Model
validates :content, presence: true, length: { maximum: 1000 }
validates :user_id, presence: true
belongs_to :device_story, required: true
belongs_to :user, required: true
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class User < ActiveRecord::Base
has_many :bgeigie_imports
has_many :measurements
has_many :maps

has_many :device_story_comments
scope :moderator, -> { where(moderator: true) }

# Include default devise modules. Others available are:
Expand Down
40 changes: 38 additions & 2 deletions app/views/device_stories/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
<div class="row">
<h2>
<%= @device_story.last_location_name %> (<%= @device_story.device_urn %>)
<div style="font-size: 15px"> <%= time_ago_in_words(@device_story.last_seen) %> ago</div>
<%- if @device_story.last_seen %>
<div style="font-size: 15px"> <%= time_ago_in_words(@device_story.last_seen) %> ago</div>
<%- end %>
</h2>
<div class="span2" style="margin: 0; margin-right: 20px">
<table class="table table-hover" style="min-width: 164px; max-width: 164px; table-layout: fixed; margin-top: 10%; word-wrap: break-word">
Expand Down Expand Up @@ -47,5 +49,39 @@
</div>
</div>

<div>
<h2>Comments: <br></h2>
<table class="table table-striped">
<tbody>
<%- if @device_story.device_story_comments.empty? %>
No Comments yet.
<%- else %>
<%- @device_story.device_story_comments.each do |comment| %>
<tr>
<td>
<b><%= link_to comment.user.name , comment.user %></b> <div style="display: inline-block; color: dimgrey; font-size: small">(<%= time_ago_in_words(comment.created_at) %> ago)</div> <br>
<%= comment.content %>
<%- if user_signed_in? -%>
<%- if (comment.user_id == current_user.id) || moderator?(current_user) %>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like you got the right idea here, but we should protect the route as well. People could build their own requests to delete the user if the API allows it.

<div style="float:right"> <%= link_to 'Delete', device_story_device_story_comment_path(@device_story, comment), method: :delete %></div>
<%- end %>
<%- end %>
</td>
</tr>
<%-end %>
<%- end %>
</tbody>
</table>
</div>
<div style="margin-left: 5px;min-width: 500px">
<%- if user_signed_in? %>
<%= form_with model: DeviceStoryComment, url: device_story_device_story_comments_path(@device_story), local: true, method: 'post' do |form| %>
<%= form.label :content, current_user.name %>
<%= form.text_area :content, :rows => 3, style: 'width:97%;' %>
<p><%= form.submit "Submit comment" %></p>
<% end %>
<% end %>
</div>

<div class="spacer"></div>
<%= current_page_api_example :device_stories_url %>
<%= current_page_api_example :device_stories_url %>
4 changes: 4 additions & 0 deletions config/initializers/rakismet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

Safecast::Application.config.rakismet.key = ENV['AKISMET_API_KEY']
Safecast::Application.config.rakismet.url = "https://#{ENV['DEFAULT_HOSTNAME']}/"
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
resources :users do
resources :measurements, only: :index
end
resources :device_stories, only: %i(index show)
resources :device_stories, only: %i(index show) do
resources :device_story_comments
end
end

match '/api/*path' => redirect('/%{path}.%{format}'), :format => true, via: %i(get post put delete)
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20201207083958_create_device_story_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class CreateDeviceStoryComments < ActiveRecord::Migration[5.2]
def change
create_table :device_story_comments do |t|
t.text :content
t.references :device_story, foreign_key: true
t.references :user, foreign_key: true

t.timestamps
end
end
end
Loading