diff options
author | David Gay <david@davidgay.org> | 2021-06-06 17:57:43 -0400 |
---|---|---|
committer | David Gay <david@davidgay.org> | 2021-06-06 17:57:43 -0400 |
commit | 3622126380278d9bed8ea0e1e05a0bd1ea040596 (patch) | |
tree | f34f85368138cc3038f362ac4c68a9d5756f55fc /app | |
parent | 387684c8be4691c4469677e48e1b0a46ad600363 (diff) |
Messages
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/messages_controller.rb | 19 | ||||
-rw-r--r-- | app/models/message.rb | 6 | ||||
-rw-r--r-- | app/views/application/_navbar.html.erb | 3 | ||||
-rw-r--r-- | app/views/messages/index.html.erb | 20 |
4 files changed, 48 insertions, 0 deletions
diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb new file mode 100644 index 0000000..fdde395 --- /dev/null +++ b/app/controllers/messages_controller.rb @@ -0,0 +1,19 @@ +class MessagesController < ApplicationController + def index + @messages = Message.where(recipient: current_char).order(created_at: :desc) + end + + def destroy + @message = Message.find(params[:id]) + if @message.recipient == current_char + if @message.destroy + flash[:notice] = "Deleted message." + else + flash[:alert] = "Failed to delete message. Please report this bug." + end + else + flash[:alert] = "You can't delete someone else's message." + end + redirect_to messages_path + end +end diff --git a/app/models/message.rb b/app/models/message.rb new file mode 100644 index 0000000..68d6d94 --- /dev/null +++ b/app/models/message.rb @@ -0,0 +1,6 @@ +class Message < ApplicationRecord + belongs_to :sender, class_name: "Character", optional: true + belongs_to :recipient, class_name: "Character" + + validates :body, length: { minimum: 1, maximum: 10000 } +end diff --git a/app/views/application/_navbar.html.erb b/app/views/application/_navbar.html.erb index c0e2761..be2d9f2 100644 --- a/app/views/application/_navbar.html.erb +++ b/app/views/application/_navbar.html.erb @@ -15,5 +15,8 @@ <li class="mr-6 inline"> <%= link_to "Bazaar", bazaar_path %> </li> + <li class="mr-6 inline"> + <%= link_to "Messages", messages_path %> + </li> <% end %> </ul> diff --git a/app/views/messages/index.html.erb b/app/views/messages/index.html.erb new file mode 100644 index 0000000..9c47439 --- /dev/null +++ b/app/views/messages/index.html.erb @@ -0,0 +1,20 @@ +<h1 class="text-3xl mb-4">Messages</h1> + +<% @messages.each do |message| %> + <div class="border-2 border-gray-500 rounded p-2 my-2"> + <div class="text-sm"><%= distance_of_time_in_words_to_now(message.created_at) %> ago</div> + <div class="italic"> + From: <strong><%= message.sender&.name || "System" %></strong> + </div> + <div class="text-lg font-bold"><%= message.subject || "No subject" %></div> + <div> + <%= message.body %> + </div> + <div class="text-sm mt-2"><%= link_to "Delete", message_path(message), method: :delete, + data: { confirm: "Are you sure you want to delete this message?" } %></div> + </div> +<% end %> + +<% if @messages.none? %> + <p class="my-2">You don't have any messages.</p> +<% end %> |