Skip to content

Commit

Permalink
add restful actions for tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
fosterfarrell9 authored and tynsh committed Sep 18, 2020
1 parent 113b7a6 commit 80383ae
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/assets/javascripts/lectures.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,16 @@ $(document).on 'turbolinks:load', ->
$('#newLectureSort').hide()
return

$(document).on 'click', '#cancelNewTutorial', ->
$('.tutorialRow[data-id="0"]').remove()
return

return

# clean up everything before turbolinks caches
$(document).on 'turbolinks:before-cache', ->
$('.lecture-tag').removeClass('badge-warning').addClass('badge-light')
$('.lecture-lesson').removeClass('badge-info').addClass('badge-secondary')
$(document).off 'change', '#lecture_course_id'
$(document).off 'click', '#cancelNewTutorial'
return
42 changes: 42 additions & 0 deletions app/controllers/tutorials_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# TutorialsController
class TutorialsController < ApplicationController
before_action :set_tutorial, only: [:edit, :destroy, :update, :cancel_edit]
authorize_resource

def new
@tutorial = Tutorial.new
lecture = Lecture.find_by_id(params[:lecture_id])
@tutorial.lecture = lecture
end

def create
@tutorial = Tutorial.new(tutorial_params)
@tutorial.save
end

def edit
end

def update
@tutorial.update(tutorial_params)
end

def destroy
@tutorial.destroy
end

def cancel_edit
end

private

def set_tutorial
@tutorial = Tutorial.find_by_id(params[:id])
return if @tutorial.present?
redirect_to :root, alert: I18n.t('controllers.no_tutorial')
end

def tutorial_params
params.require(:tutorial).permit(:title, :tutor_id, :lecture_id)
end
end
7 changes: 7 additions & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ def initialize(user)

cannot :read, Term

can :new, Tutorial

can [:edit, :create, :update, :destroy,
:cancel_edit_tutorial], Tutorial do |tutorial|
tutorial.lecture.edited_by?(user)
end

