From 2e0543fe46ca390dfbd416ce90eaeab759111469 Mon Sep 17 00:00:00 2001 From: maximedelpit Date: Tue, 15 Sep 2015 07:14:12 +0200 Subject: [PATCH 1/3] change gemspec --- spree-print-invoice.gemspec | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spree-print-invoice.gemspec b/spree-print-invoice.gemspec index af7f0917..2a70fac0 100755 --- a/spree-print-invoice.gemspec +++ b/spree-print-invoice.gemspec @@ -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' @@ -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' From ab3105bd62b32cde3bf60da6bac08a2363b088f5 Mon Sep 17 00:00:00 2001 From: maximedelpit Date: Wed, 16 Sep 2015 09:30:14 +0200 Subject: [PATCH 2/3] first try s3 --- app/models/spree/bookkeeping_document.rb | 40 ++++++++++++++++--- ...3616_create_spree_bookkeeping_documents.rb | 14 +++---- ...pdf_attachment_to_bookkeeping_documents.rb | 11 +++++ 3 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 db/migrate/20150916061921_add_attachment_pdf_attachment_to_bookkeeping_documents.rb diff --git a/app/models/spree/bookkeeping_document.rb b/app/models/spree/bookkeeping_document.rb index 826c4aec..a2ba66c6 100644 --- a/app/models/spree/bookkeeping_document.rb +++ b/app/models/spree/bookkeeping_document.rb @@ -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 = { + 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') } @@ -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 @@ -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. @@ -139,10 +162,17 @@ 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 diff --git a/db/migrate/20150616133616_create_spree_bookkeeping_documents.rb b/db/migrate/20150616133616_create_spree_bookkeeping_documents.rb index 36eb218d..d590bd72 100644 --- a/db/migrate/20150616133616_create_spree_bookkeeping_documents.rb +++ b/db/migrate/20150616133616_create_spree_bookkeeping_documents.rb @@ -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 diff --git a/db/migrate/20150916061921_add_attachment_pdf_attachment_to_bookkeeping_documents.rb b/db/migrate/20150916061921_add_attachment_pdf_attachment_to_bookkeeping_documents.rb new file mode 100644 index 00000000..53085a7f --- /dev/null +++ b/db/migrate/20150916061921_add_attachment_pdf_attachment_to_bookkeeping_documents.rb @@ -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 From 2355ec826cd54e2c932dc79719abe8258fa7c755 Mon Sep 17 00:00:00 2001 From: maximedelpit Date: Thu, 17 Sep 2015 01:03:22 +0200 Subject: [PATCH 3/3] test --- app/models/spree/bookkeeping_document.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/models/spree/bookkeeping_document.rb b/app/models/spree/bookkeeping_document.rb index a2ba66c6..2f478629 100644 --- a/app/models/spree/bookkeeping_document.rb +++ b/app/models/spree/bookkeeping_document.rb @@ -118,7 +118,7 @@ def storage_path # storage_path ## Max S3 test - file_path + # file_path end # Renders the prawn template for give template name in context of ActionView. @@ -166,7 +166,6 @@ def send_or_create_pdf # => MAX S3 Test pdf_upload = File.open(file_path, 'wb') { |f| f.puts render_pdf } - end IO.binread(file_path)