Add initial Conference model implementation

This commit is contained in:
Petko Bordjukov 2014-08-10 19:57:46 +03:00
parent b49eb7ef4b
commit de9a3aea9d
4 changed files with 82 additions and 0 deletions

16
app/models/conference.rb Normal file
View File

@ -0,0 +1,16 @@
class Conference < ActiveRecord::Base
validates :title, presence: true, uniqueness: true
validates :email, presence: true, format: {with: /\A[^@]+@[^@]+\z/}
validates :description, presence: true
validates :start_date, presence: true
validates :end_date, presence: true
validate :end_date_is_before_start_date
private
def end_date_is_before_start_date
if start_date.present? and end_date.present? and start_date > end_date
errors.add(:end_date, :cannot_be_before_start_date)
end
end
end

View File

@ -0,0 +1,13 @@
class CreateConferences < ActiveRecord::Migration
def change
create_table :conferences do |t|
t.string :title, null: false
t.string :email, null: false
t.text :description, null: false
t.timestamp :start_date, null: false
t.timestamp :end_date, null: false
t.timestamps
end
end
end

View File

@ -0,0 +1,11 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :conference do
title { |n| "Conference-#{n}"}
email
description 'MyText'
start_date '2014-07-29 21:29:13'
end_date '2014-07-31 21:29:13'
end
end

View File

@ -0,0 +1,42 @@
require 'rails_helper'
RSpec.describe Conference, :type => :model do
describe 'title' do
it 'must not be blank' do
expect(build(:conference, title: '')).to have_error_on :title
end
it 'must be unique' do
create :conference, title: 'ExampleConf'
expect(build(:conference, title: 'ExampleConf')).to have_error_on :title
end
end
describe 'email' do
it 'must be present' do
expect(build(:conference, email: '')).to have_error_on :email
end
it 'can contain exatly one @' do
expect(build(:conference, email: 'test@@example.com')).to have_error_on :email
expect(build(:conference, email: 'test@example.com')).to_not have_error_on :email
expect(build(:conference, email: 'testexample.com')).to have_error_on :email
end
end
it 'is invalid without a description' do
expect(build(:conference, description: '')).to have_error_on :description
end
it 'is invalid without a start date' do
expect(build(:conference, start_date: nil)).to have_error_on :start_date
end
it 'is invalid without an end date' do
expect(build(:conference, end_date: nil)).to have_error_on :end_date
end
it 'is invalid when the end date is before the start date' do
expect(build(:conference, start_date: '2014-07-29 21:29:13', end_date: '2014-07-28 01:00:00')).to have_error_on :end_date
end
end