Skip to content

Commit

Permalink
Merge b6c14e3 into 2e4fd23
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny4381 committed Mar 28, 2023
2 parents 2e4fd23 + b6c14e3 commit a0e66e3
Show file tree
Hide file tree
Showing 17 changed files with 226 additions and 9 deletions.
2 changes: 2 additions & 0 deletions app/models/concerns/cms/addon/high_contrast_mode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ module HighContrastMode
permit_params :high_contrast_mode, :font_color, :background_color

validates :font_color, presence: true, if: -> { high_contrast_mode_enabled? }
validates :font_color, "ss/color" => true
validates :background_color, presence: true, if: -> { high_contrast_mode_enabled? }
validates :background_color, "ss/color" => true
end

def high_contrast_mode_enabled?
Expand Down
2 changes: 2 additions & 0 deletions app/models/concerns/garbage/addon/k5374/description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module K5374::Description
field :bgcolor, type: String

permit_params :style, :bgcolor

validates :bgcolor, "ss/color" => true
end
end
end
1 change: 1 addition & 0 deletions app/models/concerns/gws/model/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Gws::Model::Category
validates :state, presence: true
validates :name, presence: true, length: { maximum: 80 }
validates :color, presence: true, if: ->{ color_required? }
validates :color, "ss/color" => true
validates :order, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 999_999, allow_blank: true }

scope :search, ->(params) do
Expand Down
7 changes: 4 additions & 3 deletions app/models/concerns/gws/schedule/colorize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ module Gws::Schedule::Colorize
def brightness
return nil if self.color.blank?

color = self.color.sub(/^#/, '').sub(/^(.)(.)(.)$/, '\\1\\1\\2\\2\\3\\3')
r, g, b = color.scan(/../).map { |c| c.hex }
((r * 299) + (g * 587) + (b * 114)).to_f / 1000
rgb = SS::Color.parse(self.color)
return nil unless rgb

((rgb.red * 299) + (rgb.green * 587) + (rgb.blue * 114)).to_f / 1000
end

def text_color
Expand Down
2 changes: 2 additions & 0 deletions app/models/gws/contrast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class Gws::Contrast
validates :name, presence: true, length: { maximum: 40 }
validates :state, presence: true, inclusion: { in: %w(public closed), allow_blank: true }
validates :text_color, presence: true, if: -> { state == 'public' }
validates :text_color, "ss/color" => true
validates :color, presence: true, if: -> { state == 'public' }
validates :color, "ss/color" => true

scope :and_public, ->(_date = nil){ where state: 'public' }

Expand Down
2 changes: 1 addition & 1 deletion app/models/gws/schedule/holiday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Gws::Schedule::Holiday

permit_params :color

validates :color, presence: true
validates :color, presence: true, "ss/color" => true

def readable?(user, opts = {})
true
Expand Down
1 change: 1 addition & 0 deletions app/models/gws/schedule/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Gws::Schedule::Plan

permit_params :color

validates :color, "ss/color" => true
validate :validate_color
validate :validate_file_size

Expand Down
1 change: 1 addition & 0 deletions app/models/gws/schedule/todo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Gws::Schedule::Todo

before_validation :set_todo_state

validates :color, "ss/color" => true
validates :todo_state, inclusion: { in: %w(unfinished progressing finished), allow_blank: true }
validates :achievement_rate, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100, allow_blank: true }

Expand Down
10 changes: 10 additions & 0 deletions app/validators/ss/color_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class SS::ColorValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value.blank?

if SS::Color.parse(value).blank?
record.errors.add(attribute, options[:message] || :malformed_color)
return
end
end
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ en:
division_after_blank: The first split destination has not been entered.
add_after_blank: The new destination has not been entered.
malformed_duration: Mulformed period.
malformed_color: Mulformed color.
password_short: must be at least 10 characters long
password_short_upcase: must be at least 10 uppercase alphabetical characters (A-Z).
password_short_downcase: must be at least 10 lowercase alphabetical characters (A-Z).
Expand Down
1 change: 1 addition & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ ja:
division_after_blank: 1番目の分割先が入力されていません。
add_after_blank: 新設先が入力されていません。
malformed_duration: は不正な期間です。
malformed_color: は不正な色です。
password_short: "は%{count}文字以上にしてください。"
password_short_upcase: "は英大文字(A-Z)を%{count}文字以上含むようにしてください。"
password_short_downcase: "は英小文字(a-z)を%{count}文字以上含むようにしてください。"
Expand Down
31 changes: 31 additions & 0 deletions lib/ss/color.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class SS::Color
class << self
def parse(color)
return if color.blank?

color = color[1..-1] if color.start_with?("#")
case color.length
when 3
red = color[0] * 2
green = color[1] * 2
blue = color[2] * 2
when 6
red = color[0..1]
green = color[2..3]
blue = color[4..5]
end
return if !red || !green || !blue

numeric_red = red.hex
return if numeric_red == 0 && red != "00"

numeric_green = green.hex
return if numeric_green == 0 && green != "00"

numeric_blue = blue.hex
return if numeric_blue == 0 && blue != "00"

SS::RandomColor::Rgb.new(numeric_red, numeric_green, numeric_blue)
end
end
end
4 changes: 2 additions & 2 deletions spec/features/cms/theme_templates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
select "公開", from: "item_state"

select "有効", from: "item_high_contrast_mode"
fill_in "item[font_color]", with: "html-#{unique_id}"
fill_in "item[background_color]", with: "html-#{unique_id}"
fill_in "item[font_color]", with: unique_color
fill_in "item[background_color]", with: unique_color

