Skip to content

Commit

Permalink
Added test event to things created by db:seed
Browse files Browse the repository at this point in the history
  • Loading branch information
tjgrathwell committed Mar 18, 2013
1 parent 02201bb commit d9f9e22
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 30 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/_base.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ table tr td .btn {
margin: 0 20px;
}

td.wide {
width: 20%;
}

.btn {
font-size: 16px;
font-family: 'AmbleLight';
Expand Down
2 changes: 1 addition & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Event < ActiveRecord::Base
has_many :organizers, through: :organizer_rsvps, source: :user, source_type: 'User'
has_many :legacy_organizers, through: :organizer_rsvps, source: :user, source_type: 'MeetupUser'

has_many :event_sessions
has_many :event_sessions, dependent: :destroy
accepts_nested_attributes_for :event_sessions, allow_destroy: true
validates :event_sessions, length: { minimum: 1 }

Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class User < ActiveRecord::Base
has_many :rsvps, conditions: { user_type: 'User' }
has_many :events, through: :rsvps

has_one :profile
has_one :profile, dependent: :destroy

attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :time_zone

Expand Down
2 changes: 1 addition & 1 deletion app/views/volunteers/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<td><%= rsvp.subject_experience %></td>
<td><%= rsvp.teaching_experience %></td>
<td><%= rsvp.formatted_preference %></td>
<td>
<td class='wide'>
<%= form_for([@event, rsvp], method: :put, url: event_volunteer_path(@event, rsvp.id), remote: true) do %>
<%= label_tag do %>
<%= radio_button_tag(:volunteer_assignment_id, VolunteerAssignment::UNASSIGNED, rsvp.volunteer_assignment_id == VolunteerAssignment::UNASSIGNED) %> Unassigned
Expand Down
31 changes: 5 additions & 26 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
require Rails.root.join('db', 'seeds', 'admin_user')
require Rails.root.join('db', 'seeds', 'seed_event')

#this seeds the database with an admin user--for development only

if Rails.env.development? then
new_user=User.new(
:name => 'admin',
:email => 'admin@example.com',
:password => 'password',
:password_confirmation => 'password',
:first_name => 'Admin',
:last_name => 'User',
)
new_user.admin = true
if new_user.save
puts "Finished running seeds.rb. Check to see if the there is an admin user."
else
puts "Could not save an admin user. #{new_user.inspect}"
end
else
puts "This seeds.rb task is intended for the development environment only."
if Rails.env.development?
Seeder::admin_user
Seeder::seed_event
end
14 changes: 14 additions & 0 deletions db/seeds/admin_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Seeder
def self.admin_user
# seeds the database with an admin user
admin = User.where(email: 'admin@example.com').first_or_initialize
admin.update_attributes(
name: 'admin',
password: 'password',
first_name: 'Admin',
last_name: 'User',
)
admin.admin = true
admin.save!
end
end
107 changes: 107 additions & 0 deletions db/seeds/seed_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
require 'faker'

module Seeder
def self.create_user email
user = User.create!(
email: email,
password: 'password',
first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name
)
user.confirm!
user
end

def self.create_volunteer_rsvp options
rsvp = Rsvp.create!(
event: options[:event],
user: options[:user],
role_id: Role::VOLUNTEER,
volunteer_assignment_id: options[:assignment],
subject_experience: Faker::Lorem.sentence,
teaching_experience: Faker::Lorem.sentence
)
options[:event].event_sessions.each do |session|
RsvpSession.create!(rsvp: rsvp, event_session: session)
end
end

def self.destroy_event event
event.rsvps.each do |rsvp|
rsvp.user.destroy
end
event.location.destroy if event.location.present?
event.destroy
end

def self.seed_event
old_event = Event.where(title: 'Seeded Test Event').first
destroy_event(old_event) if old_event.present?

location = Location.create!(
name: "Sutro Tower",
address_1: "Sutro Tower",
city: "San Francisco",
state: "CA",
zip: "94131",
latitude: 37.75519999999999,
longitude: -122.4528,
gmaps: true
)

event = Event.new(
title: 'Seeded Test Event',
time_zone: 'Pacific Time (US & Canada)',
details: <<DETAILS
<h2>Workshop Description</h2>
This workshop is created by seeds.rb. It is to help you see what it looks like to have an event with multiple people RSVPed.
<h2>Location and Sponsors</h2>
The location of this workshop is located in the Cloud. That is where it is located.
<h2>Transportation and Parking</h2>
You can park in this workshop if you are able to fly an airship into the cloud. Otherwise, parking is restricted.
<h2>Food and Drinks</h2>
Food will be provided by you, if you bring it in a knapsack.
<h2>Childcare</h2>
Childcare will not be provided.
<h2>Afterparty</h2>
The afterparty will be at the Fancy Goat at 7:09 PM.
DETAILS
)
event.event_sessions << EventSession.create(name: 'First Session', starts_at: 60.days.from_now, ends_at: 61.days.from_now)
event.event_sessions << EventSession.create(name: 'Second Session', starts_at: 65.days.from_now, ends_at: 66.days.from_now)

event.location = location

event.save!

first_session = event.event_sessions.find_by_name('First Session')
second_session = event.event_sessions.find_by_name('Second Session')

organizer = create_user('organizer@example.com')
event.organizers << organizer

teacher = create_user('teacher@example.com')
create_volunteer_rsvp(event: event, user: teacher, assignment: VolunteerAssignment::TEACHER)

ta = create_user('ta@example.com')
create_volunteer_rsvp(event: event, user: ta, assignment: VolunteerAssignment::TA)

unassigned1 = create_user('unassigned1@example.com')
create_volunteer_rsvp(event: event, user: unassigned1, assignment: VolunteerAssignment::UNASSIGNED)

unassigned2 = create_user('unassigned2@example.com')
create_volunteer_rsvp(event: event, user: unassigned2, assignment: VolunteerAssignment::UNASSIGNED)
end
end
2 changes: 1 addition & 1 deletion spec/requests/event_organizer_dashboard_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
it "lets the user manage volunteers" do
visit organize_event_path(@event)
click_link "Manage Volunteers"
page.should have_content("Volunteer Assignment")
page.should have_content("Assign Volunteer")
end

it "lets the user check in volunteers", js: true do
Expand Down
35 changes: 35 additions & 0 deletions spec/seeds/seeds_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'spec_helper'
require Rails.root.join('db', 'seeds', 'seed_event')

def assert_no_rows_present
rows = {}
total = 0
ActiveRecord::Base.send(:subclasses).each do |sc|
rows[sc.name] = sc.all.size
total += sc.all.size
end
if total > 0
puts "Leaked the following rows: "
rows.each do |klass, count|
next unless count > 0
puts "#{klass}: #{count}"
end
total.should == 0
end
end

describe "#seed_event" do
it "creates an event which can cleanly destroy itself" do
Seeder::seed_event
event = Event.last
event.title.should == 'Seeded Test Event'
Seeder::destroy_event(event)
assert_no_rows_present
end

it "destroys itself when asked to create itself twice" do
Seeder::seed_event
Seeder::seed_event
Event.count.should == 1
end
end

0 comments on commit d9f9e22

Please sign in to comment.