Merge pull request #1079 from Ana06/suggest_track_color

Different colors for new tracks, types and levels
This commit is contained in:
Christian Bruckmayer 2016-07-11 15:10:26 +02:00 committed by GitHub
commit 28f0973fc4
17 changed files with 91 additions and 16 deletions

View file

@ -95,7 +95,7 @@ Metrics/BlockNesting:
Max: 4
Metrics/ClassLength:
Max: 560
Max: 570
# avoid redundunt curly braces when it is obvious that hash is used
Style/BracesAroundHashParameters:

View file

@ -11,7 +11,7 @@ module Admin
def edit; end
def new
@difficulty_level = @conference.program.difficulty_levels.new
@difficulty_level = @conference.program.difficulty_levels.new(color: @conference.next_color_for_collection(:levels))
end
def create

View file

@ -9,7 +9,7 @@ module Admin
def edit; end
def new
@event_type = @conference.program.event_types.new
@event_type = @conference.program.event_types.new(color: @conference.next_color_for_collection(:types))
end
def create

View file

@ -14,7 +14,7 @@ module Admin
end
def new
@track = @program.tracks.new
@track = @program.tracks.new(color: @conference.next_color_for_collection(:tracks))
end
def create

View file

@ -595,6 +595,20 @@ class Conference < ActiveRecord::Base
registration_limit > 0 && registrations.count >= registration_limit
end
# Returns an hexadecimal color given a collection. The returned color changed
# when the number of element in the collection changes and for consecutive
# number of elements it returns highly different colors.
def next_color_for_collection(collection)
# we have different start indices for every collection to generate a
# different color for every of them.
start_index = {
tracks: (program.tracks.count + 1),
levels: (program.difficulty_levels.count + 51),
types: (program.event_types.count + 101)
}
next_color(start_index[collection])
end
private
# Returns a different html colour for every i and consecutive colors are

View file

@ -3,4 +3,13 @@ class DifficultyLevel < ActiveRecord::Base
has_many :events, dependent: :nullify
validates :title, presence: true
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,6 +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-F]{6}\z/
before_validation :capitalize_color
alias_attribute :name, :title
@ -21,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,6 +4,9 @@ class Track < ActiveRecord::Base
before_create :generate_guid
validates :name, presence: true
validates :color, format: /\A#[0-9A-F]{6}\z/
before_validation :capitalize_color
private
@ -14,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

@ -6,6 +6,7 @@ FactoryGirl.define do
length 30
minimum_abstract_length 0
maximum_abstract_length 500
color '#ffffff'
program
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