From 346ccf644960aa1d65e572082eda669e3eed756a Mon Sep 17 00:00:00 2001 From: Meutel Date: Sun, 5 Jul 2015 13:01:47 +0200 Subject: [PATCH] download book refactoring cover, layouts --- app/assets/javascripts/application.js | 1 + app/assets/javascripts/data.coffee | 3 +++ app/assets/stylesheets/data.scss | 3 +++ app/controllers/books_controller.rb | 6 ++++++ app/controllers/data_controller.rb | 9 +++++++++ app/helpers/data_helper.rb | 2 ++ app/models/book.rb | 2 ++ app/models/book_data_link.rb | 7 +++++++ app/views/books/show.html.erb | 17 +++++++++++------ app/views/layouts/_book_dl.html.erb | 15 +++++++++++++++ app/views/layouts/_dl_link.html.erb | 10 ++++++++++ config/routes.rb | 5 ++++- test/controllers/data_controller_test.rb | 7 +++++++ test/fixtures/book_data_links.yml | 11 +++++++++++ test/models/book_data_link_test.rb | 7 +++++++ 15 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 app/assets/javascripts/data.coffee create mode 100644 app/assets/stylesheets/data.scss create mode 100644 app/controllers/data_controller.rb create mode 100644 app/helpers/data_helper.rb create mode 100644 app/models/book_data_link.rb create mode 100644 app/views/layouts/_book_dl.html.erb create mode 100644 app/views/layouts/_dl_link.html.erb create mode 100644 test/controllers/data_controller_test.rb create mode 100644 test/fixtures/book_data_links.yml create mode 100644 test/models/book_data_link_test.rb diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index e07c5a8..7465856 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -13,4 +13,5 @@ //= require jquery //= require jquery_ujs //= require turbolinks +//= require bootstrap-sprockets //= require_tree . diff --git a/app/assets/javascripts/data.coffee b/app/assets/javascripts/data.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/data.coffee @@ -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/ diff --git a/app/assets/stylesheets/data.scss b/app/assets/stylesheets/data.scss new file mode 100644 index 0000000..36909b2 --- /dev/null +++ b/app/assets/stylesheets/data.scss @@ -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/ diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index 6a4cb72..888f9c9 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -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 diff --git a/app/controllers/data_controller.rb b/app/controllers/data_controller.rb new file mode 100644 index 0000000..74de2d6 --- /dev/null +++ b/app/controllers/data_controller.rb @@ -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 diff --git a/app/helpers/data_helper.rb b/app/helpers/data_helper.rb new file mode 100644 index 0000000..e68f25e --- /dev/null +++ b/app/helpers/data_helper.rb @@ -0,0 +1,2 @@ +module DataHelper +end diff --git a/app/models/book.rb b/app/models/book.rb index d6868ea..7130beb 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -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 diff --git a/app/models/book_data_link.rb b/app/models/book_data_link.rb new file mode 100644 index 0000000..a32c94f --- /dev/null +++ b/app/models/book_data_link.rb @@ -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 diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb index be4b63f..45e24cc 100644 --- a/app/views/books/show.html.erb +++ b/app/views/books/show.html.erb @@ -1,8 +1,13 @@
<% if @book.has_cover? %> @@ -14,7 +19,7 @@ <% if @book.serie %> @@ -57,7 +62,7 @@
<% if @book.has_cover? %>
- No cover"/> + <%= image_tag(cover_book_path(@book), alt: "No cover", class: "img-responsive img-rounded") %>
<% end %> diff --git a/app/views/layouts/_book_dl.html.erb b/app/views/layouts/_book_dl.html.erb new file mode 100644 index 0000000..a7793a9 --- /dev/null +++ b/app/views/layouts/_book_dl.html.erb @@ -0,0 +1,15 @@ +<% if @book.data.count == 1 %> + <%= render partial: 'layouts/dl_link', locals: { d: @book.data.first } %> +<% elsif @book.data.count > 1 %> +
+ + +
+<% end %> diff --git a/app/views/layouts/_dl_link.html.erb b/app/views/layouts/_dl_link.html.erb new file mode 100644 index 0000000..3375040 --- /dev/null +++ b/app/views/layouts/_dl_link.html.erb @@ -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 %> + Download +<% end %> + <%= d.format %> (<%= number_to_human_size(d.uncompressed_size) %>) +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 86a06d9..de6af97 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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' diff --git a/test/controllers/data_controller_test.rb b/test/controllers/data_controller_test.rb new file mode 100644 index 0000000..9154fdf --- /dev/null +++ b/test/controllers/data_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class DataControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/book_data_links.yml b/test/fixtures/book_data_links.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/book_data_links.yml @@ -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 diff --git a/test/models/book_data_link_test.rb b/test/models/book_data_link_test.rb new file mode 100644 index 0000000..3b114b0 --- /dev/null +++ b/test/models/book_data_link_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class BookDataLinkTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end