From 9df4d508320365cfd3a2b535038f1a6d2cc4d144 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Fri, 31 Mar 2017 16:45:52 -0700 Subject: [PATCH] Generate the indexer for a work This gives the developer a place to put custom indexing logic --- lib/generators/hyrax/work/templates/indexer.rb.erb | 10 ++++++++++ lib/generators/hyrax/work/templates/model.rb.erb | 4 +++- lib/generators/hyrax/work/work_generator.rb | 4 ++++ spec/features/work_generator_spec.rb | 2 ++ 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 lib/generators/hyrax/work/templates/indexer.rb.erb diff --git a/lib/generators/hyrax/work/templates/indexer.rb.erb b/lib/generators/hyrax/work/templates/indexer.rb.erb new file mode 100644 index 0000000000..5588951ce5 --- /dev/null +++ b/lib/generators/hyrax/work/templates/indexer.rb.erb @@ -0,0 +1,10 @@ +# Generated via +# `rails generate hyrax:work <%= class_name %>` +class <%= class_name %>Indexer < Hyrax::WorkIndexer + # Uncomment this block if you want to add custom indexing behavior: + # def generate_solr_document + # super.tap do |solr_doc| + # solr_doc['my_custom_field_ssim'] = object.my_custom_property + # end + # end +end diff --git a/lib/generators/hyrax/work/templates/model.rb.erb b/lib/generators/hyrax/work/templates/model.rb.erb index fc927d86a5..dff1076596 100644 --- a/lib/generators/hyrax/work/templates/model.rb.erb +++ b/lib/generators/hyrax/work/templates/model.rb.erb @@ -3,9 +3,11 @@ class <%= class_name %> < ActiveFedora::Base include ::Hyrax::WorkBehavior include ::Hyrax::BasicMetadata + + self.indexer = <%= class_name %>Indexer # Change this to restrict which works can be added as a child. # self.valid_child_concerns = [] validates :title, presence: { message: 'Your work must have a title.' } - + self.human_readable_type = '<%= name.underscore.titleize %>' end diff --git a/lib/generators/hyrax/work/work_generator.rb b/lib/generators/hyrax/work/work_generator.rb index 27a8b8224e..6cd8b32695 100644 --- a/lib/generators/hyrax/work/work_generator.rb +++ b/lib/generators/hyrax/work/work_generator.rb @@ -23,6 +23,10 @@ def create_controller template('controller.rb.erb', "app/controllers/hyrax/#{plural_file_name}_controller.rb") end + def create_indexer + template('indexer.rb.erb', "app/indexers/#{file_name}_indexer.rb") + end + def create_form template('form.rb.erb', "app/forms/hyrax/#{file_name}_form.rb") end diff --git a/spec/features/work_generator_spec.rb b/spec/features/work_generator_spec.rb index 5f09ff3a1f..b7280c263a 100644 --- a/spec/features/work_generator_spec.rb +++ b/spec/features/work_generator_spec.rb @@ -5,6 +5,7 @@ feature 'Creating a new Work', :workflow do before do 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/controllers/hyrax/catapults_controller.rb" load "#{EngineCart.destination}/app/actors/hyrax/actors/catapult_actor.rb" @@ -23,5 +24,6 @@ expect(defined? Hyrax::Actors::CatapultActor).to eq 'constant' expect(defined? Hyrax::CatapultsController).to eq 'constant' expect(defined? Hyrax::CatapultForm).to eq 'constant' + expect(Catapult.indexer).to eq CatapultIndexer end end