Permalink
Browse files

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

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

0 comments on commit 9caea66

Please sign in to comment.