Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial Commit

Ripped from a recent project for all to use…
  • Loading branch information...
commit 270d36474050a76600cc304ea6cb7b51949e81ad 0 parents
@ohokay authored
20 .gitignore
@@ -0,0 +1,20 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
+.DS_Store
+.rvmrc
+test_rails_project
4 Gemfile
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in placeholdit.gemspec
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Jeff Mehlhoff
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44 README.md
@@ -0,0 +1,44 @@
+# Placeholdit
+
+A Rails view helper for placeholder images via http://placehold.it
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'placeholdit'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install placeholdit
+
+## Usage
+
+A basic 500x500 placeholder image:
+
+ $ <%= placeholdit_image_tag "500" %>
+
+The same placeholder image with custom text:
+
+ $ <%= placeholdit_image_tag "500", text: "Buy me!" %>
+
+The same placeholder image with a blue background:
+
+ $ <%= placeholdit_image_tag "500", text: "Buy me!", background_color: '#004eff' %>
+
+The same placeholder image with a blue background and red text (not recommended):
+
+ $ <%= placeholdit_image_tag "500", text: "Buy me!", background_color: '#004eff', text_color: '#ff0000' %>
+
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
2  Rakefile
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
45 lib/placeholdit.rb
@@ -0,0 +1,45 @@
+require "placeholdit/version"
+
+module Placeholdit
+ module ViewHelpers
+ def placeholdit_image_tag(size, opts={})
+
+ size = "#{size}" unless size.is_a?(String)
+ src = "http://placehold.it/#{size}"
+
+ config = {
+ :alt => (opts[:text] || "A placeholder image"),
+ :class => "placeholder",
+ :height => (size.split('x')[1] || size.split('x')[0]),
+ :width => size.split('x')[0]
+ }.merge!(opts)
+
+ # Placehold.it preferences
+ if config[:background_color]
+ src += "/#{remove_hex_pound(config[:background_color])}"
+ end
+ if config[:text_color]
+ src += "/#{remove_hex_pound(config[:text_color])}"
+ end
+ if config[:text]
+ src += "&text=#{config[:text]}"
+ end
+
+ image_tag = "<img src='#{src}' alt='#{config[:alt]}' class='#{config[:class]}' height='#{config[:height]}' width='#{config[:width]}' />"
+ return image_tag.html_safe if defined?(Rails)
+ image_tag
+ end
+
+ private
+
+ def remove_hex_pound(str)
+ if str.include?("#")
+ return str.delete("#")
+ end
+ str
+ end
+
+ end
+end
+
+require 'placeholdit/railtie' if defined?(Rails)
7 lib/placeholdit/railtie.rb
@@ -0,0 +1,7 @@
+module Placeholdit
+ class Railtie < Rails::Railtie
+ initializer "placeholdit.view_helpers" do
+ ActionView::Base.send :include, ViewHelpers
+ end
+ end
+end
3  lib/placeholdit/version.rb
@@ -0,0 +1,3 @@
+module Placeholdit
+ VERSION = "0.0.1"
+end
20 placeholdit.gemspec
@@ -0,0 +1,20 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/placeholdit/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["Jeff Mehlhoff"]
+ gem.email = ["jeffmehlhoff@mac.com"]
+ gem.description = %q{A Rails view helper for placeholder images via http://placehold.it}
+ gem.summary = %q{A Rails view helper for placeholder images via http://placehold.it}
+ gem.homepage = "http://github.com/ohokay/placeholdit"
+
+ gem.files = `git ls-files`.split($\)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.name = "placeholdit"
+ gem.require_paths = ["lib"]
+ gem.version = Placeholdit::VERSION
+
+ gem.add_development_dependency "rspec"
+
+end
37 spec/placeholdit_spec.rb
@@ -0,0 +1,37 @@
+require 'spec_helper'
+
+class MyView
+ include Placeholdit::ViewHelpers
+end
+
+describe Placeholdit::ViewHelpers do
+ let(:view) { MyView.new }
+
+ it "should accept integers as params" do
+ view.placeholdit_image_tag(500).should == "<img src='http://placehold.it/500' alt='A placeholder image' class='placeholder' height='500' width='500' />"
+ end
+
+ it "should accept a string" do
+ view.placeholdit_image_tag("500").should == "<img src='http://placehold.it/500' alt='A placeholder image' class='placeholder' height='500' width='500' />"
+ end
+
+ it "should accept a componded WxH" do
+ view.placeholdit_image_tag("500x500").should == "<img src='http://placehold.it/500x500' alt='A placeholder image' class='placeholder' height='500' width='500' />"
+ end
+
+ it "should accept custom text" do
+ view.placeholdit_image_tag("500x500", text: 'Buy me!').should == "<img src='http://placehold.it/500x500&text=Buy me!' alt='Buy me!' class='placeholder' height='500' width='500' />"
+ end
+
+ it "should accept custom colors" do
+ view.placeholdit_image_tag("500", text_color: 'fff', background_color: '000').should == "<img src='http://placehold.it/500/000/fff' alt='A placeholder image' class='placeholder' height='500' width='500' />"
+ end
+
+ it "should strip the pound from hex colors" do
+ view.placeholdit_image_tag("500", text_color: '#fff', background_color: '000').should == "<img src='http://placehold.it/500/000/fff' alt='A placeholder image' class='placeholder' height='500' width='500' />"
+ view.placeholdit_image_tag("500", text_color: '#fff', background_color: '#000000').should == "<img src='http://placehold.it/500/000000/fff' alt='A placeholder image' class='placeholder' height='500' width='500' />"
+ view.placeholdit_image_tag("500", text_color: 'fff', background_color: '#000000').should == "<img src='http://placehold.it/500/000000/fff' alt='A placeholder image' class='placeholder' height='500' width='500' />"
+ view.placeholdit_image_tag("500", text_color: 'ffffff', background_color: '#000000').should == "<img src='http://placehold.it/500/000000/ffffff' alt='A placeholder image' class='placeholder' height='500' width='500' />"
+ end
+
+end
1  spec/spec_helper.rb
@@ -0,0 +1 @@
+require 'placeholdit'
Please sign in to comment.
Something went wrong with that request. Please try again.