Skip to content

Commit

Permalink
Clone all existing Regions into Chapters
Browse files Browse the repository at this point in the history
This is to facilitate a different meaning between 'region'
and 'chapter'.

A "region" is a collection of locations. Attendees can subscribe
to emails for events that take place in their region, and will
(currently) be mailed regardless of whether the event belongs
to "RailsBridge" or "MobileBridge"

A "chapter" represents the organizing group who caused that
event to happen. Every "event" belongs to one "chapter".
Chapters also belong to one "organization", like "RailsBridge"
or "MobileBridge"

An event now requires a Chapter to be created, but there is not
yet any UI to create one.
  • Loading branch information
tjgrathwell committed Dec 7, 2015
1 parent b49b099 commit 98de79e
Show file tree
Hide file tree
Showing 22 changed files with 209 additions and 28 deletions.
7 changes: 7 additions & 0 deletions app/models/chapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Chapter < ActiveRecord::Base
belongs_to :organization
has_many :events, counter_cache: true
has_many :external_events, counter_cache: true

validates_presence_of :organization
end
4 changes: 3 additions & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Event < ActiveRecord::Base
PERMITTED_ATTRIBUTES = [:title, :target_audience, :location_id, :details, :time_zone, :volunteer_details, :public_email, :starts_at, :ends_at, :student_rsvp_limit, :volunteer_rsvp_limit, :course_id, :allow_student_rsvp, :student_details, :plus_one_host_toggle, :email_on_approval, :has_childcare, :restrict_operating_systems,
PERMITTED_ATTRIBUTES = [:title, :target_audience, :location_id, :chapter_id, :details, :time_zone, :volunteer_details, :public_email, :starts_at, :ends_at, :student_rsvp_limit, :volunteer_rsvp_limit, :course_id, :allow_student_rsvp, :student_details, :plus_one_host_toggle, :email_on_approval, :has_childcare, :restrict_operating_systems,
:survey_greeting]

serialize :allowed_operating_system_ids, JSON
Expand All @@ -14,6 +14,7 @@ class Event < ActiveRecord::Base
end

belongs_to :location, counter_cache: true
belongs_to :chapter, counter_cache: true

extend ActiveHash::Associations::ActiveRecordExtensions
belongs_to_active_hash :course
Expand Down Expand Up @@ -46,6 +47,7 @@ class Event < ActiveRecord::Base

validates_presence_of :title
validates_presence_of :time_zone
validates_presence_of :chapter
validates_inclusion_of :time_zone, in: ActiveSupport::TimeZone.all.map(&:name), allow_blank: true
validates :allowed_operating_system_ids, array_of_ids: OperatingSystem.all.map(&:id), if: :restrict_operating_systems?
validates_presence_of :target_audience, unless: :historical?, if: [:allow_student_rsvp?, :target_audience_required?]
Expand Down
1 change: 1 addition & 0 deletions app/models/external_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ExternalEvent < ActiveRecord::Base
PERMITTED_ATTRIBUTES = [:city, :ends_at, :location, :name, :organizers, :starts_at, :url, :region_id]

belongs_to :region, counter_cache: true
belongs_to :chapter, counter_cache: true

validates_presence_of :name, :starts_at, :location

Expand Down
3 changes: 3 additions & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Organization < ActiveRecord::Base
has_many :chapters
end
21 changes: 19 additions & 2 deletions app/views/events/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,25 @@
$('#event_location_id').select2();
</script>
<p>
<em>If your location isn't in this list, head over to the <%= link_to 'locations page', locations_path %> and add
it before you continue.</em>
<em>If your location isn't in this list, head over to the <%= link_to 'locations page', locations_path %> and add it before you continue.</em>
</p>
</div>

<div class="field">
<div>
<%= f.label :chapter_id %>
</div>
<%= collection_select(:event,
:chapter_id,
Chapter.all,
:id,
:name,
{prompt: true}) %>
<script>
$('#event_chapter_id').select2();
</script>
<p>
<em>If your chapter isn't in this list, contact an admin to get it added before you continue.</em>
</p>
</div>

Expand Down
3 changes: 3 additions & 0 deletions config/before-travis.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
bundle exec rake db:create:all
# ensure that migrations can run successfully
bundle exec rake db:migrate
# re-load from schema to ensure DB is empty
bundle exec rake db:reset
8 changes: 8 additions & 0 deletions db/migrate/20151207004328_create_organizations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateOrganizations < ActiveRecord::Migration
def change
create_table :organizations do |t|
t.string :name
t.timestamps null: false
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20151207004402_seed_initial_organizations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class SeedInitialOrganizations < ActiveRecord::Migration
class Organization < ActiveRecord::Base; end

def change
Organization.create(name: 'RailsBridge')
Organization.create(name: 'MobileBridge')
Organization.create(name: 'GoBridge')
Organization.create(name: 'ClojureBridge')
end
end
13 changes: 13 additions & 0 deletions db/migrate/20151207004523_create_chapters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateChapters < ActiveRecord::Migration
def change
create_table :chapters do |t|
t.string :name
t.integer :events_count
t.integer :external_events_count
t.references :organization, null: false

t.timestamps null: false
end
add_foreign_key :chapters, :organizations
end
end
67 changes: 67 additions & 0 deletions db/migrate/20151207004629_seed_chapters_from_regions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class SeedChaptersFromRegions < ActiveRecord::Migration
class Chapter < ActiveRecord::Base
belongs_to :organization
has_many :events
has_many :external_events
end

class Location < ActiveRecord::Base
has_many :events
end

class Region < ActiveRecord::Base
has_many :locations
has_many :events, through: :locations
has_many :external_events
end

class Event < ActiveRecord::Base
belongs_to :region
belongs_to :chapter
end

class ExternalEvent < ActiveRecord::Base
belongs_to :region
belongs_to :chapter
end

def up
add_column :events, :chapter_id, :integer
add_column :external_events, :chapter_id, :integer

rb_org = Organization.find_by(name: 'RailsBridge')

Region.find_each do |region|
chapter_name = region.name
chapter_name = "RailsBridge #{chapter_name}" unless chapter_name.match('RailsBridge')
chapter = Chapter.create(name: chapter_name, organization: rb_org)
region.events.update_all(chapter_id: chapter.id)
end

sf_chapter = Chapter.find_by(name: 'RailsBridge San Francisco')
if sf_chapter
Event.where('chapter_id IS NULL').update_all(chapter_id: sf_chapter.id)
end

ExternalEvent.find_each do |external_event|
if external_event.region
chapter_name = external_event.region.name
chapter_name = "RailsBridge #{chapter_name}" unless chapter_name.match('RailsBridge')
chapter = Chapter.find_or_create_by(name: chapter_name, organization: rb_org)
external_event.update_attributes(chapter_id: chapter.id)
else
end
end

change_column_null :events, :chapter_id, false
add_index :events, :chapter_id
add_foreign_key :events, :chapters

add_index :external_events, :chapter_id
add_foreign_key :external_events, :chapters
end

def down
remove_column :events, :chapter_id
end
end
25 changes: 24 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: 20151201070911) do
ActiveRecord::Schema.define(version: 20151207004629) do

