Skip to content

Commit

Permalink
Add supplements to admin section
Browse files Browse the repository at this point in the history
  • Loading branch information
mwean committed Jan 13, 2014
1 parent 0a89ca9 commit 6fba43a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
43 changes: 43 additions & 0 deletions app/admin/supplement.rb
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
6 changes: 5 additions & 1 deletion app/decorators/supplement_decorator.rb
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
10 changes: 10 additions & 0 deletions app/views/admin/supplements/_form.html.haml
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
33 changes: 33 additions & 0 deletions spec/features/admin_supplements_spec.rb
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

0 comments on commit 6fba43a

Please sign in to comment.