From 5f00fc87a3f573915ce44893e02ec45df1bdad13 Mon Sep 17 00:00:00 2001 From: jomo Date: Sat, 17 Jan 2015 16:04:07 +0100 Subject: [PATCH] proper color support for labels --- app/assets/stylesheets/style.css.scss | 6 ++-- app/models/label.rb | 30 +++++++++++++++++++ app/views/forums/show.html.erb | 2 +- app/views/labels/_label.html.erb | 2 +- .../20150117134404_add_color_to_label.rb | 5 ++++ db/schema.rb | 3 +- 6 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20150117134404_add_color_to_label.rb diff --git a/app/assets/stylesheets/style.css.scss b/app/assets/stylesheets/style.css.scss index 312f599..fb383e3 100644 --- a/app/assets/stylesheets/style.css.scss +++ b/app/assets/stylesheets/style.css.scss @@ -987,10 +987,8 @@ nav.pagination { } .label { - background: #d40; + background: #333; padding: 0.1em 0.2em; border-radius: 0.2em; - color: #b20; - border: 1px solid #B20; - text-shadow: -1px -1px #e50, 1px 1px #a10; + text-shadow: none; } \ No newline at end of file diff --git a/app/models/label.rb b/app/models/label.rb index 0bcc7b9..e0cf98e 100644 --- a/app/models/label.rb +++ b/app/models/label.rb @@ -1,5 +1,6 @@ class Label < ActiveRecord::Base validates_presence_of :name + validate :color_valid belongs_to :role has_and_belongs_to_many :forums has_many :forumthreads @@ -8,4 +9,33 @@ class Label < ActiveRecord::Base name.upcase end + # calculate the foreground color + # either black or white, based on the bg color + def fcolor + bg = color.dup + # convert 3 char to 6 char hex + bg.gsub!(/./, '\&\&') if bg.length == 3 + [0, 2, 4].each do |i| + if bg[i..i+1] >= "7f" + return "000" + end + end + return "fff" + end + + private + + def color_valid + color.downcase! + unless [3, 6].include? color.length + errors.add(:color, "Must be 3 or 6 characters long") + end + valid_chars = ("0".."9").to_a + ("a".."f").to_a + color.split("").each do |c| + unless valid_chars.include? c + errors.add(:color, "Must be a valid HEX code") + return + end + end + end end \ No newline at end of file diff --git a/app/views/forums/show.html.erb b/app/views/forums/show.html.erb index d844fe7..7200c78 100644 --- a/app/views/forums/show.html.erb +++ b/app/views/forums/show.html.erb @@ -22,7 +22,7 @@
"> - <%= link_to truncate(thread.title, length: 60, omission: " …"), forumthread_path(thread), title: thread.title %> + <%= render partial: "labels/label", locals: {label: thread.label} %><%= link_to truncate(thread.title, length: 60, omission: " …"), forumthread_path(thread), title: thread.title %>
<% if rpl = thread.replies.last %> <%= rpl.author.name %> diff --git a/app/views/labels/_label.html.erb b/app/views/labels/_label.html.erb index ee3e602..a13fba0 100644 --- a/app/views/labels/_label.html.erb +++ b/app/views/labels/_label.html.erb @@ -1,3 +1,3 @@ <% if label %> - <%= label %> + <%= label %> <% end %> \ No newline at end of file diff --git a/db/migrate/20150117134404_add_color_to_label.rb b/db/migrate/20150117134404_add_color_to_label.rb new file mode 100644 index 0000000..40b6f4a --- /dev/null +++ b/db/migrate/20150117134404_add_color_to_label.rb @@ -0,0 +1,5 @@ +class AddColorToLabel < ActiveRecord::Migration + def change + add_column :labels, :color, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index e0ce7e0..cd86337 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141120202853) do +ActiveRecord::Schema.define(version: 20150117134404) do create_table "blogposts", force: true do |t| t.string "title" @@ -72,6 +72,7 @@ ActiveRecord::Schema.define(version: 20141120202853) do create_table "labels", force: true do |t| t.string "name" t.integer "role_id" + t.string "color" end create_table "register_tokens", force: true do |t|