Skip to content
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

create book #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class BooksController < ApplicationController
def new
@books = Book.new
end

def create
@books = Book.new(book_params)
if @books.save
flash[:info] = I18n.t('es.flash.book.created')
redirect_to @books
else
render 'new'
end
end

def show
@books = Book.find(params[:id])
end

def book_params
params.require(:book).permit(:title)
end
end
2 changes: 2 additions & 0 deletions app/helpers/book_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module BookHelper
end
9 changes: 9 additions & 0 deletions app/views/books/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div>
<%= form_for(@books) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :title %>
<%= f.text_field :title, placeholder: true %>

<%= f.submit %>
<% end %>
</div>
4 changes: 4 additions & 0 deletions app/views/books/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<% flash.each do |message_type, message| %>
<%= content_tag(:div, message, class: "alert alert-#{message_type}") %>
<% end %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parece que las lineas 1 a 3 son las mismas que en app/views/recipes/show.html.erb
¿Se te ocurre como podríamos mejorarlo @Vollcode ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Podríamos hacer un extract method de ello, que te parece @Pardiez ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

me parece que para extraer un "método" habría que tener uno 😉
pero la idea es la misma: evitar esa repetición
De hecho me sé de un tutorial de Rails que te enseña a hacer una aplicación tipo twitter que tiene un ejemplo idéntico, con esas mismas lineas de código 😉 😉

<h1><%= @books.title %></h1>
1 change: 1 addition & 0 deletions app/views/static_pages/home.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<h1>Recetame App</h1>
<%= link_to t('button.recipe.create'), new_recipe_path %></a>
<%= link_to t('button.book.create'), new_book_path %></a>
<h2>Aquí están las últimas 10 recetas</h2>

<ul id="recipes" style= 'list-style: none'>
Expand Down
19 changes: 18 additions & 1 deletion config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,24 @@

es:
flash:
book:
created: "Su libro se ha creado"
recipe:
created: "Su receta se ha creado"
button:
book:
create: "Crear libro"
recipe:
create: "Crear receta"
activerecord:
errors:
models:
book:
attributes:
title:
too_short: "Titulo demasiado corto"
too_long: "Titulo demasiado largo"
blank: "Titulo en blanco, inserta un titulo"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Vollcode ¿has probado como se muestran estos errores al usuario?
¿Sabes si son necesarias estas lineas? @guzfdez hizo un comentario en el PR #3 interesante. Lo digo solo para que eches un ojo a las lineas 49 a 51 y entiendas como actúa Rails al buscar las cadenas de traducción

Según http://guides.rubyonrails.org/i18n.html

The key for the error message in this case is :blank. Active Record will look up this key in the namespaces:

activerecord.errors.models.[model_name].attributes.[attribute_name]
activerecord.errors.models.[model_name]
activerecord.errors.messages
errors.attributes.[attribute_name]
errors.messages

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, voy a echar un vistazo y ver como puedo mejorarlo.

recipe:
attributes:
title:
Expand All @@ -40,14 +50,21 @@ es:
too_long: "Demasiado largo"
blank: "Escribe algo"
models:
book:
one: "Libro"
other: "Libros"
recipe:
one: "Receta"
other: "Recetas"
attributes:
book:
title: "Nombre del Libro"
recipe:
title: "Nombre Receta"
title: "Nombre de la Receta"
helpers:
placeholder:
book:
title: "Escribe un título"
recipe:
title: "Escribe un título"
submit:
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Rails.application.routes.draw do
root 'static_pages#home'
#get 'recipes/new' => 'recipes#new'
resources :recipes
resources :recipes, :books

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
Expand Down
23 changes: 23 additions & 0 deletions test/integration/books_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'test_helper'

class BooksTest < ActionDispatch::IntegrationTest
test "invalid new book information" do
get new_book_path
assert_no_difference 'Book.count' do
post books_path, book: { title: "" }
end
assert_select 'div.alert-danger'
assert_template 'books/new'
end

test "valid new book information" do
get new_book_path
assert_difference 'Book.count', 1 do
post books_path, book: { title: "Arroces del mundo" }
end
follow_redirect!
assert_not flash.empty?
assert_select 'div.alert-info'
assert_template 'books/show'
end
end
22 changes: 22 additions & 0 deletions test/models/book_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

class BookTest < ActiveSupport::TestCase

def setup
@books = Book.new(title: "Curry con todo")
end

test "title should be present" do
@books.title = " "

assert_not @books.valid?
end

test "title should not be too short" do
@books.title = "a"*4

assert_not @books.valid?
end

test "title should not be too long" do
@books.title = "a"*101

assert_not @books.valid?
end

test "last_five should have 5 items" do
if Book.all.size >= 5
assert_equal Book.last_five.size, 5
Expand Down