Skip to content
Browse files

add relation between books and users

  • Loading branch information...
1 parent 982b5b1 commit bdf12744aadcaa8e737f19ba7ed924e1252405ff @FuriKuri committed Nov 8, 2012
View
5 app/assets/stylesheets/books.css.scss
@@ -35,6 +35,11 @@
padding-left: 1em;
}
+ .list_owners {
+ font-size: x-small;
+ padding-left: 1em;
+ }
+
.list_image {
width: 60px;
height: 70px;
View
1 app/controllers/application_controller.rb
@@ -1,6 +1,7 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+ protected
def authorize
unless User.find_by_id(session[:user_id])
redirect_to login_url, notice: 'Please log in'
View
2 app/controllers/books_controller.rb
@@ -42,6 +42,8 @@ def edit
# POST /books.json
def create
@book = Book.new(params[:book])
+ @book.owner_id = session[:user_id]
+ @book.lent_to_user_id = session[:user_id]
respond_to do |format|
if @book.save
View
14 app/models/book.rb
@@ -1,10 +1,22 @@
class Book < ActiveRecord::Base
attr_accessible :description, :image_url, :lent_to_user_id, :owner_id, :title
- validates_presence_of :description, :title
+ validates_presence_of :description, :title, :image_url, :owner_id, :lent_to_user_id
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)$}i,
message: 'must be a URL for GIF, JPG or PNG image.'
}
+
+ before_validation :set_default_image
+
+ belongs_to :owner, :class_name => 'User', :foreign_key => 'owner_id'
+ belongs_to :lent_to, :class_name => 'User', :foreign_key => 'lent_to_user_id'
+
+ private
+ def set_default_image
+ if !self.image_url or self.image_url.empty?
+ self.image_url = 'no_image.png'
+ end
+ end
end
View
4 app/models/user.rb
@@ -1,6 +1,10 @@
class User < ActiveRecord::Base
attr_accessible :name, :password, :password_confirmation
validates :name, presence: true, uniqueness: true
+ validates_length_of :name, :within => 3..40
+ validates_length_of :password, :within => 5..40
has_secure_password
+ has_many :owner_books, :class_name => 'Book', :foreign_key => 'owner_id'
+ has_many :lent_to_books, :class_name => 'Book', :foreign_key => 'lent_to_user_id'
end
View
8 app/views/books/_form.html.erb
@@ -23,14 +23,6 @@
<%= f.label :image_url %><br />
<%= f.text_field :image_url %>
</div>
- <div class="field">
- <%= f.label :owner_id %><br />
- <%= f.number_field :owner_id %>
- </div>
- <div class="field">
- <%= f.label :lent_to_user_id %><br />
- <%= f.number_field :lent_to_user_id %>
- </div>
<div class="actions">
<%= f.submit %>
</div>
View
7 app/views/books/index.html.erb
@@ -12,6 +12,13 @@
</td>
+ <td class="list_owners">
+ Owner:
+ <%= book.owner.name %> <br/>
+ Is lent to:
+ <%= book.lent_to.name %>
+ </td>
+
<td class="list_actions">
<%= link_to 'Show', book %> <br/>
<%= link_to 'Edit', edit_book_path(book) %> <br/>
View
40 test/unit/book_test.rb
@@ -1,9 +1,13 @@
require 'test_helper'
class BookTest < ActiveSupport::TestCase
+ fixtures :books
+
test "should save book" do
book = Book.new
book.title = 'Clean Code'
+ book.owner_id = users(:dave).id
+ book.lent_to_user_id = users(:tom).id
book.image_url = 'cleancode.jpg'
book.description = 'Clean Code book form Robert C. Martin'
assert book.valid?
@@ -12,19 +16,55 @@ class BookTest < ActiveSupport::TestCase
test "should not save book with no title" do
book = Book.new
book.description = 'Clean Code book form Robert C. Martin'
+ book.owner_id = users(:dave).id
+ book.lent_to_user_id = users(:tom).id
+ book.image_url = 'cleancode.jpg'
assert book.invalid?
end
test "should not save book with no description" do
book = Book.new
book.title = 'Clean Code'
+ book.owner_id = users(:dave).id
+ book.lent_to_user_id = users(:tom).id
+ book.image_url = 'cleancode.jpg'
assert book.invalid?
end
test "should not save book with invalid image url" do
book = Book.new
book.title = 'Clean Code'
book.image_url = 'wrong_image'
+ book.lent_to_user_id = users(:tom).id
+ book.owner_id = users(:dave).id
+ book.description = 'Clean Code book form Robert C. Martin'
+ assert book.invalid?
+ end
+
+ test "set default value for image url" do
+ book = Book.new
+ book.title = 'Clean Code'
+ book.lent_to_user_id = users(:tom).id
+ book.owner_id = users(:dave).id
+ book.description = 'Clean Code book form Robert C. Martin'
+ assert book.valid?
+ assert_equal('no_image.png', book.image_url)
+ end
+
+ test "should not save book without owner" do
+ book = Book.new
+ book.title = 'Clean Code'
+ book.lent_to_user_id = users(:tom).id
+ book.image_url = 'cleancode.jpg'
+ book.description = 'Clean Code book form Robert C. Martin'
+ assert book.invalid?
+ end
+
+ test "should not save book without lent to user" do
+ book = Book.new
+ book.title = 'Clean Code'
+ book.owner_id = users(:dave).id
+ book.image_url = 'cleancode.jpg'
book.description = 'Clean Code book form Robert C. Martin'
assert book.invalid?
end
View
63 test/unit/user_test.rb
@@ -1,7 +1,64 @@
require 'test_helper'
class UserTest < ActiveSupport::TestCase
- # test "the truth" do
- # assert true
- # end
+ fixtures :users
+
+ test "should save user" do
+ user = User.new
+ user.name = 'hans'
+ user.password = 'secret'
+ assert user.valid?
+ end
+
+ test "should not save user with no name" do
+ user = User.new
+ user.password = 'secret'
+ assert user.invalid?
+ end
+
+ test "should not save user with no password" do
+ user = User.new
+ user.name = 'hans'
+ assert user.invalid?
+ end
+
+ test "should not save user with too short name" do
+ user = User.new
+ user.name = 'u'
+ user.password = 'secret'
+ assert user.invalid?
+ end
+
+
+ test "should not save user with too long name" do
+ user = User.new
+ name = ''
+ 41.times {name += 'u'}
+ user.name = name
+ user.password = 'secret'
+ assert user.invalid?
+ end
+
+ test "should not save user with too short password" do
+ user = User.new
+ user.name = 'hans'
+ user.password = 'pw'
+ assert user.invalid?
+ end
+
+ test "should not save user with too long password" do
+ user = User.new
+ user.name = 'hans'
+ pw = ''
+ 41.times {pw += 'p'}
+ user.password = pw
+ assert user.invalid?
+ end
+
+ test "should not save user with an existing name" do
+ user = User.new
+ user.name = 'dave'
+ user.password = 'secret'
+ assert user.invalid?
+ end
end

0 comments on commit bdf1274

Please sign in to comment.
Something went wrong with that request. Please try again.