download book

refactoring cover, layouts
This commit is contained in:
Meutel 2015-07-05 13:01:47 +02:00
parent 5f7dd8514f
commit 346ccf6449
15 changed files with 98 additions and 7 deletions

View File

@ -13,4 +13,5 @@
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .

View File

@ -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/

View File

@ -0,0 +1,3 @@
// Place all the styles related to the data controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View File

@ -1,5 +1,6 @@
class BooksController < ApplicationController
include PreferencesHelper
include ERB::Util
before_action :preferences, only: :index
@ -18,4 +19,9 @@ class BooksController < ApplicationController
end
end
def cover
@book = Book.find(params[:id])
redirect_to "/calibre/#{url_encode(@book.path)}/cover.jpg"
end
end

View File

@ -0,0 +1,9 @@
class DataController < ApplicationController
include ERB::Util
def show
d = BookDataLink.find_by(book: params[:id], format: params[:data_format])
redirect_to "/calibre/#{url_encode(d.book.path)}/#{url_encode(d.name)}.#{d.format.downcase}"
end
end

View File

@ -0,0 +1,2 @@
module DataHelper
end

View File

@ -20,6 +20,8 @@ class Book < ActiveRecord::Base
has_one :book_serie_link, foreign_key: "book"
has_one :serie, through: :book_serie_link
has_many :data, class_name: "BookDataLink", foreign_key: "book"
def has_cover?
has_cover == 1
end

View File

@ -0,0 +1,7 @@
class BookDataLink < ActiveRecord::Base
establish_connection Rails.configuration.database_configuration["calibre"]
self.table_name = "data"
belongs_to :book, foreign_key: "book"
end

View File

@ -1,8 +1,13 @@
<div class="page-header">
<h1>
<span class="glyphicon glyphicon-book"></span>
<%= @book.title %>
</h1>
<div class="row">
<div class="col-xs-12 col-md-9">
<h1>
<span class="glyphicon glyphicon-book"></span>
<%= @book.title %>
</h1>
</div>
<div class="col-xs-12 col-md-3 text-right"> <%= render "layouts/book_dl" %> </div>
</div>
</div>
<div class="row">
<% if @book.has_cover? %>
@ -14,7 +19,7 @@
</h2>
<ul>
<% @book.authors.each do |author| %>
<li><%= author.name %></li>
<li><%= link_to author.name, author %></li>
<% end %>
</ul>
<% if @book.serie %>
@ -57,7 +62,7 @@
</div>
<% if @book.has_cover? %>
<div class="col-xs-12 col-md-6">
<img alt="No cover" class="img-responsive img-rounded" src="<%= "/calibre/#{url_encode(@book.path)}/cover.jpg" %>"/>
<%= image_tag(cover_book_path(@book), alt: "No cover", class: "img-responsive img-rounded") %>
</div>
<% end %>
</div>

View File

@ -0,0 +1,15 @@
<% if @book.data.count == 1 %>
<%= render partial: 'layouts/dl_link', locals: { d: @book.data.first } %>
<% elsif @book.data.count > 1 %>
<div class="btn-group">
<button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-download-alt"></span>
Download <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<% @book.data.each do |d| %>
<li><%= render partial: 'layouts/dl_link', locals: { d: d } %></li>
<% end %>
</ul>
</div>
<% end %>

View File

@ -0,0 +1,10 @@
<%
is_single = d.book.data.count == 1
btn_class = is_single ? "btn btn-success" : nil
%>
<%= link_to book_data_path(id: d.book, data_format: d.format), class: btn_class do %>
<% if is_single %>
<span class="glyphicon glyphicon-download-alt"></span> Download
<% end %>
<%= d.format %> (<%= number_to_human_size(d.uncompressed_size) %>)
<% end %>

View File

@ -2,7 +2,10 @@ Rails.application.routes.draw do
root 'static_pages#home'
get 'about' => 'static_pages#about'
resources :books, only: [ :index, :show ]
resources :books, only: [ :index, :show ] do
get 'cover', on: :member
end
get '/books/:id/data/:data_format/' => 'data#show', as: "book_data"
resources :authors, only: [ :index, :show ]
resources :series, only: [ :index, :show ]
#get 'series/:id' => 'series#show'

View File

@ -0,0 +1,7 @@
require 'test_helper'
class DataControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

11
test/fixtures/book_data_links.yml vendored Normal file
View File

@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value

View File

@ -0,0 +1,7 @@
require 'test_helper'
class BookDataLinkTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end