Skip to content

Commit

Permalink
Add a modal when there are many types of works to select from
Browse files Browse the repository at this point in the history
Fixes #2434
  • Loading branch information
jcoyne committed Nov 8, 2016
1 parent 66dec00 commit a070f1d
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 13 deletions.
15 changes: 15 additions & 0 deletions app/presenters/sufia/select_type_list_presenter.rb
@@ -0,0 +1,15 @@
module Sufia
class SelectTypeListPresenter
# @param classification [CurationConcerns::QuickClassificationQuery]
def initialize(classification)
@classification = classification
end

class_attribute :row_presenter
self.row_presenter = SelectTypePresenter

def each
@classification.each { |i| yield row_presenter.new(i) }
end
end
end
35 changes: 35 additions & 0 deletions app/presenters/sufia/select_type_presenter.rb
@@ -0,0 +1,35 @@
module Sufia
class SelectTypePresenter
def initialize(concern)
@concern = concern
end

attr_reader :concern

def icon_class
translate('icon_class')
end

def description
translate('description')
end

def name
translate('name')
end

private

def object_name
@object_name ||= concern.model_name.i18n_key
end

def translate(key)
defaults = []
defaults << :"sufia.select_type.#{object_name}.#{key}"
defaults << :"sufia.select_type.#{key}"
defaults << ''
I18n.t(defaults.shift, default: defaults)
end
end
end
16 changes: 16 additions & 0 deletions app/views/dashboard/_create_work_action.html.erb
@@ -0,0 +1,16 @@
<% classification = CurationConcerns::QuickClassificationQuery.new(current_user) %>
<% if classification.authorized_models.size > 1 %>
<%= render 'select_work_type', presenter: Sufia::SelectTypeListPresenter.new(classification) %>
<% else %>
<div class="col-xs-6 col-sm-2 heading-tile">
<% classification.each do |concern| %>
<%= link_to(new_polymorphic_path([main_app, concern]),
class: "item-option contextual-quick-classify #{dom_class(concern, 'new').gsub('_', '-')}",
role: 'menuitem'
) do %>
<span class="glyphicon glyphicon-upload"></span>
<%= I18n.t("sufia.dashboard.create_work", work_type: concern.human_readable_type) %>
<% end %>
<% end %>
</div>
<% end %>
15 changes: 2 additions & 13 deletions app/views/dashboard/_index_partials/_heading_actions.html.erb
@@ -1,18 +1,7 @@
<div class="col-xs-12 heading-row">
<div class="row">
<% if can?(:create, Sufia.primary_work_type) %>
<div class="col-xs-6 col-sm-2 heading-tile">
<% classification = CurationConcerns::QuickClassificationQuery.new(current_user) %>
<% classification.each do |concern| %>
<%= link_to(new_polymorphic_path([main_app, concern]),
class: "item-option contextual-quick-classify #{dom_class(concern, 'new').gsub('_', '-')}",
role: 'menuitem'
) do %>
<span class="glyphicon glyphicon-upload"></span>
<%= I18n.t("sufia.dashboard.create_work", work_type: concern.human_readable_type) %>
<% end %>
<% end %>
</div>
<% if can_ever_create_works? %>
<%= render 'create_work_action' %>
<% end %>
<% if can?(:create, Collection) %>
<div class="col-xs-6 col-sm-2 heading-tile">
Expand Down
46 changes: 46 additions & 0 deletions app/views/dashboard/_select_work_type.html.erb
@@ -0,0 +1,46 @@
<div class="modal fade" id="worktypes-to-create" tabindex="-1" role="dialog" aria-labelledby="select-worktype-label">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="select-worktype-label"><%= t('sufia.dashboard.heading_actions.select_type_of_work') %></h4>
</div>
<div class="modal-body">
<table class="table">
<% presenter.each do |row_presenter| %>
<tr>
<td class="select-work-icon">
<%= link_to(new_polymorphic_path([main_app, row_presenter.concern]),
class: "item-option contextual-quick-classify #{dom_class(row_presenter.concern, 'new').gsub('_', '-')}",
role: 'menuitem'
) do %>
<span class="<%= row_presenter.icon_class %>"></span>
<% end %>
</td>
<td class="select-work-description">
<%= link_to(new_polymorphic_path([main_app, row_presenter.concern]),
class: "item-option contextual-quick-classify #{dom_class(row_presenter.concern, 'new').gsub('_', '-')}",
role: 'menuitem'
) do %>
<h3><%= row_presenter.name %></h3>
<p><%= row_presenter.description %></p>
<% end %>
</td>
</tr>
<% end %>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"><%= t('sufia.dashboard.heading_actions.close') %></button>
<button type="button" class="btn btn-primary"><%= t('sufia.dashboard.heading_actions.create_work') %></button>
</div>
</div>
</div>
</div>

