Skip to content

Commit

Permalink
Add an index page for collections. Fixes #58
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Mar 9, 2015
1 parent 6f17870 commit f124436
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 30 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in hydra-collections.gemspec
gemspec

gem 'kaminari', github: 'harai/kaminari', branch: 'route_prefix_prototype'

group :development, :test do
gem 'sqlite3'
gem "factory_girl_rails"
Expand Down
39 changes: 29 additions & 10 deletions app/controllers/concerns/hydra/collections_controller_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ module CollectionsControllerBehavior
before_filter :authenticate_user!, :except => [:show]
load_and_authorize_resource :except=>[:index], instance_name: :collection

#This includes only the collection members in the search
self.search_params_logic += [:include_collection_ids]
layout 'collections'
end

def index
# run the solr query to find the collections
query = collections_search_builder.with(params).query
@response = repository.search(query)
@document_list = @response.documents
end

def new
Expand Down Expand Up @@ -123,9 +129,9 @@ def collection

protected

# Defines which search_params_logic should be used when searching for Collections
def collection_search_params_logic
search_params_logic
# Defines which search_params_logic should be used when searching for Collection members
def collection_member_search_logic
search_params_logic + [:include_collection_ids]
end

def collection_params
Expand All @@ -137,18 +143,26 @@ def collection_params
# Queries Solr for members of the collection.
# Populates @response and @member_docs similar to Blacklight Catalog#index populating @response and @documents
def query_collection_members
query = params[:cq]

#default the rows to 100 if not specified then merge in the user parameters and the attach the collection query
solr_params = { rows: 100 }.merge(params.symbolize_keys).merge(q: query)
solr_params = { rows: 100 }.merge(params.symbolize_keys).merge(q: params[:cq])

# run the solr query to find the collections
# (@response, @member_docs) = search_results(solr_params, search_params_logic)
query = collections_search_builder.with(solr_params).query
# run the solr query to find the collection members
query = collection_member_search_builder.with(solr_params).query
@response = repository.search(query)
@member_docs = @response.documents
end

def collection_member_search_builder_class
Hydra::Collections::SearchBuilder
end

def collection_member_search_builder(access_level = nil)
@collection_member_search_builder ||= collection_member_search_builder_class.new(collection_member_search_logic, self).tap do |builder|
builder.current_ability = current_ability
end
end

def process_member_changes
case params[:collection][:members]
when "add" then add_members_to_collection
Expand Down Expand Up @@ -182,5 +196,10 @@ def move_members_between_collections
flash[:error] = "An error occured. Files were not moved to #{destination_collection.title} Collection."
end
end

# Override rails path for the views
def _prefixes
@_prefixes ||= super + ['catalog']
end
end # module CollectionsControllerBehavior
end # module Hydra
2 changes: 1 addition & 1 deletion app/search_builders/hydra/collections/search_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def some_rows(solr_parameters)

def add_collection_filter(solr_parameters)
solr_parameters[:fq] ||= []
solr_parameters[:fq] << "#{Solrizer.solr_name("has_model", :symbol)}:Collection"
solr_parameters[:fq] << ActiveFedora::SolrQueryBuilder.construct_query_for_rel(has_model: ::Collection.to_class_uri)
end

def discovery_perms= perms
Expand Down
1 change: 1 addition & 0 deletions app/views/collections/_paginate_compact.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= paginate paginate_compact, page_entries_info: page_entries_info(paginate_compact), theme: :blacklight_compact, route_set: collections %>
9 changes: 9 additions & 0 deletions app/views/collections/_results_pagination.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% if show_pagination? and @response.total_pages > 1 %>
<div class="row record-padding">
<div class="col-md-9">
<div class="pagination">
<%= paginate @response, :outer_window => 2, :theme => 'blacklight', route_set: collections %>
</div>
</div>
</div>
<% end %>
23 changes: 23 additions & 0 deletions app/views/collections/_search_results.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h2 class="sr-only top-content-title"><%= t('blacklight.search.search_results_header') %></h2>

<% @page_title = t('blacklight.search.title', :application_name => application_name) %>


<% content_for(:head) do -%>
<%= render_opensearch_response_metadata %>
<% end -%>


<%= render 'search_header' %>

<h2 class="sr-only"><%= t('blacklight.search.search_results') %></h2>

<%- if @response.empty? %>
<%= render "zero_results" %>
<%- elsif render_grouped_response? %>
<%= render_grouped_document_index %>
<%- else %>
<%= render_document_index %>
<%- end %>

<%= render 'results_pagination' %>
9 changes: 9 additions & 0 deletions app/views/collections/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div id="sidebar" class="col-md-3 col-sm-4">
<%= render 'search_sidebar' %>
</div>

<div id="content" class="col-md-9 col-sm-8">
<h2>Collections</h2>
<%= render 'search_results' %>
</div>

48 changes: 48 additions & 0 deletions app/views/layouts/collections.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<!-- Mobile viewport optimization h5bp.com/ad -->
<meta name="HandheldFriendly" content="True">
<meta name="viewport" content="width=device-width,initial-scale=1.0">

<!-- Internet Explorer use the highest version available -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">

