Skip to content

Commit

Permalink
[#406] Add validation and attribute settings for Story model (#421)
Browse files Browse the repository at this point in the history
* Address issue #406
- Validate presence of speaker_stories for creation of Story
- Make interview-location and interviewer optional for creation of Story

* Add validation tests for Story model
- Test validation of speakers, through speaker_stories
- Make interviewer and interview_location optional in association tests
- Create traits for interviewer/interview_location and speakers in the story factory
  • Loading branch information
slaloggia authored May 24, 2020
1 parent 9cb98f3 commit 145ae99
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
3 changes: 1 addition & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ NGINX_PORT=3000
MAPBOX_STYLE=mapbox:https://styles/mapbox/light-v10
MAPBOX_ACCESS_TOKEN=your pk token here
DB_USER_PASSWORD=postgres
DB_HOST=db

DB_HOST=db
2 changes: 1 addition & 1 deletion rails/app/decorators/file_import/story_row_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ def to_h
def at(index)
@row[index]
end
end
end
8 changes: 5 additions & 3 deletions rails/app/models/story.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
class Story < ApplicationRecord
MEDIA_PATH = Rails.env.test? ? 'spec/fixtures/media' : 'media'

has_many :speaker_stories
has_many :speaker_stories, inverse_of: :story
has_many :speakers, through: :speaker_stories
has_many_attached :media
has_and_belongs_to_many :places
belongs_to :interview_location, class_name: "Place", foreign_key: "interview_location_id"
belongs_to :interviewer, class_name: "Speaker", foreign_key: "interviewer_id"
belongs_to :interview_location, class_name: "Place", foreign_key: "interview_location_id", optional: true
belongs_to :interviewer, class_name: "Speaker", foreign_key: "interviewer_id", optional: true

validates_presence_of :speaker_stories, message: ': Your story must have at least one Speaker'

def self.import_csv(file_contents)
CSV.parse(file_contents, headers: true) do |row|
Expand Down
16 changes: 14 additions & 2 deletions rails/spec/factories/story_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@
desc { "ACT program manager Rudo Kemper discusses why the organization decided to start building Terrastories to support local communities retain their oral history traditions." }
language { 'English' }
permission_level { 0 }
association :interviewer, factory: :speaker
association :interview_location, factory: :place

trait :with_interviewer do
after(:build) do |story|
story.interviewer << FactoryBot.build(:speaker)
story.interview_location << FactoryBot.build(:place)

end
end

trait :with_speakers do
after(:build) do |story|
story.speakers = FactoryBot.build_list(:speaker, 2)
end
end
end
end
14 changes: 12 additions & 2 deletions rails/spec/models/story_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@
it { should have_many :speakers }
it { should have_and_belong_to_many :places }

it { should belong_to :interview_location }
it { should belong_to :interviewer }
# Issue 406 requested that interviewer and interview location be made optional for story creation
it { should belong_to(:interview_location).optional }
it { should belong_to(:interviewer).optional }
end

describe 'validation' do
let(:story_1) {build(:story)}
let(:story_2) {create(:story, :with_speakers)}
it 'must have at least one speaker' do
expect(story_1).to_not be_valid
expect(story_2).to be_valid
end
end

describe '.import_csv' do
Expand Down

0 comments on commit 145ae99

Please sign in to comment.