cannot :read, User
can :update, User do |u|
user == u
Expand Down
30 changes: 30 additions & 0 deletions app/views/lectures/edit/_exercise_classes.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,36 @@
aria-labelledby="heading"
data-parent="#lectureAccordion">
<div class="card-body">
<div class="row mb-3">
<div class="col-12 text-center">
<%= link_to t('admin.tutorial.new'),
new_tutorial_path(params: { lecture_id: lecture.id }),
class: 'btn btn-sm btn-primary',
remote: true %>
</div>
</div>
<div class="row border border-botttom py-2 mx-3"
id="tutorialListHeader">
<div class="col-4">
<h6>
<%= t('basics.title') %>
</h6>
</div>
<div class="col-4">
<h6>
<%= t('basics.tutor') %>
</h6>
</div>
<div class="col-4">
<h6>
<%= t('basics.action') %>
</h6>
</div>
</div>
<% lecture.tutorials.each do |t| %>
<%= render partial: 'tutorials/row',
locals: { tutorial: t } %>
<% end %>
</div>
</div>
</div>
33 changes: 33 additions & 0 deletions app/views/tutorials/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%= form_with model: tutorial do |f| %>
<div class="form-row tutorialRow py-2 mx-3 border"
data-id="<%= tutorial.id.to_i %>">
<div class="form-group col-4">
<%= f.text_field :title,
class: 'form-control' %>
</div>
<div class="form-group col-4">
<%= f.select :tutor_id,
options_for_select(User.select_editors, tutorial.tutor_id),
{},
{ class: 'form-control' } %>
</div>
<div class="form-group col-4">
<%= f.submit t('buttons.save'),
class: 'btn btn-sm btn-primary ml-2' %>
<% if tutorial.persisted? %>
<%= link_to t('buttons.cancel'),
cancel_edit_tutorial_path(tutorial),
class: 'btn btn-sm btn-secondary',
remote: true %>
<% else %>
<button class="btn btn-sm btn-secondary"
id="cancelNewTutorial"
role="button">
<%= t('buttons.cancel') %>
</button>
<% end %>
</div>
</div>
<%= f.hidden_field :lecture_id,
value: tutorial.lecture.id %>
<% end %>
32 changes: 32 additions & 0 deletions app/views/tutorials/_row.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<div class="row tutorialRow py-2 mx-3 border" data-id="<%= tutorial.id.to_i %>">
<div class="col-4">
<%= tutorial.title %>
</div>
<div class="col-4">
<%= tutorial.tutor.name %>
</div>
<div class="col-4">
<%= link_to edit_tutorial_path(tutorial),
class: 'text-dark',
data: { toggle: 'tooltip',
placement: 'botttom' },
style: 'text-decoration: none;',
title: t('buttons.edit'),
remote: true do %>
<i class="far fa-lg fa-edit mr-2">
</i>
<% end %>
<%= link_to tutorial_path(tutorial),
class: 'text-dark',
data: { toggle: 'tooltip',
placement: 'botttom',
confirm: t('confirmation.generic') },
style: 'text-decoration: none;',
title: t('buttons.delete'),
method: :delete,
remote: true do %>
<i class="fas fa-lg fa-trash-alt">
</i>
<% end %>
</div>
</div>
3 changes: 3 additions & 0 deletions app/views/tutorials/cancel_edit.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$('.tutorialRow[data-id="<%= @tutorial.id %>')
.replaceWith('<%= j render partial: "tutorials/row",
locals: { tutorial: @tutorial } %>')
3 changes: 3 additions & 0 deletions app/views/tutorials/create.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$('.tutorialRow[data-id="0"')
.replaceWith('<%= j render partial: "tutorials/row",
locals: { tutorial: @tutorial } %>')
1 change: 1 addition & 0 deletions app/views/tutorials/destroy.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$('.tutorialRow[data-id="<%= @tutorial.id %>"').remove()
3 changes: 3 additions & 0 deletions app/views/tutorials/edit.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$('.tutorialRow[data-id="<%= @tutorial.id %>"')
.replaceWith('<%= j render partial: "tutorials/form",
locals: { tutorial: @tutorial } %>')
3 changes: 3 additions & 0 deletions app/views/tutorials/new.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$('#tutorialListHeader')
.after('<%= j render partial: "tutorials/form",
locals: { tutorial: @tutorial } %>')
3 changes: 3 additions & 0 deletions app/views/tutorials/update.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$('.tutorialRow[data-id="<%= @tutorial.id %>')
.replaceWith('<%= j render partial: "tutorials/row",
locals: { tutorial: @tutorial } %>')
4 changes: 4 additions & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,8 @@ de:
export_probes: Quizdaten exportieren
start: Startdatum
end: Enddatum
tutorial:
new: 'Neues Tutorium anlegen'
profile:
account: 'Account'
name: 'Anzeigename'
Expand Down Expand Up @@ -2485,6 +2487,7 @@ de:
programs: 'Studiengänge'
visibility: 'Zusatzmaterialien'
exercise_classes: 'Übungsbetrieb'
tutor: 'TutorIn'
access:
unpublished: 'unveröffentlicht'
all: 'frei'
Expand Down Expand Up @@ -2670,6 +2673,7 @@ de:
no_subject: 'Ein Fach mit der angeforderten id existiert nicht.'
no_program: 'Ein Studiengang mit der angeforderten id existiert nicht.'
no_division: 'Ein Teilbereich mit der angeforderten id existiert nicht.'
no_tutorial: 'Ein Tutorium mit der angeforderten id existiert nicht.'
activerecord:
errors:
no_valid_url: 'Das ist keine gültige URL.'
Expand Down
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,8 @@ en:
export_probes: Export Probes
start: Start Date
end: End Date
tutorial:
new: 'Add tutorial'
profile:
account: 'Account'
name: 'Display name'
Expand Down Expand Up @@ -2344,6 +2346,7 @@ en:
programs: 'Programs'
visibility: 'Additional material'
exercise_classes: 'Exercise Classes'
tutor: 'Tutor'
warnings:
save_before: 'Before you continue, you need to save or discard your changes'
unsaved_changes: >
Expand Down Expand Up @@ -2525,6 +2528,7 @@ en:
no_answer: 'There is no subject with this id.'
no_program: 'There is no program with this id.'
no_division: 'There is no division with this id.'
no_tutorial: 'There is no tutorial with this id.'
activerecord:
errors:
no_valid_url: 'This is not a valid URL.'
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@
as: 'postprocess_tags'
resources :tags

get 'tutorials/:id/cancel_edit', to: 'tutorials#cancel_edit',
as: 'cancel_edit_tutorial'

resources :tutorials, only: [ :new, :edit, :create, :update, :destroy]

get 'sections/list_tags', to: 'sections#list_tags',
as: 'list_section_tags'
get 'sections/:id/display', to: 'sections#display',
Expand Down

0 comments on commit 80383ae

Please sign in to comment.