Skip to content

Commit

Permalink
Merge pull request #116 from montrealrb/make-events-location-id-required
Browse files Browse the repository at this point in the history
Events: make 'location_id' a required field
  • Loading branch information
sophiedeziel committed Dec 10, 2015
2 parents c461d69 + bc63528 commit 9fd8f6d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 30 deletions.
5 changes: 1 addition & 4 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ class Event < ActiveRecord::Base

validates :title, presence: true
validates :starts_at, presence: true
validates :location, presence: true

def self.published
order(starts_at: :desc)
end

def location
super || TBALocation.new
end

def title_with_date
date = starts_at.strftime("%B %d")
[title, date].join(' : ')
Expand Down
23 changes: 0 additions & 23 deletions app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,3 @@ class Location < ActiveRecord::Base

validates :name, presence: true
end

class TBALocation
def name
'TBA'
end

def address
'TBA'
end

def directions
''
end

def description
''
end

def url
''
end

end
9 changes: 9 additions & 0 deletions db/migrate/20151204174632_make_events_location_id_required.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class MakeEventsLocationIdRequired < ActiveRecord::Migration
def up
change_column :events, :location_id, :integer, null: false
end

def down
change_column :events, :location_id, :integer, null: true
end
end
4 changes: 2 additions & 2 deletions 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: 20151204172708) do
ActiveRecord::Schema.define(version: 20151204174632) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -33,7 +33,7 @@
t.datetime "starts_at", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "location_id"
t.integer "location_id", null: false
end

add_index "events", ["location_id"], name: "index_events_on_location_id", using: :btree
Expand Down
8 changes: 7 additions & 1 deletion spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

require "rails_helper"

RSpec.describe Event, :type => :model do
RSpec.describe Event, type: :model do

describe "attribute validations" do
it "does not validate when 'starts_at' is not defined" do
Expand All @@ -20,6 +20,12 @@
expect(event.errors.messages.keys).to include :starts_at
end

it "does not validate when 'location_id' is not defined" do
event = Event.new(location_id: nil)
expect(event).to be_invalid
expect(event.errors.messages.keys).to include :location
end

it "does not validate when 'title' is not defined" do
event = Event.new(title: nil)
expect(event).to be_invalid
Expand Down

0 comments on commit 9fd8f6d

Please sign in to comment.