Skip to content

Commit

Permalink
Add a position field to books (#1593)
Browse files Browse the repository at this point in the history
* 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 #1446
  • Loading branch information
astronaut-wannabe authored May 24, 2020
1 parent f3cb7f8 commit c26a5ed
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 1 addition & 26 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 2 additions & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions app/views/admin/books/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@
<%= render "admin/books/categories_form", form: form %>

<div class="row">
<div class="col-6">
<%= render "admin/books/form/position", form: form %>
<p class="form-text text-muted">This will set the order that
this book shows up on <a href="/books">crimethinc.com/books</a>.</p>
</div>

<div class="col-6">
<%= form.check_box :hide_from_index %>
<%= form.label :has_index, "Hide this book from the index page?" %><br>
<p class="form-text text-muted">This will prevent the book from
showing up on <a href="/books">crimethinc.com/books</a>.</p>
</div>
</div>

<%= render "admin/books/downloads_form", form: form, resource: resource %>
<%= render "admin/form_actions", cancel_url: [:admin, resource.namespace] %>
Expand Down
4 changes: 4 additions & 0 deletions app/views/admin/books/form/_position.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div id="position" class="form-group">
<%= form.label :position, "Position this book on the index page" %>
<%= number_field :book, :position, step: 1, class: 'form-control' %>
</div><!-- #position -->
11 changes: 11 additions & 0 deletions db/migrate/20200511024239_add_position_to_books.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit c26a5ed

Please sign in to comment.