From 88bd4f77db3a4372c118a9faef613615db66bc52 Mon Sep 17 00:00:00 2001 From: David Gay Date: Sat, 22 May 2021 14:32:38 -0400 Subject: Titles --- app/controllers/characters/titles_controller.rb | 26 ++++++++++++++++++ app/models/character.rb | 3 ++ app/models/chat_room.rb | 2 +- app/models/title.rb | 3 ++ app/models/title_award.rb | 6 ++++ .../application/components/text/_title.html.erb | 8 ++++++ app/views/characters/show.html.erb | 4 +++ app/views/characters/titles/index.html.erb | 32 ++++++++++++++++++++++ app/views/chat_messages/_message.html.erb | 4 +-- 9 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 app/controllers/characters/titles_controller.rb create mode 100644 app/models/title.rb create mode 100644 app/models/title_award.rb create mode 100644 app/views/application/components/text/_title.html.erb create mode 100644 app/views/characters/titles/index.html.erb (limited to 'app') 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" %> + Steward + <% 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 %> +
+ <%= link_to "Titles", character_titles_path(@character) %> +
+

First entered the planes <%= pluralize((Date.current - @character.created_at.to_date).to_i, "day") %> ago.

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 @@ +

Titles

+ +

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?

+ + + + + + + + + + + <% @title_awards.each do |title_award| %> + + + + + + <% end %> + +
Displayed?Title
<%= title_award.title == @character.active_title ? "Yes️" : "" %> + <%= render "application/components/text/title", title: title_award.title %> + + <%= button_to "Display", character_title_activate_path(title_id: title_award.title.id) %> +
+ +<% unless @title_awards.any? %> +

You haven't earned any titles, but fret not. The time has come for champions of all kinds. + Your hour is not far off.

+<% 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" %> <%= chat_message.body %> <% 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 %> <%# TODO: Sort out this subject/target stuff that I just half-blindly ported over from old Esoterra. %> <%= chat_message.target&.name %> <%= chat_message.body %> <% 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 %> <% if chat_message.sender %> -- cgit v1.2.3