From 3622126380278d9bed8ea0e1e05a0bd1ea040596 Mon Sep 17 00:00:00 2001 From: David Gay Date: Sun, 6 Jun 2021 17:57:43 -0400 Subject: Messages --- CHANGELOG.md | 4 ++++ app/controllers/messages_controller.rb | 19 +++++++++++++++++++ app/models/message.rb | 6 ++++++ app/views/application/_navbar.html.erb | 3 +++ app/views/messages/index.html.erb | 20 ++++++++++++++++++++ config/routes.rb | 1 + db/migrate/20210606214340_create_messages.rb | 12 ++++++++++++ db/schema.rb | 15 ++++++++++++++- test/fixtures/messages.yml | 13 +++++++++++++ test/models/message_test.rb | 7 +++++++ 10 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 app/controllers/messages_controller.rb create mode 100644 app/models/message.rb create mode 100644 app/views/messages/index.html.erb create mode 100644 db/migrate/20210606214340_create_messages.rb create mode 100644 test/fixtures/messages.yml create mode 100644 test/models/message_test.rb 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 @@
  • <%= link_to "Bazaar", bazaar_path %>
  • +
  • + <%= link_to "Messages", messages_path %> +
  • <% end %> 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 @@ +

    Messages

    + +<% @messages.each do |message| %> +
    +
    <%= distance_of_time_in_words_to_now(message.created_at) %> ago
    +
    + From: <%= message.sender&.name || "System" %> +
    +
    <%= message.subject || "No subject" %>
    +
    + <%= message.body %> +
    +
    <%= link_to "Delete", message_path(message), method: :delete, + data: { confirm: "Are you sure you want to delete this message?" } %>
    +
    +<% end %> + +<% if @messages.none? %> +

    You don't have any messages.

    +<% 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 -- cgit v1.2.3