Skip to content

Commit

Permalink
🧹 More spec & controller fixes
Browse files Browse the repository at this point in the history
 Ref notch8/hykuup_knapsack#55

 - Contact form controller and pages controller duplicate some of the
 homepage controller behavior, so corresponding fixes are needed.
 - Update collections factory and collection_ability_spec to match hyrax
 and stop calling `.gid` on a collection_type.
 - Adjust permission_template_form_spec to adjust for removal of method
 `reset_access_controls!` and add a few new expectations instead.
  • Loading branch information
LaRita Robinson committed Dec 16, 2023
1 parent 3a4084d commit fd1c932
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 143 deletions.
37 changes: 16 additions & 21 deletions app/controllers/hyrax/contact_form_controller.rb
Original file line number Diff line number Diff line change
@@ -1,71 +1,65 @@
# frozen_string_literal: true

# OVERRIDE: Hyrax v3.4.0
# OVERRIDE: Hyrax v5.0.0
# - add inject_theme_views method for theming
# - add homepage presenter for access to feature flippers
# - add access to content blocks in the show method
# - add @featured_collection_list to new method

module Hyrax
class ContactFormController < ApplicationController
# OVERRIDE: Hyrax v3.4.0 Add for theming
# OVERRIDE: Add for theming
# Adds Hydra behaviors into the application controller
include Blacklight::SearchContext
include Blacklight::AccessControls::Catalog
before_action :build_contact_form
layout 'homepage'

# OVERRIDE: Adding inject theme views method for theming
around_action :inject_theme_views
class_attribute :model_class
self.model_class = Hyrax::ContactForm
before_action :setup_negative_captcha, only: %i[new create]
# OVERRIDE: Hyrax v3.4.0 Add for theming

# OVERRIDE: Add for theming
# The search builder for finding recent documents
# Override of Blacklight::RequestBuilders
def search_builder_class
Hyrax::HomepageSearchBuilder
end

# OVERRIDE: Hyrax v3.4.0 Add for theming
# OVERRIDE: Add for theming
class_attribute :presenter_class
# OVERRIDE: Hyrax v3.4.0 Add for theming
self.presenter_class = Hyrax::HomepagePresenter

helper Hyrax::ContentBlockHelper

def new
# OVERRIDE: Hyrax v3.4.0 Add for theming
# OVERRIDE: Add for theming
@presenter = presenter_class.new(current_ability, collections)
@featured_researcher = ContentBlock.for(:researcher)
@marketing_text = ContentBlock.for(:marketing)
@home_text = ContentBlock.for(:home_text)
@featured_work_list = FeaturedWorkList.new
# OVERRIDE: Hyrax 3.4.0 add @featured_collection_list
@featured_collection_list = FeaturedCollectionList.new
@announcement_text = ContentBlock.for(:announcement)
end

# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def create
# not spam, form is valid, and captcha is valid
@captcha.values[:category] = params[:contact_form][:category]
@captcha.values[:contact_method] = params[:contact_form][:contact_method]
@captcha.values[:subject] = params[:contact_form][:subject]
@contact_form = model_class.new(@captcha.values)
if @contact_form.valid? && @captcha.valid?
# not spam and a valid form
if @contact_form.valid?
ContactMailer.contact(@contact_form).deliver_now
flash.now[:notice] = 'Thank you for your message!'
after_deliver
@contact_form = model_class.new
else
flash.now[:error] = 'Sorry, this message was not sent successfully. ' +
@contact_form.errors.full_messages.map(&:to_s).join(", ") +
"" + @captcha.error
@contact_form.errors.full_messages.map(&:to_s).join(", ")
end
render :new
rescue RuntimeError => exception
handle_create_exception(exception)
end
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize

def handle_create_exception(exception)
logger.error("Contact form failed to send: #{exception.inspect}")
Expand All @@ -90,11 +84,12 @@ def contact_form_params
end

