diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/character.rb | 1 | ||||
-rw-r--r-- | app/models/chat_message.rb | 15 | ||||
-rw-r--r-- | app/models/chat_room.rb | 27 |
3 files changed, 43 insertions, 0 deletions
diff --git a/app/models/character.rb b/app/models/character.rb index 55ea7ba..9a3569f 100644 --- a/app/models/character.rb +++ b/app/models/character.rb @@ -5,6 +5,7 @@ class Character < ApplicationRecord has_many :character_items has_many :items, through: :character_items has_many :character_skills + has_many :chat_messages validates :name, presence: true validates_length_of :name, maximum: 15, message: "can't be longer than 15 characters" validates_uniqueness_of :name, message: "is already being used" diff --git a/app/models/chat_message.rb b/app/models/chat_message.rb new file mode 100644 index 0000000..e1aae6c --- /dev/null +++ b/app/models/chat_message.rb @@ -0,0 +1,15 @@ +class ChatMessage < ApplicationRecord + belongs_to :chat_room + belongs_to :sender, class_name: "Character", optional: true + belongs_to :target, class_name: "Character", optional: true + + validates :body, length: { minimum: 1, maximum: 255 } + validate :sender_has_access + + private + def sender_has_access + if sender && !sender.user.has_permission_or_higher?(self.chat_room.permission_level) + errors.add(:chat, "You don't have permission to send that chat.") + end + end +end diff --git a/app/models/chat_room.rb b/app/models/chat_room.rb new file mode 100644 index 0000000..17e3f19 --- /dev/null +++ b/app/models/chat_room.rb @@ -0,0 +1,27 @@ +class ChatRoom < ApplicationRecord + has_many :chat_messages + validates :gid, :name, presence: true, uniqueness: true + + attribute :permission_level, :integer, default: 1 + enum permission_level: { + banned: 0, + player: 1, + helper: 2, + builder: 3, + admin: 4, + developer: 5, + owner: 6 + } + + def self.accessible_to(user) + all.select { |x| x.accessible_to?(user) } + end + + def accessible_to?(user) + user.has_permission_or_higher?(self.permission_level) + end + + def short_name + self.name[0..6] + end +end |