Skip to content

Commit

Permalink
Support Rails 5
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Jul 29, 2016
1 parent 6d56029 commit dd5163b
Show file tree
Hide file tree
Showing 22 changed files with 94 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ rvm:
- 2.3.1
- 2.2.5

matrix:
include:
- rvm: 2.3.1
env: "RAILS_VERSION=5.0.0"

notifications:
irc: "irc.freenode.org#blacklight"

Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ else
end
end
# END ENGINE_CART BLOCK

eval_gemfile File.expand_path("spec/test_app_templates/Gemfile.extra", File.dirname(__FILE__))
3 changes: 1 addition & 2 deletions app/controllers/concerns/spotlight/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ def exhibit_search_action_url(*args)

def exhibit_search_facet_url(*args)
options = args.extract_options!
options = params.merge(options).except(:exhibit_id, :only_path)

options = params.to_unsafe_h.merge(options).except(:exhibit_id, :only_path)
spotlight.facet_exhibit_catalog_url(current_exhibit, *args, options)
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/spotlight/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def redirect_to_exhibit_home_without_search_params!
end

def add_breadcrumb_with_search_params
add_breadcrumb t(:'spotlight.catalog.breadcrumb.index'), request.fullpath if has_search_parameters?
add_breadcrumb t(:'spotlight.catalog.breadcrumb.index'), spotlight.search_exhibit_catalog_path(params.to_unsafe_h) if has_search_parameters?
end

# rubocop:disable Metrics/AbcSize
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/spotlight/searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SearchesController < Spotlight::ApplicationController

def create
@search.attributes = search_params
@search.query_params = params.except(:exhibit_id, :search, *blacklisted_search_session_params).reject { |_k, v| v.blank? }
@search.query_params = params.to_unsafe_h.except(:exhibit_id, :search, *blacklisted_search_session_params).reject { |_k, v| v.blank? }

if @search.save
redirect_to :back, notice: t(:'helpers.submit.search.created', model: @search.class.model_name.human.downcase)
Expand Down
8 changes: 8 additions & 0 deletions app/models/concerns/spotlight/ar_light.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ module ArLight
##
# Mock activerecord class-level methods
module ClassMethods
def has_attribute?(*args)
false
end

def columns_hash
{}
end

def base_class
self
end
Expand Down
1 change: 0 additions & 1 deletion app/models/concerns/spotlight/solr_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module SolrDocument
include GlobalID::Identification

included do
extend ActsAsTaggableOn::Compatibility
extend ActsAsTaggableOn::Taggable

acts_as_taggable
Expand Down
4 changes: 3 additions & 1 deletion app/models/spotlight/blacklight_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@ def field_weight(fields, index)
end

def value_to_boolean(v)
if defined? ActiveRecord::Type
if defined? ActiveModel::Type::Boolean
ActiveModel::Type::Boolean.new.cast v
elsif defined? ActiveRecord::Type::Boolean
# Rails 4.2+
ActiveRecord::Type::Boolean.new.type_cast_from_database v
else
Expand Down
13 changes: 12 additions & 1 deletion app/models/spotlight/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,21 @@ def cast_value
return value unless field

if field.ends_with? Spotlight::Engine.config.solr_fields.boolean_suffix
ActiveRecord::Type::Boolean.new.type_cast_from_database(value)
value_to_boolean(value)
else
value
end
end

def value_to_boolean(v)
if defined? ActiveModel::Type::Boolean
ActiveModel::Type::Boolean.new.cast v
elsif defined? ActiveRecord::Type::Boolean
# Rails 4.2+
ActiveRecord::Type::Boolean.new.type_cast_from_database v
else
ActiveRecord::ConnectionAdapters::Column.value_to_boolean v
end
end
end
end
12 changes: 10 additions & 2 deletions app/models/spotlight/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,19 @@ def waiting!
end

def enqueued_at
ActiveRecord::Type::DateTime.new.type_cast_from_database(super)
if defined? ActiveModel::Type::DateTime
ActiveModel::Type::DateTime.new.cast(super)
else
ActiveRecord::Type::DateTime.new.type_cast_from_database(super)
end
end

def last_indexed_finished
ActiveRecord::Type::DateTime.new.type_cast_from_database(super)
if defined? ActiveModel::Type::DateTime
ActiveModel::Type::DateTime.new.cast(super)
else
ActiveRecord::Type::DateTime.new.type_cast_from_database(super)
end
end

def document_model
Expand Down
4 changes: 4 additions & 0 deletions app/models/spotlight/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def documents
end
end

