caxlsx / caxlsx_rails

A Rails plugin to provide templates for the axlsx gem
MIT License
744 stars 84 forks source link

saving template as file with specific location #118

Closed Kuassari closed 5 years ago

Kuassari commented 5 years ago

So I work with mailer and sidekiq. I try to make the file not as attachment in the mail, but as a url that leads to a function in a controller so it can send file to the user after it was saved locally. I didn't see anything like that in the documentation and no matter what I try I get errors like 'undefined url' or 'no implicit conversion of nil into String'.

I searched every issue here and questions on stackoverflow and didn't find an answer. I have a location I prepared and saved and I want to tell the template somehow to save it in this specific location so I can access it later when the user click on the download link.

Relevant parts of my code:

Job:

def perform(export_args)
  begin
    account = Admin::Account.find_by(id: export_args[:account_id])
    account.connect_to_target_db
    request_id = export_args[:request_id].to_s
    request = ExportRequest.find_by(id: request_id)
    accounts = System::Export.new.get_items_for_server_report(export_args)
    file_location = request.create_file_location(export_to_csv)
    # request.data = here need to tell the template to insert data
    request.update(data: file_location)
    email = Admin::ReportMailer.export_server_report(member, request_id, accounts, file_location)
    if (email.deliver)
      request.update(sent: true)
    end
  ensure
    ActiveRecord::Base.clear_active_connections!
  end
end

Mailer:

  def export_server_report(member, request_id, accounts, file_location)
    @member = member
    @title = "Export"
    @header = subject = "Your Export is Ready"
    @link = "#{AppConfig['host']}/system/exports/download_collection?request_id=#{request_id}"
    mail(:to => member.email, :subject => subject) do |format|
      format.html
    end
  end

download function:

  def download_collection
    request_id = params[:request_id] || params[:request]
    request ||= ExportRequest.find_by(id: request_id)
    if request
      file = request.data #the data is the export file location
      if AppConfig['host_assets_on_s3']
        redirect_to file
      else
          send_file(file, type: "application/vnd.ms-excel", x_sendfile: true)
      end
    else
      flash[:error] = "The export request was not found for this account. Make sure you are logged in to the account which created the export."
      redirect_to '/'
    end
  end
OpenCoderX commented 5 years ago

In rails 4 we used the paperclip gem, which is now deprecated in favor of ActiveStorage in rails 5. Active Storage has some helper methods that will help : https://guides.rubyonrails.org/active_storage_overview.html

straydogstudio commented 5 years ago

@Kuassari If you are looking to write a template to a file, look at something like this: https://gist.github.com/straydogstudio/323139591f2cc5d48fbc or https://gist.github.com/straydogstudio/dceb775ead81470cea70 I assume you'll do that in your job.

Then you'll have to read and serve the file in a controller action. That should be pretty straight forward.