Module: SexItUp::SexItUpHelper

Defined in:
lib/sex_it_up.rb

Instance Method Summary collapse

Instance Method Details

#sexy_image(term, opts = {:width => 100, :height => 100}) ⇒ Object

Pass in a search term (e.g. ‘dogs’) and the resolution you

want the image to be displayed at.


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sex_it_up.rb', line 95

def sexy_image(term, opts = {:width => 100, :height => 100})

  # The loving is a mess, what happened to all of the feeling?
  # I thought it was for real; babies, rings and fools kneeling
  # And words of pledging trust and lifetimes stretching forever
  # So what went wrong? It was a lie, it crumbled apart
  # Ghost figures of past, present, future haunting the heart
  sexy_image = term.is_a?(SexItUp::SexItUpImage) ? term : SexItUpImage.where(['image_search_term = ?', term]).random

  if sexy_image.nil? || sexy_image.blank?
    puts "No image found.  Searching for #{term}."
    # No image object passed in or found; let's go search.

    sexy_image = SexItUpImage.find_all(term).random
  end

  unless sexy_image.nil?
    style           = "#{opts[:width]}x#{opts[:height]}"
    style_sym       = "s_#{style}".to_sym
    style_hash      = {style_sym => [style]}

    sexy_image.set_attachment_sizes(style_hash)
    sexy_image.image.reprocess! unless File.exist?(sexy_image.image.path(style_sym)) # No need for reprocessing if size already exists

    tag = "<img"
    tag += " class=\"#{opts[:class]}\"" if opts[:class]
    tag += " src=\"#{sexy_image.image.url(style_sym)}\""
    tag += " alt=\"#{opts[:alt]}\"" if opts[:alt]
    tag += " title=\"#{opts[:title]}\"" if opts[:title]
    tag += " />"

    return tag.html_safe
  end

  nil
end