<!-- Mobile IE allows us to activate ClearType technology for smoothing fonts for easy reading -->
<!--[if IEMobile]>
<meta http-equiv="cleartype" content="on">
<![endif]-->

<title><%= render_page_title %></title>
<%= opensearch_description_tag application_name, opensearch_catalog_url(:format => 'xml') %>
<%= favicon_link_tag 'favicon.ico' %>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= content_for(:head) %>

<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

</head>
<body class="<%= render_body_class %>">
<%= render :partial => 'shared/header_navbar' %>

<%= render partial: 'shared/ajax_modal' %>

<div id="main-container" class="container">
<%= render :partial=>'/flash_msg', layout: 'shared/flash_messages' %>

<div class="row">
<%= yield %>
</div>
</div>

<%= render :partial => 'shared/footer' %>
</body>
</html>
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Hydra::Collections::Engine.routes.draw do
resources :collections, except: :index
end
Hydra::Collections::Engine.routes.draw do
resources :collections
end
30 changes: 22 additions & 8 deletions spec/controllers/collections_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,29 @@ def to_solr(solr_doc={})
Object.send(:remove_const, :GenericFile)
end

let(:user) { FactoryGirl.create(:user) }

before do
allow(controller).to receive(:has_access?).and_return(true)

@user = FactoryGirl.find_or_create(:user)
sign_in @user
sign_in user
allow_any_instance_of(User).to receive(:groups).and_return([])
allow(controller).to receive(:clear_session_user) ## Don't clear out the authenticated session
end

describe "#index" do
let!(:collection1) { Collection.create { |c| c.apply_depositor_metadata(user.user_key) } }
let!(:collection2) { Collection.create { |c| c.apply_depositor_metadata(user.user_key) } }
let!(:generic_file) { GenericFile.create }

it "should show a list of collections" do
get :index
expect(response).to be_successful
expect(assigns[:document_list].map(&:id)).not_to include generic_file.id
expect(assigns[:document_list].map(&:id)).to eq [collection1.id, collection2.id]
end
end

describe '#new' do
it 'should assign @collection' do
get :new
Expand All @@ -50,7 +64,7 @@ def to_solr(solr_doc={})
}.to change { Collection.count }.by(1)
expect(assigns[:collection].title).to eq("My First Collection ")
expect(assigns[:collection].description).to eq("The Description\r\n\r\nand more")
expect(assigns[:collection].depositor).to eq(@user.user_key)
expect(assigns[:collection].depositor).to eq(user.user_key)
expect(response).to redirect_to collections.collection_path(assigns[:collection])
end
it "should add docs to collection if batch ids provided" do
Expand Down Expand Up @@ -91,7 +105,7 @@ def to_solr(solr_doc={})

describe "#update" do
before do
@collection = Collection.create { |c| c.apply_depositor_metadata(@user.user_key) }
@collection = Collection.create { |c| c.apply_depositor_metadata(user.user_key) }
@asset1 = GenericFile.create!
@asset2 = GenericFile.create!
@asset3 = GenericFile.create!
Expand Down Expand Up @@ -152,7 +166,7 @@ def to_solr(solr_doc={})
end
let(:collection2) do
Collection.create do |col|
col.apply_depositor_metadata(@user.user_key)
col.apply_depositor_metadata(user.user_key)
end
end

Expand All @@ -168,7 +182,7 @@ def to_solr(solr_doc={})
describe "#destroy" do
describe "valid collection" do
before do
@collection = Collection.create { |c| c.apply_depositor_metadata(@user.user_key) }
@collection = Collection.create { |c| c.apply_depositor_metadata(user.user_key) }
expect(controller).to receive(:authorize!).and_return(true)
end

Expand Down Expand Up @@ -208,7 +222,7 @@ def to_solr(solr_doc={})
@asset3 = GenericFile.create!(title: "Third of the Assets")
@collection = Collection.new(id:"abc123")
@collection.title = "My collection"
@collection.apply_depositor_metadata(@user.user_key)
@collection.apply_depositor_metadata(user.user_key)
@collection.members = [@asset1, @asset2, @asset3]
@collection.save!
expect(controller).to receive(:authorize!).and_return(true)
Expand All @@ -220,7 +234,7 @@ def to_solr(solr_doc={})
@asset4 = GenericFile.create!(title: "#{@asset1.id}")
@collection2 = Collection.new(id: "abc1234")
@collection2.title = "Other collection"
@collection2.apply_depositor_metadata(@user.user_key)
@collection2.apply_depositor_metadata(user.user_key)
@collection2.members = [@asset4]
@collection2.save
end
Expand Down
1 change: 1 addition & 0 deletions spec/test_app_templates/Gemfile.extra
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gem 'kaminari', github: 'harai/kaminari', branch: 'route_prefix_prototype'
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
<div id="sortAndPerPage">
<div class="page_links">
<%= render :partial => "paginate_compact" %>
</div>
<%= render :partial => 'sort_widget' %>

<%= render :partial => 'per_page_widget' %>

<div id="sortAndPerPage" class="clearfix">
<%= render "paginate_compact", object: @response if show_pagination? %>
<%= button_for_create_collection %>

<%= render partial: 'collections/form_for_select_collection', locals: {user_collections: @user_collections} %>
</div>

</div>

0 comments on commit f124436

Please sign in to comment.