Skip to content

Commit

Permalink
add new participant attributes to the session json export
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyhelbling committed Mar 19, 2016
1 parent e662dc3 commit b821bf7
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ def update

def index
@sessions = Event.current_event.sessions
respond_with(@sessions)
respond_with @sessions do |format|
format.json {
render json: SessionsJsonBuilder.new.to_json(@sessions.uniq.flatten)
}
format.html
end
end

def create
Expand Down Expand Up @@ -61,12 +66,12 @@ def words
end

def export
@sessions = Event.current_event.sessions.all(:order => 'lower(title) asc')
@sessions = Event.current_event.sessions.all.order('lower(title) asc')
render :layout => 'export'
end

def popularity
@sessions = Event.current_event.sessions.with_attendence_count.all(:order => "COALESCE(attendence_count, 0) desc")
@sessions = Event.current_event.sessions.with_attendence_count.all.order("COALESCE(attendence_count, 0) desc")
render :layout => 'export'
end

Expand Down
12 changes: 12 additions & 0 deletions src/app/models/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ def presenter_names
presenters.map(&:name)
end

def other_presenters
presenters.reject{ |p| p == self.participant }
end
def other_presenter_names
other_presenters.map(&:name)
end


def attending?(user)
return false if user.nil?
Expand Down Expand Up @@ -135,10 +142,15 @@ def estimated_interest
end
end

def to_h
SessionsJsonBuilder.new.to_hash(self)
end

private

# assign the creator as the first presenter
def create_presenter
presentations.create(participant: participant)
end

end
33 changes: 33 additions & 0 deletions src/app/models/sessions_json_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class SessionsJsonBuilder

def to_hash(session)
s = session
{ id: s.id,
participant_id: s.participant.id,
presenter_name: s.participant.name,
presenter_twitter_handle: s.participant.twitter_handle,
presenter_github_username: s.participant.github_profile_username,
presenter_github_og_image: s.participant.github_og_image,
session_title: s.title,
summary: s.summary,
description: s.description,
room_name: s.room_name,
panel: s.panel,
projector: s.projector,
starts_at: s.starts_at,
level_name: s.level_name,
categories: s.categories.map(&:name),
other_presenter_names: s.other_presenter_names,
other_presenter_ids: s.other_presenters.map(&:id),
attendance_count: s.attendances.count,
created_at: s.created_at.utc,
updated_at: s.updated_at.utc
}
end

def to_json(sessions)
require 'json'
JSON.pretty_generate(sessions.map(&:to_h))
end

end
2 changes: 1 addition & 1 deletion src/lib/tasks/app.rake
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ namespace :app do
participant.email = FFaker::Internet.safe_email
participant.password = 'standard'
participant.bio = FFaker::Lorem.paragraph if [true, false].sample
participant.twitter_handle = Faker::Internet.user_name(participant.name) if [true, false].sample
participant.twitter_handle = FFaker::Internet.user_name(participant.name) if [true, false].sample
participant.save!
progress.increment
end
Expand Down
1 change: 1 addition & 0 deletions src/spec/controllers/presentations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

describe PresentationsController do
let(:session) { create(:session) }

describe "#index" do
it "should be successful" do
get :index, session_id: session
Expand Down
18 changes: 17 additions & 1 deletion src/spec/controllers/sessions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@
end
end

describe "export" do
it "should be successful" do
get :export
expect(response).to be_successful
expect(assigns[:sessions]).to eq [session]
end
end
describe "popularity" do
it "should be successful" do
get :popularity
expect(response).to be_successful
expect(assigns[:sessions]).to eq [session]
end
end

describe "index" do
it "should be successful" do
get :index
Expand All @@ -46,10 +61,11 @@
end

context "with JSON format" do
it "is successful" do
it "is successful and have all the things" do
get :index, format: :json
expect(response).to be_successful
expect(response.content_type).to eq('application/json')
expect(response.body).to eq SessionsJsonBuilder.new.to_json([session])
end
end
end
Expand Down
36 changes: 36 additions & 0 deletions src/spec/models/sessions_json_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "spec_helper"

describe SessionsJsonBuilder do
let(:event) { create(:event) }
let(:session) { create(:session, event: event) }

describe "to_hash" do
subject { SessionsJsonBuilder.new }
it "should have all the attributes" do
h = subject.to_hash(session)

expect(h[:id]).to be session.id
expect(h[:participant_id]).to be session.participant_id

expect(h[:presenter_name]).to be session.participant.name
expect(h[:presenter_twitter_handle]).to be session.participant.twitter_handle
expect(h[:presenter_github_username]).to be session.participant.github_profile_username
expect(h[:presenter_github_og_image]).to be session.participant.github_og_image
expect(h[:session_title]).to be session.title
expect(h[:summary]).to be session.summary
expect(h[:description]).to be session.description
expect(h[:room_name]).to be session.room_name
expect(h[:panel]).to be session.panel
expect(h[:projector]).to be session.projector
expect(h[:starts_at]).to be session.starts_at
expect(h[:level_name]).to be session.level_name
expect(h[:categories]).to match_array session.categories.map(&:name)
expect(h[:other_presenter_names]).to match_array session.other_presenter_names
expect(h[:other_presenter_ids]).to match_array session.other_presenters.map(&:id)
expect(h[:attendance_count]).to be session.attendances.count
expect(h[:created_at]).to be session.created_at.utc
expect(h[:updated_at]).to be session.updated_at.utc

end
end
end

0 comments on commit b821bf7

Please sign in to comment.