Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/proposals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ def update
private

def proposal_params
params.require(:proposal).permit(:title, :body, :tag_list, :speaker_id).to_h
params.require(:proposal).permit(:title, :body, :tag_list, speaker_ids: []).to_h
end

def speakers
Speaker.all.order('name ASC')
Speaker.all.order(name: :asc)
end
end
3 changes: 2 additions & 1 deletion app/models/proposal.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Proposal < ApplicationRecord
belongs_to :speaker
has_many :proposal_speakers
has_many :speakers, through: :proposal_speakers
has_many :submissions

validates_presence_of :title
Expand Down
4 changes: 4 additions & 0 deletions app/models/proposal_speaker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ProposalSpeaker < ApplicationRecord
belongs_to :proposal
belongs_to :speaker
end
3 changes: 2 additions & 1 deletion app/models/speaker.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Speaker < ApplicationRecord
has_many :proposals
has_many :proposal_speakers
has_many :proposals, through: :proposal_speakers

validates_presence_of :name
validates_uniqueness_of :name
Expand Down
2 changes: 1 addition & 1 deletion app/views/proposals/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<% end %>

<%= form_for @proposal do |f| %>
<%= f.collection_select(:speaker_id, @speakers, :id, :name) %>
<%= f.collection_select(:speaker_ids, @speakers, :id, :name, {}, { multiple: true }) %>
<%= f.label(:title) %>
<%= f.text_field(:title, class: "u-full-width medium-height") %>
<%= f.label(:tags, "Tags (comma-separated, max 3)", for: :proposal_tag_list) %>
Expand Down
7 changes: 6 additions & 1 deletion app/views/proposals/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<div class='row'>
<div class="one-half column">
<h4><%= @proposal.title %>,
by <%= link_to @proposal.speaker.name, speaker_path(@proposal.speaker) %>
by
<% last_speaker_id = @proposal.speakers.last.id %>
<% @proposal.speakers.each do |speaker| %>
<%= link_to speaker.name, speaker_path(speaker) %><%= ", " unless speaker.id == last_speaker_id %>
<% end %>
</h4>
<div class="tags">
<% @proposal.tag_list.each do |tag| %>
<span class="tag"><a href="/tags/<%= tag %>">#<%= tag.gsub(" ", "") %></a></span>
Expand Down
8 changes: 6 additions & 2 deletions app/views/shared/_instance_submissions.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<%-# Sort submissions by title before sorting by result -%>
<% instance.sorted_submissions.includes(proposal: :speaker).each do |submission| %>
<% instance.sorted_submissions.includes(proposal: :speakers).each do |submission| %>
<p>
<%= link_to submission.proposal.title, submission.proposal %> -
<%= link_to submission.proposal.speaker.name, submission.proposal.speaker %> -
<% last_speaker_id = submission.proposal.speakers.last.id %>
<% submission.proposal.speakers.each do |speaker| %>
<%= link_to speaker.name, speaker %><%= ", " unless last_speaker_id == speaker.id %>
<% end %>
-
<%= submission.result.capitalize %>
</p>
<% end %>
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20250511200328_create_proposal_speakers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateProposalSpeakers < ActiveRecord::Migration[7.2]
def change
create_table :proposal_speakers do |t|
t.references :proposal, foreign_key: true
t.references :speaker, foreign_key: true
t.timestamps
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_05_08_172840) do
ActiveRecord::Schema[7.2].define(version: 2025_05_11_200328) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand All @@ -29,6 +29,15 @@
t.index ["name"], name: "index_events_on_name", unique: true
end

create_table "proposal_speakers", force: :cascade do |t|
t.bigint "proposal_id"
t.bigint "speaker_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["proposal_id"], name: "index_proposal_speakers_on_proposal_id"
t.index ["speaker_id"], name: "index_proposal_speakers_on_speaker_id"
end

create_table "proposals", id: :serial, force: :cascade do |t|
t.string "title"
t.text "body"
Expand Down Expand Up @@ -80,6 +89,8 @@
end

