Add an initial implementation of the Event model

Initial implementation & specs of the Event model and its children
This commit is contained in:
Petko Bordjukov 2014-08-10 21:17:06 +03:00
parent 3316c06e42
commit 89de56d94d
10 changed files with 144 additions and 0 deletions

View File

@ -7,6 +7,7 @@ class Conference < ActiveRecord::Base
validate :end_date_is_before_start_date
has_many :tracks
has_many :events, through: :tracks
private

10
app/models/event.rb Normal file
View File

@ -0,0 +1,10 @@
class Event < ActiveRecord::Base
validates :title, presence: true
validates :length, presence: true, numericality: {only_integer: true, greater_than: 0}
validates :abstract, presence: true
validates :description, presence: true
belongs_to :track
has_one :conference, through: :track
belongs_to :user
end

3
app/models/lecture.rb Normal file
View File

@ -0,0 +1,3 @@
class Lecture < Event
validates :length, numericality: {greater_than_or_equal_to: 40, less_than_or_equal_to: 45}
end

View File

@ -1,5 +1,6 @@
class Track < ActiveRecord::Base
belongs_to :conference
has_many :events
validates :name, presence: true
validates :color, presence: true, format: {with: /\A[a-f0-9]{6}\z/i}

3
app/models/workshop.rb Normal file
View File

@ -0,0 +1,3 @@
class Workshop < Event
validates :length, numericality: {greater_than_or_equal_to: 30, less_than_or_equal_to: 120}
end

View File

@ -0,0 +1,20 @@
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.string :title, null: false
t.string :subtitle
t.string :type
t.integer :length, null: false, default: 45
t.string :language, null: false, default: 'bg_BG'
t.integer :state
t.text :abstract, null: false
t.text :description, null: false
t.text :notes
t.references :track, index: true
t.references :user, index: true
t.timestamps
end
add_index :events, :type
end
end

27
spec/factories/events.rb Normal file
View File

@ -0,0 +1,27 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :lecture do
title { |n| "Lecture #{n}" }
subtitle "Lorem Ipsum"
length 45
language "bg_BG"
abstract "An Abstract"
description "A Description"
notes "Some Notes"
track
user
end
factory :workshop do
title { |n| "Workshop #{n}" }
subtitle "Lorem Ipsum"
length 60
language "bg_BG"
abstract "An Abstract"
description "A Description"
notes "Some Notes"
track
user
end
end

View File

@ -0,0 +1,23 @@
require 'rails_helper'
RSpec.describe Lecture, :type => :model do
let(:event) { build :lecture }
it_behaves_like 'an event'
describe 'length' do
it 'must be between 40 and 45 minutes' do
event.length = 30
expect(event).to have_error_on :length
event.length = 60
expect(event).to have_error_on :length
event.length = 40
expect(event).to_not have_error_on :length
event.length = 45
expect(event).to_not have_error_on :length
end
end
end

View File

@ -0,0 +1,23 @@
require 'rails_helper'
RSpec.describe Workshop, :type => :model do
let(:event) { build :workshop }
it_behaves_like 'an event'
describe 'length' do
it 'must be between 30 and 120 minutes' do
event.length = 20
expect(event).to have_error_on :length
event.length = 240
expect(event).to have_error_on :length
event.length = 30
expect(event).to_not have_error_on :length
event.length = 120
expect(event).to_not have_error_on :length
end
end
end

View File

@ -0,0 +1,33 @@
RSpec.shared_examples 'an event' do
it 'is invalid without a title' do
event.title = ''
expect(event).to have_error_on :title
end
describe 'length' do
it 'must be present' do
event.length = ''
expect(event).to have_error_on :length
end
it 'must be a number' do
event.length = 'foo'
expect(event).to have_error_on :length
end
it 'must be larger than zero' do
event.length = '-10'
expect(event).to have_error_on :length
end
end
it 'is invalid without an abstract' do
event.abstract = ''
expect(event).to have_error_on :abstract
end
it 'is invalid without a description' do
event.description = ''
expect(event).to have_error_on :description
end
end