Skip to content

Commit

Permalink
Merge branch 'main' into 187376318-ticket-purchases-curr-conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
tiffanylamm authored Apr 24, 2024
2 parents aef231a + ba130c8 commit f514259
Show file tree
Hide file tree
Showing 18 changed files with 120 additions and 34 deletions.
32 changes: 22 additions & 10 deletions app/controllers/admin/commercials_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,40 @@ def render_commercial
# Received a file from user
# Reads file and creates commercial for event
# File content example:
# EventID:MyURL
# EventID, Title, URL
def mass_upload
errors = Commercial.read_file(params[:file]) if params[:file]

if !params[:file]
flash[:error] = 'Empty file detected while adding materials to Event'
elsif errors.all? { |_k, v| v.blank? }
flash[:notice] = 'Successfully added materials.'
else
errors_text = ''
errors_text += 'Unable to find event with ID: ' + errors[:no_event].join(', ') + '. ' if errors[:no_event].any?
if errors[:validation_errors].any?
errors_text += 'There were some errors: ' + errors[:validation_errors].join('. ')
end
elsif errors.present?
errors_text = aggregate_errors(errors)
flash[:notice] = if errors_text.length > 4096
'Errors are too long to be displayed. Please check the logs.'
else
errors_text
end

flash[:error] = errors_text
else
flash[:notice] = 'Successfully added materials.'
end
redirect_back(fallback_location: root_path)
end

private

# Aggregate errors and ensure that they do not exceed 4 KB in total size
def aggregate_errors(errors)
errors_text = ''
if errors[:no_event].any?
errors_text += 'Unable to find events with IDs: ' + errors[:no_event].join(', ') + '. '
end
if errors[:validation_errors].any?
errors_text += 'Validation errors: ' + errors[:validation_errors].join('. ')
end
errors_text
end

def commercial_params
params.require(:commercial).permit(:title, :url)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/event_types_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def destroy

def event_type_params
params.require(:event_type).permit(:title, :length, :minimum_abstract_length, :maximum_abstract_length,
:submission_template, :color, :conference_id, :description)
:submission_template, :color, :conference_id, :description, :enable_public_submission)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/proposals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,6 @@ def event_params
end

def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :username)
params.require(:user).permit(:email, :password, :password_confirmation, :username, :is_admin)
end
end
8 changes: 6 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,12 @@ def volunteer_links(event)
end, ', ')
end

def event_types_sentence(conference)
conference.event_types.map { |et| et.title.pluralize }.to_sentence
def event_types_sentence(conference, is_admin)
if is_admin
conference.event_types.map { |et| et.title.pluralize }.to_sentence
else
conference.event_types.available_for_public.map { |et| et.title.pluralize }.to_sentence
end
end

def sign_in_path
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/event_types_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def event_type_select_options(event_types = {})
"#{type.title} - #{show_time(type.length)}",
type.id,
{ data: {
min_words: type.minimum_abstract_length,
max_words: type.maximum_abstract_length,
instructions: type.submission_template
min_words: type.minimum_abstract_length,
max_words: type.maximum_abstract_length,
template: type.submission_template
} }
]
end
Expand Down
23 changes: 16 additions & 7 deletions app/models/commercial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# commercial_id :string
# commercialable_id :integer
#
require 'csv'

class Commercial < ApplicationRecord
require 'oembed'

Expand Down Expand Up @@ -46,17 +48,24 @@ def self.read_file(file)
errors[:no_event] = []
errors[:validation_errors] = []

file.read.each_line do |line|
# Get the event id (text before :)
id = line.match(/:/).pre_match.to_i
# Get the commercial url (text after :)
url = line.match(/:/).post_match
# Check if the file has a .csv extension
unless File.extname(file.original_filename).casecmp('.csv').zero?
errors[:validation_errors] << 'File must be a CSV.'
return errors
end

CSV.foreach(file.path, headers: true) do |row|
# You can access columns by their names if headers are included in the file
id = row['Event_ID'].to_i
title = row['Title']
url = row['URL']

event = Event.find_by(id: id)

# Go to next event, if the event is not found
# Go to next event if the event is not found
(errors[:no_event] << id) && next unless event

commercial = event.commercials.new(url: url)
commercial = event.commercials.new(title: title, url: url)
unless commercial.save
errors[:validation_errors] << ("Could not create materials for event with ID #{event.id} (" + commercial.errors.full_messages.to_sentence + ')')
end
Expand Down
7 changes: 7 additions & 0 deletions app/models/event_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ class EventType < ApplicationRecord
validates :color, format: /\A#[0-9A-F]{6}\z/

before_validation :capitalize_color
before_validation :strip_title

alias_attribute :name, :title

scope :available_for_public, -> { where(enable_public_submission: true) }

private

##
Expand All @@ -54,4 +57,8 @@ def capitalize_color
def conference_id
program.conference_id
end

