Class: Backend::AttachmentsController

Inherits:
BaseController show all
Defined in:
app/controllers/backend/attachments_controller.rb

Instance Attribute Summary

Attributes inherited from ApplicationController

#current_theme

Instance Method Summary collapse

Methods inherited from BaseController

accounts_range_crit, crit_params, journal_entries_states_crit, journal_period_crit, journals_crit, search_conditions

Methods inherited from ApplicationController

#after_sign_in_path_for, #authorized?, human_action_name, #human_action_name

Instance Method Details

#createObject


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/backend/attachments_controller.rb', line 6

def create
  subject = params[:subject_type].constantize.find(params[:subject_id])
  unless params[:attachments]
    head :unprocessable_entity
    return
  end
  @attachment = subject.attachments.create!(params[:attachments].permit!)
  respond_to do |format|
    if @attachment.save(params)
      format.json do
        render json: {
          name: @attachment.name,
          nature: @attachment.nature,
          path: backend_attachment_path(@attachment),
          document: {
            path: backend_document_path(@attachment.document),
            file_path: backend_document_path(@attachment.document, format: :pdf),
            thumbnail_path: backend_document_path(@attachment.document, format: :jpg)
          }
        }, status: :created
      end
    else
      format.json { render json: @attachment.errors.full_messages, status: :unprocessable_entity }
    end
    format.html
  end
end

#destroyObject


48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/backend/attachments_controller.rb', line 48

def destroy
  return unless (@attachment = find_and_check)
  document = @attachment.document
  @attachment.destroy
  document.destroy
  if document.destroyed? && @attachment.destroyed?
    render json: { attachment: 'deleted', status: :ok }
  else
    render json: { message: 'error' }, status: :unprocessable_entity
  end
end

#showObject


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/backend/attachments_controller.rb', line 34

def show
  return unless (@attachment = find_and_check)
  if @attachment.document.file?
    render json: {
      document: {
        file_path: backend_document_path(@attachment.document, format: :pdf)
      },
      name: @attachment.name
    }
  else
    head :not_found
  end
end