create_table "authentications", force: :cascade do |t|
t.integer "user_id"
Expand All @@ -21,6 +21,15 @@
t.datetime "updated_at", null: false
end

create_table "chapters", force: :cascade do |t|
t.string "name"
t.integer "events_count"
t.integer "external_events_count"
t.integer "organization_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "dietary_restrictions", force: :cascade do |t|
t.string "restriction"
t.integer "rsvp_id"
Expand Down Expand Up @@ -95,8 +104,11 @@
t.datetime "announcement_email_sent_at"
t.integer "current_state", default: 0
t.string "external_event_data"
t.integer "chapter_id", null: false
end

add_index "events", ["chapter_id"], name: "index_events_on_chapter_id"

create_table "external_events", force: :cascade do |t|
t.string "name"
t.string "url"
Expand All @@ -108,8 +120,10 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "region_id"
t.integer "chapter_id"
end

add_index "external_events", ["chapter_id"], name: "index_external_events_on_chapter_id"
add_index "external_events", ["region_id"], name: "index_external_events_on_region_id"

create_table "locations", force: :cascade do |t|
Expand Down Expand Up @@ -138,6 +152,12 @@
t.datetime "updated_at", null: false
end

create_table "organizations", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "profiles", force: :cascade do |t|
t.integer "user_id"
t.boolean "childcaring"
Expand Down Expand Up @@ -265,13 +285,16 @@
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

