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

Instance Method Details

#all_contentObject



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

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



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

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



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

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



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

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

#indexObject



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

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

#newObject



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

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

#showObject



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

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

#sortObject



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

def sort
  authorize! :update, ContentModules::LessonModule

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

#updateObject



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

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