Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Updated SexItUp::SexItUpImage.find_all to return the found images as …

…an ActiveRelation. Updated Readme and got the rake task working.
  • Loading branch information...
commit 9caea66b63553bca3ebd4e49bdeec503a49f71bc 1 parent 8da2f01
Jim Jones authored November 29, 2010
51  README.rdoc
Source Rendered
... ...
@@ -1,25 +1,42 @@
1 1
 == Description
2  
-Turn your boring "Lorem ipsum" website and placeholder images into a beautiful display of artwork.
  2
+Turn your boring demo website and placeholder images into a beautiful display of artwork.
3 3
 
4  
-== View Helper
5  
-# Searches wikimedia for passed in term.  Caches all initial results.
6  
-#  As you search through results, image files will generally follow the form e.g. "File:<image name>.jpg - Wikimedia Commons"
7  
-# Retains the original image and will generate scaled versions based on coordinates passed in
8  
-# Allow for an optional watermark that defaults to the text "Placeholder"
9  
-sexy_image('dogs', :width => 100, :height => 100)
  4
+In your Gemfile, add the following :
  5
+    gem "sex_it_up", ">= 1.0.0"
10 6
 
  7
+Then perform the following from the root of your application directory :
11 8
 
12  
-== Model
13  
-# If you want to replace your boring user place holder image, you can do the following
14  
-user.icon = SexItUp.sexy_image(['flowers','stars','butterflies'], :width => 75, :height => 75)
  9
+    bundle install
15 10
 
16  
-== Rake Task
17  
-# The inital caching of images for a given term may be slow (hence causing your initial page view to be slow), so you can "pre-warm" the image
18  
-#  cache for a given search term by using the following rake task.
19  
-rake sex_it_up:cache butterflies
  11
+Now we need to copy over the needed migration :
  12
+    rails g sex_it_up
  13
+    rake db:migrate
  14
+
  15
+== Displaying Images
  16
+
  17
+If you want to display a random image for the term 'dog', do the following.
  18
+
  19
+Add the following to your Rakefile :
  20
+    require 'tasks/sex_it_up'
  21
+
  22
+Once you have the rake task available, you can issue the following to warm the image cache for a given term :
  23
+    rake sex_it_up:cache[<Term>]
  24
+
  25
+    e.g. :
  26
+    rake sex_it_up:cache["dog"]
  27
+
  28
+Once the rake task has completed, change your erb view to call the following helper :
  29
+    sexy_image('dog', :width => 100, :height => 100)
  30
+
  31
+This helper will find a random image for the given term, scale it to the passed in dimensions, and display it.
  32
+
  33
+== Other Uses
  34
+If you wanted to replace your boring default user avatars, you could do the following
  35
+    user.icon = SexItUp::SexItUpImage.find_all('dog').random.first
  36
+
  37
+== Sample Screenshot
  38
+![Sample website using SexItUp](http://imgur.com/bmHvj.png)
20 39
 
21  
-# Show Lovdbyless or some other social site with a before (with boring placeholders) and after (with exciting images)
22  
-# Show a screenshot of a sample site with the Lorem text and placeholder images
23 40
 == Copyright
24 41
 
25  
-Copyright (c) 2010 Jim Jones. See LICENSE for details.
  42
+Copyright (c) 2010 Jim Jones. See LICENSE for details.
19  lib/sex_it_up.rb
@@ -15,6 +15,10 @@ module SexItUp
15 15
   class SexItUpImage < ActiveRecord::Base
16 16
     RESULT_LIMIT = 10
17 17
 
  18
+    scope :images_for, lambda {|term|
  19
+      where(['image_search_term = ?', term])
  20
+    }
  21
+
18 22
     scope :random, lambda {
19 23
       max_id  = connection.select_value("select max(id) from #{self.table_name}")
20 24
       where("id >= #{rand(max_id)}")
@@ -27,6 +31,7 @@ class SexItUpImage < ActiveRecord::Base
27 31
     def self.find_all(term)
28 32
       query = "site:commons.wikimedia.org \"is in the public domain\" #{term}"
29 33
       cache_search(query, term)
  34
+      images_for(term)
30 35
     end
31 36
 
32 37
     def set_attachment_sizes(sizes = {:thumb => ["100x100"]})
@@ -39,20 +44,18 @@ def attachment_sizes
39 44
 
40 45
     private
41 46
     def self.cache_search(query, term)
42  
-      images      = []
43 47
       num_results = 0
44 48
 
45 49
       Google::Search::Web.new(:query => query).each do |result|
46 50
         page        = agent.get(result.uri)
47 51
         image       = find_image_link(page)
48 52
 
49  
-        images << cache(term, image.href) unless image.nil?
  53
+        cache(term, image.href) unless image.nil?
50 54
 
51 55
         num_results+=1
52 56
         break if num_results == RESULT_LIMIT
53 57
       end
54 58
 
55  
-      images
56 59
     end
57 60
 
58 61
     def self.find_image_link(page)
@@ -79,9 +82,10 @@ def self.cache(search_term, img_url)
79 82
       def image.original_filename; CGI.unescape(base_uri.path.split('/').last); end
80 83
       image.original_filename.blank? ? nil : image
81 84
 
82  
-      SexItUpImage.create!(:image_original_url => img_url,
83  
-                           :image_search_term => search_term,
84  
-                           :image => image)
  85
+      # Any sort of failure on record creation is probably a file format not supported (e.g. .ogg)
  86
+      SexItUpImage.create(:image_original_url => img_url,
  87
+                          :image_search_term => search_term,
  88
+                          :image => image)
85 89
     end
86 90
 
87 91
   end
@@ -102,8 +106,7 @@ def sexy_image(term, opts = {:width => 100, :height => 100})
102 106
       if sexy_image.nil? || sexy_image.blank?
103 107
         # No image object passed in or found; let's go search.
104 108
 
105  
-        SexItUpImage.find_all(term)
106  
-        sexy_image = SexItUpImage.where(['image_search_term = ?', term]).random.first
  109
+        sexy_image = SexItUpImage.find_all(term).random.first
107 110
       end
108 111
 
109 112
       unless sexy_image.nil?
2  lib/tasks/sex_it_up.rb
@@ -4,7 +4,7 @@
4 4
 
5 5
   desc "Retrieve and cache all of the public domain images for a given search term."
6 6
   task :cache, :term, :needs => :environment do |t, args|
7  
-    SexItUp::SexItUp.find_all(args[:term])
  7
+    SexItUp::SexItUpImage.find_all(args[:term])
8 8
   end
9 9
 
10 10
 end
2  sex_it_up.gemspec
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9 9
 
10 10
   s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11 11
   s.authors = ["Jim Jones"]
12  
-  s.date = %q{2010-11-26}
  12
+  s.date = %q{2010-11-29}
13 13
   s.description = %q{SexItUp allows for the easy drop-in of public domain images for site mockups and user avatars.}
14 14
   s.email = %q{jjones@aantix.com}
15 15
   s.extra_rdoc_files = [
2  spec/helpers/sex_it_up_helper.rb
@@ -12,7 +12,7 @@
12 12
 
13 13
   describe "sexy_image" do
14 14
     it "should source a random image for the search term and size it accordingly" do
15  
-      sexy_image('school', {:width => 50, :height => 50}).should =~ /.jpg/
  15
+      sexy_image('school', {:width => 50, :height => 50}).should =~ /.jpg/i
16 16
       sexy_image('school', {:width => 50, :height => 50}).should =~ /s_50x50/
17 17
     end
18 18
 

0 notes on commit 9caea66

Please sign in to comment.
Something went wrong with that request. Please try again.