<div class="col-xs-6 col-sm-2 heading-tile">
<%= link_to('#', data: { toggle: "modal", target:"#worktypes-to-create" }) do %>
<span class="glyphicon glyphicon-upload"></span>
<%= t("sufia.dashboard.create_work") %>
<% end %>
</div>
8 changes: 8 additions & 0 deletions config/locales/sufia.en.yml
Expand Up @@ -48,6 +48,10 @@ en:
button:
html: '<span class="glyphicon glyphicon-search"></span> Go'
text: "Search"
select_type:
name: "Work"
description: "General purpose worktype"
icon_class : 'fa fa-file-text-o'
upload:
my_computer:
sr_tab_label: "Access Files from"
Expand Down Expand Up @@ -120,6 +124,10 @@ en:
logout: "Logout"
login: "Login"
dashboard:
heading_actions:
close: "Close"
create_work: "Create work"
select_type_of_work: "Select type of work"
title: "My Dashboard"
view_collections: "View Collections"
create_collection: "Create Collection"
Expand Down
6 changes: 6 additions & 0 deletions lib/generators/sufia/work/templates/locale.en.yml.erb
@@ -1,4 +1,10 @@
en:
sufia:
select_type:
<%= file_name %>:
name: "<%= human_name.titleize %>"
description: "<%= human_name %> works"
icon_class: 'fa fa-file-text-o'
simple_form:
labels:
<%= file_name %>:
Expand Down
21 changes: 21 additions & 0 deletions spec/presenters/sufia/select_type_presenter_spec.rb
@@ -0,0 +1,21 @@
require 'spec_helper'

RSpec.describe Sufia::SelectTypePresenter do
let(:instance) { described_class.new(model) }
let(:model) { GenericWork }

describe "#icon_class" do
subject { instance.icon_class }
it { is_expected.to eq 'fa fa-file-text-o' }
end

describe "#description" do
subject { instance.description }
it { is_expected.to eq 'Generic work works' }
end

describe "#name" do
subject { instance.name }
it { is_expected.to eq 'Generic Work' }
end
end
33 changes: 33 additions & 0 deletions spec/views/dashboard/create_work_action.html.erb_spec.rb
@@ -0,0 +1,33 @@
require 'spec_helper'

RSpec.describe 'dashboard/create_work_action.html.erb', type: :view do
let(:classification) { double }
before do
allow(CurationConcerns::QuickClassificationQuery).to receive(:new).and_return(classification)
allow(view).to receive(:current_user).and_return(double)
allow(classification).to receive(:authorized_models).and_return(results)
end

context "when we have more than one model" do
let(:results) { [GenericWork, double] }
before do
stub_template 'dashboard/_select_work_type.html.erb' => 'SelectType'
render 'dashboard/create_work_action', classification: classification
end
it "renders the select template" do
expect(rendered).to have_content 'SelectType'
end
end

context "when we have one model" do
let(:results) { [GenericWork] }
before do
allow(classification).to receive(:each).and_yield(GenericWork)
render 'dashboard/create_work_action', classification: classification
end
it "doesn't draw the modal" do
expect(rendered).not_to include "modal"
expect(rendered).to have_link "Create Work", href: '/concern/generic_works/new'
end
end
end
36 changes: 36 additions & 0 deletions spec/views/dashboard/select_work_type.html.erb_spec.rb
@@ -0,0 +1,36 @@
require 'spec_helper'

RSpec.describe 'dashboard/select_work_type.html.erb', type: :view do
let(:presenter) { instance_double Sufia::SelectTypeListPresenter }
let(:row1) do
instance_double(Sufia::SelectTypePresenter,
icon_class: 'icon',
name: 'Generic Work',
description: 'Workhorse',
concern: GenericWork)
end
let(:row2) do
instance_double(Sufia::SelectTypePresenter,
icon_class: 'icon',
name: 'Book',
description: 'Book of things',
concern: other_model)
end
let(:other_model) do
double(persisted?: false,
model_name: double(singular_route_key: 'foo', param_key: 'foo'))
end
before do
allow(presenter).to receive(:each).and_yield(row1).and_yield(row2)
allow(view).to receive(:new_polymorphic_path).and_return('/foos/new')
render 'dashboard/select_work_type', presenter: presenter
end
let(:results) { [GenericWork, other_model] }

it "draws the modal" do
expect(rendered).to have_selector "#worktypes-to-create.modal"
expect(rendered).to have_selector 'a[data-toggle="modal"][data-target="#worktypes-to-create"]'
expect(rendered).to have_link "Book", href: '/foos/new'
expect(rendered).to have_link "Generic Work"
end
end

0 comments on commit a070f1d

Please sign in to comment.