Sha256: 8e737b461abdb7183d2518f17f7f16794ab3a75ee94131fb2634d4bf99a91e37

Contents?: true

Size: 1.88 KB

Versions: 11

Compression:

Stored size: 1.88 KB

Contents

module SimpleFormAttachments
  class UploadController < ActionController::Base

    def create
      res = attachment.save
      render json: attachment_json, status: (res ? :ok : :unprocessable_entity)
    end

    private # =============================================================

    def attachment
      @attachment ||= attachment_class.new attachment_params
    end

    def attachment_json
      { html: attachment_html }
    end

    def attachment_html
      render_to_string partial: 'simple_form_attachments/attachment_upload_template', locals: partial_locals
    end

    def partial_locals
      {
        parent: parent,
        parent_name: parent_name,
        parent_class: parent_class,
        child_index: child_index,
        attachment: attachment,
        multiple: multiple?,
        attachment_relation: params[:attachment_relation]
      }
    end

    def attachment_params
      params.require(:attachment).permit!
    end

    def parent_name
      params[:attachment_parent][:name]
    end

    def attachment_class
      params[:attachment_type].constantize
    end

    def parent_class
      return unless params[:attachment_parent]
      params[:attachment_parent][:class].constantize
    end

    def parent
      return unless params[:attachment_relation]
      if multiple? && attachment.errors.empty?
        parent_class.new{ |o| o.send(params.fetch(:attachment_relation).fetch(:name).to_sym) << attachment }
      else
        parent_class.new
      end
    end

    def child_index
      DateTime.now.strftime('%Q').to_i
    end

    # ---------------------------------------------------------------------

    def multiple?
      ['true', true, '1', 1].include? params.fetch(:attachment_relation, {}).fetch(:multiple, false)
    end

    def referenced?
      ['true', true, '1', 1].include? params.fetch(:attachment_relation, {}).fetch(:referenced, false)
    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
simple_form_attachments-0.1.5 app/controllers/simple_form_attachments/upload_controller.rb
simple_form_attachments-0.1.4 app/controllers/simple_form_attachments/upload_controller.rb
simple_form_attachments-0.1.3 app/controllers/simple_form_attachments/upload_controller.rb
simple_form_attachments-0.1.2 app/controllers/simple_form_attachments/upload_controller.rb
simple_form_attachments-0.1.1 app/controllers/simple_form_attachments/upload_controller.rb
simple_form_attachments-0.1.0 app/controllers/simple_form_attachments/upload_controller.rb
simple_form_attachments-0.0.9 app/controllers/simple_form_attachments/upload_controller.rb
simple_form_attachments-0.0.8 app/controllers/simple_form_attachments/upload_controller.rb
simple_form_attachments-0.0.7 app/controllers/simple_form_attachments/upload_controller.rb
simple_form_attachments-0.0.6 app/controllers/simple_form_attachments/upload_controller.rb
simple_form_attachments-0.0.5 app/controllers/simple_form_attachments/upload_controller.rb