class Avatar::Source::PaperclipSource

Source for a file attachment using Paperclip. See giantrobots.thoughtbot.com/2008/3/18/for-attaching-files-use-paperclip

Attributes

default_field[RW]
default_style[RW]

Public Instance Methods

avatar_url_for(person, options = {}) click to toggle source

Returns a URL for a has_attached_file attribute, via person.<paperclip_field>.url, passing in :paperclip_style if present. Returns nil under any of the following circumstances:

  • person is nil

  • person.<paperclip_field> is nil

  • person.<paperclip_field>? returns false

  • person.<paperclip_field>.styles does not include :paperclip_style (if present)

Options:

  • :paperclip_field - the has_attached_file column within person; by default, self.default_field

  • :paperclip_style - one of the styles of the has_attached_file; by default, self.default_style

# File lib/avatar/source/paperclip_source.rb, line 30
def avatar_url_for(person, options = {})
  return nil if person.nil?
  options = parse_options(person, options)
  field = options[:paperclip_field]
  return nil if field.nil?
  return nil unless person.send("#{field}?".to_sym)
  avatar = person.send(field)
  style = options[:paperclip_style]
  return nil if style && !avatar.styles.keys.include?(style)
  avatar.url(style)
end
parse_options(person, options) click to toggle source

Copies :paperclip_field and :paperclip_style from options, adding defaults if necessary.

# File lib/avatar/source/paperclip_source.rb, line 43
def parse_options(person, options)
  returning({}) do |result|
    result[:paperclip_field] = options[:paperclip_field] || default_field
    result[:paperclip_style] = options[:paperclip_style] || default_style
  end
end

Public Class Methods

new(default_field = :avatar, default_style = nil) click to toggle source

Create a new FileColumnSource with a default_field (by default, :avatar), and a default_style (by default, nil)

# File lib/avatar/source/paperclip_source.rb, line 15
def initialize(default_field = :avatar, default_style = nil)
  @default_field = default_field
  @default_style = default_style
end