Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add indexer to work_resource generator #4219

Merged
merged 4 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .regen
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4
5
12 changes: 6 additions & 6 deletions app/indexers/hyrax/valkyrie_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Hyrax
# Custom indexers inheriting from others are responsible for providing a full
# index hash. A common pattern for doing this is to employ method composition
# to retrieve the parent's data, then modify it:
# `def to_solr; super.tap { |index_hash| transform(index_hash) }; end`.
# `def to_solr; super.tap { |index_document| transform(index_document) }; end`.
# This technique creates infinitely composible index building behavior, with
# indexers that can always see the state of the resource and the full current
# index document.
Expand All @@ -24,9 +24,9 @@ module Hyrax
# @example defining a custom indexer with composition
# class MyIndexer < ValkyrieIndexer
# def to_solr
# super.tap do |index_hash|
# index_hash[:my_field_tesim] = resource.my_field.map(&:to_s)
# index_hash[:other_field_ssim] = resource.other_field
# super.tap do |index_document|
# index_document[:my_field_tesim] = resource.my_field.map(&:to_s)
# index_document[:other_field_ssim] = resource.other_field
# end
# end
# end
Expand All @@ -40,8 +40,8 @@ module Hyrax
# Hyrax::ValkyrieIndexer.register self, as_indexer_for: Book
#
# def to_solr
# super.tap do |index_hash|
# index_hash[:author_si] = resource.author
# super.tap do |index_document|
# index_document[:author_si] = resource.author
# end
# end
# end
Expand Down
4 changes: 4 additions & 0 deletions lib/generators/hyrax/config_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def create_initializer_config_file
copy_file 'config/initializers/hyrax.rb'
end

def create_initializer_indexers_file
copy_file 'config/initializers/indexers.rb'
end

# Add mini-magick configuration
def minimagick_config
copy_file 'config/initializers/mini_magick.rb'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Rails.application.config.to_prepare do
# Register Indexers
Hyrax::ValkyrieIndexer.register Hyrax::ValkyrieWorkIndexer, as_indexer_for: Hyrax::Work
end
1 change: 1 addition & 0 deletions lib/generators/hyrax/work_resource/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Example:
This will create:
app/models/monograph.rb
app/controllers/hyrax/monographs_controller.rb
app/indexer/monograph_indexer.rb
app/views/monographs/_monograph.html.erb
spec/models/monograph_spec.rb
spec/views/monographs/_monograph.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Generated via
# `rails generate hyrax:work <%= class_name %>`
# `rails generate hyrax:work_resource <%= class_name %>`
module Hyrax
# Generated controller for <%= class_name %>
class <%= class_name.pluralize %>Controller < ApplicationController
Expand Down
15 changes: 15 additions & 0 deletions lib/generators/hyrax/work_resource/templates/indexer.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# Generated via
# `rails generate hyrax:work_resource <%= class_name %>`
class <%= class_name %>Indexer < Hyrax::ValkyrieWorkIndexer
Hyrax::ValkyrieIndexer.register self, as_indexer_for: <%= class_name %>

# Uncomment this block if you want to add custom indexing behavior:
# def to_solr
# super.tap do |index_document|
# index_document[:my_field_tesim] = resource.my_field.map(&:to_s)
# index_document[:other_field_ssim] = resource.other_field
# end
# end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true
# Generated via
# `rails generate hyrax:work <%= class_name %>`
# `rails generate hyrax:work_resource <%= class_name %>`
RSpec.describe '<%= plural_file_name %>/<%= file_name %>.html.erb', type: :view do

end
13 changes: 13 additions & 0 deletions lib/generators/hyrax/work_resource/work_resource_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ def create_model_spec
rspec_installed?
end

def create_indexer
template('indexer.rb.erb', File.join('app/indexers/', class_path, "#{file_name}_indexer.rb"))
end

def register_indexer
config = 'config/initializers/indexers.rb'
register_line = " Hyrax::ValkyrieIndexer.register #{class_name}Indexer, as_indexer_for: #{class_name}\n"
inject_into_file config, after: "# Register Indexers\n" do
return if File.read(config).include?(register_line)
register_line
end
end

def create_views
create_file File.join('app/views/hyrax', class_path, "#{plural_file_name}/_#{file_name}.html.erb") do
"<%# This is a search result view %>\n" \
Expand Down
2 changes: 2 additions & 0 deletions spec/factories/hyrax_work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,7 @@
members { [valkyrie_create(:hyrax_work), valkyrie_create(:hyrax_work)] }
end
end

factory :monograph, class: 'Monograph'
end
end
10 changes: 10 additions & 0 deletions spec/indexers/hyrax/valkyrie_indexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
.to be_a indexer_class
end
end

context 'with registered Monograph indexer' do
let(:resource) { build(:monograph) }
let(:indexer_class) { MonographIndexer }

it 'gives an instance of MonographIndexer for Monograph' do
expect(described_class.for(resource: resource))
.to be_a indexer_class
end
end
end

describe "#to_solr" do
Expand Down