diff options
-rw-r--r-- | app/controllers/characters/titles_controller.rb | 26 | ||||
-rw-r--r-- | app/models/character.rb | 3 | ||||
-rw-r--r-- | app/models/chat_room.rb | 2 | ||||
-rw-r--r-- | app/models/title.rb | 3 | ||||
-rw-r--r-- | app/models/title_award.rb | 6 | ||||
-rw-r--r-- | app/views/application/components/text/_title.html.erb | 8 | ||||
-rw-r--r-- | app/views/characters/show.html.erb | 4 | ||||
-rw-r--r-- | app/views/characters/titles/index.html.erb | 32 | ||||
-rw-r--r-- | app/views/chat_messages/_message.html.erb | 4 | ||||
-rw-r--r-- | config/routes.rb | 3 | ||||
-rw-r--r-- | data/titles.yml | 2 | ||||
-rw-r--r-- | db/migrate/20210520230216_create_titles.rb | 12 | ||||
-rw-r--r-- | db/migrate/20210520230859_create_title_awards.rb | 10 | ||||
-rw-r--r-- | db/schema.rb | 23 | ||||
-rw-r--r-- | db/seeds.rb | 5 | ||||
-rw-r--r-- | test/fixtures/title_awards.yml | 9 | ||||
-rw-r--r-- | test/fixtures/titles.yml | 7 | ||||
-rw-r--r-- | test/models/title_award_test.rb | 7 | ||||
-rw-r--r-- | test/models/title_test.rb | 7 |
19 files changed, 169 insertions, 4 deletions
diff --git a/app/controllers/characters/titles_controller.rb b/app/controllers/characters/titles_controller.rb new file mode 100644 index 0000000..b321af9 --- /dev/null +++ b/app/controllers/characters/titles_controller.rb @@ -0,0 +1,26 @@ +class Characters::TitlesController < ApplicationController + before_action :set_character + + def index + @title_awards = @character.title_awards + end + + def activate + @title = Title.find(params[:title_id]) + if current_char.title_awards.exists?(title: @title) + current_char.update(active_title: @title) + else + flash[:alert] = "You haven't earned that title." + end + redirect_to character_titles_path(current_char) + end + + private + def set_character + @character = Character.find(params[:character_id]) + unless current_char == @character + flash[:alert] = "You can only look at your own titles." + redirect_to character_path(@character) + end + end +end diff --git a/app/models/character.rb b/app/models/character.rb index 41dc951..73d2c4b 100644 --- a/app/models/character.rb +++ b/app/models/character.rb @@ -1,6 +1,9 @@ class Character < ApplicationRecord belongs_to :user belongs_to :activity, optional: true + has_many :title_awards + has_many :titles, through: :title_awards + belongs_to :active_title, class_name: "Title", optional: true has_one :hearth has_many :character_items has_many :learned_activities diff --git a/app/models/chat_room.rb b/app/models/chat_room.rb index 17e3f19..f1e9a24 100644 --- a/app/models/chat_room.rb +++ b/app/models/chat_room.rb @@ -14,7 +14,7 @@ class ChatRoom < ApplicationRecord } def self.accessible_to(user) - all.select { |x| x.accessible_to?(user) } + all.select { |x| x.accessible_to?(user) && x.gid != "achievement" } end def accessible_to?(user) diff --git a/app/models/title.rb b/app/models/title.rb new file mode 100644 index 0000000..c80a1d1 --- /dev/null +++ b/app/models/title.rb @@ -0,0 +1,3 @@ +class Title < ApplicationRecord + validates :gid, :name, presence: true +end diff --git a/app/models/title_award.rb b/app/models/title_award.rb new file mode 100644 index 0000000..ef74958 --- /dev/null +++ b/app/models/title_award.rb @@ -0,0 +1,6 @@ +class TitleAward < ApplicationRecord + belongs_to :title + belongs_to :character + + validates :character_id, uniqueness: { scope: :title_id } +end diff --git a/app/views/application/components/text/_title.html.erb b/app/views/application/components/text/_title.html.erb new file mode 100644 index 0000000..7d87180 --- /dev/null +++ b/app/views/application/components/text/_title.html.erb @@ -0,0 +1,8 @@ +<% if title %> + <% case title.gid %> + <% when "steward" %> + <span class="text-red-500 text-glow">Steward</span> + <% else %> + <%= title.name %> + <% end %> +<% end %> diff --git a/app/views/characters/show.html.erb b/app/views/characters/show.html.erb index 4f2ae2e..819881b 100644 --- a/app/views/characters/show.html.erb +++ b/app/views/characters/show.html.erb @@ -2,6 +2,10 @@ <%= @character.name %> </h1> +<div class="text-lg mb-4"> + <%= link_to "Titles", character_titles_path(@character) %> +</div> + <p class="mb-4">First entered the planes <%= pluralize((Date.current - @character.created_at.to_date).to_i, "day") %> ago.</p> diff --git a/app/views/characters/titles/index.html.erb b/app/views/characters/titles/index.html.erb new file mode 100644 index 0000000..3fb2781 --- /dev/null +++ b/app/views/characters/titles/index.html.erb @@ -0,0 +1,32 @@ +<h1 class="text-3xl mb-4">Titles</h1> + +<p class="mb-4">Here are the titles you've earned. Though they are forever yours until the end of these realms, + you may only display one at a time. How do you wish to be known?</p> + +<table class="table-auto"> + <thead> + <tr> + <th class="table-header-padded">Displayed?</th> + <th class="table-header-padded">Title</th> + <th class="table-header-padded"></th> + </tr> + </thead> + <tbody> + <% @title_awards.each do |title_award| %> + <tr> + <td><%= title_award.title == @character.active_title ? "Yes️" : "" %></td> + <td class="table-cell-padded"> + <%= render "application/components/text/title", title: title_award.title %> + </td> + <td class="table-cell-padded"> + <%= button_to "Display", character_title_activate_path(title_id: title_award.title.id) %> + </td> + </tr> + <% end %> + </tbody> +</table> + +<% unless @title_awards.any? %> + <p>You haven't earned any titles, but fret not. The time has come for champions of all kinds. + Your hour is not far off.</p> +<% end %> diff --git a/app/views/chat_messages/_message.html.erb b/app/views/chat_messages/_message.html.erb index e9663ec..793ed7f 100644 --- a/app/views/chat_messages/_message.html.erb +++ b/app/views/chat_messages/_message.html.erb @@ -28,14 +28,14 @@ <% elsif chat_message.chat_room.gid == "news" %> <span class="<%= chat_room_text_class %>"><%= chat_message.body %></span> <% elsif chat_message.chat_room.gid == "achievement" %> - <%#= render "components/text/title", title: chat_message.target.current_title %> + <%= render "application/components/text/title", title: chat_message.target.active_title %> <span class="<%= chat_room_text_class %>"> <%# TODO: Sort out this subject/target stuff that I just half-blindly ported over from old Esoterra. %> <%= chat_message.target&.name %> <%= chat_message.body %> </span> <% else %> <% if chat_message.sender %> - <%#= render "components/text/title", title: chat_message.sender.current_title %> + <%= render "application/components/text/title", title: chat_message.sender.active_title %> <% end %> <span class="<%= chat_room_text_class %>"> <% if chat_message.sender %> diff --git a/config/routes.rb b/config/routes.rb index 8f0513e..e29f9e9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,6 +18,9 @@ Rails.application.routes.draw do resources :characters, only: [:show, :new, :create] do scope module: :characters do resources :items, only: [:index] + resources :titles, only: [:index] do + post "/activate", to: "titles#activate" + end get "/hearth", to: "hearth#index" end end diff --git a/data/titles.yml b/data/titles.yml new file mode 100644 index 0000000..e5dc758 --- /dev/null +++ b/data/titles.yml @@ -0,0 +1,2 @@ +steward: + name: "Steward" diff --git a/db/migrate/20210520230216_create_titles.rb b/db/migrate/20210520230216_create_titles.rb new file mode 100644 index 0000000..512773f --- /dev/null +++ b/db/migrate/20210520230216_create_titles.rb @@ -0,0 +1,12 @@ +class CreateTitles < ActiveRecord::Migration[6.1] + def change + create_table :titles do |t| + t.string :gid + t.string :name + + t.timestamps + end + + add_reference :characters, :active_title, foreign_key: { to_table: :titles } + end +end diff --git a/db/migrate/20210520230859_create_title_awards.rb b/db/migrate/20210520230859_create_title_awards.rb new file mode 100644 index 0000000..795cd5e --- /dev/null +++ b/db/migrate/20210520230859_create_title_awards.rb @@ -0,0 +1,10 @@ +class CreateTitleAwards < ActiveRecord::Migration[6.1] + def change + create_table :title_awards do |t| + t.references :title, null: false, foreign_key: true + t.references :character, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 562eacf..f2d776a 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_05_20_222335) do +ActiveRecord::Schema.define(version: 2021_05_20_230859) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -66,6 +66,8 @@ ActiveRecord::Schema.define(version: 2021_05_20_222335) do t.datetime "recent_request_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.bigint "active_title_id" + t.index ["active_title_id"], name: "index_characters_on_active_title_id" t.index ["activity_id"], name: "index_characters_on_activity_id" t.index ["user_id"], name: "index_characters_on_user_id" end @@ -147,6 +149,22 @@ ActiveRecord::Schema.define(version: 2021_05_20_222335) do t.index ["gid"], name: "index_skills_on_gid" end + create_table "title_awards", force: :cascade do |t| + t.bigint "title_id", null: false + t.bigint "character_id", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["character_id"], name: "index_title_awards_on_character_id" + t.index ["title_id"], name: "index_title_awards_on_title_id" + end + + create_table "titles", force: :cascade do |t| + t.string "gid" + t.string "name" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false @@ -183,6 +201,7 @@ ActiveRecord::Schema.define(version: 2021_05_20_222335) do add_foreign_key "character_skills", "characters" add_foreign_key "character_skills", "skills" add_foreign_key "characters", "activities" + add_foreign_key "characters", "titles", column: "active_title_id" add_foreign_key "characters", "users" add_foreign_key "chat_messages", "characters", column: "sender_id" add_foreign_key "chat_messages", "characters", column: "target_id" @@ -190,5 +209,7 @@ ActiveRecord::Schema.define(version: 2021_05_20_222335) do add_foreign_key "hearths", "characters" add_foreign_key "learned_activities", "activities" add_foreign_key "learned_activities", "characters" + add_foreign_key "title_awards", "characters" + add_foreign_key "title_awards", "titles" add_foreign_key "users", "characters", column: "active_character_id" end diff --git a/db/seeds.rb b/db/seeds.rb index 7adf0fc..ca72f1e 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -15,6 +15,11 @@ load_data_file("data/chat_rooms.yml").map do |gid, hash| chat_room.update(hash) end +load_data_file("data/titles.yml").map do |gid, hash| + title = Title.find_or_create_by(gid: gid) + title.update(hash) +end + load_data_file("data/skills.yml").map do |gid, hash| skill = Skill.find_or_create_by(gid: gid) skill.update(hash) diff --git a/test/fixtures/title_awards.yml b/test/fixtures/title_awards.yml new file mode 100644 index 0000000..d4a8c6f --- /dev/null +++ b/test/fixtures/title_awards.yml @@ -0,0 +1,9 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: one + character: one + +two: + title: two + character: two diff --git a/test/fixtures/titles.yml b/test/fixtures/titles.yml new file mode 100644 index 0000000..7d41224 --- /dev/null +++ b/test/fixtures/titles.yml @@ -0,0 +1,7 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + +two: + name: MyString diff --git a/test/models/title_award_test.rb b/test/models/title_award_test.rb new file mode 100644 index 0000000..bfbb6cd --- /dev/null +++ b/test/models/title_award_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class TitleAwardTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/title_test.rb b/test/models/title_test.rb new file mode 100644 index 0000000..ce86265 --- /dev/null +++ b/test/models/title_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class TitleTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end |