Hexadecimal color changed to capital letters

The default colors in event_types and difficulty levels are in capital letters and the ones that comes from the color picker were not. Now colors are capitalized before saving the track, level or type. A rake task has also been created to capitalize color from old tracks, levels or types.
This commit is contained in:
Ana 2016-07-04 17:53:21 +02:00
parent 0deaf1abc7
commit 8d9bb56f06
11 changed files with 72 additions and 15 deletions

View file

@ -3,5 +3,13 @@ class DifficultyLevel < ActiveRecord::Base
has_many :events, dependent: :nullify
validates :title, presence: true
validates :color, format: /\A#[0-9a-fA-F]{6}\z/
validates :color, format: /\A#[0-9A-F]{6}\z/
before_validation :capitalize_color
private
def capitalize_color
self.color = color.upcase if color.present?
end
end

View file

@ -115,7 +115,7 @@ class Event < ActiveRecord::Base
json = super(options)
json[:room_guid] = room.try(:guid)
json[:track_color] = track.try(:color) || '#ffffff'
json[:track_color] = track.try(:color) || '#FFFFFF'
json[:length] = event_type.try(:length) || 25
json

View file

@ -7,7 +7,9 @@ class EventType < ActiveRecord::Base
validates :minimum_abstract_length, presence: true
validates :maximum_abstract_length, presence: true
validate :length_step
validates :color, format: /\A#[0-9a-fA-F]{6}\z/
validates :color, format: /\A#[0-9A-F]{6}\z/
before_validation :capitalize_color
alias_attribute :name, :title
@ -22,4 +24,8 @@ class EventType < ActiveRecord::Base
def length_step
errors.add(:length, "must be multiple of #{LENGTH_STEP}") if length % LENGTH_STEP != 0
end
def capitalize_color
self.color = color.upcase if color.present?
end
end

View file

@ -4,7 +4,9 @@ class Track < ActiveRecord::Base
before_create :generate_guid
validates :name, presence: true
validates :color, format: /\A#[0-9a-fA-F]{6}\z/
validates :color, format: /\A#[0-9A-F]{6}\z/
before_validation :capitalize_color
private
@ -15,4 +17,8 @@ class Track < ActiveRecord::Base
# end while Person.where(:guid => guid).exists?
self.guid = guid
end
def capitalize_color
self.color = color.upcase if color.present?
end
end

View file

@ -0,0 +1,21 @@
namespace :data do
desc 'Catitalize tracks, event types and difficult levels colors'
task capitalize_colors: :environment do
capitalize_collection_colors(Track)
puts "Tracks' colors capitalized"
capitalize_collection_colors(DifficultyLevel)
puts "Difficulty levels' colors capitalized"
capitalize_collection_colors(EventType)
puts "Event types' colors capitalized"
end
def capitalize_collection_colors(model)
model.all.each do |item|
item.color = item.color.upcase
item.save!
end
end
end

View file

@ -32,7 +32,7 @@ feature EventType do
within('table#event_types') do
expect(page.has_content?('Party')).to be true
expect(page.has_content?('13042')).to be true
expect(page.has_content?('#e4e4e4')).to be true
expect(page.has_content?('#E4E4E4')).to be true
expect(page.assert_selector('tr', count: 3)).to be true
end

View file

@ -426,7 +426,7 @@ describe Conference do
result = {}
result['Hard'] = {
'value' => 1,
'color' => '#ffffff',
'color' => '#FFFFFF',
}
result['Easy'] = {
'value' => 2,
@ -476,7 +476,7 @@ describe Conference do
result = {}
result['Hard'] = {
'value' => 1,
'color' => '#ffffff'
'color' => '#FFFFFF'
}
result['Easy'] = {
'value' => 1,
@ -526,7 +526,7 @@ describe Conference do
}
result['Lecture'] = {
'value' => 1,
'color' => '#ffffff',
'color' => '#FFFFFF',
}
expect(subject.event_type_distribution).to eq(result)
end
@ -572,7 +572,7 @@ describe Conference do
result = {}
result['Lecture'] = {
'value' => 1,
'color' => '#ffffff'
'color' => '#FFFFFF'
}
result['Workshop'] = {
'value' => 1,
@ -622,7 +622,7 @@ describe Conference do
}
result['Track Two'] = {
'value' => 1,
'color' => '#ffffff',
'color' => '#FFFFFF',
}
expect(subject.tracks_distribution).to eq(result)
end
@ -672,7 +672,7 @@ describe Conference do
}
result['Track Two'] = {
'value' => 1,
'color' => '#ffffff'
'color' => '#FFFFFF'
}
expect(subject.tracks_distribution(:confirmed)).to eq(result)
end

View file

@ -236,7 +236,7 @@ describe Event do
json_hash = event.as_json(nil)
expect(json_hash[:room_guid]).to eq(event.room.guid)
expect(json_hash[:track_color]).to eq('#efefef')
expect(json_hash[:track_color]).to eq('#EFEFEF')
expect(json_hash[:length]).to eq(30)
end
@ -245,7 +245,7 @@ describe Event do
json_hash = event.as_json(nil)
expect(json_hash[:room_guid]).to be_nil
expect(json_hash[:track_color]).to eq('#ffffff')
expect(json_hash[:track_color]).to eq('#FFFFFF')
expect(json_hash[:length]).to eq(25)
end
end

View file

@ -18,6 +18,22 @@ describe EventType do
it { is_expected.to validate_presence_of(:minimum_abstract_length) }
it { is_expected.to validate_presence_of(:maximum_abstract_length) }
it 'is valid if its color is a correct hexadecimal color with 7 characters' do
should allow_value('#FF0000').for(:color)
end
it 'is not valid if its color is not a correct hexadecimal color' do
should_not allow_value('#AB1H7G').for(:color)
end
it 'is not valid if its color has less than 7 characters' do
should_not allow_value('#fff').for(:color)
end
it 'is not valid if its color has more than 7 characters' do
should_not allow_value('#123A4567').for(:color)
end
describe 'length' do
it 'validates numericality and greater than 0' do
is_expected.to validate_numericality_of(:length).is_greater_than(0)

View file

@ -9,6 +9,6 @@ describe 'admin/difficulty_levels/index' do
render
expect(rendered).to include('Example Difficulty Level')
expect(rendered).to include('Lorem Ipsum dolsum')
expect(rendered).to include('#ffffff')
expect(rendered).to include('#FFFFFF')
end
end

View file

@ -10,6 +10,6 @@ describe 'admin/tracks/index' do
render
expect(rendered).to include('Example Track')
expect(rendered).to include('Lorem Ipsum dolsum')
expect(rendered).to include('#ffffff')
expect(rendered).to include('#FFFFFF')
end
end