Class: Course::Material::FoldersController

Inherits:
Controller show all
Defined in:
app/controllers/course/material/folders_controller.rb

Instance Method Summary collapse

Methods included from ApplicationSignInCallbacksConcern

#sign_in

Methods included from ApplicationPaginationConcern

#page_param

Methods included from ApplicationAnnouncementsConcern

#global_announcements, #unread_global_announcements

Methods included from ApplicationInstanceUserConcern

#current_instance_user

Methods included from ApplicationUserTimeZoneConcern

#set_time_zone

Methods included from ApplicationUserConcern

#handle_access_denied, #publicly_accessible?

Methods included from ApplicationComponentsConcern

#handle_component_not_found

Instance Method Details

#create_subfolderObject


40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/course/material/folders_controller.rb', line 40

def create_subfolder
  @subfolder = Course::Material::Folder.new(folder_params)
  @subfolder.course = current_course
  if @subfolder.save
    redirect_to course_material_folder_path(current_course, @folder),
                success: t('.success', name: @subfolder.name)
  else
    render 'new_subfolder'
  end
end

#destroyObject


25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/course/material/folders_controller.rb', line 25

def destroy
  parent_folder = @folder.parent
  if @folder.destroy
    redirect_to course_material_folder_path(current_course, parent_folder),
                success: t('.success', name: @folder.name)
  else
    redirect_to course_material_folder_path(current_course, parent_folder),
                danger: t('.failure', error: @folder.errors.full_messages.to_sentence)
  end
end

#downloadObject


73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/course/material/folders_controller.rb', line 73

def download
  @materials = (@folder.descendants.select { |f| can?(:read_owner, f) } + [@folder]).
               map { |f| f.materials.accessible_by(current_ability) }.flatten
  zip_filename = @folder.root? ? root_folder_name : @folder.name
  job = Course::Material::ZipDownloadJob.perform_later(@folder, @materials, zip_filename).job
  respond_to do |format|
    format.html { redirect_to(job_path(job)) }
    format.json { render json: { redirect_url: job_path(job) } }
  end
end

#editObject


12
13
# File 'app/controllers/course/material/folders_controller.rb', line 12

def edit
end

#new_materialsObject


51
52
# File 'app/controllers/course/material/folders_controller.rb', line 51

def new_materials
end

#new_subfolderObject


36
37
38
# File 'app/controllers/course/material/folders_controller.rb', line 36

def new_subfolder
  @subfolder = Course::Material::Folder.new
end

#showObject


5
6
7
8
9
10
# File 'app/controllers/course/material/folders_controller.rb', line 5

def show
  @subfolders = @folder.children.with_content_statistics.accessible_by(current_ability).
                order(:name).includes(:owner).without_empty_linked_folder
  # Don't display the folder if the user cannot access its owner.
  @subfolders.select! { |f| can?(:read_owner, f) }
end

#updateObject


15
16
17
18
19
20
21
22
23
# File 'app/controllers/course/material/folders_controller.rb', line 15

def update
  if @folder.update_attributes(folder_params)
    redirect_folder = @folder.parent ? @folder.parent : @folder
    redirect_to course_material_folder_path(current_course, redirect_folder),
                success: t('.success', name: @folder.name)
  else
    render 'edit'
  end
end

#upload_materialsObject


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/course/material/folders_controller.rb', line 54

def upload_materials
  @materials = @folder.build_materials(files_params[:files_attributes])
  respond_to do |format|
    if @folder.save
      format.html do
        redirect_to course_material_folder_path(current_course, @folder),
                    success: t('.success', name: @folder.name)
      end
      format.json
    else
      format.html { upload_materials_failure }
      format.json do
        render json: { message: @folder.errors.full_messages.to_sentence },
               status: :unprocessable_entity
      end
    end
  end
end