def strip_title
self.title = title.strip unless title.nil?
end
end
2 changes: 1 addition & 1 deletion app/views/admin/cfps/_events_cfp.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
%dt
Event types:
%dd
= event_types_sentence(conference)
= event_types_sentence(conference, true)
%dt
Tracks:
%dd
Expand Down
3 changes: 3 additions & 0 deletions app/views/admin/event_types/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
= f.label :description
= f.text_area :description, class: 'form-control', rows: 5, data: { provide: 'markdown' }
.help-block= markdown_hint
.form-group
= f.label "Allow public submission?", for: :enable_public_submission
= f.check_box :enable_public_submission, class: 'switch-checkbox'
.form-group
= f.label :minimum_abstract_length
%abbr{title: 'This field is required'} *
Expand Down
5 changes: 4 additions & 1 deletion app/views/admin/event_types/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
%tr
%th Title
%th Description
%th Instructions
%th Enable Public Submission
%th Template
%th Length
%th Abstract Length
%th Color
Expand All @@ -23,6 +24,8 @@
= event_type.title
%td
= markdown(event_type.description)
%td
= event_type.enable_public_submission ? 'Yes' : 'No'
%td
= markdown(event_type.submission_template)
%td
Expand Down
9 changes: 5 additions & 4 deletions app/views/admin/events/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@
.form-group
= f.file_field 'file', as: :file
%span.help-block
Upload your file with data in the following format:
%b Event_ID:Commercial_Link
Upload your file.csv with data in the following format:
%b Event_ID,Title,URL
for instance:
%pre
11:https://youtube.com/myvideo
23:https://vimeo.com/myvideo
Event_ID,Title,URL
1,Session Recording,https://youtube.com/myvideo
2,Demo Video,https://vimeo.com/myvideo
.modal-footer
= f.submit nil, class: 'btn btn-primary'
.row
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/programs/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
%dt
Event types:
%dd
= event_types_sentence(@conference)
= event_types_sentence(@conference, true)
%dt
Tracks:
%dd
Expand Down
2 changes: 1 addition & 1 deletion app/views/proposals/_encouragement_text.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%p.lead
- if @program.event_types.any?
You can submit proposals for
= "#{event_types_sentence(@conference)}."
= "#{event_types_sentence(@conference, current_user&.is_admin || false)}."
- if @program.tracks.confirmed.cfp_active.any?
Proposals should fit in one of the
= "#{pluralize(@program.tracks.confirmed.cfp_active.count, 'track')}:"
Expand Down
7 changes: 5 additions & 2 deletions app/views/proposals/_submission_type_content_form.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
%p Please select a submission type, then fill in the abstract and extended details.

.form-group
= f.label :event_type_id, 'Type'
= f.select :event_type_id, event_type_select_options(@conference.program.event_types), { include_blank: false }, { class: 'select-help-toggle form-control' }
= f.label :event_type_id, "Type"
- if current_user&.is_admin
= f.select :event_type_id, event_type_select_options(@conference.program.event_types), { include_blank: false }, { class: 'select-help-toggle form-control' }
- else
= f.select :event_type_id, event_type_select_options(@conference.program.event_types.available_for_public), { include_blank: false }, { class: 'select-help-toggle form-control' }

- program.event_types.each do |event_type|
.help-block.event_event_type_id.collapse{ id: "#{dom_id(event_type)}-help" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddEnablePublicSubmissionToEventTypes < ActiveRecord::Migration[7.0]
def change
add_column :event_types, :enable_public_submission, :boolean, default: true, null: false
end
end
1 change: 1 addition & 0 deletions spec/factories/event_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
title { 'Example Event Type' }
length { 30 }
description { 'Example Event Description\nThis event type is an example.' }
enable_public_submission { true }
minimum_abstract_length { 0 }
maximum_abstract_length { 500 }
submission_template { 'Example Event Template _with_ **markdown**' }
Expand Down
18 changes: 18 additions & 0 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,22 @@
expect(conference_logo_url(conference2)).to include('2.png')
end
end

describe '#event_type_sentence' do
before do
create(:event_type, title: 'Keynote', program: conference.program, enable_public_submission: false)
end

context 'when a user is an admin' do
it 'returns a sentence with all event types' do
expect(helper.event_types_sentence(conference, true)).to eq 'Talks, Workshops, and Keynotes'
end
end

context 'when a user is not an admin' do
it 'returns a sentence only event types that allow public submission' do
expect(helper.event_types_sentence(conference, false)).to eq 'Talks and Workshops'
end
end
end
end
20 changes: 20 additions & 0 deletions spec/models/event_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,25 @@
expect(build(:event_type, program: conference.program, length: 37)).not_to be_valid
end
end

describe 'title' do
it 'removes leading and trailing whitespace from title' do
event_type = EventType.new(title: ' Movie ')
event_type.valid?
expect(event_type.title).to eq('Movie')
end
end
end

describe 'scope :available_for_public' do
it 'includes event types with enable_public_submission set to true' do
public_event_type = create(:event_type, program: conference.program, enable_public_submission: true)
expect(EventType.available_for_public).to include(public_event_type)
end

it 'excludes event types with enable_public_submission set to false' do
non_public_event_type = create(:event_type, program: conference.program, enable_public_submission: false)
expect(EventType.available_for_public).not_to include(non_public_event_type)
end
end
end

0 comments on commit f514259

Please sign in to comment.