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 | |
parent | 387684c8be4691c4469677e48e1b0a46ad600363 (diff) |
Messages
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-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 | ||||
-rw-r--r-- | config/routes.rb | 1 | ||||
-rw-r--r-- | db/migrate/20210606214340_create_messages.rb | 12 | ||||
-rw-r--r-- | db/schema.rb | 15 | ||||
-rw-r--r-- | test/fixtures/messages.yml | 13 | ||||
-rw-r--r-- | test/models/message_test.rb | 7 |
10 files changed, 99 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index f0dce45..192aabb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. ## [Unreleased] ### General +- New feature: Messages! Currently you can't exchange messages with other characters, but you may receive + messages from other sources... + - A "new messages" indicator will be added in the future. For now, you'll have to go to the Messages view + to see if you have any messages. - Implemented monitoring (error tracking, uptime, server health, etc.) via Honeybadger. This means the server will automatically send errors to me, with context, for analysis. Please continue to report bugs in Discord, it will be useful to corroborate with Honeybadger. 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 %> diff --git a/config/routes.rb b/config/routes.rb index 2f094a5..fa6d748 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,6 +17,7 @@ Rails.application.routes.draw do get :costs_and_requirements, on: :member end resources :items, only: [:index, :show] + resources :messages, only: [:index, :destroy] resources :hearth_amenities, only: [] do post "/use", to: "hearth_amenities#use" diff --git a/db/migrate/20210606214340_create_messages.rb b/db/migrate/20210606214340_create_messages.rb new file mode 100644 index 0000000..5886af6 --- /dev/null +++ b/db/migrate/20210606214340_create_messages.rb @@ -0,0 +1,12 @@ +class CreateMessages < ActiveRecord::Migration[6.1] + def change + create_table :messages do |t| + t.references :sender, foreign_key: { to_table: :characters } + t.references :recipient, null: false, foreign_key: { to_table: :characters } + t.string :subject + t.text :body + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 9a41002..55a66d9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_06_05_223042) do +ActiveRecord::Schema.define(version: 2021_06_06_214340) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -185,6 +185,17 @@ ActiveRecord::Schema.define(version: 2021_06_05_223042) do t.index ["gid"], name: "index_locations_on_gid" end + create_table "messages", force: :cascade do |t| + t.bigint "sender_id" + t.bigint "recipient_id", null: false + t.string "subject" + t.text "body" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["recipient_id"], name: "index_messages_on_recipient_id" + t.index ["sender_id"], name: "index_messages_on_sender_id" + end + create_table "monsters", force: :cascade do |t| t.string "gid" t.string "name" @@ -279,6 +290,8 @@ ActiveRecord::Schema.define(version: 2021_06_05_223042) do add_foreign_key "hearths", "characters" add_foreign_key "learned_activities", "activities" add_foreign_key "learned_activities", "characters" + add_foreign_key "messages", "characters", column: "recipient_id" + add_foreign_key "messages", "characters", column: "sender_id" add_foreign_key "states", "characters" add_foreign_key "states", "conditions" add_foreign_key "title_awards", "characters" diff --git a/test/fixtures/messages.yml b/test/fixtures/messages.yml new file mode 100644 index 0000000..f869a42 --- /dev/null +++ b/test/fixtures/messages.yml @@ -0,0 +1,13 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + sender: one + recipient: one + subject: MyString + body: MyText + +two: + sender: two + recipient: two + subject: MyString + body: MyText diff --git a/test/models/message_test.rb b/test/models/message_test.rb new file mode 100644 index 0000000..0e0d35b --- /dev/null +++ b/test/models/message_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class MessageTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end |