From c26a5edd62f918ac3335241c69dfa345bf3b1b1b Mon Sep 17 00:00:00 2001 From: astronaut-wannabe Date: Sun, 24 May 2020 15:56:17 -0700 Subject: [PATCH] Add a `position` field to books (#1593) * Add a `position` field to books backfill script: This script isn't that important as we can always manually fiddle with the positions in production ```ruby BOOK_SLUGS_IN_ORDER = %w[no-wall-they-can-build from-democracy-to-freedom contradictionary work expect-resistance recipes-for-disaster days-of-war-nights-of-love off-the-map no-habra-muro-que-nos-pare da-democracia-a-liberdade trabalho-edicao-resumida-de-emergencia espere-resistencia receitas-para-o-desastre dias-de-guerra-noites-de-amor].freeze BOOK_SLUGS_IN_ORDER.each_with_index do |slug, index| Book.find_by(slug: slug)&.update!(position: index) end ``` resolves https://github.com/crimethinc/website/issues/1446 --- app/controllers/admin/books_controller.rb | 2 +- app/controllers/books_controller.rb | 27 +------------------ app/models/book.rb | 2 ++ app/views/admin/books/_form.html.erb | 15 +++++++++++ app/views/admin/books/form/_position.html.erb | 4 +++ .../20200511024239_add_position_to_books.rb | 11 ++++++++ db/schema.rb | 4 ++- 7 files changed, 37 insertions(+), 28 deletions(-) create mode 100644 app/views/admin/books/form/_position.html.erb create mode 100644 db/migrate/20200511024239_add_position_to_books.rb diff --git a/app/controllers/admin/books_controller.rb b/app/controllers/admin/books_controller.rb index 97f2246c8..0f512993f 100644 --- a/app/controllers/admin/books_controller.rb +++ b/app/controllers/admin/books_controller.rb @@ -59,7 +59,7 @@ def book_params :print_black_and_white_a4_download_present, :print_color_a4_download_present, :print_color_download_present, :print_black_and_white_download_present, :screen_single_page_view_download_present, :screen_two_page_view_download_present, - :featured_status, :featured_at, :canonical_id) + :featured_status, :featured_at, :canonical_id, :position, :hide_from_index) end end end diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index 5ba2618de..e1fab858a 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -1,43 +1,18 @@ class BooksController < ApplicationController before_action :set_book, only: %i[show extras] - INDEX_BOOK_SLUGS = %w[ - no-wall-they-can-build - from-democracy-to-freedom - contradictionary - work - expect-resistance - recipes-for-disaster - days-of-war-nights-of-love - off-the-map - no-habra-muro-que-nos-pare - da-democracia-a-liberdade - trabalho-edicao-resumida-de-emergencia - espere-resistencia - receitas-para-o-desastre - dias-de-guerra-noites-de-amor - ].map(&:freeze).freeze - - NON_INDEX_BOOK_SLUGS = %w[ - days-of-war-nights-of-love-ne-plus-ultra-edition - ].map(&:freeze).freeze - - BOOK_SLUGS = INDEX_BOOK_SLUGS + NON_INDEX_BOOK_SLUGS - def index @html_id = 'page' @body_id = 'tools' @type = 'books' @title = PageTitle.new title_for(:books) - @books = INDEX_BOOK_SLUGS.map { |slug| Book.find_by(slug: slug) } + @books = Book.english.for_index render "#{Theme.name}/books/index" end def show - return redirect_to [:books] unless BOOK_SLUGS.include?(@book.slug) - @html_id = 'page' @body_id = 'tools' @type = 'books' diff --git a/app/models/book.rb b/app/models/book.rb index ad8552c96..6ae8f4fd7 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -2,6 +2,8 @@ class Book < ApplicationRecord include MultiPageTool include Featureable + scope :for_index, -> { where.not(hide_from_index: true).reorder(position: :asc).order(title: :asc) } + def ask_for_donation? downloads_available? end diff --git a/app/views/admin/books/_form.html.erb b/app/views/admin/books/_form.html.erb index cb53dddc3..61b7766cb 100644 --- a/app/views/admin/books/_form.html.erb +++ b/app/views/admin/books/_form.html.erb @@ -61,6 +61,21 @@ <%= render "admin/books/categories_form", form: form %> +
+
+ <%= render "admin/books/form/position", form: form %> +

This will set the order that + this book shows up on crimethinc.com/books.

+
+ +
+ <%= form.check_box :hide_from_index %> + <%= form.label :has_index, "Hide this book from the index page?" %>
+

This will prevent the book from + showing up on crimethinc.com/books.

+
+
+ <%= render "admin/books/downloads_form", form: form, resource: resource %> <%= render "admin/form_actions", cancel_url: [:admin, resource.namespace] %> diff --git a/app/views/admin/books/form/_position.html.erb b/app/views/admin/books/form/_position.html.erb new file mode 100644 index 000000000..cb0afa4de --- /dev/null +++ b/app/views/admin/books/form/_position.html.erb @@ -0,0 +1,4 @@ +
+ <%= form.label :position, "Position this book on the index page" %> + <%= number_field :book, :position, step: 1, class: 'form-control' %> +
diff --git a/db/migrate/20200511024239_add_position_to_books.rb b/db/migrate/20200511024239_add_position_to_books.rb new file mode 100644 index 000000000..c143c8068 --- /dev/null +++ b/db/migrate/20200511024239_add_position_to_books.rb @@ -0,0 +1,11 @@ +class AddPositionToBooks < ActiveRecord::Migration[6.0] + def up + add_column :books, :position, :integer + add_column :books, :hide_from_index, :boolean, default: false + end + + def down + remove_column :books, :position, :integer + remove_column :books, :hide_from_index, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 32da3a781..cf099e470 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_04_05_003649) do +ActiveRecord::Schema.define(version: 2020_05_11_024239) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" @@ -98,6 +98,8 @@ t.integer "canonical_id" t.boolean "featured_status", default: false t.datetime "featured_at" + t.integer "position" + t.boolean "hide_from_index", default: false t.index ["canonical_id"], name: "index_books_on_canonical_id" end