Class: Guts::MediaController

Inherits:
ApplicationController show all
Defined in:
app/controllers/guts/media_controller.rb

Overview

Media controller

Instance Method Summary collapse

Instance Method Details

#createObject

Note:

Redirects to #index if successfull or re-renders #new if not

Creates a medium through post



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/guts/media_controller.rb', line 38

def create
  @medium = Medium.new medium_params
  authorize @medium

  if @medium.save
    respond_to do |format|
      format.html do
        flash[:notice] = 'Media was successfully created.'
        redirect_to edit_polymorphic_path([@object, @medium])
      end

      format.json do
        render json: { success: true }, status: :created
      end
    end
  else
    respond_to do |format|
      format.html do
        render :new
      end

      format.json do
        render json: { error: @medium.errors.full_messages.last }, status: :unprocessable_entity
      end
    end
  end
end

#destroyObject

Note:

Redirects to #index on success

Destroys a medium



81
82
83
84
85
86
87
# File 'app/controllers/guts/media_controller.rb', line 81

def destroy
  authorize @medium
  @medium.destroy

  flash[:notice] = 'Media was successfully destroyed.'
  redirect_to polymorphic_path([@object, :media])
end

#editObject

Editing for a medium



32
33
34
# File 'app/controllers/guts/media_controller.rb', line 32

def edit
  authorize @medium
end

#editor_insertObject

Handles showing the insert medium allowing TinyMce to use it



91
92
93
94
# File 'app/controllers/guts/media_controller.rb', line 91

def editor_insert
  authorize @medium, :index?
  render :editor_insert, layout: false
end

#indexObject

Note:

Depending on the object passed (polymorphic)

Displays a list of media



12
13
14
15
16
17
18
# File 'app/controllers/guts/media_controller.rb', line 12

def index
  @media = if @object
             policy_scope(@object).media.paginate(page: params[:page], per_page: @per_page)
           else
             policy_scope(Medium).paginate(page: params[:page], per_page: @per_page)
           end
end

#newObject

Creation of a new medium



26
27
28
29
# File 'app/controllers/guts/media_controller.rb', line 26

def new
  @medium = Medium.new
  authorize @medium
end

#showObject

Shows details about a single medium



21
22
23
# File 'app/controllers/guts/media_controller.rb', line 21

def show
  authorize @medium
end

#updateObject

Note:

Redirects to #index if successfull or re-renders #edit if not

Updates a medium through patch



68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/guts/media_controller.rb', line 68

def update
  authorize @medium

  if @medium.update(medium_params)
    flash[:notice] = 'Media was successfully updated.'
    redirect_to edit_polymorphic_path([@object, @medium])
  else
    render :edit
  end
end