-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
149 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Hyrax | ||
class SharedBehaviorsController < ApplicationController | ||
# Adds Hyrax behaviors to the controller. | ||
include Hyrax::WorksControllerBehavior | ||
include Hyrax::BreadcrumbsForWorks | ||
|
||
private | ||
|
||
def after_update_response | ||
download_stats = WorkDownloadStat.find_by(work_uid: presenter.solr_document.id) | ||
download_stats.update_attributes(title: presenter.solr_document.title.first) if download_stats | ||
super | ||
end | ||
end | ||
end |
9 changes: 9 additions & 0 deletions
9
app/decorators/controllers/hyrax/admin/stats_controller_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Hyrax | ||
Admin::StatsController.class_eval do | ||
def show | ||
super | ||
@works_to_display ||= ::WorkDownloadStat.where(owner_id: current_user.id) | ||
@total_downloads = @works_to_display.map(&:downloads).reduce(:+) | ||
end | ||
end | ||
end |
9 changes: 9 additions & 0 deletions
9
app/decorators/controllers/hyrax/downloads_controller_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Hyrax | ||
module DownloadsControllerDecorator | ||
# `asset` is inherited from hydra-head/hydra-core/app/controllers/concerns/hydra/controller/download_behavior.rb | ||
def send_content | ||
super | ||
WorkDownloadStat.new.log_download(asset) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class WorkDownloadStat < ApplicationRecord | ||
def current_work_stats(work) | ||
WorkDownloadStat.find_or_create_by(work_uid: work.parent.id) do |wds| | ||
wds.title = work.parent.title.first | ||
wds.owner_id = ::User.find_by(email: work.depositor).id | ||
end | ||
end | ||
|
||
## method below works with a FileSet as argument | ||
def log_download(record) | ||
raise TypeError, 'argument is not a file_set' unless record.file_set? | ||
stats = current_work_stats(record) | ||
total = stats.downloads + 1 | ||
dates_arr = stats.date << Time.now.utc | ||
stats.update_attributes(downloads: total, | ||
date: dates_arr) ## rewrites the array in db, could be made more efficient? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<h3>Total Downloads: <%= @total_downloads %></h3> | ||
<br/> | ||
<h3>Downloads per Work</h3> | ||
<ul> | ||
<% @works_to_display.each do |dwnld| %> | ||
<li><%= dwnld.title %><span class="count">: <%= dwnld.downloads %></span></li> | ||
<% end %> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class CreateWorkDownloadStats < ActiveRecord::Migration[5.1] | ||
def change | ||
create_table :work_download_stats do |t| | ||
t.string :work_uid | ||
t.string :title | ||
t.integer :downloads, default: 0 | ||
t.integer :owner_id | ||
t.datetime :date, array: true, default: [] | ||
|
||
t.timestamps null: false | ||
end | ||
|
||
add_index :work_download_stats, :owner_id | ||
add_index :work_download_stats, :work_uid | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe WorkDownloadStat, type: :model do | ||
let(:work) { create(:work_with_one_file) } | ||
let(:fs) { FileSet.find(work.file_sets[0].id) } | ||
let(:existing_record) do | ||
WorkDownloadStat.create(work_uid: fs.parent.id, | ||
title: fs.parent.title.first, | ||
owner_id: ::User.find_by(email: fs.depositor).id) | ||
end | ||
|
||
describe '#log_download' do | ||
it 'adds 1 to the downloads field when the Work is downloaded' do | ||
r = existing_record | ||
WorkDownloadStat.new.log_download(fs) | ||
expect(r.reload.downloads).to eq 1 | ||
end | ||
it 'raises an error if the argument is not a file_set' do | ||
expect { WorkDownloadStat.new.log_download(work) }.to raise_error(TypeError) | ||
end | ||
it 'records the datetime when the Work was downloaded' do | ||
r = existing_record | ||
WorkDownloadStat.new.log_download(fs) | ||
expect(r.reload.date[0]).to be_within(0.1).of(Time.now.utc) | ||
end | ||
end | ||
|
||
describe '#current_work_stats' do | ||
it 'finds a record when it exists' do | ||
r = existing_record | ||
expect(WorkDownloadStat.new.current_work_stats(fs)).to eq(r) | ||
end | ||
it 'creates a record when there is none yet for the specified Work' do | ||
expect(WorkDownloadStat.new.current_work_stats(fs).id).to eq(WorkDownloadStat.last.id) | ||
end | ||
|
||
context "when creating a new record" do | ||
it 'assigns a work_uid which is the same as the corresponding Work id' do | ||
expect(WorkDownloadStat.new.current_work_stats(fs).work_uid).to eq(work.id) | ||
end | ||
it 'assigns an owner_id' do ## TO DO: who should be the owner? currently is the `depositor` | ||
expect(WorkDownloadStat.new.current_work_stats(fs).owner_id).not_to be_nil | ||
end | ||
it 'has 0 downloads' do | ||
expect(WorkDownloadStat.new.current_work_stats(fs).downloads).to eq 0 | ||
end | ||
it 'does not create duplicates' do | ||
r = existing_record | ||
stats = WorkDownloadStat.new.current_work_stats(fs) | ||
expect(stats.id).to eq(r.id) | ||
end | ||
end | ||
end | ||
end |