Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/models/sessions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ class Sessions < ActiveRecord::Base
scope :upcoming, -> { where("date_and_time >= ?", DateTime.now).order(:date_and_time) }
scope :next, -> { upcoming.first }

def host
SponsorSession.hosts.for_session(self.id).first.sponsor
end
end
2 changes: 1 addition & 1 deletion app/models/sponsor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ class Sponsor < ActiveRecord::Base
has_many :sponsor_sessions
has_many :sessions, through: :sponsor_sessions

validates :name, :description, :address, presence: true
validates :name, :description, :address, :avatar, presence: true
end
4 changes: 4 additions & 0 deletions app/models/sponsor_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ class SponsorSession < ActiveRecord::Base
belongs_to :sponsor
belongs_to :sessions

validates :host, uniqueness: { scope: :sessions_id }

scope :hosts, -> { where('sponsor_sessions.host = ?', true) }
scope :for_session, ->(session_id) { where('sponsor_sessions.sessions_id = ?', session_id) }
end
5 changes: 5 additions & 0 deletions db/migrate/20131107182457_add_avatar_to_sponsor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAvatarToSponsor < ActiveRecord::Migration
def change
add_column :sponsors, :avatar, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20131104030726) do
ActiveRecord::Schema.define(version: 20131107182457) do

create_table "addresses", force: true do |t|
t.string "flat"
Expand Down Expand Up @@ -124,6 +124,7 @@
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
t.string "avatar"
end

end
1 change: 1 addition & 0 deletions spec/fabricators/sponsor_fabricator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Fabricator(:sponsor) do
name { Faker::Name.name }
description { Faker::Lorem.paragraph }
avatar { Faker::Internet.url }
address { Fabricate(:address) }
end
8 changes: 8 additions & 0 deletions spec/fabricators/sponsor_session_fabricator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Fabricator(:sponsor_session) do
sponsor
sessions
end

Fabricator(:hosted_session, from: :sponsor_session) do
host true
end
9 changes: 9 additions & 0 deletions spec/models/sessions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
it "#next" do
Sessions.next.should eq set_upcoming.first
end

describe "#host" do
let(:sponsor) { Fabricate(:sponsor) }
before do
Fabricate(:sponsor_session, sponsor: sponsor, sessions: session, host: true)
end

it { expect(session.host).to eq(sponsor) }
end
end

it "#attending" do
Expand Down
32 changes: 32 additions & 0 deletions spec/models/sponsor_session_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'

describe SponsorSession do
subject(:sponsor_session) { Fabricate(:hosted_session) }

context 'when the session has more than one host' do
let(:other_sponsor) { Fabricate(:sponsor) }

let(:another_host_session) do
Fabricate.build(:sponsor_session,
sponsor: other_sponsor,
sessions: sponsor_session.sessions,
host: true)
end

it { another_host_session.should have(1).error_on(:host) }
end

context '#scopes' do
let!(:hosted_session) { Fabricate(:hosted_session) }

it '#hosts' do
expect(SponsorSession.hosts.length).to eq(1)
end

describe '#for_session' do
let(:session) { hosted_session.sessions }

it { expect(SponsorSession.for_session(session.id).length).to eq(1) }
end
end
end
12 changes: 10 additions & 2 deletions spec/models/sponsor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
it { should respond_to(:name) }
it { should respond_to(:description) }
it { should respond_to(:address) }
it { should respond_to(:sessions)}
it { should respond_to(:sponsor_sessions)}
it { should respond_to(:sessions) }
it { should respond_to(:sponsor_sessions) }
it { should respond_to(:avatar) }
it { should be_valid }

context 'validations' do
Expand All @@ -30,6 +31,13 @@
it { should_not be_valid}
it { should have(1).error_on(:address) }
end

describe '#avatar' do
before { sponsor.avatar = nil }

it{ should_not be_valid }
it{ should have(1).error_on(:avatar) }
end
end
end

Expand Down