Skip to content

Commit

Permalink
Generate the presenter into the local application
Browse files Browse the repository at this point in the history
This makes it easier to add custom behavior to the local application in
the correct place. This follows the behavior of the form object.
  • Loading branch information
jcoyne committed May 8, 2017
1 parent 993b9d0 commit eb0b778
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/generators/hyrax/work/templates/controller.rb.erb
Expand Up @@ -7,5 +7,8 @@ module Hyrax
include Hyrax::WorksControllerBehavior
include Hyrax::BreadcrumbsForWorks
self.curation_concern_type = ::<%= class_name %>

# Use this line if you want to use a custom presenter
self.show_presenter = Hyrax::<%= class_name %>Presenter
end
end
6 changes: 6 additions & 0 deletions lib/generators/hyrax/work/templates/presenter.rb.erb
@@ -0,0 +1,6 @@
# Generated via
# `rails generate hyrax:work <%= class_name %>`
module Hyrax
class <%= class_name %>Presenter < Hyrax::WorkShowPresenter
end
end
9 changes: 9 additions & 0 deletions lib/generators/hyrax/work/templates/presenter_spec.rb.erb
@@ -0,0 +1,9 @@
# Generated via
# `rails generate hyrax:work <%= class_name %>`
require 'rails_helper'

RSpec.describe Hyrax::<%= class_name %>Presenter do
it "has tests" do
skip "Add your tests here"
end
end
9 changes: 9 additions & 0 deletions lib/generators/hyrax/work/work_generator.rb
Expand Up @@ -38,6 +38,10 @@ def create_form
template('form.rb.erb', File.join('app/forms/hyrax', class_path, "#{file_name}_form.rb"))
end

def create_presenter
template('presenter.rb.erb', File.join('app/presenters/hyrax', class_path, "#{file_name}_presenter.rb"))
end

def create_model
template('model.rb.erb', File.join('app/models/', class_path, "#{file_name}.rb"))
end
Expand Down Expand Up @@ -91,6 +95,11 @@ def create_form_spec
template('form_spec.rb.erb', File.join('spec/forms/hyrax/', class_path, "#{file_name}_form_spec.rb"))
end

def presenter_spec
return unless rspec_installed?
template('presenter_spec.rb.erb', File.join('spec/presenters/hyrax/', class_path, "#{file_name}_form_spec.rb"))
end

def create_model_spec
return unless rspec_installed?
template('model_spec.rb.erb', File.join('spec/models/', class_path, "#{file_name}_spec.rb"))
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/hyrax/generic_works_controller_spec.rb
Expand Up @@ -21,7 +21,7 @@
get :show, params: { id: work }
expect(response.code).to eq '401'
expect(response).to render_template(:unavailable)
expect(assigns[:presenter]).to be_instance_of Hyrax::WorkShowPresenter
expect(assigns[:presenter]).to be_instance_of Hyrax::GenericWorkPresenter
expect(flash[:notice]).to eq 'The work is not currently available because it has not yet completed the approval process'
end
end
Expand Down Expand Up @@ -71,7 +71,7 @@
it "sets the parent presenter" do
get :show, params: { id: work, parent_id: parent }
expect(response).to be_success
expect(assigns[:parent_presenter]).to be_instance_of Hyrax::WorkShowPresenter
expect(assigns[:parent_presenter]).to be_instance_of Hyrax::GenericWorkPresenter
end
end

Expand Down
3 changes: 2 additions & 1 deletion spec/features/work_generator_spec.rb
Expand Up @@ -7,6 +7,7 @@
Rails::Generators.invoke('hyrax:work', ['Catapult', '--quiet'], destination_root: Rails.root)
load "#{EngineCart.destination}/app/indexers/catapult_indexer.rb"
load "#{EngineCart.destination}/app/models/catapult.rb"
load "#{EngineCart.destination}/app/presenters/hyrax/catapult_presenter.rb"
load "#{EngineCart.destination}/app/controllers/hyrax/catapults_controller.rb"
load "#{EngineCart.destination}/app/actors/hyrax/actors/catapult_actor.rb"
load "#{EngineCart.destination}/app/forms/hyrax/catapult_form.rb"
Expand All @@ -22,7 +23,7 @@
it 'catapults should behave like generic works' do
expect(Hyrax.config.curation_concerns).to include Catapult
expect(defined? Hyrax::Actors::CatapultActor).to eq 'constant'
expect(defined? Hyrax::CatapultsController).to eq 'constant'
expect(Hyrax::CatapultsController.show_presenter).to eq Hyrax::CatapultPresenter
expect(defined? Hyrax::CatapultForm).to eq 'constant'
expect(Catapult.indexer).to eq CatapultIndexer
end
Expand Down

0 comments on commit eb0b778

Please sign in to comment.