-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
ActiveAdmin.register Supplement do | ||
decorate_with SupplementDecorator | ||
config.filters = false | ||
|
||
index download_links: false do | ||
column :title | ||
column :pages | ||
default_actions | ||
end | ||
|
||
form partial: 'form' | ||
|
||
show title: :title do |supplement| | ||
attributes_table do | ||
row :number | ||
row :title | ||
row :author_names | ||
row :pages | ||
row :description | ||
row(Supplement.human_attribute_name(:pdf)) { supplement.pdf_download_link } | ||
end | ||
end | ||
|
||
controller do | ||
def authors | ||
Author.all.decorate.map { |author| [author.full_name, author.id] } | ||
end | ||
|
||
def current_author_ids | ||
@supplement.authors.pluck(:id) | ||
end | ||
|
||
def form_action | ||
@supplement.persisted? ? [:admin, @supplement] : [:admin, :supplements] | ||
end | ||
|
||
helper_method :volumes, :authors, :current_volume_id, :current_author_ids, :keyword_names, :keywords, :form_action | ||
|
||
def permitted_params | ||
params.permit({ supplement: [{ author_ids: [] }, :number, :title, :pages, :description, :pdf, :id] }) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
class SupplementDecorator < ApplicationDecorator | ||
delegate :number, :title, :pages, :pdf, :pdf_file_name | ||
delegate :number, :title, :pages, :description, :pdf, :pdf_file_name | ||
decorates_associations :authors | ||
|
||
def author_names | ||
authors.map { |author| "<span class=nowrap>#{author.full_name}</span>" }.join(', ').html_safe | ||
end | ||
|
||
def pdf_download_link | ||
h.link_to('Download', pdf.url) if pdf_file_name | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
= semantic_form_for(@supplement, url: form_action) do |f| | ||
= f.inputs do | ||
= f.input :number | ||
= f.input :title, as: :text, input_html: { rows: 3 } | ||
= f.input :authors, collection: authors, selected: current_author_ids, required: false, input_html: { class: :select2, multiple: true } | ||
= f.input :pages | ||
= f.input :description | ||
= f.input :pdf, label: 'PDF', required: false | ||
|
||
= f.actions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'spec_helper' | ||
|
||
feature 'visiting the supplements admin section' do | ||
scenario 'the user sees a list of supplements' do | ||
supplement = FactoryGirl.create(:supplement) | ||
log_in_admin_user | ||
visit admin_supplements_path | ||
|
||
expect(page).to have_css('tr', text: supplement.title) | ||
end | ||
end | ||
|
||
feature 'adding a new supplement', :js do | ||
scenario 'the user adds a new supplement and is redirected to the show page' do | ||
author = FactoryGirl.create(:author).decorate | ||
supplement = FactoryGirl.build_stubbed(:supplement) | ||
|
||
log_in_admin_user | ||
visit new_admin_supplement_path | ||
|
||
fill_in 'Number', with: supplement.number | ||
fill_in 'Title', with: supplement.title | ||
select2 author.full_name, from: 'Authors' | ||
fill_in 'Pages', with: supplement.pages | ||
fill_in 'Description', with: supplement.description | ||
attach_file 'PDF', File.join(Rails.root, 'spec', 'support', 'test.pdf') | ||
click_on 'Create Supplement' | ||
|
||
expect(current_path).to eql(admin_supplement_path(Supplement.last)) | ||
expect(page).to have_content('Supplement was successfully created') | ||
expect(Supplement.count).to eql(1) | ||
end | ||
end |