def count
documents.size
end

delegate :blacklight_config, to: :exhibit

def display_masthead?
Expand Down
2 changes: 1 addition & 1 deletion app/views/spotlight/pages/_view_type_group.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<span class="sr-only"><%= t('blacklight.search.view_title') %></span>
<div class="view-type-group btn-group">
<% views.each do |view, config| %>
<%= link_to url_for(params.merge(:view => view)), :title => t("blacklight.search.view_title.#{view}", default: t("blacklight.search.view.#{view}", default: blacklight_config.view[view].title)), :class => "btn btn-default view-type-#{ view.to_s.parameterize } #{"active" if block_document_index_view_type(block) == view}" do %>
<%= link_to url_for(search_state.to_h.merge(view: view)), :title => t("blacklight.search.view_title.#{view}", default: t("blacklight.search.view.#{view}", default: blacklight_config.view[view].title)), :class => "btn btn-default view-type-#{ view.to_s.parameterize } #{"active" if block_document_index_view_type(block) == view}" do %>
<%= render_view_type_group_icon view %>
<span class="caption"><%= t("blacklight.search.view.#{view}") %></span>
<% end %>
Expand Down
7 changes: 4 additions & 3 deletions blacklight-spotlight.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ these collections.)

s.required_ruby_version = '~> 2.2'

s.add_dependency 'rails', '~> 4.0', '>= 4.2.0'
s.add_dependency 'rails', '>= 4.2.0', '< 6'
s.add_dependency 'blacklight', '~> 6.3'
s.add_dependency 'autoprefixer-rails'
s.add_dependency 'cancancan'
Expand All @@ -28,8 +28,8 @@ these collections.)
s.add_dependency 'carrierwave-crop'
s.add_dependency 'mini_magick'
s.add_dependency 'bootstrap_form', '~> 2.2'
s.add_dependency 'acts-as-taggable-on', '~> 3.5'
s.add_dependency 'friendly_id', '~> 5.1.0'
s.add_dependency 'acts-as-taggable-on', '>= 4.0.0.pre'
s.add_dependency 'friendly_id', '>= 5.2.0.beta.1', '< 6'
s.add_dependency 'breadcrumbs_on_rails', '~> 2.3.0'
s.add_dependency 'social-share-button', '~> 0.3'
s.add_dependency 'blacklight-gallery', '>= 0.3.0'
Expand All @@ -55,6 +55,7 @@ these collections.)
s.add_development_dependency 'rspec-its'
s.add_development_dependency 'rspec-activemodel-mocks'
s.add_development_dependency 'rspec-collection_matchers'
s.add_development_dependency 'rails-controller-testing'
s.add_development_dependency 'capybara', '>= 2.5.0'
s.add_development_dependency 'rubocop', '~> 0.41', '>= 0.41.2'
s.add_development_dependency 'rubocop-rspec'
Expand Down
4 changes: 2 additions & 2 deletions lib/generators/spotlight/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def inject_spotlight_routes
end

def friendly_id
gem 'friendly_id'
gem 'friendly_id', github: 'norman/friendly_id'
generate 'friendly_id'
end

Expand Down Expand Up @@ -119,7 +119,7 @@ def add_mailer_defaults
end

def generate_social_share_button_initializer
gem 'social-share-button'
gem 'social-share-button', github: 'cbeer/social-share-button', branch: 'on_load'
directory 'config'
end

Expand Down
10 changes: 9 additions & 1 deletion lib/spotlight/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ module Spotlight
class Engine < ::Rails::Engine
isolate_namespace Spotlight
# Breadcrumbs on rails must be required outside of an initializer or it doesn't get loaded.
require 'breadcrumbs_on_rails'
require 'breadcrumbs_on_rails/breadcrumbs'
require 'breadcrumbs_on_rails/action_controller'

initializer 'breadcrumbs_on_rails.initialize' do
ActiveSupport.on_load(:action_controller) do
include BreadcrumbsOnRails::ActionController
end
end