click_button I18n.t('ss.buttons.save')
end
Expand Down
6 changes: 3 additions & 3 deletions spec/features/gws/schedule/csv/basic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
let!(:plan_to_csv) do
Gws::Schedule::Plan.create!(
cur_site: site, cur_user: user,
name: unique_id, start_at: now, end_at: now + 1.hour, category: category1, priority: priority, color: unique_id,
member_ids: [user.id]
name: unique_id, start_at: now, end_at: now + 1.hour, category: category1, priority: priority,
color: unique_color, member_ids: [user.id]
)
end

Expand All @@ -86,7 +86,7 @@
Gws::Schedule::Plan.create!(
cur_site: site, cur_user: user, name: unique_id,
start_at: now, end_at: now + 1.hour, start_on: now.tomorrow.beginning_of_day, end_on: now.tomorrow.beginning_of_day, allday: "allday",
category: category1, priority: priority, color: unique_id,
category: category1, priority: priority, color: unique_color,
member_ids: [user.id]
)
end
Expand Down
90 changes: 90 additions & 0 deletions spec/lib/ss/color_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require 'spec_helper'

describe SS::Color do
describe ".parse" do
context "with valid colors" do
it do
# 6-digit starting with '#'
SS::Color.parse("#000000").tap do |rgb|
expect(rgb.red).to eq 0
expect(rgb.green).to eq 0
expect(rgb.blue).to eq 0
end
SS::Color.parse("#ffffff").tap do |rgb|
expect(rgb.red).to eq 255
expect(rgb.green).to eq 255
expect(rgb.blue).to eq 255
end
SS::Color.parse("#ffffff".upcase).tap do |rgb|
expect(rgb.red).to eq 255
expect(rgb.green).to eq 255
expect(rgb.blue).to eq 255
end

# 3-digits starting with '#'
SS::Color.parse("#000").tap do |rgb|
expect(rgb.red).to eq 0
expect(rgb.green).to eq 0
expect(rgb.blue).to eq 0
end
SS::Color.parse("#fff").tap do |rgb|
expect(rgb.red).to eq 255
expect(rgb.green).to eq 255
expect(rgb.blue).to eq 255
end
SS::Color.parse("#fff".upcase).tap do |rgb|
expect(rgb.red).to eq 255
expect(rgb.green).to eq 255
expect(rgb.blue).to eq 255
end

# just 6-digit
SS::Color.parse("000000").tap do |rgb|
expect(rgb.red).to eq 0
expect(rgb.green).to eq 0
expect(rgb.blue).to eq 0
end
SS::Color.parse("ffffff").tap do |rgb|
expect(rgb.red).to eq 255
expect(rgb.green).to eq 255
expect(rgb.blue).to eq 255
end
SS::Color.parse("ffffff".upcase).tap do |rgb|
expect(rgb.red).to eq 255
expect(rgb.green).to eq 255
expect(rgb.blue).to eq 255
end

# just 3-digits
SS::Color.parse("000").tap do |rgb|
expect(rgb.red).to eq 0
expect(rgb.green).to eq 0
expect(rgb.blue).to eq 0
end
SS::Color.parse("fff").tap do |rgb|
expect(rgb.red).to eq 255
expect(rgb.green).to eq 255
expect(rgb.blue).to eq 255
end
SS::Color.parse("fff".upcase).tap do |rgb|
expect(rgb.red).to eq 255
expect(rgb.green).to eq 255
expect(rgb.blue).to eq 255
end
end
end

context "with invalid colors" do
it do
expect(SS::Color.parse(nil)).to be_nil
expect(SS::Color.parse("")).to be_nil
expect(SS::Color.parse("z")).to be_nil
expect(SS::Color.parse("zz")).to be_nil
expect(SS::Color.parse("zzz")).to be_nil
expect(SS::Color.parse("zzzz")).to be_nil
expect(SS::Color.parse("zzzzz")).to be_nil
expect(SS::Color.parse("zzzzzz")).to be_nil
end
end
end
end
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ def unique_email
"#{unique_id}@example.jp"
end

def unique_color
@random_color ||= SS::RandomColor.new
@random_color.next.to_rgb.to_s
end

def ss_japanese_text(length: 10, separator: '')
@japanese_chars ||= begin
hiragana = ('あ'..'ん').to_a
Expand Down
69 changes: 69 additions & 0 deletions spec/validators/ss/color_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'spec_helper'

describe SS::ColorValidator, type: :validator do
subject { build :gws_schedule_plan, color: value }

context 'with nil' do
let(:value) { nil }

it do
is_expected.to be_valid
end
end

context 'with blank' do
let(:value) { '' }

it do
is_expected.to be_valid
end
end

context 'with black' do
let(:value) { '#000000' }

it do
is_expected.to be_valid
end
end

context 'with white (lowercase)' do
let(:value) { '#ffffff'.downcase }

it do
is_expected.to be_valid
end
end

context 'with white (uppercase)' do
let(:value) { '#ffffff'.upcase }

it do
is_expected.to be_valid
end
end

context 'not staring with "#"' do
let(:value) { 'ffffff' }

it do
is_expected.to be_valid
end
end

context "length isn't 7" do
let(:value) { '#fff' }

it do
is_expected.to be_valid
end
end

context "not hex decimal" do
let(:value) { '#ghijkl' }

it do
is_expected.to be_invalid
end
end
end

0 comments on commit a0e66e3

Please sign in to comment.