Skip to content

Commit

Permalink
mod notes for collaboration
Browse files Browse the repository at this point in the history
Bunch of extraneous Rails 5.2 churn in db/schema.rb that I'm accepting here.
  • Loading branch information
pushcx committed Jul 11, 2018
1 parent 353cdfa commit 67b2fe3
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 32 deletions.
15 changes: 13 additions & 2 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class MessagesController < ApplicationController
before_action :require_logged_in_user
before_action :find_message, :only => [:show, :destroy, :keep_as_new]
before_action :require_logged_in_moderator, only: [:mod_note]
before_action :find_message, :only => [:show, :destroy, :keep_as_new, :mod_note]

def index
@messages = @user.undeleted_received_messages
Expand Down Expand Up @@ -55,6 +56,9 @@ def create
@messages = @user.undeleted_received_messages

if @new_message.save
if @user.is_moderator? && @new_message.mod_note
ModNote.create_from_message(@new_message, @user)
end
flash[:success] = "Your message has been sent to " <<
@new_message.recipient.username.to_s << "."
return redirect_to "/messages"
Expand Down Expand Up @@ -143,11 +147,18 @@ def keep_as_new
return redirect_to "/messages"
end

def mod_note
ModNote.create_from_message(@message, @user)

return redirect_to messages_path, notice: 'ModNote created'
end

private

def message_params
params.require(:message).permit(
:recipient_username, :subject, :body, :hat_id
:recipient_username, :subject, :body, :hat_id,
@user.is_moderator? ? :mod_note : nil
)
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/mod_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def default_periods
end

