Skip to content

Commit

Permalink
Merge branch '8314-public-citations' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Oct 11, 2024
2 parents 25142ac + 1b1bdf3 commit 9677315
Show file tree
Hide file tree
Showing 30 changed files with 560 additions and 93 deletions.
33 changes: 33 additions & 0 deletions app/assets/stylesheets/responsive/_citations.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.citations-types {
border-top: 1px solid #d1d1d1;
border-bottom: 1px solid #d1d1d1;
margin-top: 2em;
margin-bottom: 1em;
}

ul.citations-list {
list-style: none;
padding-left: 0;

li.citations-list__citation {
padding: 1em 0;
border-bottom: 1px solid #d1d1d1;

display: flex;
align-items: flex-start;
gap: 1em;

.citation_icon {
flex-shrink: 0;
}

.citation__title {
flex-basis: 50%;
word-break: break-word;
}

.citation__description {
flex-basis: 50%;
}
}
}
2 changes: 1 addition & 1 deletion app/assets/stylesheets/responsive/_sidebar_layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}

.sidebar__section.citations {
.citations-list {
.citations-list--compact {
overflow-wrap: break-word;
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/assets/stylesheets/responsive/_sidebar_style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
}

.sidebar__section.citations {
.citations-list {
.citations-list--compact {
list-style: none;
padding: 0;
}
Expand Down
2 changes: 2 additions & 0 deletions app/assets/stylesheets/responsive/all.scss
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
@import "responsive/_notes_layout.scss";
@import "responsive/_notes_styles.scss";

@import "responsive/_citations";

@import "responsive/alaveteli_pro/_pro_layout";
@import "responsive/alaveteli_pro/_pro_style";

Expand Down
28 changes: 28 additions & 0 deletions app/controllers/admin/citations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
class Admin::CitationsController < AdminController
before_action :find_citation, except: :index

def index
@citations =
Citation.
order(created_at: :desc).
paginate(page: params[:page], per_page: 50)
end

def edit
end

def update
if @citation.update(citation_params)
redirect_to admin_citations_path, notice: 'Citation updated successfully.'
else
render :edit
end
end

def destroy
@citation.destroy
redirect_to admin_citations_path, notice: 'Citation deleted successfully.'
end

private

def find_citation
@citation = Citation.find(params[:id])
end

def citation_params
params.require(:citation).permit(:source_url, :title, :description)
end
end
21 changes: 18 additions & 3 deletions app/controllers/citations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@
# Controller to create a new Citation for an InfoRequest or an InfoRequestBatch.
#
class CitationsController < ApplicationController
before_action :authenticate
before_action :load_resource_and_authorise
before_action :set_in_pro_area
before_action :authenticate, except: :index
before_action :load_resource_and_authorise, except: :index
before_action :set_in_pro_area, except: :index

skip_before_action :html_response, only: :index

def index
per_page = 10
page = get_search_page_from_params

@citations = Citation.not_embargoed.order(created_at: :desc).
paginate(page: page, per_page: per_page)

respond_to do |format|
format.html { @has_json = true }
format.json { render json: @citations }
end
end

def new
@citation = current_user.citations.build
Expand Down
16 changes: 12 additions & 4 deletions app/helpers/admin/citations_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ module Admin::CitationsHelper
ICONS = {
journalism: '🗞️',
campaigning: '📣',
academic: '🎓',
research: '📚',
other: '🌐'
}.with_indifferent_access.freeze

def citation_title(citation)
citation.title.presence || citation.source_url
end

def citation_icon(citation)
citation_icon_for_type(citation.type)
end

def citation_icon_for_type(type)
html_attrs = {
title: citation.type.humanize,
class: "citation-icon citation-icon--#{citation.type}"
title: type.humanize,
class: "citation-icon citation-icon--#{type}"
}

tag.span(ICONS.fetch(citation.type), **html_attrs)
tag.span(ICONS.fetch(type), **html_attrs)
end
end
9 changes: 9 additions & 0 deletions app/helpers/admin/link_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ def category_both_links(category)
title: admin_title)
end

def citation_both_links(citation)
title = 'View citation'
icon = eye

link_to(icon, citation.source_url, title: title) + ' ' +
link_to(citation.source_url, edit_admin_citation_path(citation),
title: admin_title)
end

def admin_title
'View full details'
end
Expand Down
24 changes: 24 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,28 @@ class ApplicationRecord < ActiveRecord::Base
def self.admin_title
name
end

def self.belongs_to(name, scope = nil, **options)
if options.key?(:via)
via = options.delete(:via)
polymorphic_association = reflect_on_association(via)

unless polymorphic_association&.polymorphic?
raise ArgumentError, "Association #{via} must be polymorphic"
end

options[:foreign_key] ||= polymorphic_association.foreign_key
options[:class_name] ||= name.to_s.classify

scope = -> {
where(
polymorphic_association.active_record.table_name => {
polymorphic_association.foreign_type => options[:class_name]
}
)
}
end

super(name, scope, **options)
end
end
38 changes: 34 additions & 4 deletions app/models/citation.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# == Schema Information
# Schema version: 20210114161442
# Schema version: 20241007090524
#
# Table name: citations
#
Expand All @@ -11,31 +11,48 @@
# type :string
# created_at :datetime not null
# updated_at :datetime not null
# title :string
# description :text
#

##
# A Citation of an InfoRequest or InfoRequestBatch in news stories or an
# academic paper
# A Citation of an InfoRequest or InfoRequestBatch
#
class Citation < ApplicationRecord
include Rails.application.routes.url_helpers
include LinkToHelper

self.inheritance_column = nil

belongs_to :user, inverse_of: :citations
belongs_to :citable, polymorphic: true

belongs_to :info_request, via: :citable
belongs_to :info_request_batch, via: :citable

validates :user, :citable, presence: true
validates :citable_type, inclusion: { in: %w(InfoRequest InfoRequestBatch) }
validates :source_url, length: { maximum: 255,
message: _('Source URL is too long') },
format: { with: /\Ahttps?:\/\/.*\z/,
message: _('Please enter a Source URL') }
validates :type, inclusion: { in: %w(journalism academic campaigning other),
validates :type, inclusion: { in: %w(journalism research campaigning other),
message: _('Please select a type') }

scope :newest, ->(limit = 1) do
order(created_at: :desc).limit(limit)
end

scope :not_embargoed, -> do
left_joins(info_request: :embargo, info_request_batch: []).
where(citable_type: 'InfoRequest').
merge(InfoRequest.not_embargoed).
or(
where(citable_type: 'InfoRequestBatch').
merge(InfoRequestBatch.not_embargoed)
)
end

scope :for_request, ->(info_request) do
where(citable: info_request).
or(where(citable: info_request.info_request_batch))
Expand All @@ -49,4 +66,17 @@ class Citation < ApplicationRecord
def applies_to_batch_request?
citable.is_a?(InfoRequestBatch)
end

def as_json(_options)
citable_path = case citable
when InfoRequest
request_path(citable)
when InfoRequestBatch
info_request_batch_path(citable)
end

attributes.
except('user_id', 'citable_id', 'citable_type').
merge(citable_path: citable_path)
end
end
2 changes: 1 addition & 1 deletion app/views/admin/citations/_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="row">
<span class="item-title span6">
<%= citation_icon(citation) %>
<tt><%= link_to citation.source_url, citation.source_url %></tt>
<tt><%= both_links(citation) %></tt>
</span>

<span class="item-metadata span6">
Expand Down
41 changes: 41 additions & 0 deletions app/views/admin/citations/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<div class="row">
<div class="span12">
<div class="page-header">
<h1><%= @title = 'Edit citation' %></h1>
</div>
</div>
</div>

<%= form_for [:admin, @citation], class: 'form form-inline' do |f| %>
<div class="control-group">
<%= f.label :source_url %>
<div class="controls">
<%= f.text_field :source_url, class: 'form-control' %>
</div>
</div>

<div class="control-group">
<%= f.label :title %>
<div class="controls">
<%= f.text_field :title, class: 'form-control' %>
</div>
</div>

<div class="control-group">
<%= f.label :description, class: 'control-label' %>
<div class="controls">
<%= f.text_area :description, class: 'input-block-level', rows: 10 %>
</div>
</div>

<div class="form-actions">
<%= f.submit 'Update ciation', class: 'btn btn-success' %>
</div>
<% end %>
<%= form_for [:admin, @citation], class: 'form form-inline', method: 'delete' do |f| %>
<%= f.submit 'Destroy citation',
class: 'btn btn-danger',
data: { confirm: 'Are you sure? This is irreversible.' } %>
(this is permanent!)
<% end %>
32 changes: 27 additions & 5 deletions app/views/citations/_citation.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
<li class="citations-list__citation">
<%=
MySociety::Format.make_clickable(
citation.source_url, contract: true, nofollow: true
).html_safe
%>
<div class="citation__icon">
<%= citation_icon(citation) %>
</div>

<div class="citation__title">
<strong><%= link_to citation_title(citation), citation.source_url, rel: 'nofollow' %></strong>
<br>
<% case citation.citable %>
<% when InfoRequest %>
<%= _('<a href="{{info_request_url}}">{{title}}</a> requested from <a href="{{public_body_url}}">{{public_body_name}}</a> on {{date}}',
title: citation.citable.title,
info_request_url: request_path(citation.citable),
public_body_name: citation.citable.public_body.name,
public_body_url: public_body_path(citation.citable.public_body),
date: simple_date(citation.citable.created_at)) %>
<% when InfoRequestBatch %>
<%= _('<a href="{{info_request_batch_url}}">{{title}}</a> part of a batch sent to {{count}} authorities on {{date}}',
title: citation.citable.title,
info_request_batch_url: info_request_batch_path(citation.citable),
count: citation.citable.public_bodies.count,
date: simple_date(citation.citable.created_at)) %>
<% end %>
</div>

<div class="citation__description">
<%= citation.description %>
</div>
</li>
3 changes: 3 additions & 0 deletions app/views/citations/_compact.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<li class="citations-list__citation--compact">
<%= link_to citation_title(citation), citation.source_url, rel: 'nofollow' %>
</li>
Loading

0 comments on commit 9677315

Please sign in to comment.