Class: View::Image

Inherits:
Formatter show all
Defined in:
lib/view/formatters/image.rb

Overview

Makes an image tag. It also works with paperclip objects.

With paperclip, it also checks if the file was really uploaded.

This is actually longer than the regular Rails way. But the added benefit of this formatter comes from using it inside the table and definition list formatter.

Examples:

Without paperclip


= view "foo.jpg", :as => :image

With paperclip


= view @user.avatar, :as => :image

With options


= view @user.avatar, :as => :image, :html => { :class => "meh" }

Inside the definition_list formatter:


= definition_list_for @user do |dl|
  = dl.view :name
  = dl.view :avatar, :as => :image

Instance Attribute Summary

Attributes inherited from Formatter

#block, #template, #value

Instance Method Summary collapse

Methods inherited from Formatter

#all_options, as, #format!, inherited, skip_blank_formatter, #to_s, type

Instance Method Details

#file?Boolean

Returns:



68
69
70
# File 'lib/view/formatters/image.rb', line 68

def file?
  value.is_a?(String) || value.send(file_method)
end

#file_methodObject



72
73
74
# File 'lib/view/formatters/image.rb', line 72

def file_method
  View.configuration.file_methods.find { |method| value.respond_to?(method) }
end

#formatObject



34
35
36
# File 'lib/view/formatters/image.rb', line 34

def format
  template.image_tag(path, options) if file?
end

#optionsObject

The options for the image tag are in the :html option. You can use the default options to change them.

Examples:


View::Image.default_options = { :html => { :class => "avatar" } }


54
55
56
# File 'lib/view/formatters/image.rb', line 54

def options
  super[:html]
end

#pathObject

Returns the path of the image, based on the path_methods and path_arguments configuration.



40
41
42
43
44
45
46
# File 'lib/view/formatters/image.rb', line 40

def path
  if value.is_a?(String)
    value
  else
    value.send(path_method, *path_arguments)
  end
end

#path_argumentsObject



64
65
66
# File 'lib/view/formatters/image.rb', line 64

def path_arguments
  all_options[:style] || View.configuration.path_arguments
end

#path_methodObject

TODO I'm only guessing here, I don't actually know how other upload gems work, besides paperclip.



60
61
62
# File 'lib/view/formatters/image.rb', line 60

def path_method
  View.configuration.path_methods.find { |method| value.respond_to?(method) }
end