require 'carrierwave'
require 'carrierwave/crop'
require 'social-share-button'
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/spotlight/appearances_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
first_nav = exhibit.main_navigations.first
last_nav = exhibit.main_navigations.last
patch :update, exhibit_id: exhibit, exhibit: {
main_navigations_attributes: [
{ id: first_nav.id, label: 'Some Label', weight: 500 },
{ id: last_nav.id, display: false }
]
main_navigations_attributes: {
0 => { id: first_nav.id, label: 'Some Label', weight: 500 },
1 => { id: last_nav.id, display: false }
}
}
expect(flash[:notice]).to eq 'The exhibit was successfully updated.'
expect(response).to redirect_to edit_exhibit_appearance_path(exhibit)
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/spotlight/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
it 'shows the index when there are parameters' do
expect(controller).to receive(:add_breadcrumb).with('Home', exhibit_path(exhibit, q: ''))
expect(controller).to receive(:add_breadcrumb).with('Search Results', search_exhibit_catalog_path(exhibit, q: 'map'))
get :index, exhibit_id: exhibit, q: 'map'
get :index, params: { exhibit_id: exhibit, q: 'map' }
expect(response).to be_successful
end
it 'redirects to the exhibit home page when there are no parameters' do
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/spotlight/solr_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
doc = arr.first
end

post :update, { a: 1 }.to_json, content_type: :json, exhibit_id: exhibit
post :update, body: { a: 1 }.to_json, params: { exhibit_id: exhibit }, as: :json

expect(response).to be_successful
expect(doc).to include 'a' => 1
Expand All @@ -41,7 +41,7 @@
end

it 'raises an error' do
post :update, { a: 1 }.to_json, content_type: :json, exhibit_id: exhibit
post :update, body: { a: 1 }.to_json, params: { exhibit_id: exhibit }, as: :json

expect(response.code).to eq '409'
end
Expand All @@ -53,7 +53,7 @@
doc = arr.first
end

post :update, { a: 1 }.to_json, content_type: :json, exhibit_id: exhibit
post :update, body: { a: 1 }.to_json, params: { exhibit_id: exhibit }, as: :json

expect(response).to be_successful
expect(doc).to include exhibit.solr_data
Expand All @@ -67,7 +67,7 @@

allow_any_instance_of(SolrDocument).to receive(:to_solr).and_return(b: 1)

post :update, { a: 1 }.to_json, content_type: :json, exhibit_id: exhibit
post :update, body: { a: 1 }.to_json, params: { exhibit_id: exhibit }, as: :json

expect(response).to be_successful
expect(doc).to include b: 1
Expand Down
7 changes: 6 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
ENV['RAILS_ENV'] ||= 'test'

require 'factory_girl'
require 'database_cleaner'
require 'devise'
require 'engine_cart'
EngineCart.load_application!

Internal::Application.config.active_job.queue_adapter = :inline

require 'rspec/collection_matchers'
require 'rspec/its'
require 'rspec/rails'
require 'rspec/active_model/mocks'
require 'rails-controller-testing'

require 'capybara/poltergeist'

Expand Down Expand Up @@ -47,6 +49,7 @@

RSpec.configure do |config|
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!

config.use_transactional_fixtures = false

Expand Down Expand Up @@ -88,6 +91,8 @@
config.after(:each, type: :feature) { Warden.test_reset! }
config.include Controllers::EngineHelpers, type: :controller
config.include Capybara::DSL
config.include ::Rails.application.routes.url_helpers
config.include ::Rails.application.routes.mounted_helpers
config.include Spotlight::TestFeaturesHelpers, type: :feature
end

Expand Down
9 changes: 9 additions & 0 deletions spec/support/helpers/controller_level_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ def search_state
@search_state ||= Blacklight::SearchState.new(params, blacklight_config)
end

def current_site
Spotlight::Site.instance
end

def blacklight_configuration_context
@blacklight_configuration_context ||= Blacklight::Configuration::Context.new(controller)
end

def initialize_controller_helpers(helper)
helper.extend ControllerLevelHelpers
helper.class.include ::Rails.application.routes.url_helpers

if ::Rails.application.routes.respond_to?(:mounted_helpers)
helper.class.include ::Rails.application.routes.mounted_helpers
end
end
end
3 changes: 3 additions & 0 deletions spec/support/views/test_view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module TestViewHelpers
before do
view.send(:extend, Spotlight::CrudLinkHelpers)
view.send(:extend, Spotlight::TitleHelper)
view.send(:extend, Spotlight::NavbarHelper)
view.send(:extend, Blacklight::ComponentHelperBehavior)
view.send(:extend, BreadcrumbsOnRails::ActionController::HelperMethods)
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/test_app_templates/Gemfile.extra
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gem 'acts-as-taggable-on', github: 'mbleigh/acts-as-taggable-on'
gem 'rails-controller-testing'
gem 'friendly_id', github: 'norman/friendly_id'
gem 'social-share-button', github: 'cbeer/social-share-button', branch: 'on_load'

0 comments on commit dd5163b

Please sign in to comment.