gbdev-pdf_filler 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/pdf_filler/pdf_db_mapper.rb +74 -0
- data/tasks/build_gem.rake +1 -1
- metadata +2 -1
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module GBDev #:nodoc:
|
4
|
+
module PDF #:nodoc:
|
5
|
+
module Acts #:nodoc:
|
6
|
+
module PDFDBMapper # :nodoc:
|
7
|
+
|
8
|
+
def self.included(mod)
|
9
|
+
mod.extend(ClassMethods)
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
|
14
|
+
# Automatically maps database fields to fields found in the template PDF with the same name.
|
15
|
+
# Database fields not found in the template PDF are ignored. If no options are specified then
|
16
|
+
# by default all table fields will attempt to be mapped.
|
17
|
+
#
|
18
|
+
# Configuration options are:
|
19
|
+
#
|
20
|
+
# * :only - Only map the database fields specified. i.e :only => [:first_name, :last_name]
|
21
|
+
# * :except - Map all the table fields except those specified. i.e. :except => [:middle_name]
|
22
|
+
# * :include - Allows for a method to map to a field in the PDF. If you have a method called full_name
|
23
|
+
# that combines the first name and last name, you could map that method to a PDF text field called full_name.
|
24
|
+
# :include => [:full_name].
|
25
|
+
# You could all specify a model attribute/method to map to a different PDF field.
|
26
|
+
# i.e. :include => [{:full_name => :dog_name}]
|
27
|
+
# :include will still work with :only and :except.
|
28
|
+
# * :only_include - Will ignore :only and :except and only includes the manual mappings specified.
|
29
|
+
# i.e. :only_include => [:full_name, {:animal_name => :dog_name}]
|
30
|
+
def acts_as_pdf_db_mapper(*fields)
|
31
|
+
|
32
|
+
# Make sure that the table to be mapped actually exists
|
33
|
+
if self.table_exists?
|
34
|
+
|
35
|
+
# Get a collection of fields to be searched on.
|
36
|
+
if fields.first.class.to_s == 'Hash'
|
37
|
+
|
38
|
+
if fields.first.has_key?(:only_include)
|
39
|
+
fields_to_map = fields.first[:only_include]
|
40
|
+
else
|
41
|
+
if fields.first.has_key?(:only)
|
42
|
+
# only map on these fields.
|
43
|
+
fields_to_map = fields.first[:only]
|
44
|
+
elsif fields.first.has_key?(:except)
|
45
|
+
# Get all the fields and remove any that are in the -except- list.
|
46
|
+
fields_to_map = self.column_names.collect { |column| fields.first[:except].include?(column.to_sym) ? nil : column.to_sym }.compact
|
47
|
+
else
|
48
|
+
fields_to_map = self.column_names.collect { |column| column.to_sym }
|
49
|
+
end
|
50
|
+
|
51
|
+
if fields.first.has_key?(:include)
|
52
|
+
fields_to_map += fields.first[:include]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
else
|
56
|
+
fields_to_map = self.column_names.collect { |column| column.to_sym }
|
57
|
+
end
|
58
|
+
|
59
|
+
# Set the appropriate class attributes.
|
60
|
+
self.cattr_accessor :mapped_fields
|
61
|
+
self.mapped_fields = fields_to_map
|
62
|
+
|
63
|
+
end # End table exists check
|
64
|
+
|
65
|
+
end # acts_as_pdf_db_mapper
|
66
|
+
|
67
|
+
end # ClassMethods
|
68
|
+
|
69
|
+
end # PDFDBMapper
|
70
|
+
end # Acts
|
71
|
+
end # PDF
|
72
|
+
end # GBDev
|
73
|
+
|
74
|
+
ActiveRecord::Base.send(:include, GBDev::PDF::Acts::PDFDBMapper)
|
data/tasks/build_gem.rake
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gbdev-pdf_filler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wes Hays
|
@@ -42,6 +42,7 @@ files:
|
|
42
42
|
- lib/pdf_filler.rb
|
43
43
|
- lib/pdf_filler/book.rb
|
44
44
|
- lib/pdf_filler/page.rb
|
45
|
+
- lib/pdf_filler/pdf_db_mapper.rb
|
45
46
|
- lib/pdf_filler/util_methods.rb
|
46
47
|
- spec/lib/pdf_book_spec.rb
|
47
48
|
- spec/lib/pdf_db_mapper_spec.rb
|