Class: ThinkFeelDoEngine::BitMaker::SlidesController

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

Overview

Enables admin to create, update, destroy, and reorder (sort) slides Slides belong to slideshows

Constant Summary collapse

FIRST_SLIDE_POSITION =
1

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

#createObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/think_feel_do_engine/bit_maker/slides_controller.rb', line 25

def create
  @slide = @slideshow.slides.build(slide_params)
  authorize! :create, @slide
  @slide.position = @slideshow.slides.count + 1
  if @slide.save
    flash[:success] = "Successfully created slide for slideshow"
    redirect_to arm_bit_maker_slideshow_path(@arm, @slideshow)
  else
    flash[:alert] = @slide.errors.full_messages.join(", ")
    render :new
  end
end

#create_table_of_contentsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/think_feel_do_engine/bit_maker/slides_controller.rb', line 38

def create_table_of_contents
  toc_increment_slide_pos(@slideshow)

  @slide = @slideshow.slides.build(
    position: FIRST_SLIDE_POSITION,
    title: "Table of Contents",
    is_title_visible: true,
    body: "")
  authorize! :create, @slide

  if @slide.save
    @slideshow.update(has_table_of_contents: true)
    flash[:success] = "Successfully created table of contents for "\
    "slideshow."
    redirect_to arm_bit_maker_slideshow_path(@arm, @slideshow)
  else
    toc_decrement_slide_pos(@slideshow)
    flash[:alert] = @slide.errors.full_messages.join(", ")
    render :new
  end
end

#destroyObject



95
96
97
98
99
100
101
102
103
# File 'app/controllers/think_feel_do_engine/bit_maker/slides_controller.rb', line 95

def destroy
  if @slide.destroy
    flash[:success] = "Slide deleted."
    redirect_to arm_bit_maker_slideshow_path(@arm, @slide.slideshow)
  else
    flash[:error] = "There were errors."
    redirect_to arm_bit_maker_slideshow_path(@arm, @slide.slideshow)
  end
end

#destroy_table_of_contentsObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/think_feel_do_engine/bit_maker/slides_controller.rb', line 60

def destroy_table_of_contents
  @slide = @slideshow.slides.where(position: FIRST_SLIDE_POSITION).first
  if @slide.destroy
    toc_decrement_slide_pos(@slideshow)
    @slideshow.update(has_table_of_contents: false)
    flash[:success] = "Table of contents deleted."
    redirect_to arm_bit_maker_slideshow_path(@arm, @slide.slideshow)
  else
    flash[:error] = "There were errors."
    redirect_to arm_bit_maker_slideshow_path(@arm, @slide.slideshow)
  end
end

#editObject



82
83
# File 'app/controllers/think_feel_do_engine/bit_maker/slides_controller.rb', line 82

def edit
end

#indexObject



16
17
18
# File 'app/controllers/think_feel_do_engine/bit_maker/slides_controller.rb', line 16

def index
  authorize! :index, @slideshow
end

#newObject



20
21
22
23
# File 'app/controllers/think_feel_do_engine/bit_maker/slides_controller.rb', line 20

def new
  @slide = @slideshow.slides.build(type: params[:type])
  authorize! :create, @slide
end

#previewObject



124
125
126
127
128
129
130
131
# File 'app/controllers/think_feel_do_engine/bit_maker/slides_controller.rb', line 124

def preview
  render text: Redcarpet::Markdown.new(
    Redcarpet::Render::HTML.new(
      filter_html: true,
      safe_links_only: true
    )
  ).render(params[:content] || "").html_safe
end

#showObject



73
74
75
76
77
78
79
80
# File 'app/controllers/think_feel_do_engine/bit_maker/slides_controller.rb', line 73

def show
  if @slideshow.has_table_of_contents? &&
     FIRST_SLIDE_POSITION == @slide.position
    render "think_feel_do_engine/slides/"
  else
    render "think_feel_do_engine/slides/show"
  end
end

#sortObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/think_feel_do_engine/bit_maker/slides_controller.rb', line 105

def sort
  authorize! :update, BitCore::Slideshow
  first_slide = BitCore::Slide.find(params[:slide][0])

  if @slideshow.has_table_of_contents &&
     first_slide.title != "Table of Contents"
    flash[:alert] = "Table of contents cannot be moved out of"\
                        " the first position."
    render "think_feel_do_engine/slides/sort_with_reload.js"

  elsif @slideshow.sort(params[:slide])
    flash.now[:notice] = "Reorder was successful."
    render "think_feel_do_engine/slides/sort.js"
  else
    flash.now[:alert] = @slideshow.errors.full_messages.join(", ")
    render "think_feel_do_engine/slides/sort.js"
  end
end

#updateObject



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

def update
  if @slide.update(slide_params)
    flash[:success] = "Successfully updated slide for slideshow"
    redirect_to arm_bit_maker_slideshow_path(@arm, @slide.slideshow)
  else
    flash[:alert] = @slide.errors.full_messages.join(", ")
    render :edit
  end
end