Skip to content

Commit

Permalink
Add option to disable/enable self-deposit (#285)
Browse files Browse the repository at this point in the history
* add site option to check if deposits are enabled

* toggle deposit upload links in views

* add deposit enable/disable button to admin

* refactor deposits enable site option code

* fix incorrect paginate variable name

* add button styling

* use local: true to disable ajax form submission

Co-authored-by: Eric O <[email protected]>

* create default value for site option on first access

---------

Co-authored-by: Eric O <[email protected]>
  • Loading branch information
JackBlackLight and elohanlon authored Sep 16, 2024
1 parent 8aaec91 commit 8a4815d
Show file tree
Hide file tree
Showing 27 changed files with 1,274 additions and 1,031 deletions.
20 changes: 20 additions & 0 deletions app/controllers/admin/site_options_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Admin
class SiteOptionsController < AdminController
load_and_authorize_resource class: SiteOption

def update
option_keys = params.keys.select { |key| SiteOption::OPTIONS.include?(key) }
option_keys.each do |option_key|
option = SiteOption.find_by(name: option_key)
if option.nil?
option = SiteOption.create!(name: option_key,
value: SiteOption.default_value_for_option(option_key))
end
option.update(value: params[option_key])
redirect_back fallback_location: { controller: '/admin', action: 'index' }
end
end
end
end
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ApplicationController < ActionController::Base

helper :all # include all helpers, all the time
helper_method :fedora_config # share some methods w/ views via helpers
helper_method :deposits_enabled?

rescue_from CanCan::AccessDenied do |exception|
if current_user.nil?
Expand All @@ -21,6 +22,10 @@ class ApplicationController < ActionController::Base
end
end

def deposits_enabled?
SiteOption.deposits_enabled
end

def fedora_config
@fedora_config ||= Rails.application.config_for(:fedora)
end
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def transform_values
end

configure_blacklight do |config|
allowed_params = [:deposits_enabled]
config.search_state_fields.concat(allowed_params)

# Delete default blacklight components we aren't using
config.index.document_actions.delete(:bookmark)

Expand Down
14 changes: 9 additions & 5 deletions app/controllers/uploads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ def index
# GET /upload/new
# Show upload form, if user is logged in.
def new
@deposit ||= Deposit.new(
creators: [
{ first_name: current_user.first_name || nil, last_name: current_user.last_name || nil, uni: current_user.uid }
]
)
if deposits_enabled?
@deposit ||= Deposit.new(
creators: [
{ first_name: current_user.first_name || nil, last_name: current_user.last_name || nil, uni: current_user.uid }
]
)
else
redirect_to root_path
end
end

# POST /upload
Expand Down
1 change: 0 additions & 1 deletion app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
// layout file, like app/views/layouts/application.html.erb


// Uncomment to copy all static images under ../images to the output folder and reference
// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
// or the `imagePath` JavaScript helper below.
Expand Down
22 changes: 22 additions & 0 deletions app/models/site_option.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

class SiteOption < ApplicationRecord
DEPOSITS_ENABLED = 'deposits_enabled'
OPTIONS = [DEPOSITS_ENABLED].freeze

validates :name, presence: { inclusion: { in: OPTIONS } }
validates :value, inclusion: { in: [true, false] }

def self.deposits_enabled
find_by(name: DEPOSITS_ENABLED)&.value
end

def self.default_value_for_option(option_key)
unless OPTIONS.include?(option_key)
raise ArgumentError, "Invalid option key: #{option_key}. Must be one of: #{OPTIONS.join(', ')}"
end

# options will default to false so that admins must turn the option on
false
end
end
5 changes: 4 additions & 1 deletion app/views/_user_util_links.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@
<% end %>

<li>
<%= link_to uploads_path, class: 'share-button' do %>

<% if deposits_enabled? %>
<%= link_to uploads_path, class: 'share-button' do %>
<%= t('blacklight.header_links.upload') %>
<span><%= t('blacklight.header_links.upload_full_text') %></span>
<% end %>
</li>
<% end %>
4 changes: 2 additions & 2 deletions app/views/admin/agreements/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<%= link_to('Download CSV File', '/admin/agreements.csv') %>
</p>

<div class="pagination"> <%= will_paginate @posts %></div>
<div class="pagination"> <%= will_paginate @agreements %></div>
<table class="table">
<thead>
<tr>
Expand Down Expand Up @@ -36,4 +36,4 @@
<% end %>
</tbody>
</table>
<div class="pagination"> <%= will_paginate @posts %></div>
<div class="pagination"> <%= will_paginate @agreements %></div>
13 changes: 13 additions & 0 deletions app/views/admin/deposits/_enable_deposits_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="col-4 text-right">
<%= form_with url: admin_site_options_path, method: :patch, local: true do |form| %>
<% if deposits_enabled? %>
<% action_label = "Disable Deposits" %>
<% enable = false %>
<% else %>
<% action_label = "Enable Deposits" %>
<% enable = true %>
<% end %>
<%= form.hidden_field SiteOption::DEPOSITS_ENABLED, value: enable %>
<%= form.submit action_label, class: "btn btn-secondary"%>
<% end %>
</div>
12 changes: 9 additions & 3 deletions app/views/admin/deposits/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<h2>Self-Deposits</h2>
<div class="pagination"> <%= will_paginate @posts %></div>
<div class="row align-items-center">
<div class="col-8">
<h2>Self-Deposits <%= @posts %>
</h2>
</div>
<%= render :partial=>'enable_deposits_form' %>
</div>
<div class="pagination"> <%= will_paginate @deposits %></div>
<table class="table">
<thead>
<tr>
Expand Down Expand Up @@ -28,4 +34,4 @@
<% end %>
</tbody>
</table>
<div class="pagination"> <%= will_paginate @posts %></div>
<div class="pagination"> <%= will_paginate @deposits %></div>
2 changes: 2 additions & 0 deletions app/views/catalog/_home_text.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
</div>
<p>Discover what researchers and scholars are sharing in Academic Commons. Browse highlighted contributions from across Columbia and its affiliate institutions.</p>
<% end %>
<% if deposits_enabled? %>
<a href="/upload" class="col-md-6 tile upload">
<div class="top">
<%= svg 'icon-upload' %>
<h2>Upload Your Research</h2>
</div>
<p>Are you a member of the Columbia community? Want your research to be more discoverable and accessible? Upload it to Academic Commons today.</p>
</a>
<% end %>
</div>

<div class="row homepage-info">
Expand Down
2 changes: 2 additions & 0 deletions app/views/layouts/admin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
<i class="fas fa-list-ul" aria-hidden></i> <%= t('blacklight.dashboard_links.my_works') %>
<% end %>
</li>
<% if deposits_enabled? %>
<li>
<%= link_to uploads_path do %>
<i class="far fa-plus-square" aria-hidden></i> <%= t('blacklight.dashboard_links.add_new_work') %>
<% end %>
</li>
<% end %>
<li class="active">
<%= link_to admin_path do %>
<i class="fas fa-cog" aria-hidden></i> <%= t('blacklight.dashboard_links.admin') %>
Expand Down
10 changes: 6 additions & 4 deletions app/views/layouts/dashboard.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
<i class="fas fa-list-ul" aria-hidden></i> <%= t('blacklight.dashboard_links.my_works') %>
<% end %>
</li>
<li class="<%= active_class(new_upload_path) %>">
<%= link_to uploads_path do %>
<i class="far fa-plus-square" aria-hidden></i> <%= t('blacklight.dashboard_links.add_new_work') %>
<% if deposits_enabled? %>
<li class="<%= active_class(new_upload_path) %>">
<%= link_to uploads_path do %>
<i class="far fa-plus-square" aria-hidden></i> <%= t('blacklight.dashboard_links.add_new_work') %>
<% end %>
</li>
<% end %>
</li>
<% if current_user.admin? %>
<li class="<%= active_class(admin_path) %>">
<%= link_to admin_path do %>
Expand Down
4 changes: 3 additions & 1 deletion app/views/user/my_works.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
<% if @current_works_with_stats.empty? %>
<h3>Available Works</h3>
<h4>No works available </h4>
<p>Visit our <%= link_to 'upload page', uploads_path %> to contribute your works.</p>
<% if deposits_enabled? %>
<p>Visit our <%= link_to 'upload page', uploads_path %> to contribute your works.</p>
<% end %>
<% else %>
<table class="table">
<tr>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
get 'author_affiliation_report/create'
resources :request_agreements, only: [:new, :create]
resource :alert_message, only: [:edit, :update]
resource :site_options, only: [:edit, :update]
resources :deposits, only: [:index, :show]
resources :agreements, only: :index
resources :email_preferences
Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20240820183333_create_site_options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateSiteOptions < ActiveRecord::Migration[6.0]
def change
create_table :site_options do |t|
t.string :name
t.boolean :value
end
end
end
7 changes: 6 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2022_12_08_202313) do
ActiveRecord::Schema.define(version: 2024_08_20_183333) do

