diff --git a/Gemfile b/Gemfile index 379c7d8..b8005f2 100644 --- a/Gemfile +++ b/Gemfile @@ -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" diff --git a/app/controllers/concerns/hydra/collections_controller_behavior.rb b/app/controllers/concerns/hydra/collections_controller_behavior.rb index 0c750e9..59c53b3 100644 --- a/app/controllers/concerns/hydra/collections_controller_behavior.rb +++ b/app/controllers/concerns/hydra/collections_controller_behavior.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/app/search_builders/hydra/collections/search_builder.rb b/app/search_builders/hydra/collections/search_builder.rb index 183a736..4b460ab 100644 --- a/app/search_builders/hydra/collections/search_builder.rb +++ b/app/search_builders/hydra/collections/search_builder.rb @@ -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 diff --git a/app/views/collections/_paginate_compact.html.erb b/app/views/collections/_paginate_compact.html.erb new file mode 100644 index 0000000..3814479 --- /dev/null +++ b/app/views/collections/_paginate_compact.html.erb @@ -0,0 +1 @@ +<%= paginate paginate_compact, page_entries_info: page_entries_info(paginate_compact), theme: :blacklight_compact, route_set: collections %> diff --git a/app/views/collections/_results_pagination.html.erb b/app/views/collections/_results_pagination.html.erb new file mode 100644 index 0000000..dda5b37 --- /dev/null +++ b/app/views/collections/_results_pagination.html.erb @@ -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 %> diff --git a/app/views/collections/_search_results.html.erb b/app/views/collections/_search_results.html.erb new file mode 100644 index 0000000..caaf2d4 --- /dev/null +++ b/app/views/collections/_search_results.html.erb @@ -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' %> diff --git a/app/views/collections/index.html.erb b/app/views/collections/index.html.erb new file mode 100644 index 0000000..5f1a373 --- /dev/null +++ b/app/views/collections/index.html.erb @@ -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> + diff --git a/app/views/layouts/collections.html.erb b/app/views/layouts/collections.html.erb new file mode 100644 index 0000000..78b45ba --- /dev/null +++ b/app/views/layouts/collections.html.erb @@ -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> diff --git a/config/routes.rb b/config/routes.rb index 941ee74..52d9d54 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,3 @@ -Hydra::Collections::Engine.routes.draw do - resources :collections, except: :index -end \ No newline at end of file +Hydra::Collections::Engine.routes.draw do + resources :collections +end diff --git a/spec/controllers/collections_controller_spec.rb b/spec/controllers/collections_controller_spec.rb index 495637e..5f3ecdf 100644 --- a/spec/controllers/collections_controller_spec.rb +++ b/spec/controllers/collections_controller_spec.rb @@ -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 @@ -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 @@ -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! @@ -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 @@ -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 @@ -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) @@ -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 diff --git a/spec/test_app_templates/Gemfile.extra b/spec/test_app_templates/Gemfile.extra new file mode 100644 index 0000000..4c8d8a9 --- /dev/null +++ b/spec/test_app_templates/Gemfile.extra @@ -0,0 +1 @@ +gem 'kaminari', github: 'harai/kaminari', branch: 'route_prefix_prototype' diff --git a/spec/test_app_templates/app/views/catalog/_sort_and_per_page.html.erb b/spec/test_app_templates/app/views/catalog/_sort_and_per_page.html.erb index d9f64c0..893a217 100644 --- a/spec/test_app_templates/app/views/catalog/_sort_and_per_page.html.erb +++ b/spec/test_app_templates/app/views/catalog/_sort_and_per_page.html.erb @@ -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>