def period(query)
length = time_interval(params[:period])
length = time_interval(params[:period] || default_periods.first)
query.where("#{query.model.table_name}.created_at >=
(NOW() - INTERVAL #{length[:dur]} #{length[:intv].upcase})")
end
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/mod_notes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ModNotesController < ModController
before_action :require_logged_in_moderator

def index
@notes = period(ModNote.includes(:moderator, :user).all)
end
end
4 changes: 4 additions & 0 deletions app/models/hat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def to_html_label
h.html_safe
end

def to_txt
"(#{self.hat}) "
end

def log_moderation
m = Moderation.new
m.created_at = self.created_at
Expand Down
1 change: 1 addition & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Message < ApplicationRecord

validates :recipient, presence: true

attr_accessor :mod_note
attr_reader :recipient_username

validates :subject, length: { :in => 1..100 }
Expand Down
35 changes: 35 additions & 0 deletions app/models/mod_note.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class ModNote < ApplicationRecord
belongs_to :moderator,
:class_name => "User",
:foreign_key => "moderator_user_id",
:inverse_of => :moderations
belongs_to :user

scope :recent, -> { where('created_at >= ?', 1.week.ago).order('created_at desc') }
scope :for, ->(user) { includes(:moderator).where('user_id = ?', user).order('created_at desc') }

validates :moderator, :user, :note, presence: true

def note=(n)
self[:note] = n.to_s.strip
self.markeddown_note = self.generated_markeddown
end

def generated_markeddown
Markdowner.to_html(self.note)
end

def self.create_from_message(message, moderator)
user = moderator.id == message.recipient.id ? message.author : message.recipient
ModNote.create!(
moderator: moderator,
user: user,
created_at: message.created_at,
note: <<~NOTE
*#{message.author.username} #{message.hat ? message.hat.to_txt : ''}-> #{message.recipient.username}*: #{message.subject}
#{message.body}
NOTE
)
end
end
4 changes: 2 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,11 @@ def enable_invite_by_user!(mod)
end

def undeleted_received_messages
received_messages.where(:deleted_by_recipient => false)
received_messages.where(:deleted_by_recipient => false).order('id asc')
end

def undeleted_sent_messages
sent_messages.where(:deleted_by_author => false)
sent_messages.where(:deleted_by_author => false).order('id asc')
end

def unread_message_count
Expand Down
9 changes: 7 additions & 2 deletions app/views/messages/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@

<% if @user.wearable_hats.any? %>
<div class="boxline">
<%= f.label :hat_id, 'Put on hat:', :class => "required" %>
<%= f.label :hat_id, 'Put on hat:' %>
<%= f.select "hat_id", options_from_collection_for_select(@user.hats, "id", "hat", nil), :include_blank => true %>
</div>

<% if @user.is_moderator? %>
&nbsp;&nbsp;
<%= f.check_box 'mod_note', class: 'normal' %>
<%= f.label :mod_note, 'ModNote', class: 'normal' %>
<% end %>
</div>
<% end %>

Expand Down
11 changes: 9 additions & 2 deletions app/views/messages/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,18 @@
</div>

<div style="float: left; padding-left: 1em;">
<%= form_tag message_path(@message.short_id) + "/keep_as_new",
:method => :post do %>
<%= form_tag message_keep_as_new_path(@message.short_id), :method => :post do %>
<%= submit_tag "Keep As New" %>
<% end %>
</div>

<% if @user.is_moderator? %>
<div style="float: left; padding-left: 1em;">
<%= form_tag message_mod_note_path(@message.short_id), :method => :post do %>
<%= submit_tag "ModNote" %>
<% end %>
</div>
<% end %>
</div>

<div style="clear: both;"></div>
Expand Down
1 change: 1 addition & 0 deletions app/views/mod/_nav.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="box wide">
<div class="legend right">
<%= link_to 'Notes', mod_notes_path(period: '2w') %>
Flagged: <% @periods.each do |p| %><%= link_to_different_page(p, mod_flagged_path(period: p)) %> <% end %>
Downvoted: <% @periods.each do |p| %><%= link_to_different_page(p, mod_downvoted_path(period: p)) %> <% end %>
Commenters: <% %w{1m 2m 3m 6m}.each do |p| %><%= link_to_different_page(p, mod_commenters_path(period: p)) %> <% end %>
Expand Down
18 changes: 18 additions & 0 deletions app/views/mod_notes/_table.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<table class="data zebra tall" width="100%" cellspacing=0>
<tr>
<th>Mod &rarr; User/When</th>
<th>Note</th>
</tr>
<% mod_notes.each do |note| %>
<tr class="nobottom">
<td>
<%= note.moderator.username %>
&rarr;
<%= link_to note.user.username, user_path(note.user) %>
<br>
<%= raw note.created_at.strftime("%Y-%m-%d&nbsp;%H:%M&nbsp;%z") %>
</td>
<td><%= raw note.markeddown_note %></td>
</tr>
<% end %>
</table>
5 changes: 5 additions & 0 deletions app/views/mod_notes/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= render partial: 'mod/nav' %>

<%= render partial: 'table', locals: {
mod_notes: @notes
} %>
5 changes: 5 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@
</span>
<br>

<label class="required">Last 10 Notes:</label>
<%= render partial: 'mod_notes/table', locals: {
mod_notes: ModNote.for(@showing_user).limit(10),
} %>

<label class="required">Last 10 Moderations:</label>
<%= render partial: 'moderations/table', locals: {
moderations: Moderation.for(@showing_user).order('id desc').limit(10),
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
:as => "batch_delete_messages"
resources :messages do
post "keep_as_new"
post "mod_note"
end

get "/c/:id" => "comments#redirect_from_short_id"
Expand Down Expand Up @@ -187,6 +188,7 @@
get "/mod/flagged/:period" => "mod#flagged", :as => "mod_flagged"
get "/mod/downvoted/:period" => "mod#downvoted", :as => "mod_downvoted"
get "/mod/commenters/:period" => "mod#commenters", :as => "mod_commenters"
get "/mod/notes/:period" => "mod_notes#index", :as => "mod_notes"

get "/privacy" => "home#privacy"
get "/about" => "home#about"
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20180711123439_create_mod_notes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateModNotes < ActiveRecord::Migration[5.2]
def change
create_table :mod_notes do |t|
t.integer :moderator_user_id, null: false
t.integer :user_id, null: false
t.text :note, null: false
t.text :markeddown_note, null: false
t.datetime :created_at, null: false
end

add_index :mod_notes, [:id, :user_id]
end
end
Loading

0 comments on commit 67b2fe3

Please sign in to comment.