diff --git a/app/models/conference.rb b/app/models/conference.rb index b645c0c..393d575 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -6,6 +6,8 @@ class Conference < ActiveRecord::Base validates :end_date, presence: true validate :end_date_is_before_start_date + has_many :tracks + private def end_date_is_before_start_date diff --git a/app/models/track.rb b/app/models/track.rb new file mode 100644 index 0000000..77ae746 --- /dev/null +++ b/app/models/track.rb @@ -0,0 +1,10 @@ +class Track < ActiveRecord::Base + belongs_to :conference + + validates :name, presence: true + validates :color, presence: true, format: {with: /\A[a-f0-9]{6}\z/i} + + def color=(hex_triplet) + write_attribute :color, hex_triplet.gsub(/\A#/,'') if hex_triplet + end +end diff --git a/db/migrate/20140810172123_create_tracks.rb b/db/migrate/20140810172123_create_tracks.rb new file mode 100644 index 0000000..8bd3852 --- /dev/null +++ b/db/migrate/20140810172123_create_tracks.rb @@ -0,0 +1,11 @@ +class CreateTracks < ActiveRecord::Migration + def change + create_table :tracks do |t| + t.string :name, null: false + t.string :color, null: false + t.references :conference, index: true + + t.timestamps + end + end +end diff --git a/spec/factories/tracks.rb b/spec/factories/tracks.rb new file mode 100644 index 0000000..156e920 --- /dev/null +++ b/spec/factories/tracks.rb @@ -0,0 +1,9 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :track do + name { |n| "Track#{n}" } + color '#000000' + conference + end +end diff --git a/spec/models/track_spec.rb b/spec/models/track_spec.rb new file mode 100644 index 0000000..31c599e --- /dev/null +++ b/spec/models/track_spec.rb @@ -0,0 +1,19 @@ +require 'rails_helper' + +RSpec.describe Track, :type => :model do + it 'is invalid without a name' do + expect(build(:track, name: '')).to have_error_on :name + end + + describe 'color' do + it 'must be present' do + expect(build(:track, color: '')).to have_error_on :color + end + + it 'must be a hex RGB triplet' do + expect(build(:track, color: 'foobar')).to have_error_on :color + expect(build(:track, color: '000000')).to_not have_error_on :color + expect(build(:track, color: '#000000')).to_not have_error_on :color + end + end +end