mirror of
https://github.com/wahyd4/Libr.git
synced 2026-08-19 09:47:24 +10:00
add code climate
This commit is contained in:
@@ -6,7 +6,7 @@ tw-libr
|
||||
Build Status
|
||||
====
|
||||
[](https://travis-ci.org/wahyd4/Libr)
|
||||
|
||||
[](https://codeclimate.com/github/wahyd4/Libr)
|
||||
#### Export System variable
|
||||
|
||||
export CLIENT_ID='0858f96ac849895e190aec4058dc9c1a'
|
||||
|
||||
@@ -4,9 +4,9 @@ class BookController < ApplicationController
|
||||
|
||||
def view
|
||||
@book = Book.find_by_id params[:id]
|
||||
@can_borrow = @book.users.count > @book.current_borrowers.count
|
||||
@can_borrow = @book.book_instances.count > @book.current_borrowers.count
|
||||
@records =BorrowRecord.records_of @book
|
||||
@borrowers = @book.borrowers.uniq
|
||||
@borrowers = @book.total_borrowers.uniq
|
||||
@owners = @book.users.uniq
|
||||
end
|
||||
|
||||
|
||||
@@ -27,14 +27,14 @@ class UserController < ApplicationController
|
||||
when 'wanted'
|
||||
@books = nil
|
||||
else
|
||||
@books = @user.books.order 'id DESC'
|
||||
@books = @user.books
|
||||
end
|
||||
@query = query
|
||||
render :books
|
||||
end
|
||||
|
||||
def return_book
|
||||
records = BorrowRecord.where(user_id: params[:id],book_id: params[:book_id],return_date: nil)
|
||||
records = BorrowRecord.where(user_id: params[:id],book_instance_id: params[:book_id],return_date: nil)
|
||||
unless records.empty?
|
||||
records[0].return_book
|
||||
@message = 'Return book success.'
|
||||
|
||||
+20
-8
@@ -1,14 +1,26 @@
|
||||
class Book < ActiveRecord::Base
|
||||
attr_accessible :author, :image, :isbn, :name, :users ,:id
|
||||
attr_accessible :author, :image, :isbn, :name ,:id
|
||||
|
||||
has_many :user_to_books
|
||||
has_many :users, through: :user_to_books
|
||||
|
||||
has_many :borrow_records
|
||||
has_many :borrowers,:source => :user, through: :borrow_records
|
||||
has_many :book_instances
|
||||
has_many :users, through: :book_instances
|
||||
|
||||
def current_borrowers
|
||||
records = borrow_records.where(return_date: nil)
|
||||
records.map {|record| User.find_by_id record.user_id }
|
||||
borrowers = []
|
||||
book_instances.each{|instance|
|
||||
if instance.borrowed?
|
||||
borrowers << instance.current_borrower
|
||||
end
|
||||
}
|
||||
borrowers
|
||||
end
|
||||
|
||||
def total_borrowers
|
||||
borrowers = []
|
||||
book_instances.map{|instance|
|
||||
unless instance.borrowers.empty?
|
||||
borrowers << instance.borrowers
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
class BorrowRecord < ActiveRecord::Base
|
||||
attr_accessible :borrow_date, :return_date, :user_id, :book_id
|
||||
attr_accessible :borrow_date, :return_date, :user_id, :book_instance_id
|
||||
|
||||
belongs_to :user
|
||||
belongs_to :book
|
||||
belongs_to :book_instance
|
||||
|
||||
delegate :name, :avatar, to: :user,prefix: true
|
||||
|
||||
def self.records_of(book)
|
||||
records = BorrowRecord.includes(:user).where("book_id = ?", book.id).where(return_date:nil)
|
||||
records = BorrowRecord.includes(:user).where("book_instance_id = ?", book.id).where(return_date:nil)
|
||||
end
|
||||
|
||||
#def self.create_record(user_id,book_id)
|
||||
|
||||
+11
-7
@@ -1,28 +1,32 @@
|
||||
class User < ActiveRecord::Base
|
||||
attr_accessible :email, :name, :books, :avatar, :id
|
||||
attr_accessible :email, :name, :avatar, :id
|
||||
|
||||
validates_uniqueness_of :name
|
||||
|
||||
|
||||
has_many :user_to_books
|
||||
has_many :books, through: :user_to_books
|
||||
has_many :book_instances
|
||||
|
||||
has_many :borrow_records
|
||||
has_many :borrowed_books ,:class_name => 'Book',:source => :book, through: :borrow_records
|
||||
|
||||
has_many :borrowed_books ,:source => :book_instance, through: :borrow_records
|
||||
|
||||
|
||||
def self.create_user(name,avatar)
|
||||
def self.create_user(name,avatar)
|
||||
User.create name: name, avatar: avatar
|
||||
end
|
||||
|
||||
def borrow(book)
|
||||
record = BorrowRecord.create user_id: self.id, book_id: book.id, borrow_date: DateTime.now
|
||||
record = BorrowRecord.create user_id: self.id, book_instance_id: book.id, borrow_date: DateTime.now
|
||||
self.borrow_records << record
|
||||
end
|
||||
|
||||
def borrowed_and_not_returned_books
|
||||
records = borrow_records.where(return_date: nil).order('id DESC')
|
||||
records.map{ |record| record = Book.find_by_id record.book_id }
|
||||
records.map{ |record| record = Book.find_by_id record.book_instance_id }
|
||||
|
||||
end
|
||||
|
||||
def books
|
||||
book_instances.order('id DESC').map{|instance| Book.find_by_id instance.book_id }
|
||||
end
|
||||
end
|
||||
|
||||
+12
-4
@@ -11,7 +11,15 @@
|
||||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20130110095134) do
|
||||
ActiveRecord::Schema.define(:version => 20130123084128) do
|
||||
|
||||
create_table "book_instances", :force => true do |t|
|
||||
t.integer "user_id"
|
||||
t.integer "book_id"
|
||||
t.boolean "public", :default => true
|
||||
t.datetime "created_at", :null => false
|
||||
t.datetime "updated_at", :null => false
|
||||
end
|
||||
|
||||
create_table "books", :force => true do |t|
|
||||
t.string "name"
|
||||
@@ -25,9 +33,9 @@ ActiveRecord::Schema.define(:version => 20130110095134) do
|
||||
create_table "borrow_records", :force => true do |t|
|
||||
t.date "borrow_date"
|
||||
t.date "return_date"
|
||||
t.datetime "created_at", :null => false
|
||||
t.datetime "updated_at", :null => false
|
||||
t.integer "book_id"
|
||||
t.datetime "created_at", :null => false
|
||||
t.datetime "updated_at", :null => false
|
||||
t.integer "book_instance_id"
|
||||
t.integer "user_id"
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user