Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3 invoices #97

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions app/models/spree/bookkeeping_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ class BookkeepingDocument < ActiveRecord::Base
# template should be a string, such as "invoice" or "packaging_slip"
#
belongs_to :printable, polymorphic: true

# MAX S3 test
has_attached_file :pdf_attachment,
# styles: { invoice: "invoice", packaging_slip: "packaging_slip" },
:storage => :s3,
# :s3_credentials => "#{Rails.root}/config/aws.yml",
:url => ':s3_domain_url',
# :path => ":class/:attachment/:id/:style/:filename",
:path => '/:rails_env/:class/:attachment/:id_partition/:template/:filename',
:s3_credentials = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unexpected token tEQL

bucket: ENV['S3_BUCKET_NAME'], # \
access_key_id: ENV['AWS_ACCESS_KEY_ID'], # |- DO NOT replace this
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'] # /
}
validates_attachment :pdf_attachment,
:presence => true,
content_type: 'application/pdf'

validates :printable, :template, presence: true
validates *PERSISTED_ATTRS, presence: true, if: -> { self.persisted? }
scope :invoices, -> { where(template: 'invoice') }
Expand Down Expand Up @@ -76,7 +94,8 @@ def file_name
# = PDF file path
#
def file_path
@_file_path ||= Rails.root.join(storage_path, "#{file_name}")
# @_file_path ||= Rails.root.join(storage_path, "#{file_name}") # => original method
@_file_path ||= pdf_attachment.url # => Max S3 test
end

# = PDF storage folder path for given template name
Expand All @@ -93,9 +112,13 @@ def file_path
# Creates the folder if it's not present yet.
#
def storage_path
storage_path = Rails.root.join(Spree::PrintInvoice::Config.storage_path, template.pluralize)
FileUtils.mkdir_p(storage_path)
storage_path
# # => original method
# storage_path = Rails.root.join(Spree::PrintInvoice::Config.storage_path, template.pluralize)
# FileUtils.mkdir_p(storage_path)
# storage_path

## Max S3 test
# file_path
end

# Renders the prawn template for give template name in context of ActionView.
Expand Down Expand Up @@ -139,10 +162,16 @@ def single_lower_case_name(class_string)
#
def send_or_create_pdf
unless File.exist?(file_path)
File.open(file_path, 'wb') { |f| f.puts render_pdf }
# File.open(file_path, 'wb') { |f| f.puts render_pdf } # => original method

# => MAX S3 Test
pdf_upload = File.open(file_path, 'wb') { |f| f.puts render_pdf }
end

IO.binread(file_path)
end
end
end


#MAX TO DO change filepath and IO
14 changes: 7 additions & 7 deletions db/migrate/20150616133616_create_spree_bookkeeping_documents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ def up
t.timestamps null: false
end

Spree::Order.complete.where.not(invoice_number: nil).find_each do |order|
order.pdfs.create(
template: 'invoice',
number: order.invoice_number,
created_at: order.completed_at.to_date
)
end
# Spree::Order.complete.where.not(invoice_number: nil).find_each do |order|
# order.pdfs.create(
# template: 'invoice',
# number: order.invoice_number,
# created_at: order.completed_at.to_date
# )
# end

remove_column :spree_orders, :invoice_number, :string
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddAttachmentPdfAttachmentToBookkeepingDocuments < ActiveRecord::Migration
def self.up
change_table :bookkeeping_documents do |t|
t.attachment :pdf_attachment
end
end

def self.down
remove_attachment :bookkeeping_documents, :pdf_attachment
end
end
10 changes: 5 additions & 5 deletions spree-print-invoice.gemspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# coding: utf-8
lib = File.expand_path('../lib/', __FILE__)
$LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)
# lib = File.expand_path('../lib/', __FILE__)
# $LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)

require 'spree_print_invoice/version'
# require 'spree_print_invoice/version'

Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = 'spree_print_invoice'
s.version = SpreePrintInvoice.version
s.version = '3.0.1' #SpreePrintInvoice.version
s.summary = 'Print invoices and slips from Spree Commerce'
s.description = s.summary
s.required_ruby_version = '>= 2.1.0'
Expand All @@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.requirements << 'none'

s.add_runtime_dependency 'prawn-rails', '~> 0.1.1'
s.add_runtime_dependency 'spree_core', '~> 3.1.0.beta'
s.add_runtime_dependency 'spree_core', '~> 3.0.1'

s.add_development_dependency 'capybara', '~> 2.4.4'
s.add_development_dependency 'poltergeist', '~> 1.5'
Expand Down