# OVERRIDE: return collections for theming
# Return 6 collections, sorts by title
def collections(rows: 6)
builder = Hyrax::CollectionSearchBuilder.new(self)
.rows(rows)
response = repository.search(builder)
response.documents
Hyrax::CollectionsService.new(self).search_results do |builder|
builder.rows(rows)
builder.merge(sort: "title_ssi")
end
rescue Blacklight::Exceptions::ECONNREFUSED, Blacklight::Exceptions::InvalidRequest
[]
end
Expand Down
20 changes: 10 additions & 10 deletions app/controllers/hyrax/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# OVERRIDE: Hyrax v3.4.0
# OVERRIDE: Hyrax v5.0.0
# - add inject_theme_views method for theming
# - add homepage presenter for access to feature flippers
# - add access to content blocks in the show method
Expand All @@ -12,15 +12,15 @@ class PagesController < ApplicationController
load_and_authorize_resource class: ContentBlock, except: :show
layout :pages_layout

# OVERRIDE: Hyrax v3.4.0 Add for theming
# OVERRIDE: Add for theming
# Adds Hydra behaviors into the application controller
include Blacklight::SearchContext
include Blacklight::AccessControls::Catalog

# OVERRIDE: Adding inject theme views method for theming
around_action :inject_theme_views

# OVERRIDE: Hyrax v3.4.0 Add for theming
# OVERRIDE: Add for theming
# The search builder for finding recent documents
# Override of Blacklight::RequestBuilders
def search_builder_class
Expand All @@ -29,20 +29,19 @@ def search_builder_class

# OVERRIDE: Hyrax v3.4.0 Add for theming
class_attribute :presenter_class
# OVERRIDE: Hyrax v3.4.0 Add for theming
self.presenter_class = Hyrax::HomepagePresenter

helper Hyrax::ContentBlockHelper

def show
@page = ContentBlock.for(params[:key])
# OVERRIDE: Hyrax v3.4.0 Add for theming

# OVERRIDE: Additional for theming
@presenter = presenter_class.new(current_ability, collections)
@featured_researcher = ContentBlock.for(:researcher)
@marketing_text = ContentBlock.for(:marketing)
@home_text = ContentBlock.for(:home_text)
@featured_work_list = FeaturedWorkList.new
# OVERRIDE here to add featured collection list to show page
@featured_collection_list = FeaturedCollectionList.new
@announcement_text = ContentBlock.for(:announcement)
end
Expand Down Expand Up @@ -87,11 +86,12 @@ def pages_layout
end

# OVERRIDE: return collections for theming
# Return 6 collections, sorts by title
def collections(rows: 6)
builder = Hyrax::CollectionSearchBuilder.new(self)
.rows(rows)
response = repository.search(builder)
response.documents
Hyrax::CollectionsService.new(self).search_results do |builder|
builder.rows(rows)
builder.merge(sort: "title_ssi")
end
rescue Blacklight::Exceptions::ECONNREFUSED, Blacklight::Exceptions::InvalidRequest
[]
end
Expand Down
8 changes: 4 additions & 4 deletions spec/abilities/collection_ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let(:ability) { Ability.new(current_user) }
let(:user) { create(:user) }
let(:current_user) { user }
let(:collection_type_gid) { create(:collection_type).gid }
let(:collection_type_gid) { create(:collection_type).to_global_id.to_s }
let(:solr_document) { SolrDocument.new(collection.to_solr) }
let(:id) { collection.id }

Expand Down Expand Up @@ -321,7 +321,7 @@
permission_template: collection.permission_template,
agent_type: 'user',
agent_id: user.user_key)
collection.reset_access_controls!
collection.permission_template.reset_access_controls_for(collection: collection)
end

it 'allows most abilities' do
Expand Down Expand Up @@ -366,7 +366,7 @@
permission_template: collection.permission_template,
agent_type: 'user',
agent_id: user.user_key)
collection.reset_access_controls!
collection.permission_template.reset_access_controls_for(collection: collection)
end

it 'allows deposit related abilities' do
Expand Down Expand Up @@ -413,7 +413,7 @@
permission_template: collection.permission_template,
agent_type: 'user',
agent_id: user.user_key)
collection.reset_access_controls!
collection.permission_template.reset_access_controls_for(collection: collection)
end

it 'allows viewing only ability' do
Expand Down
Loading

0 comments on commit fd1c932

Please sign in to comment.