Skip to content
Draft
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
3 changes: 3 additions & 0 deletions app/assets/javascripts/books.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/news_papers.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/books.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Books controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/news_papers.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the NewsPapers controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
84 changes: 84 additions & 0 deletions app/assets/stylesheets/scaffolds.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
body {
background-color: #fff;
color: #333;
margin: 33px;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

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;

&:visited {
color: #666;
}

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

th {
padding-bottom: 5px;
}

td {
padding: 0 5px 7px;
}

div {
&.field, &.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;

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

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

label {
display: block;
}
74 changes: 74 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class BooksController < ApplicationController
before_action :set_book, only: [:show, :edit, :update, :destroy]

# GET /books
# GET /books.json
def index
@books = Book.in_any_status
end

# GET /books/1
# GET /books/1.json
def show
end

# GET /books/new
def new
@book = Book.new
end

# GET /books/1/edit
def edit
end

# POST /books
# POST /books.json
def create
@book = Book.new(book_params)

respond_to do |format|
if @book.save
format.html { redirect_to @book, notice: 'Book was successfully created.' }
format.json { render :show, status: :created, location: @book }
else
format.html { render :new }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /books/1
# PATCH/PUT /books/1.json
def update
respond_to do |format|
if @book.update(book_params)
format.html { redirect_to @book, notice: 'Book was successfully updated.' }
format.json { render :show, status: :ok, location: @book }
else
format.html { render :edit }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
end

# DELETE /books/1
# DELETE /books/1.json
def destroy
@book.destroy
respond_to do |format|
format.html { redirect_to books_url, notice: 'Book was successfully destroyed.' }
format.json { head :no_content }
end
end

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

# Never trust parameters from the scary internet, only allow the white list through.
def book_params
params.require(:book).permit(:title, :status)
end
end
74 changes: 74 additions & 0 deletions app/controllers/news_papers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class NewsPapersController < ApplicationController
before_action :set_news_paper, only: [:show, :edit, :update, :destroy]

# GET /news_papers
# GET /news_papers.json
def index
@news_papers = NewsPaper.in_any_status
end

# GET /news_papers/1
# GET /news_papers/1.json
def show
end

# GET /news_papers/new
def new
@news_paper = NewsPaper.new
end

# GET /news_papers/1/edit
def edit
end

# POST /news_papers
# POST /news_papers.json
def create
@news_paper = NewsPaper.new(news_paper_params)

respond_to do |format|
if @news_paper.save
format.html { redirect_to @news_paper, notice: 'News paper was successfully created.' }
format.json { render :show, status: :created, location: @news_paper }
else
format.html { render :new }
format.json { render json: @news_paper.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /news_papers/1
# PATCH/PUT /news_papers/1.json
def update
respond_to do |format|
if @news_paper.update(news_paper_params)
format.html { redirect_to @news_paper, notice: 'News paper was successfully updated.' }
format.json { render :show, status: :ok, location: @news_paper }
else
format.html { render :edit }
format.json { render json: @news_paper.errors, status: :unprocessable_entity }
end
end
end

# DELETE /news_papers/1
# DELETE /news_papers/1.json
def destroy
@news_paper.destroy
respond_to do |format|
format.html { redirect_to news_papers_url, notice: 'News paper was successfully destroyed.' }
format.json { head :no_content }
end
end

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

# Never trust parameters from the scary internet, only allow the white list through.
def news_paper_params
params.require(:news_paper).permit(:title, :status)
end
end
2 changes: 2 additions & 0 deletions app/helpers/books_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module BooksHelper
end
2 changes: 2 additions & 0 deletions app/helpers/news_papers_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module NewsPapersHelper
end
8 changes: 8 additions & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Book < ApplicationRecord
include Useful

validates :title, presence: true
validates :status, presence: true

enum status: { draft: 0, published: 1 }
end
11 changes: 11 additions & 0 deletions app/models/concerns/draftable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Draftable
extend ActiveSupport::Concern

included do
before_save { check_and_change_status }
end

def check_and_change_status
self.status = 'draft' if self.title.include?('::draft')
end
end
9 changes: 9 additions & 0 deletions app/models/concerns/findable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Findable
extend ActiveSupport::Concern

class_methods do
def in_any_status
where(status: statuses.keys)
end
end
end
5 changes: 5 additions & 0 deletions app/models/concerns/useful.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Useful
extend ActiveSupport::Concern
include Findable
include Draftable
end
8 changes: 8 additions & 0 deletions app/models/news_paper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class NewsPaper < ApplicationRecord
include Useful

validates :title, presence: true
validates :status, presence: true

enum status: { draft: 0, published: 1 }
end
2 changes: 2 additions & 0 deletions app/views/books/_book.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! book, :id, :title, :status, :created_at, :updated_at
json.url book_url(book, format: :json)
27 changes: 27 additions & 0 deletions app/views/books/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%= form_with(model: book, local: true) do |form| %>
<% if book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(book.errors.count, "error") %> prohibited this book from being saved:</h2>

<ul>
<% book.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 :status %>
<%= form.select :status, Book.statuses.keys %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/books/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing Book</h1>

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

<%= link_to 'Show', @book %> |
<%= link_to 'Back', books_path %>
29 changes: 29 additions & 0 deletions app/views/books/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<p id="notice"><%= notice %></p>

<h1>Books</h1>

<table>
<thead>
<tr>
<th>Title</th>
<th>Status</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @books.each do |book| %>
<tr>
<td><%= book.title %></td>
<td><%= book.status %></td>
<td><%= link_to 'Show', book %></td>
<td><%= link_to 'Edit', edit_book_path(book) %></td>
<td><%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Book', new_book_path %>
1 change: 1 addition & 0 deletions app/views/books/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @books, partial: 'books/book', as: :book
5 changes: 5 additions & 0 deletions app/views/books/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New Book</h1>

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

<%= link_to 'Back', books_path %>
14 changes: 14 additions & 0 deletions app/views/books/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Title:</strong>
<%= @book.title %>
</p>

<p>
<strong>Status:</strong>
<%= @book.status %>
</p>

<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Back', books_path %>
1 change: 1 addition & 0 deletions app/views/books/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "books/book", book: @book
Loading