create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
Expand Down Expand Up @@ -179,6 +179,11 @@
t.index ["updated_at"], name: "index_sessions_on_updated_at"
end

create_table "site_options", force: :cascade do |t|
t.string "name"
t.boolean "value"
end

create_table "statistics", force: :cascade do |t|
t.string "session_id"
t.string "event", null: false
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"popper.js": "1.16.1",
"readmore-js": "^3.0.0-beta-1",
"resolve-url-loader": "^4.0.0",
"turbolinks": "^5.2.0",
"typeahead.js": "^0.11.1",
"url-loader": "^4.1.1",
"video.js": "^7.17.0",
Expand Down
21 changes: 18 additions & 3 deletions spec/controllers/uploads_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'rails_helper'

RSpec.describe UploadsController, type: :controller do
describe 'POST create' do
context 'when user logged in ' do
include_context 'non-admin user for controller'
context 'when user logged in ' do
include_context 'non-admin user for controller'

describe 'deposits are enabled' do
let(:deposit) { Deposit.first }

let(:http_request) do
Expand All @@ -24,6 +24,10 @@
}
end

before do
SiteOption.create!(name: SiteOption::DEPOSITS_ENABLED, value: true)
end

it 'response is successful' do
http_request
expect(response).to have_http_status :success
Expand All @@ -48,5 +52,16 @@
expect(deposit.user).to eql controller.current_user
end
end

describe 'when deposits are disabled' do
before do
SiteOption.create!(name: SiteOption::DEPOSITS_ENABLED, value: false)
get :new
end

it 'redirects to home page' do
expect(response).to redirect_to(root_path)
end
end
end
end
8 changes: 8 additions & 0 deletions spec/factories/site_option.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

FactoryBot.define do
factory :site_option do
name { '' }
value { false }
end
end
1 change: 1 addition & 0 deletions spec/features/agreement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
include_context 'non-admin user for feature'

before do
SiteOption.create!(name: SiteOption::DEPOSITS_ENABLED, value: true)
visit agreement_path
end

Expand Down
Loading

0 comments on commit 8a4815d

Please sign in to comment.