diff --git a/app/models/chapter.rb b/app/models/chapter.rb index 7718159c1..cf081b637 100644 --- a/app/models/chapter.rb +++ b/app/models/chapter.rb @@ -25,7 +25,8 @@ class Chapter < ActiveRecord::Base def self.available_to_user(user) return Chapter.all if user.has_role?(:organiser) || user.has_role?(:admin) || user.has_role?(:organiser, Chapter) - return Chapter.find_roles(:organiser, user).map(&:resource) + + Chapter.find_roles(:organiser, user).map(&:resource) end def organisers diff --git a/spec/models/chapter_spec.rb b/spec/models/chapter_spec.rb index 141601c35..64ca558a0 100644 --- a/spec/models/chapter_spec.rb +++ b/spec/models/chapter_spec.rb @@ -1,46 +1,70 @@ require 'spec_helper' RSpec.describe Chapter, type: :model do - it { should validate_presence_of(:city) } - it { should validate_length_of(:description).is_at_most(280) } - it { should respond_to(:image) } + subject(:chapter) { described_class.new } context 'validations' do - context '#slug' do - it 'a chapter must have a slug set' do - chapter = Chapter.new(name: 'London', city: 'London', email: 'london@codebar.io') - chapter.save + it { is_expected.to validate_presence_of(:name) } + it { is_expected.to validate_presence_of(:email) } + it { is_expected.to validate_presence_of(:city) } + it { is_expected.to validate_presence_of(:time_zone) } - expect(chapter.slug).to eq('london') + it do + Fabricate(:chapter) + is_expected.to validate_uniqueness_of(:email) + end + + it { is_expected.to validate_length_of(:description).is_at_most(280) } + + describe '#time_zone' do + it 'denys an invalid time zone' do + chapter.time_zone = 'Fake time' + + expect(chapter).not_to be_valid + + expect(chapter.errors[:time_zone]).to include('does not exist') end - it 'the slug is parameterized' do - chapter = Chapter.new(name: 'New York', city: 'New York', email: 'newyork@codebar.io') - chapter.save + it 'allows a valid time zone' do + chapter.time_zone = 'Eastern Time (US & Canada)' + + chapter.valid? - expect(chapter.slug).to eq('new-york') + expect(chapter.errors[:time_zone]).to_not include('does not exist') end end + end - context '#time_zone' do - it 'requires a time zone' do - chapter = Fabricate(:chapter) - expect(chapter).to be_valid - chapter.time_zone = nil - expect(chapter).not_to be_valid - chapter.time_zone = 'Fake time' - expect(chapter).not_to be_valid - end + describe '#slug' do + it 'is defaulted to the name on save' do + chapter = Fabricate.build(:chapter, name: 'London', slug: nil) + + chapter.save! + + expect(chapter.reload.slug).to eq('london') + end + + it 'can be used as a URL' do + chapter = Fabricate.build(:chapter, name: 'New York', slug: nil) + + chapter.save! + + expect(chapter.reload.slug).to eq('new-york') end end context 'scopes' do - context '#active' do - it 'only returns active Chapters' do - 2.times { Fabricate(:chapter) } - 3.times { Fabricate(:chapter, active: false) } + describe '#active' do + it 'includes chapters with active flag' do + active_chapter = Fabricate(:chapter, active: true) + + expect(Chapter.active).to include(active_chapter) + end + + it 'excludes chapters without active flag' do + inactive_chapter = Fabricate(:chapter, active: false) - expect(Chapter.active.all.count).to eq(2) + expect(Chapter.active).not_to include(inactive_chapter) end end end