Permalink
Browse files

Only show the requested fields in a template preview. Close #245.

  • Loading branch information...
1 parent 782eff6 commit 0fae55622d727670d1be3b448b44529b045db394 @bamnet bamnet committed Dec 18, 2012
Showing with 13 additions and 2 deletions.
  1. +6 −1 app/controllers/templates_controller.rb
  2. +7 −1 app/models/template.rb
View
7 app/controllers/templates_controller.rb
@@ -129,12 +129,17 @@ def preview
if !params[:hide_text].nil?
@hide_text = [true, "true", 1, "1"].include?(params[:hide_text])
end
+
+ @only_fields = []
+ if !params[:fields].nil?
+ @only_fields = params[:fields].split(',').map{|i| i.to_i}
+ end
jpg = Mime::Type.lookup_by_extension(:jpg) #JPG is getting defined elsewhere.
if([jpg, Mime::PNG, Mime::HTML].include?(request.format))
image = nil
benchmark("Template#preview_image") do
- image = @template.preview_image(@hide_fields, @hide_text)
+ image = @template.preview_image(@hide_fields, @hide_text, @only_fields)
end
# Resize the image if needed.
View
8 app/models/template.rb
@@ -69,7 +69,8 @@ def last_modified
# Generate a preview image of a template.
# Hide the fields all together, or just hide the field text.
- def preview_image(hide_fields=false, hide_text=false)
+ # Or just show certain fields
+ def preview_image(hide_fields=false, hide_text=false, only_fields=[])
template_media = self.media.original.first
image = Magick::Image.from_blob(template_media.file_contents).first
@@ -79,6 +80,11 @@ def preview_image(hide_fields=false, hide_text=false)
if !hide_fields && !self.positions.empty?
dw = Magick::Draw.new
self.positions.each do |position|
+ Rails.logger.debug(only_fields)
+ Rails.logger.debug(position.field_id)
+ if !only_fields.empty? && !only_fields.include?(position.field_id)
+ next
+ end
#Draw the rectangle
dw.fill("grey")
dw.stroke_opacity(0)

0 comments on commit 0fae556

Please sign in to comment.