favgroups: unify find by name methods.

Unify the `name_to_id`, `named`, and `find_by_name` methods into a
single `find_by_name_or_id` method that has consistent behavior in how
names are normalized.
This commit is contained in:
evazion
2020-01-17 13:25:15 -06:00
parent d461bb2ad0
commit e6ffdfdf81
4 changed files with 19 additions and 47 deletions

View File

@@ -820,20 +820,14 @@ class Post < ApplicationRecord
post.update!(parent_id: id)
end
when /^-favgroup:(\d+)$/i
favgroup = FavoriteGroup.where("id = ?", $1.to_i).for_creator(CurrentUser.user.id).first
favgroup&.remove!(self)
when /^-favgroup:(.+)$/i
favgroup = FavoriteGroup.named($1).for_creator(CurrentUser.user.id).first
favgroup = FavoriteGroup.find_by_name_or_id!($1, CurrentUser.user)
raise User::PrivilegeError unless favgroup.editable_by?(CurrentUser.user)
favgroup&.remove!(self)
when /^favgroup:(\d+)$/i
favgroup = FavoriteGroup.where("id = ?", $1.to_i).for_creator(CurrentUser.user.id).first
favgroup&.add!(self)
when /^favgroup:(.+)$/i
favgroup = FavoriteGroup.named($1).for_creator(CurrentUser.user.id).first
favgroup = FavoriteGroup.find_by_name_or_id!($1, CurrentUser.user)
raise User::PrivilegeError unless favgroup.editable_by?(CurrentUser.user)
favgroup&.add!(self)
end