Skip to content

Commit

Permalink
Update to Rails 5 exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisma committed Oct 22, 2018
1 parent 24f6f21 commit 32b7c89
Show file tree
Hide file tree
Showing 31 changed files with 636 additions and 17 deletions.
2 changes: 2 additions & 0 deletions app/assets/javascripts/authors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2 changes: 2 additions & 0 deletions app/assets/javascripts/papers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/authors.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/
4 changes: 4 additions & 0 deletions app/assets/stylesheets/papers.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/
80 changes: 80 additions & 0 deletions app/assets/stylesheets/scaffold.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
body {
background-color: #fff;
color: #333;
margin: 33px;
}

body, p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a {
color: #000;
}

a:visited {
color: #666;
}

a:hover {
color: #fff;
background-color: #000;
}

th {
padding-bottom: 5px;
}

td {
padding: 0 5px 7px;
}

div.field,
div.actions {
margin-bottom: 10px;
}

#notice {
color: green;
}

.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px 7px 0;
margin-bottom: 20px;
background-color: #f0f0f0;
}

#error_explanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px -7px 0;
background-color: #c00;
color: #fff;
}

#error_explanation ul li {
font-size: 12px;
list-style: square;
}

label {
display: block;
}
54 changes: 54 additions & 0 deletions app/controllers/authors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# A frequent practice is to place the standard CRUD actions in each controller in the following order:
# index, show, new, edit, create, update and destroy.
# You may use any order you choose, but keep in mind that these are public methods;
# they must be placed before declaring private visibility in the controller.

class AuthorsController < ApplicationController
def index
@authors = Author.all
end

def show
@author = Author.find(params[:id])
end

def new
@author = Author.new
end

def edit
@author = Author.find(params[:id])
end

def create
@author = Author.new(author_params)

if @author.save
redirect_to @author
else
render 'new'
end
end

def update
@author = Author.find(params[:id])

if @author.update(author_params)
redirect_to @author
else
render 'edit'
end
end

def destroy
@author = Author.find(params[:id])
@author.destroy

redirect_to authors_path
end

private
def author_params
params.require(:author).permit(:first_name, :last_name, :homepage)
end
end
59 changes: 59 additions & 0 deletions app/controllers/papers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class PapersController < ApplicationController
before_action :set_paper, only: [:show, :edit, :update, :destroy]

# GET /papers
def index
@papers = Paper.all
@papers = @papers.published_in params[:year] if params[:year]
end

# GET /papers/1
def show
end

# GET /papers/new
def new
@paper = Paper.new
end

# GET /papers/1/edit
def edit
end

# POST /papers
def create
@paper = Paper.new(paper_params)

if @paper.save
redirect_to @paper, notice: 'Paper was successfully created.'
else
render :new
end
end

# PATCH/PUT /papers/1
def update
if @paper.update(paper_params)
redirect_to @paper, notice: 'Paper was successfully updated.'
else
render :edit
end
end

# DELETE /papers/1
def destroy
@paper.destroy
redirect_to papers_url, notice: 'Paper was successfully destroyed.'
end

private
# Use callbacks to share common setup or constraints between actions.
def set_paper
@paper = Paper.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def paper_params
params.require(:paper).permit(:title, :venue, :year, :author_ids => [])
end
end
2 changes: 2 additions & 0 deletions app/helpers/authors_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module AuthorsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/papers_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PapersHelper
end
11 changes: 11 additions & 0 deletions app/models/author.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Author < ApplicationRecord
#Validations
#validates :last_name, presence: true

# Relations
has_and_belongs_to_many :papers

def name
"#{self.first_name} #{self.last_name}"
end
end
12 changes: 12 additions & 0 deletions app/models/paper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Paper < ApplicationRecord
#Scopes
scope :published_in, ->(year) { where("year == ?", year)}

#Validations
#validates :title, presence: true
#validates :venue, presence: true
#validates :year, numericality: true, presence: true

# Relations
has_and_belongs_to_many :authors
end
28 changes: 28 additions & 0 deletions app/views/authors/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1>Edit author</h1>

<%= form_with(model: @author, local: true) do |form| %>

<% if @author.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@author.errors.count, "error") %> prohibited this author from being saved:</h2>
<ul>
<% @author.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<% [:first_name, :last_name, :homepage].each do |attribute| %>
<p>
<strong><%= Author.human_attribute_name(attribute) %>:</strong>
<br>
<%= form.text_field attribute %>
</p>
<% end %>

<p><%= form.submit %></p>

<% end %>

<%= link_to 'Back to author index', authors_path %>
31 changes: 31 additions & 0 deletions app/views/authors/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<h1>Author Index</h1>

<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>

<table>
<tr>
<th>full_name</th>
<th>homepage</th>
<th></th>
</tr>

<% @authors.each do |author| %>
<tr>
<td><%= author.name %></td>
<td><%= author.homepage %></td>
<td><%= link_to 'Show', author_path(author) %>
<%= link_to 'Edit', edit_author_path(author) %>
<%= link_to 'Delete', author_path(author), method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
</tr>
<% end %>
</table>

<p>
<%= link_to 'Add new author', new_author_path %>
</p>
26 changes: 26 additions & 0 deletions app/views/authors/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<h1>New Author</h1>

<%= form_with scope: :author, url: authors_path, local: true do |form| %>
<%# Show validation errors%>
<% if @author.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@author.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% @author.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<% [:first_name, :last_name, :homepage].each do |attr| %>
<p>
<%= form.label attr %><br>
<%= form.text_field attr %>
</p>
<% end %>

<p>
<%= form.submit %>
</p>
<% end %>
9 changes: 9 additions & 0 deletions app/views/authors/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>Author Details: <%= @author.name %></h1>

<% [:first_name, :last_name, :homepage].each do |attribute| %>
<p>
<strong><%= Author.human_attribute_name(attribute) %>:</strong>
<br>
<%= @author.send(attribute) %>
</p>
<% end %>
3 changes: 2 additions & 1 deletion app/views/home/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<p id="notice"><%= notice %></p>

<h1>views/home/index.html.erb</h1>
<h1>A System</h1>
<%# link_to 'authors', authors_path %>
42 changes: 42 additions & 0 deletions app/views/papers/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<%= form_with(model: paper, local: true) do |form| %>
<% if paper.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(paper.errors.count, "error") %> prohibited this paper from being saved:</h2>

<ul>
<% paper.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>

<div class="field">
<%= form.label :venue %>
<%= form.text_field :venue %>
</div>

<div class="field">
<%= form.label :year %>
<%= form.number_field :year %>
</div>

<div class="field">
<%= form.label 'Authors' %>
<%= form.select 'author_ids',
options_from_collection_for_select(Author.all, :id, :name, paper.author_ids),
{},
multiple: true, size: 10 %>
</div>

<div class="actions">
<%= form.submit %>
</div>


<% end %>
6 changes: 6 additions & 0 deletions app/views/papers/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing Paper</h1>

<%= render 'form', paper: @paper %>

<%= link_to 'Show', @paper %> |
<%= link_to 'Back', papers_path %>
Loading

0 comments on commit 32b7c89

Please sign in to comment.