Last active
February 7, 2017 22:00
-
-
Save ychaker/81f3334ad6a1189a0acae9851497cc95 to your computer and use it in GitHub Desktop.
Rails + Refile + Dropzone (7)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DocsController < ApplicationController | |
respond_to :json, :js | |
before_action :get_doc, only: [ :show, :reload_doc ] | |
def create | |
create_params = doc_params.to_h.merge!(current_user: current_user) | |
result, op = Doc::Create.run(create_params) | |
@operation = op | |
@model = op.model | |
@form = op.contract | |
if @operation.valid? && @model.valid? | |
load_tasks | |
flash.now[:notice] = 'Doc uploaded successfully.' | |
respond_with @model, location: -> { root_path } | |
elsif !@model.valid? | |
flash.now[:error] = 'Unable to upload doc.' | |
respond_with @model, location: -> { root_path } | |
elsif !@operation.valid? | |
flash.now[:error] = 'Unable to upload doc.' | |
respond_with @operation, location: -> { root_path } | |
end | |
end | |
def show | |
path = @doc.file.backend.path(@doc.file.id) | |
filename = @doc.file_filename | |
type = @doc.file_content_type | |
send_file path, disposition: 'inline', type: type, filename: filename | |
end | |
def update | |
update_params = doc_params.to_h.merge!(current_user: current_user, id: params[:id]) | |
result, op = Doc::Update.run(update_params) | |
@operation = op | |
@model = op.model | |
@form = op.contract | |
@doc_task = @model.doc_task | |
if @operation.valid? && @model.valid? | |
flash.now[:notice] = 'Doc uploaded successfully.' | |
respond_with @model, location: -> { task_path(@doc_task) } | |
elsif !@model.valid? | |
flash.now[:error] = 'Unable to upload doc.' | |
respond_with @model, location: -> { task_path(@doc_task) } | |
elsif !@operation.valid? | |
flash.now[:error] = 'Unable to upload doc.' | |
respond_with @operation, location: -> { task_path(@doc_task) } | |
end | |
end | |
def reload | |
load_tasks | |
end | |
def reload_doc | |
@doc_task = @doc.doc_task | |
end | |
private | |
def get_doc | |
@doc = Doc.find(params[:id]) | |
end | |
def doc_params | |
params.require(:doc).permit(:file, :doc_task_id) | |
end | |
def load_tasks | |
@pending_tasks = current_user.tasks.pending | |
@completed_tasks = current_user.tasks.completed | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment