diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 09842fa6f..eefaec4a0 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -153,7 +153,7 @@ private edit_reason has_embedded_notes ] - permitted_params += %i[is_rating_locked is_note_locked] if CurrentUser.is_janitor? + permitted_params += %i[is_rating_locked is_note_locked bg_color] if CurrentUser.is_janitor? permitted_params += %i[is_status_locked locked_tags hide_from_anonymous hide_from_search_engines] if CurrentUser.is_admin? params.require(:post).permit(permitted_params) diff --git a/app/models/post.rb b/app/models/post.rb index 2c1f96301..596fff916 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -17,10 +17,12 @@ class Post < ApplicationRecord before_validation :apply_tag_diff, if: :should_process_tags? before_validation :normalize_tags, if: :should_process_tags? before_validation :strip_source + before_validation :fix_bg_color before_validation :blank_out_nonexistent_parents before_validation :remove_parent_loops validates :md5, uniqueness: { :on => :create, message: ->(obj, data) {"duplicate: #{Post.find_by_md5(obj.md5).id}"} } validates :rating, inclusion: { in: %w(s q e), message: "rating must be s, q, or e" } + validates :bg_color, format: { with: /\A[A-Fa-f0-9]{6}\z/ }, allow_nil: true validates :description, length: { maximum: 50_000 } validate :tag_names_are_valid, if: :should_process_tags? validate :added_tags_are_valid, if: :should_process_tags? @@ -1860,6 +1862,12 @@ class Post < ApplicationRecord end module ValidationMethods + def fix_bg_color + if bg_color.blank? + self.bg_color = nil + end + end + def post_is_not_its_own_parent if !new_record? && id == parent_id errors[:base] << "Post cannot have itself as a parent" diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index 9babcc920..1207c2900 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -133,6 +133,10 @@ class PostPresenter < Presenter "data-is-favorited" => post.favorited_by?(CurrentUser.user.id) } + if post.bg_color + attributes['style'] = "background-color: ##{post.bg_color}" + end + if post.visible? attributes["data-md5"] = post.md5 attributes["data-file-url"] = post.file_url diff --git a/app/views/posts/partials/show/_edit.html.erb b/app/views/posts/partials/show/_edit.html.erb index a4416e642..88f08c226 100644 --- a/app/views/posts/partials/show/_edit.html.erb +++ b/app/views/posts/partials/show/_edit.html.erb @@ -96,6 +96,11 @@ <% end %> + +