wagon 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.1
1
+ 0.9.2
data/bin/wagon CHANGED
@@ -7,7 +7,7 @@
7
7
  # -h, --help Displays this help message
8
8
  # -v, --version Display the version, then exit
9
9
  # -V, --verbose Verbose output
10
- # -t, --title Override the default title with your own
10
+ # -t, --title The title displayed on each page (default is the ward name)
11
11
  # -r, --rows Number of rows per page (default is 6)
12
12
  # -c, --columns Number of columns per page (default is 7)
13
13
  # -p, --padding Padding between households (default is 2)
@@ -4,6 +4,10 @@ require 'stringio'
4
4
 
5
5
  module Wagon
6
6
  class Directory < Page
7
+ def ward
8
+ @parent
9
+ end
10
+
7
11
  def photo_directory_path
8
12
  return @photo_directory_path unless @photo_directory_path.nil?
9
13
 
@@ -24,42 +28,56 @@ module Wagon
24
28
  end
25
29
 
26
30
  def to_pdf(options = {})
27
- options = {:columns => 7, :rows => 6, :padding => 2, :font_size => 8, :address => true, :phone_number => true, :email => true}.merge(options)
31
+ options = {
32
+ :columns => 7,
33
+ :rows => 6,
34
+ :padding => 2,
35
+ :font_size => 8,
36
+ :address => true,
37
+ :phone_number => true,
38
+ :email => true,
39
+ :title => "#{ward.name}"
40
+ }.merge(options.delete_if {|k,v| v.nil? })
28
41
 
29
- Prawn::Document.new(:skip_page_creation => true, :left_margin => 10, :right_margin => 10, :top_margin => 20, :bottom_margin => 10) do |pdf|
42
+ Prawn::Document.new(:left_margin => 10, :right_margin => 10, :top_margin => 10, :bottom_margin => 10) do |pdf|
43
+ header_height = 10
44
+ footer_height = 10
30
45
  columns = options[:columns].to_f
31
46
  rows = options[:rows].to_f
32
47
  padding = options[:padding].to_f
33
48
  grid_width = pdf.bounds.width / columns
34
- grid_height = pdf.bounds.height / rows
49
+ grid_height = (pdf.bounds.height - header_height - footer_height) / rows
35
50
  box_width = grid_width - (padding * 2)
36
51
  box_height = grid_height - (padding * 2)
37
52
  pages = (households.size.to_f / (columns * rows)).ceil()
38
- info_lines = 1 + [:address, :phone_number, :email].inject(0) { |sum, item| sum += item ? 1 : 0 }
39
53
  pdf.font_size = options[:font_size].to_i
54
+ info_count = 1 + [:address, :phone_number, :email].inject(0) { |sum, item| sum += options[item] ? 1 : 0 }
55
+ info_height = pdf.font.height*info_count
40
56
 
41
57
  (0...pages).each do |page|
42
- pdf.start_new_page
58
+ pdf.start_new_page unless page == 0
59
+ pdf.text(options[:title], :at => [pdf.bounds.right/2 - pdf.width_of(options[:title], :size => 12)/2, pdf.bounds.top - header_height/2], :size => 12)
60
+ pdf.text("For Church Use Only", :at => [pdf.bounds.right/2 - pdf.width_of("For Church Use Only")/2, pdf.bounds.bottom + footer_height/2])
43
61
  (0...rows).each do |row|
44
- y = pdf.bounds.top - row*grid_height
62
+ y = pdf.bounds.top - row*grid_height - header_height
45
63
  (0...columns).each do |column|
46
64
  break if (index = page*rows*columns+row*columns+column) >= households.size
47
65
  household = households[index]
48
66
  x = pdf.bounds.left + column*grid_width
49
67
  pdf.bounding_box([x, y], :width => grid_width, :height => grid_height) do
50
68
  pdf.bounding_box([pdf.bounds.left + padding, pdf.bounds.top - padding], :width => box_width, :height => box_height) do
51
- information = []
52
- information.push(household.name)
53
- information.push(*household.address.street) if options[:address]
54
- information.push(household.phone_number) if options[:phone_number]
55
- information.push(household.members.first.email) if options[:email]
69
+ info = []
70
+ info.push(household.name)
71
+ info.push(*household.address.street) if options[:address]
72
+ info.push(household.phone_number) if options[:phone_number]
73
+ info.push(household.members.first.email) if options[:email]
56
74
 
57
- pdf.image(household.has_image? ? StringIO.new(household.image_data) : './extra/placeholder.jpg',
58
- :position => :center, :fit => [box_width, box_height - (padding*2 + pdf.font.height*info_lines)] )
75
+ pdf.image(household.has_image? ? StringIO.new(household.image_data) : './extra/placeholder.jpg', :position => :center, :fit => [box_width, box_height - (padding*2 + info_height)] )
59
76
 
60
- pdf.move_down(padding)
61
- information.compact.each do |line|
62
- pdf.text(line, :align => :center, :size => pdf.font_size.downto(1).detect() { |size| pdf.width_of(line.to_s, :size => size) <= box_width })
77
+ pdf.bounding_box([pdf.bounds.left, pdf.bounds.bottom + info_height], :height => info_height+1, :width => pdf.bounds.width) do
78
+ info.compact.each do |line|
79
+ pdf.text(line, :align => :center, :size => pdf.font_size.downto(1).detect() { |size| pdf.width_of(line.to_s, :size => size) <= box_width })
80
+ end
63
81
  end
64
82
  end
65
83
  end
@@ -4,8 +4,8 @@ module Wagon
4
4
  class Page
5
5
  attr_reader :connection
6
6
 
7
- def initialize(connection, url)
8
- @connection, @url = connection, url
7
+ def initialize(connection, url, parent = nil)
8
+ @connection, @url, @parent = connection, url, parent
9
9
  end
10
10
 
11
11
  def source
@@ -11,7 +11,7 @@ module Wagon
11
11
  end
12
12
 
13
13
  def directory
14
- @directory ||= Directory.new(connection, directory_path)
14
+ @directory ||= Directory.new(connection, directory_path, self)
15
15
  end
16
16
 
17
17
  def households
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '../../spec_helper')
3
3
  describe "Wagon::Directory" do
4
4
 
5
5
  before(:each) do
6
- @page = Wagon::Directory.new($user, $user.ward.directory_path)
6
+ @page = Wagon::Directory.new($user, $user.ward.directory_path, $user.ward)
7
7
  end
8
8
 
9
9
  it "should find the photo directory link" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wagon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Devin Christensen