diff --git a/app/decorators/book_decorator.rb b/app/decorators/book_decorator.rb new file mode 100644 index 00000000000..d16b212eb0f --- /dev/null +++ b/app/decorators/book_decorator.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module BookDecorator + def must_read_for_any_practices? + practices_books.any?(&:must_read) + end +end diff --git a/app/javascript/components/book.vue b/app/javascript/components/book.vue index 57bccf81766..a8134a18ee2 100644 --- a/app/javascript/components/book.vue +++ b/app/javascript/components/book.vue @@ -16,6 +16,8 @@ .card-books-item__rows .card-books-item__row h2.card-books-item__title + span.a-badge.is-danger.is-sm(v-if='book.mustRead') + | 必読 a.card-books-item__title-link( :href='book.pageUrl', target='_blank', diff --git a/app/views/api/books/_book.json.jbuilder b/app/views/api/books/_book.json.jbuilder index 96eb8ab5616..0c78f713145 100644 --- a/app/views/api/books/_book.json.jbuilder +++ b/app/views/api/books/_book.json.jbuilder @@ -5,5 +5,6 @@ json.description book.description json.pageUrl book.page_url json.coverUrl book.cover_url json.editBookPath edit_book_path(book) +json.mustRead book.must_read_for_any_practices? json.practices book.practices, partial: "api/books/practice", as: :practice diff --git a/test/decorators/book_decorator_test.rb b/test/decorators/book_decorator_test.rb new file mode 100644 index 00000000000..17b50b9e408 --- /dev/null +++ b/test/decorators/book_decorator_test.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'test_helper' + +class BookDecoratorTest < ActiveSupport::TestCase + setup do + @book1 = ActiveDecorator::Decorator.instance.decorate(books(:book1)) + @book2 = ActiveDecorator::Decorator.instance.decorate(books(:book2)) + end + + test '#must_read_for_any_practices?' do + assert @book1.must_read_for_any_practices? + assert_not @book2.must_read_for_any_practices? + end +end