add_foreign_key "authentications", "users"
add_foreign_key "chapters", "organizations"
add_foreign_key "dietary_restrictions", "rsvps"
add_foreign_key "event_email_recipients", "event_emails"
add_foreign_key "event_email_recipients", "rsvps", column: "recipient_rsvp_id"
add_foreign_key "event_emails", "events"
add_foreign_key "event_emails", "users", column: "sender_id"
add_foreign_key "event_sessions", "events"
add_foreign_key "events", "chapters"
add_foreign_key "events", "locations"
add_foreign_key "external_events", "chapters"
add_foreign_key "external_events", "regions"
add_foreign_key "locations", "regions"
add_foreign_key "profiles", "users"
Expand Down
7 changes: 4 additions & 3 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require Rails.root.join('db', 'seeds', 'seed_region')
require Rails.root.join('db', 'seeds', 'admin_user')
require Rails.root.join('db', 'seeds', 'seed_event')
Dir[Rails.root.join('db', 'seeds', '*.rb')].each do |seed_file|
require seed_file
end

if Rails.env.development?
Seeder::seed_region
Seeder::seed_chapter
Seeder::admin_user
Seeder::seed_event
end
9 changes: 9 additions & 0 deletions db/seeds/seed_chapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Seeder
def self.seed_chapter
org = Organization.find_or_create_by(name: 'RailsBridge')

Chapter.find_or_create_by(name: 'RailsBridge Seattle', organization: org)
Chapter.find_or_create_by(name: 'RailsBridge San Francisco', organization: org)
Chapter.find_or_create_by(name: 'RailsBridge Tulsa', organization: org)
end
end
10 changes: 9 additions & 1 deletion db/seeds/seed_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def self.destroy_event event
region.destroy if region.events.count == 1
event.location.destroy
end
if event.chapter.present?
organization = event.chapter.organization
organization.destroy if organization.chapters.count == 1
event.chapter.destroy
end
event.destroy
end

Expand All @@ -62,7 +67,9 @@ def self.seed_event(options={})
old_event = Event.where(title: 'Seeded Test Event').first
destroy_event(old_event) if old_event.present?

region = Region.where(name: 'RailsBridge San Francisco').first_or_create!
organization = Organization.find_or_create_by(name: 'RailsBridge')
region = Region.find_or_create_by(name: 'San Francisco')
chapter = Chapter.find_or_create_by(name: 'RailsBridge San Francisco', organization: organization)

location = Location.create!(
region_id: region.id,
Expand All @@ -81,6 +88,7 @@ def self.seed_event(options={})
time_zone: 'Pacific Time (US & Canada)',
course_id: Course::RAILS.id,
location: location,
chapter: chapter,
current_state: :published,
target_audience: 'women',
details: <<-DETAILS.strip_heredoc
Expand Down
10 changes: 3 additions & 7 deletions db/seeds/seed_region.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
module Seeder
def self.seed_region
# seeds the database with an admin user
region = Region.where(name: 'RailsBridge Seattle').first_or_initialize
region.save!
region = Region.where(name: 'RailsBridge San Francisco').first_or_initialize
region.save!
region = Region.where(name: 'RailsBridge Tulsa').first_or_initialize
region.save!
Region.find_or_create_by(name: 'Seattle')
Region.find_or_create_by(name: 'San Francisco')
Region.find_or_create_by(name: 'Tulsa')
end
end
2 changes: 2 additions & 0 deletions spec/controllers/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def make_request(params = {})
end

describe "with valid params" do
let!(:chapter) { create(:chapter) }
let(:create_params) {
next_year = Date.current.year + 1
{
Expand All @@ -271,6 +272,7 @@ def make_request(params = {})
}
},
"location_id" => "1",
"chapter_id" => chapter.id,
"details" => "sdfasdfasdf"
}
}
Expand Down
10 changes: 10 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
student_rsvp_limit 100
volunteer_rsvp_limit 75
location
chapter
course_id Course::RAILS.id
volunteer_details "I am some details for volunteers."
student_details "I am some details for students."
Expand All @@ -57,6 +58,15 @@
sequence(:name) { |n| "Region #{n}" }
end

factory :chapter do
sequence(:name) { |n| "Region #{n}" }
organization
end

factory :organization do
sequence(:name) { |n| "Organization #{n}" }
end

factory :event_session do
sequence(:name) { |n| "Test Session #{n}" }
starts_at 1.day.from_now
Expand Down
Loading

0 comments on commit 98de79e

Please sign in to comment.