Created
May 14, 2010 01:16
-
-
Save patrick99e99/400684 to your computer and use it in GitHub Desktop.
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 PdfFile < ActiveRecord::Base | |
belongs_to :result_list | |
has_attached_file :attachment, :path => "pdf/:id/:basename.:extension" | |
before_post_process :set_file_name | |
def generate_labels(params) | |
unless job_finish.nil? | |
# mailing_list is whether to filter only mailing list contacts or not | |
mailing_list = params[:mailing_list] | |
setting_id = params[:from_shipping_address_id] | |
self.attachment.destroy | |
update_attributes(:job_start => Time.now, | |
:job_finish => nil, | |
:job_error => nil, | |
:label_count => 0) | |
background_process(mailing_list, setting_id) | |
end | |
end | |
def set_to_and_from_addresses(mailing_list, setting_id) | |
@from_address = Setting.find(setting_id).value | |
# get addresses that have a region and country | |
@addresses = result_list.contacts.map(&:addresses).flatten.compact.select{|a| a.region && a.country} | |
@addresses = @addresses.select{|a| a.mailing_list} if mailing_list | |
end | |
def background_process(mailing_list, setting_id) | |
begin | |
set_to_and_from_addresses(mailing_list, setting_id) | |
unless @addresses.length.zero? | |
shipping_label_renderer = ShippingLabelRenderer.new | |
@addresses.each do |to_address| | |
shipping_label_renderer.create_label(@from_address, to_address) | |
self.increment!(:label_count, 1) | |
end | |
pdf_file = Tempfile.new("shipping_labels.pdf") | |
pdf_file.write(shipping_label_renderer.output_prawn_object.render) | |
self.attachment = pdf_file | |
self.save | |
end | |
rescue => e | |
update_attribute(:job_error, e) | |
ensure | |
update_attribute(:job_finish, Time.now) | |
end | |
end | |
handle_asynchronously :background_process | |
private | |
def set_file_name | |
# when using Tempfile, filename becomes "shipping_labels,12928,0.pdf" which becomes "shiping_labels_12928_0.pdf" | |
formatted_date = self.updated_at.strftime('%m-%d-%Y') | |
self.attachment.instance_write(:file_name, "shipping_labels_#{formatted_date}.pdf") | |
end | |
end |
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 PdfLinkRenderer | |
include ActionView::Helpers | |
include ApplicationHelper | |
def initialize(result_list) | |
@result_list = result_list | |
@pdf_file = result_list.pdf_file | |
end | |
def render_link | |
return generating_label_message if @pdf_file.job_finish.nil? | |
return error_message if @pdf_file.job_error | |
return no_labels_message if @pdf_file.label_count.to_i.zero? || !@pdf_file.attachment? | |
link_to "download #{label_count_text} from #{label_date}", download_admin_shipping_label_path(@pdf_file), :class => 'box' | |
end | |
def generating_label_message | |
@pdf_file.label_count.nil? ? "generating labels..." : "generating label #{@pdf_file.label_count} of #{@result_list.contacts.count}" | |
end | |
def error_message | |
content_tag :span, "there was an error while attempting to create the labels!", :class => :error | |
end | |
def label_count_text | |
pluralize(@pdf_file.label_count, 'label') | |
end | |
def label_date | |
show_date_time(@pdf_file.updated_at, :at) | |
end | |
def no_labels_message | |
"...No labels currently generated..." | |
end | |
end |
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
require 'spec_helper' | |
describe PdfLinkRenderer do | |
def pdf_link_renderer(options = {}) | |
PdfLinkRenderer.new(@result_list) | |
end | |
before(:each) do | |
ResultList.all.map(&:destroy) | |
@result_list = ResultList.create! | |
@result_list.pdf_file = PdfFile.create! | |
@pdf_file = @result_list.pdf_file | |
end | |
it "should render a generic generating labels message when the job begins" do | |
pdf_link_renderer.generating_label_message.should == "generating labels..." | |
end | |
it "should render a generating label with number message when processing a label" do | |
@pdf_file.stub!(:job_finish).and_return(nil) | |
@pdf_file.stub!(:label_count).and_return(100) | |
@result_list.contacts.should_receive(:count).and_return(1000) | |
pdf_link_renderer.render_link.should match /\d+ of \d+/ # should match "100 of 1000" | |
end | |
it "should render an error message on error" do | |
@pdf_file.stub!(:job_error).and_return("ERROR!!!!!!!!") | |
pdf_link_renderer.render_link.should match /error/ | |
end | |
it "should render a no labels message when there is no pdf" do | |
pdf_link_renderer.render_link.should match /no labels/i | |
end | |
it "should render a no labels message when the count is 0" do | |
@pdf_file.stub!(:label_count).and_return(0) | |
pdf_link_renderer.render_link.should match /no labels/i | |
end | |
it "should render a link when there are valid labels" do | |
@pdf_file.stub!(:attachment?).and_return(true) | |
@pdf_file.stub!(:label_count).and_return(9999) | |
pdf_link_renderer.render_link.should match /href/ | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment