Class: ThinkFeelDoEngine::LessonsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/think_feel_do_engine/lessons_controller.rb

Overview

Enables Lesson CRUD functionality.

Constant Summary

Constants inherited from ApplicationController

ApplicationController::CSRF_COOKIE_NAME, ApplicationController::CSRF_HEADER_NAME, ApplicationController::INACTIVE_MESSAGE, ApplicationController::ROOT_URI

Instance Method Summary collapse

Methods inherited from ApplicationController

#access_denied_resource_path, #after_sign_in_path_for, #after_sign_out_path_for, #raise_not_found!, #render_not_found

Instance Method Details

#all_contentObject



18
19
20
21
22
23
24
25
26
# File 'app/controllers/think_feel_do_engine/lessons_controller.rb', line 18

def all_content
  authorize! :index, ContentModules::LessonModule
  @slideshows = BitCore::Slideshow
                .joins(content_provider: :content_module)
                .merge(@lessons.order(:position))
  @slides = BitCore::Slide
            .where(bit_core_slideshow_id: @slideshows.map(&:id))
            .group_by(&:bit_core_slideshow_id)
end

#createObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/think_feel_do_engine/lessons_controller.rb', line 37

def create
  authorize! :create, ContentModules::LessonModule
  lesson_tool = @arm.bit_core_tools.find_by_type("Tools::Learn")
  @lesson = lesson_tool.add_module(build_lesson)

  if @lesson.save
    redirect_to arm_lesson_url(@arm, @lesson),
                notice: "Successfully created lesson"
  else
    flash.now[:alert] =
      "Unable to create lesson: " +
      model_errors(@lesson)
    render :new
  end
end

#destroyObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/think_feel_do_engine/lessons_controller.rb', line 71

def destroy
  authorize! :destroy, ContentModules::LessonModule

  ContentModules::LessonModule.transaction do
    Task.where(bit_core_content_module_id: @lesson.id).destroy_all
    @lesson.destroy
  end

  redirect_to arm_lessons_url(@arm), notice: "Lesson deleted."

rescue
  redirect_to arm_lessons_url(@arm),
              alert: "Unable to delete lesson: #{model_errors(@lesson)}"
end

#editObject



53
54
55
# File 'app/controllers/think_feel_do_engine/lessons_controller.rb', line 53

def edit
  authorize! :edit, ContentModules::LessonModule
end

#indexObject



12
13
14
15
16
# File 'app/controllers/think_feel_do_engine/lessons_controller.rb', line 12

def index
  authorize! :index, ContentModules::LessonModule
  @lessons = @lessons.includes(content_providers: :source_content)
                     .order(:position)
end

#newObject



32
33
34
35
# File 'app/controllers/think_feel_do_engine/lessons_controller.rb', line 32

def new
  authorize! :new, ContentModules::LessonModule
  @lesson = build_lesson
end

#showObject



28
29
30
# File 'app/controllers/think_feel_do_engine/lessons_controller.rb', line 28

def show
  authorize! :show, ContentModules::LessonModule
end

#sortObject



86
87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/think_feel_do_engine/lessons_controller.rb', line 86

def sort
  authorize! :update, ContentModules::LessonModule

  if ContentModules::LessonModule.sort(@arm.id, params[:lesson])
    flash.now[:success] = "Reorder was successful."
  else
    flash.now[:alert] = "Error while sorting."
  end

  render nothing: true
end

#updateObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/think_feel_do_engine/lessons_controller.rb', line 57

def update
  authorize! :update, ContentModules::LessonModule

  if @lesson.update(lesson_params)
    redirect_to arm_lesson_url(@arm, @lesson),
                notice: "Successfully updated lesson"
  else
    flash.now[:alert] =
      "Unable to update lesson: " +
      model_errors(@lesson)
    render :edit
  end
end