-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#36 - An Event has many years #66
Merged
nodunayo
merged 1 commit into
nodunayo:master
from
flyinggrizzly:events-have-multiple-years
Feb 22, 2018
+529
−188
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
/tmp/* | ||
!/log/.keep | ||
!/tmp/.keep | ||
examples.txt | ||
|
||
# Ignore Byebug command history file. | ||
.byebug_history | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>— or —</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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } %> | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I've just learnt that partials have performance implications on Rails sites — for the worse! Interesting, eh? I'm sure it's not a problem for us now, but just interesting to know.