add_foreign_key "event_instances", "events"
add_foreign_key "proposal_speakers", "proposals"
add_foreign_key "proposal_speakers", "speakers"
add_foreign_key "proposals", "speakers"
add_foreign_key "submissions", "event_instances"
add_foreign_key "submissions", "proposals"
Expand Down
9 changes: 8 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@
end
puts "5 events with 3 instances each added (15 total instances)..."

speaker_counts = [1,2,3,4]
speaker_ids = Speaker.ids
[' the ', ' and the '].each do | mid_text |
10.times { Proposal.create!(title: Faker::Hacker.ingverb.titleize + mid_text + Faker::Hacker.adjective.titleize + " " + Faker::Hacker.noun.titleize, body: Faker::Movies::VForVendetta, speaker_id: Speaker.ids.sample) }
10.times do
speakers = Speaker.where(id: speaker_ids.sample(speaker_counts.sample))
title = Faker::Hacker.ingverb.titleize + mid_text + Faker::Hacker.adjective.titleize + " " + Faker::Hacker.noun.titleize
body = Faker::Movies::VForVendetta
Proposal.create!(title:, body:, speakers:)
end
end
puts "20 proposals added..."

Expand Down
10 changes: 10 additions & 0 deletions lib/tasks/migrate.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace :migrate do
desc "Migrate proposals from belongs_to :speaker to has_many :speakers"
task speakers: :environment do
Proposal.all.each do |proposal|
speaker = Speaker.find_by(id: proposal.speaker_id)
next if speaker.blank?
ProposalSpeaker.create(proposal:, speaker:)
end
end
end
8 changes: 4 additions & 4 deletions spec/controllers/proposals_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
RSpec.describe ProposalsController do
describe 'POST #create' do
context 'with valid attributes' do

let(:expected_proposal) { Proposal.new(id: 7, title: 'A great talk', body: 'Come listen to a great talk', tag_list: 'talks, proposals', speaker_id: 5 ) }
let(:speaker) { create(:speaker) }
let(:expected_proposal) { Proposal.new(id: 7, title: 'A great talk', body: 'Come listen to a great talk', tag_list: 'talks, proposals', speaker_ids: [speaker.id] ) }

before do
allow(Proposal).to receive(:new).and_return(expected_proposal)
allow(expected_proposal).to receive(:save).and_return(true)
post :create, params: { proposal: { title: 'A great talk', body: 'Come listen to a great talk', tag_list: 'talks, proposals',speaker_id: 5 } }
post :create, params: { proposal: { title: 'A great talk', body: 'Come listen to a great talk', tag_list: 'talks, proposals',speaker_ids: [speaker.id] } }
end

it 'creates a new proposal' do
expect(Proposal).to have_received(:new).with(title: 'A great talk', body: 'Come listen to a great talk', tag_list: 'talks, proposals', speaker_id: '5')
expect(Proposal).to have_received(:new).with(title: 'A great talk', body: 'Come listen to a great talk', tag_list: 'talks, proposals', speaker_ids: [speaker.id.to_s])
end

it { should redirect_to(proposal_path(expected_proposal)) }
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/speakers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
create(:speaker, name: "LazyTed")
create(:speaker, name: "Angel")

create(:proposal, speaker: Speaker.find_by(name: "Angel"))
create(:proposal, speaker: Speaker.find_by(name: "Zebra"))
create(:proposal, speakers: [Speaker.find_by(name: "Angel")])
create(:proposal, speakers: [Speaker.find_by(name: "Zebra")])

get :index
end
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/proposal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
factory :proposal do
title { "All The Little Things" }
body { "A talk about all the little things in code we cannot see" }
speaker
speakers { [create(:speaker)] }
end
end
6 changes: 6 additions & 0 deletions spec/factories/proposal_speakers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :proposal_speaker do
proposal_id { 1 }
speaker_id { 1 }
end
end
5 changes: 5 additions & 0 deletions spec/models/proposal_speaker_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe ProposalSpeaker, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end