Merge pull request #1079 from Ana06/suggest_track_color
Different colors for new tracks, types and levels
This commit is contained in:
commit
28f0973fc4
17 changed files with 91 additions and 16 deletions
|
|
@ -95,7 +95,7 @@ Metrics/BlockNesting:
|
||||||
Max: 4
|
Max: 4
|
||||||
|
|
||||||
Metrics/ClassLength:
|
Metrics/ClassLength:
|
||||||
Max: 560
|
Max: 570
|
||||||
|
|
||||||
# avoid redundunt curly braces when it is obvious that hash is used
|
# avoid redundunt curly braces when it is obvious that hash is used
|
||||||
Style/BracesAroundHashParameters:
|
Style/BracesAroundHashParameters:
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ module Admin
|
||||||
def edit; end
|
def edit; end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@difficulty_level = @conference.program.difficulty_levels.new
|
@difficulty_level = @conference.program.difficulty_levels.new(color: @conference.next_color_for_collection(:levels))
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ module Admin
|
||||||
def edit; end
|
def edit; end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@event_type = @conference.program.event_types.new
|
@event_type = @conference.program.event_types.new(color: @conference.next_color_for_collection(:types))
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ module Admin
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@track = @program.tracks.new
|
@track = @program.tracks.new(color: @conference.next_color_for_collection(:tracks))
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
|
|
||||||
|
|
@ -595,6 +595,20 @@ class Conference < ActiveRecord::Base
|
||||||
registration_limit > 0 && registrations.count >= registration_limit
|
registration_limit > 0 && registrations.count >= registration_limit
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
# Returns a different html colour for every i and consecutive colors are
|
# Returns a different html colour for every i and consecutive colors are
|
||||||
|
|
|
||||||
|
|
@ -3,4 +3,13 @@ class DifficultyLevel < ActiveRecord::Base
|
||||||
has_many :events, dependent: :nullify
|
has_many :events, dependent: :nullify
|
||||||
|
|
||||||
validates :title, presence: true
|
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
|
end
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,7 @@ class Event < ActiveRecord::Base
|
||||||
json = super(options)
|
json = super(options)
|
||||||
|
|
||||||
json[:room_guid] = room.try(:guid)
|
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[:length] = event_type.try(:length) || 25
|
||||||
|
|
||||||
json
|
json
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,9 @@ class EventType < ActiveRecord::Base
|
||||||
validates :minimum_abstract_length, presence: true
|
validates :minimum_abstract_length, presence: true
|
||||||
validates :maximum_abstract_length, presence: true
|
validates :maximum_abstract_length, presence: true
|
||||||
validate :length_step
|
validate :length_step
|
||||||
|
validates :color, format: /\A#[0-9A-F]{6}\z/
|
||||||
|
|
||||||
|
before_validation :capitalize_color
|
||||||
|
|
||||||
alias_attribute :name, :title
|
alias_attribute :name, :title
|
||||||
|
|
||||||
|
|
@ -21,4 +24,8 @@ class EventType < ActiveRecord::Base
|
||||||
def length_step
|
def length_step
|
||||||
errors.add(:length, "must be multiple of #{LENGTH_STEP}") if length % LENGTH_STEP != 0
|
errors.add(:length, "must be multiple of #{LENGTH_STEP}") if length % LENGTH_STEP != 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def capitalize_color
|
||||||
|
self.color = color.upcase if color.present?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,9 @@ class Track < ActiveRecord::Base
|
||||||
|
|
||||||
before_create :generate_guid
|
before_create :generate_guid
|
||||||
validates :name, presence: true
|
validates :name, presence: true
|
||||||
|
validates :color, format: /\A#[0-9A-F]{6}\z/
|
||||||
|
|
||||||
|
before_validation :capitalize_color
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
|
@ -14,4 +17,8 @@ class Track < ActiveRecord::Base
|
||||||
# end while Person.where(:guid => guid).exists?
|
# end while Person.where(:guid => guid).exists?
|
||||||
self.guid = guid
|
self.guid = guid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def capitalize_color
|
||||||
|
self.color = color.upcase if color.present?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
21
lib/tasks/capitalize_colors.rake
Normal file
21
lib/tasks/capitalize_colors.rake
Normal 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
|
||||||
|
|
@ -6,6 +6,7 @@ FactoryGirl.define do
|
||||||
length 30
|
length 30
|
||||||
minimum_abstract_length 0
|
minimum_abstract_length 0
|
||||||
maximum_abstract_length 500
|
maximum_abstract_length 500
|
||||||
|
color '#ffffff'
|
||||||
program
|
program
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ feature EventType do
|
||||||
within('table#event_types') do
|
within('table#event_types') do
|
||||||
expect(page.has_content?('Party')).to be true
|
expect(page.has_content?('Party')).to be true
|
||||||
expect(page.has_content?('13042')).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
|
expect(page.assert_selector('tr', count: 3)).to be true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -426,7 +426,7 @@ describe Conference do
|
||||||
result = {}
|
result = {}
|
||||||
result['Hard'] = {
|
result['Hard'] = {
|
||||||
'value' => 1,
|
'value' => 1,
|
||||||
'color' => '#ffffff',
|
'color' => '#FFFFFF',
|
||||||
}
|
}
|
||||||
result['Easy'] = {
|
result['Easy'] = {
|
||||||
'value' => 2,
|
'value' => 2,
|
||||||
|
|
@ -476,7 +476,7 @@ describe Conference do
|
||||||
result = {}
|
result = {}
|
||||||
result['Hard'] = {
|
result['Hard'] = {
|
||||||
'value' => 1,
|
'value' => 1,
|
||||||
'color' => '#ffffff'
|
'color' => '#FFFFFF'
|
||||||
}
|
}
|
||||||
result['Easy'] = {
|
result['Easy'] = {
|
||||||
'value' => 1,
|
'value' => 1,
|
||||||
|
|
@ -526,7 +526,7 @@ describe Conference do
|
||||||
}
|
}
|
||||||
result['Lecture'] = {
|
result['Lecture'] = {
|
||||||
'value' => 1,
|
'value' => 1,
|
||||||
'color' => '#ffffff',
|
'color' => '#FFFFFF',
|
||||||
}
|
}
|
||||||
expect(subject.event_type_distribution).to eq(result)
|
expect(subject.event_type_distribution).to eq(result)
|
||||||
end
|
end
|
||||||
|
|
@ -572,7 +572,7 @@ describe Conference do
|
||||||
result = {}
|
result = {}
|
||||||
result['Lecture'] = {
|
result['Lecture'] = {
|
||||||
'value' => 1,
|
'value' => 1,
|
||||||
'color' => '#ffffff'
|
'color' => '#FFFFFF'
|
||||||
}
|
}
|
||||||
result['Workshop'] = {
|
result['Workshop'] = {
|
||||||
'value' => 1,
|
'value' => 1,
|
||||||
|
|
@ -622,7 +622,7 @@ describe Conference do
|
||||||
}
|
}
|
||||||
result['Track Two'] = {
|
result['Track Two'] = {
|
||||||
'value' => 1,
|
'value' => 1,
|
||||||
'color' => '#ffffff',
|
'color' => '#FFFFFF',
|
||||||
}
|
}
|
||||||
expect(subject.tracks_distribution).to eq(result)
|
expect(subject.tracks_distribution).to eq(result)
|
||||||
end
|
end
|
||||||
|
|
@ -672,7 +672,7 @@ describe Conference do
|
||||||
}
|
}
|
||||||
result['Track Two'] = {
|
result['Track Two'] = {
|
||||||
'value' => 1,
|
'value' => 1,
|
||||||
'color' => '#ffffff'
|
'color' => '#FFFFFF'
|
||||||
}
|
}
|
||||||
expect(subject.tracks_distribution(:confirmed)).to eq(result)
|
expect(subject.tracks_distribution(:confirmed)).to eq(result)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -236,7 +236,7 @@ describe Event do
|
||||||
json_hash = event.as_json(nil)
|
json_hash = event.as_json(nil)
|
||||||
|
|
||||||
expect(json_hash[:room_guid]).to eq(event.room.guid)
|
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)
|
expect(json_hash[:length]).to eq(30)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -245,7 +245,7 @@ describe Event do
|
||||||
json_hash = event.as_json(nil)
|
json_hash = event.as_json(nil)
|
||||||
|
|
||||||
expect(json_hash[:room_guid]).to be_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)
|
expect(json_hash[:length]).to eq(25)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,22 @@ describe EventType do
|
||||||
it { is_expected.to validate_presence_of(:minimum_abstract_length) }
|
it { is_expected.to validate_presence_of(:minimum_abstract_length) }
|
||||||
it { is_expected.to validate_presence_of(:maximum_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
|
describe 'length' do
|
||||||
it 'validates numericality and greater than 0' do
|
it 'validates numericality and greater than 0' do
|
||||||
is_expected.to validate_numericality_of(:length).is_greater_than(0)
|
is_expected.to validate_numericality_of(:length).is_greater_than(0)
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,6 @@ describe 'admin/difficulty_levels/index' do
|
||||||
render
|
render
|
||||||
expect(rendered).to include('Example Difficulty Level')
|
expect(rendered).to include('Example Difficulty Level')
|
||||||
expect(rendered).to include('Lorem Ipsum dolsum')
|
expect(rendered).to include('Lorem Ipsum dolsum')
|
||||||
expect(rendered).to include('#ffffff')
|
expect(rendered).to include('#FFFFFF')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,6 @@ describe 'admin/tracks/index' do
|
||||||
render
|
render
|
||||||
expect(rendered).to include('Example Track')
|
expect(rendered).to include('Example Track')
|
||||||
expect(rendered).to include('Lorem Ipsum dolsum')
|
expect(rendered).to include('Lorem Ipsum dolsum')
|
||||||
expect(rendered).to include('#ffffff')
|
expect(rendered).to include('#FFFFFF')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue