Skip to content

Commit

Permalink
Merge pull request #66 from flyinggrizzly/events-have-multiple-years
Browse files Browse the repository at this point in the history
#36 - An Event has many years
  • Loading branch information
nodunayo committed Feb 22, 2018
2 parents e892a01 + 01ca04f commit 8daa9a7
Show file tree
Hide file tree
Showing 43 changed files with 529 additions and 188 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/tmp/*
!/log/.keep
!/tmp/.keep
examples.txt

# Ignore Byebug command history file.
.byebug_history
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ All of the tests can be run with:
bundle exec rake
```

or to run only the failing (Rspec) examples

```bash
bundle exec rspec --only-failures
```

To set up the test environment, run:

```bash
Expand Down
20 changes: 19 additions & 1 deletion app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,32 @@ ul .inline {
margin-top: 40px;
}

select#submission_event_id {
select#submission_event_instance_id {
display: block;
}

.g-recaptcha {
margin-bottom: 1.5rem;
}

/**
* .fieldset redeclares some Normalize styles in a .fieldset class
* so they can be used to override Skeleton styles
*/

fieldset.fieldset {
border: 1px solid #c0c0c0;
border-radius: 3px;
margin: 0 2px;
margin-bottom: 15px;
padding: 0.5em 0.8em 0.75em;
}

fieldset.fieldset legend {
border: 0; /* 1 */
padding: .2em;
}

@media only screen and (min-width: 1000px) {
.align-right {
position: absolute;
Expand Down
25 changes: 25 additions & 0 deletions app/controllers/event_instances_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class EventInstancesController < ApplicationController
def new
@event_instance = EventInstance.new
end

def create
@event_instance = EventInstance.new(event_instance_params)
if verify_recaptcha(model: @event_instance) && @event_instance.save
redirect_to speakers_path
else
flash[:alert] = 'Failed to save event'
redirect_to new_event_instance_path
end
end

def show
@event_instance = EventInstance.find(params[:id])
end

private

def event_instance_params
params.require(:event_instance).permit(:event_id, :new_parent_event_name, :year)
end
end
21 changes: 1 addition & 20 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
class EventsController < ApplicationController
def new
@event = Event.new
end

def create
@event = Event.new(event_params)
if verify_recaptcha(model: @event) && @event.save
redirect_to speakers_path
else
flash[:alert] = 'Failed to save event'
redirect_to new_event_path
end
end

def index
@events = Event.all.order('name ASC, year ASC')
@events = Event.all.order('name ASC')
end

def show
@event = Event.find(params[:id])
end

private

def event_params
params.require(:event).permit(:name, :year)
end
end
4 changes: 2 additions & 2 deletions app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class SubmissionsController < ApplicationController
def new
@proposal = Proposal.find(params[:proposal])
@submission = Submission.new(proposal_id: @proposal.id)
@events = Event.all.order('name ASC, year ASC')
@events = Event.all.order('name ASC')
end

def create
Expand All @@ -18,6 +18,6 @@ def create
private

def submission_params
params.require(:submission).permit(:event_id, :proposal_id, :result).to_h
params.require(:submission).permit(:event_instance_id, :proposal_id, :result).to_h
end
end
14 changes: 5 additions & 9 deletions app/models/event.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
class Event < ApplicationRecord
validates_presence_of :name
validates_presence_of :year
validates_numericality_of :year, only_integer: true
validates_uniqueness_of :name, scope: :year
validates :name, presence: true, uniqueness: { case_insensitive: true }

has_many :submissions

def name_and_year
"#{name} #{year}"
end
has_many :instances, foreign_key: :event_id,
class_name: 'EventInstance',
dependent: :destroy
has_many :submissions, through: :instances
end
41 changes: 41 additions & 0 deletions app/models/event_instance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class EventInstance < ApplicationRecord
# Virtual attribute for the parent event's name in case it is new
attr_accessor :new_parent_event_name

belongs_to :event
has_many :submissions

default_scope { order :year }

before_validation :set_parent_event

validates :event, presence: true
validates :year, uniqueness: { scope: :event, message: 'should happen once per year' },
numericality: { only_integer: true }

def name_and_year
"#{event.name} #{year}"
end

private

def set_parent_event
if received_bad_event_params
errors.add(:base, 'choose either an existing event or to create a new one')
else
event || create_event!(name: new_parent_event_name)
end
end

def received_bad_event_params
received_both_new_and_existing_events || received_neither_new_nor_existing_events
end

def received_both_new_and_existing_events
event && new_parent_event_name.present?
end

def received_neither_new_nor_existing_events
event.nil? && new_parent_event_name.blank?
end
end
4 changes: 2 additions & 2 deletions app/models/submission.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Submission < ApplicationRecord
belongs_to :event
belongs_to :event_instance
belongs_to :proposal

validates_presence_of :event
validates_presence_of :event_instance
validates_presence_of :proposal
validates_presence_of :result

Expand Down
32 changes: 32 additions & 0 deletions app/views/event_instances/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<h3>Add an event</h3>

<%= form_for @event_instance do |f| %>

<div class="row">
<fieldset class="fieldset">
<legend>Event</legend>
<%= f.label :event, "Choose an existing event" %>
<%= f.collection_select(:event_id, Event.all, :id, :name, prompt: "Add year to an existing event") %>

<p>&mdash; or &mdash;</p>

<%= f.label :new_parent_event_name, 'Name a new event' %>
<%= f.text_field :new_parent_event_name %>
</fieldset>
</div>

<div class="row">
<%= f.label :year %>
<%= f.text_field :year %>
</div>

<div class="row">
<%= recaptcha_tags %>
</div>

<div class="row">
<%= f.submit 'Add event' %>
</div>
<% end %>

<p><%= link_to 'Back to Speaker Directory', speakers_path %></p>
4 changes: 4 additions & 0 deletions app/views/event_instances/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h3><%= link_to @event_instance.event.name, @event_instance.event %> <%= @event_instance.year %></h3>

<h5>Proposals</h5>
<%= render partial: 'shared/instance_submissions', locals: { instance: @event_instance } %>
2 changes: 1 addition & 1 deletion app/views/events/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h3>Conferences</h3>

<% @events.each do |event| %>
<p><%= link_to event.name_and_year, event %></p>
<p><%= link_to event.name, event %></p>
<% end %>
23 changes: 0 additions & 23 deletions app/views/events/new.html.erb

This file was deleted.

16 changes: 9 additions & 7 deletions app/views/events/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<h3><%= @event.name_and_year %></h3>
<h3><%= @event.name %></h3>

<h5>Proposals</h5>
<% @event.submissions.each do |submission| %>
<p>
<%= link_to submission.proposal.title, submission.proposal %> -
<%= link_to submission.proposal.speaker.name, submission.proposal.speaker %> -
<%= submission.result.capitalize %>
</p>

<%-# Output instances, with the most recent first -%>
<% instances = @event.instances.sort_by { |i| -i.year } %>
<% instances.each do |instance| %>
<div id="<%= @event.name.parameterize %>-<%= instance.year %>">
<h6><%= instance.year %></h6>
<%= render partial: 'shared/instance_submissions', locals: { instance: instance } %>
</div>
<% end %>
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<li><%= link_to 'Speakers', speakers_path %></li>
<li><%= link_to 'Add a speaker', new_speaker_path %></li>
<li><%= link_to 'Add a proposal', new_proposal_path %></li>
<li><%= link_to 'Add an event', new_event_path %></li>
<li><%= link_to 'Add an event', new_event_instance_path %></li>
</div>
</ul>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/proposals/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<h4>Submissions </h4>
<% @proposal.submissions.each do |submission| %>
<p>
<%= link_to submission.event.name_and_year, submission.event %> -
<%= link_to submission.event_instance.name_and_year, submission.event_instance %> -
<%= submission.result.capitalize %>
</p>
<% end %>
Expand Down
11 changes: 11 additions & 0 deletions app/views/shared/_instance_submissions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%-# Sort submissions by title before sorting by result -%>
<% submissions = instance.submissions.sort_by { |s| s.proposal.title } %>
<% submissions.sort_by(&:result).each do |submission| %>
<p>
<%= link_to submission.proposal.title, submission.proposal %> -
<%= link_to submission.proposal.speaker.name, submission.proposal.speaker %> -
<%= submission.result.capitalize %>
</p>
<% end %>

2 changes: 1 addition & 1 deletion app/views/submissions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<%= form_for @submission, url: {action: 'create'} do |f| %>
<%= f.hidden_field(:proposal_id, value: @proposal.id) %>
<%= f.collection_select(:event_id, @events, :id, :name_and_year) %>
<%= f.grouped_collection_select(:event_instance_id, @events, :instances, :name, :id, :name_and_year) %>
<%= f.label(:result_accepted, "Accepted") %>
<%= f.radio_button(:result, :accepted) %>
<%= f.label(:result_rejected, "Rejected") %>
Expand Down
7 changes: 4 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
root 'pages#homepage'

resources :speakers
resources :proposals, only: [:new, :create, :show, :edit, :update]
resources :submissions, only: [:new, :create]
resources :events
resources :proposals, only: [:new, :create, :show, :edit, :update]
resources :submissions, only: [:new, :create]
resources :events, only: [:index, :show]
resources :event_instances, only: [:new, :create, :show]
end
Loading

0 comments on commit 8daa9a7

Please sign in to comment.