Add janitor selectable background color per image

This commit is contained in:
Kira 2019-12-31 10:19:11 -08:00
parent f67c888379
commit 15923af4ba
6 changed files with 27 additions and 3 deletions

View File

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

View File

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

View File

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

View File

@ -96,6 +96,11 @@
<% end %>
</fieldset>
</div>
<div class="input">
<%= f.label :bg_color, "Background Color" %>
<%= f.text_field :bg_color, :size => 6 %>
</div>
<% end %>
<% if CurrentUser.is_admin? %>

View File

@ -0,0 +1,5 @@
class AddPostBackgroundColor < ActiveRecord::Migration[6.0]
def change
add_column :posts, :bg_color, :string, null: true
end
end

View File

@ -1989,7 +1989,8 @@ CREATE TABLE public.posts (
description text DEFAULT ''::text NOT NULL,
comment_count integer DEFAULT 0 NOT NULL,
change_seq bigint NOT NULL,
tag_count_lore integer DEFAULT 0 NOT NULL
tag_count_lore integer DEFAULT 0 NOT NULL,
bg_color character varying
);
@ -5336,6 +5337,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20191006073950'),
('20191006143246'),
('20191013233447'),
('20191116032230');
('20191116032230'),
('20191231162515');