proper color support for labels

This commit is contained in:
jomo
2015-01-17 16:04:07 +01:00
parent 39fceedf56
commit 5f00fc87a3
6 changed files with 41 additions and 7 deletions

View File

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

View File

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

View File

@@ -22,7 +22,7 @@
</div>
<div class="items bold">
<div class="item <%= "#{"locked" if thread.locked}#{"sticky" if thread.sticky}" %>">
<%= 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 %>
<div class="item-info">
<% if rpl = thread.replies.last %>
<%= rpl.author.name %>

View File

@@ -1,3 +1,3 @@
<% if label %>
<span class="label label-<%= label.name.downcase %>"><%= label %></span>
<span class="label label-<%= label.name.downcase %>" style="color: #<%= label.fcolor %>;background-color: #<%= label.color %>"><%= label %></span>
<% end %>

View File

@@ -0,0 +1,5 @@
class AddColorToLabel < ActiveRecord::Migration
def change
add_column :labels, :color, :string
end
end

View File

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