Permalink
Browse files

adding episode 182

  • Loading branch information...
1 parent 59c181c commit 078e0c0e36176ba11101f196eecffeb65f25b694 @ryanb committed Oct 5, 2009
Showing with 13,649 additions and 0 deletions.
  1. +3 −0 episode-182/README
  2. +4 −0 episode-182/cropper/.gitignore
  3. +1 −0 episode-182/cropper/README
  4. +10 −0 episode-182/cropper/Rakefile
  5. +10 −0 episode-182/cropper/app/controllers/application_controller.rb
  6. +52 −0 episode-182/cropper/app/controllers/users_controller.rb
  7. +3 −0 episode-182/cropper/app/helpers/application_helper.rb
  8. +22 −0 episode-182/cropper/app/helpers/layout_helper.rb
  9. +2 −0 episode-182/cropper/app/helpers/users_helper.rb
  10. +20 −0 episode-182/cropper/app/models/user.rb
  11. +23 −0 episode-182/cropper/app/views/layouts/application.html.erb
  12. +12 −0 episode-182/cropper/app/views/users/_form.html.erb
  13. +45 −0 episode-182/cropper/app/views/users/crop.html.erb
  14. +8 −0 episode-182/cropper/app/views/users/edit.html.erb
  15. +17 −0 episode-182/cropper/app/views/users/index.html.erb
  16. +5 −0 episode-182/cropper/app/views/users/new.html.erb
  17. +14 −0 episode-182/cropper/app/views/users/show.html.erb
  18. +110 −0 episode-182/cropper/config/boot.rb
  19. +22 −0 episode-182/cropper/config/database.yml
  20. +41 −0 episode-182/cropper/config/environment.rb
  21. +17 −0 episode-182/cropper/config/environments/development.rb
  22. +28 −0 episode-182/cropper/config/environments/production.rb
  23. +28 −0 episode-182/cropper/config/environments/test.rb
  24. +7 −0 episode-182/cropper/config/initializers/backtrace_silencers.rb
  25. +10 −0 episode-182/cropper/config/initializers/inflections.rb
  26. +5 −0 episode-182/cropper/config/initializers/mime_types.rb
  27. +21 −0 episode-182/cropper/config/initializers/new_rails_defaults.rb
  28. +15 −0 episode-182/cropper/config/initializers/session_store.rb
  29. +5 −0 episode-182/cropper/config/locales/en.yml
  30. +4 −0 episode-182/cropper/config/routes.rb
  31. +12 −0 episode-182/cropper/db/migrate/20091005171132_create_users.rb
  32. +15 −0 episode-182/cropper/db/migrate/20091005171308_add_attachments_avatar_to_user.rb
  33. +24 −0 episode-182/cropper/db/schema.rb
  34. +7 −0 episode-182/cropper/db/seeds.rb
  35. +2 −0 episode-182/cropper/doc/README_FOR_APP
  36. +18 −0 episode-182/cropper/lib/paperclip_processors/cropper.rb
  37. 0 episode-182/cropper/log/.gitignore
  38. +30 −0 episode-182/cropper/public/404.html
  39. +30 −0 episode-182/cropper/public/422.html
  40. +30 −0 episode-182/cropper/public/500.html
  41. 0 episode-182/cropper/public/favicon.ico
  42. +2 −0 episode-182/cropper/public/javascripts/application.js
  43. +963 −0 episode-182/cropper/public/javascripts/controls.js
  44. +973 −0 episode-182/cropper/public/javascripts/dragdrop.js
  45. +1,128 −0 episode-182/cropper/public/javascripts/effects.js
  46. +19 −0 episode-182/cropper/public/javascripts/jquery-1.3.2.min.js
  47. +163 −0 episode-182/cropper/public/javascripts/jquery.Jcrop.min.js
  48. +4,320 −0 episode-182/cropper/public/javascripts/prototype.js
  49. +5 −0 episode-182/cropper/public/robots.txt
  50. BIN episode-182/cropper/public/stylesheets/Jcrop.gif
  51. +85 −0 episode-182/cropper/public/stylesheets/application.css
  52. +35 −0 episode-182/cropper/public/stylesheets/jquery.Jcrop.css
  53. +4 −0 episode-182/cropper/script/about
  54. +3 −0 episode-182/cropper/script/console
  55. +3 −0 episode-182/cropper/script/dbconsole
  56. +3 −0 episode-182/cropper/script/destroy
  57. +3 −0 episode-182/cropper/script/generate
  58. +3 −0 episode-182/cropper/script/performance/benchmarker
  59. +3 −0 episode-182/cropper/script/performance/profiler
  60. +3 −0 episode-182/cropper/script/plugin
  61. +3 −0 episode-182/cropper/script/runner
  62. +3 −0 episode-182/cropper/script/server
  63. +5 −0 episode-182/cropper/test/fixtures/users.yml
  64. +54 −0 episode-182/cropper/test/functional/users_controller_test.rb
  65. +9 −0 episode-182/cropper/test/performance/browsing_test.rb
  66. +38 −0 episode-182/cropper/test/test_helper.rb
  67. +7 −0 episode-182/cropper/test/unit/user_test.rb
  68. 0 episode-182/cropper/tmp/.gitignore
  69. +26 −0 episode-182/cropper/vendor/plugins/paperclip/LICENSE
  70. +174 −0 episode-182/cropper/vendor/plugins/paperclip/README.rdoc
  71. +99 −0 episode-182/cropper/vendor/plugins/paperclip/Rakefile
  72. +6 −0 episode-182/cropper/vendor/plugins/paperclip/cucumber/paperclip_steps.rb
  73. +5 −0 episode-182/cropper/vendor/plugins/paperclip/generators/paperclip/USAGE
  74. +27 −0 episode-182/cropper/vendor/plugins/paperclip/generators/paperclip/paperclip_generator.rb
  75. +19 −0 ...de-182/cropper/vendor/plugins/paperclip/generators/paperclip/templates/paperclip_migration.rb.erb
  76. +1 −0 episode-182/cropper/vendor/plugins/paperclip/init.rb
  77. +353 −0 episode-182/cropper/vendor/plugins/paperclip/lib/paperclip.rb
  78. +414 −0 episode-182/cropper/vendor/plugins/paperclip/lib/paperclip/attachment.rb
  79. +33 −0 episode-182/cropper/vendor/plugins/paperclip/lib/paperclip/callback_compatability.rb
  80. +115 −0 episode-182/cropper/vendor/plugins/paperclip/lib/paperclip/geometry.rb
  81. +108 −0 episode-182/cropper/vendor/plugins/paperclip/lib/paperclip/interpolations.rb
  82. +58 −0 episode-182/cropper/vendor/plugins/paperclip/lib/paperclip/iostream.rb
  83. +4 −0 episode-182/cropper/vendor/plugins/paperclip/lib/paperclip/matchers.rb
  84. +49 −0 episode-182/cropper/vendor/plugins/paperclip/lib/paperclip/matchers/have_attached_file_matcher.rb
  85. +66 −0 ...opper/vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
  86. +48 −0 ...2/cropper/vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_presence_matcher.rb
  87. +83 −0 ...e-182/cropper/vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_size_matcher.rb
  88. +49 −0 episode-182/cropper/vendor/plugins/paperclip/lib/paperclip/processor.rb
  89. +243 −0 episode-182/cropper/vendor/plugins/paperclip/lib/paperclip/storage.rb
  90. +73 −0 episode-182/cropper/vendor/plugins/paperclip/lib/paperclip/thumbnail.rb
  91. +48 −0 episode-182/cropper/vendor/plugins/paperclip/lib/paperclip/upfile.rb
  92. +37 −0 episode-182/cropper/vendor/plugins/paperclip/paperclip.gemspec
  93. +117 −0 episode-182/cropper/vendor/plugins/paperclip/shoulda_macros/paperclip.rb
  94. +79 −0 episode-182/cropper/vendor/plugins/paperclip/tasks/paperclip_tasks.rake
  95. +1 −0 episode-182/cropper/vendor/plugins/paperclip/test/.gitignore
  96. +780 −0 episode-182/cropper/vendor/plugins/paperclip/test/attachment_test.rb
  97. +4 −0 episode-182/cropper/vendor/plugins/paperclip/test/database.yml
  98. BIN episode-182/cropper/vendor/plugins/paperclip/test/fixtures/12k.png
  99. BIN episode-182/cropper/vendor/plugins/paperclip/test/fixtures/50x50.png
  100. BIN episode-182/cropper/vendor/plugins/paperclip/test/fixtures/5k.png
  101. +1 −0 episode-182/cropper/vendor/plugins/paperclip/test/fixtures/bad.png
  102. +8 −0 episode-182/cropper/vendor/plugins/paperclip/test/fixtures/s3.yml
  103. 0 episode-182/cropper/vendor/plugins/paperclip/test/fixtures/text.txt
  104. BIN episode-182/cropper/vendor/plugins/paperclip/test/fixtures/twopage.pdf
  105. +177 −0 episode-182/cropper/vendor/plugins/paperclip/test/geometry_test.rb
  106. +108 −0 episode-182/cropper/vendor/plugins/paperclip/test/helper.rb
  107. +483 −0 episode-182/cropper/vendor/plugins/paperclip/test/integration_test.rb
  108. +124 −0 episode-182/cropper/vendor/plugins/paperclip/test/interpolations_test.rb
  109. +71 −0 episode-182/cropper/vendor/plugins/paperclip/test/iostream_test.rb
  110. +21 −0 episode-182/cropper/vendor/plugins/paperclip/test/matchers/have_attached_file_matcher_test.rb
  111. +30 −0 ...2/cropper/vendor/plugins/paperclip/test/matchers/validate_attachment_content_type_matcher_test.rb
  112. +21 −0 ...e-182/cropper/vendor/plugins/paperclip/test/matchers/validate_attachment_presence_matcher_test.rb
  113. +50 −0 episode-182/cropper/vendor/plugins/paperclip/test/matchers/validate_attachment_size_matcher_test.rb
  114. +327 −0 episode-182/cropper/vendor/plugins/paperclip/test/paperclip_test.rb
  115. +10 −0 episode-182/cropper/vendor/plugins/paperclip/test/processor_test.rb
  116. +303 −0 episode-182/cropper/vendor/plugins/paperclip/test/storage_test.rb
  117. +227 −0 episode-182/cropper/vendor/plugins/paperclip/test/thumbnail_test.rb
View
@@ -0,0 +1,3 @@
+Railscasts Episode #182: Cropping Images
+
+http://railscasts.com/episodes/182
@@ -0,0 +1,4 @@
+tmp/**/*
+log/*.log
+*.sqlite3
+public/system
@@ -0,0 +1 @@
+Railscasts episode example application.
@@ -0,0 +1,10 @@
+# Add your own tasks in files placed in lib/tasks ending in .rake,
+# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
+
+require(File.join(File.dirname(__FILE__), 'config', 'boot'))
+
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+require 'tasks/rails'
@@ -0,0 +1,10 @@
+# Filters added to this controller apply to all controllers in the application.
+# Likewise, all the methods added will be available for all controllers.
+
+class ApplicationController < ActionController::Base
+ helper :all # include all helpers, all the time
+ protect_from_forgery # See ActionController::RequestForgeryProtection for details
+
+ # Scrub sensitive parameters from your log
+ # filter_parameter_logging :password
+end
@@ -0,0 +1,52 @@
+class UsersController < ApplicationController
+ def index
+ @users = User.all
+ end
+
+ def show
+ @user = User.find(params[:id])
+ end
+
+ def new
+ @user = User.new
+ end
+
+ def create
+ @user = User.new(params[:user])
+ if @user.save
+ if params[:user][:avatar].blank?
+ flash[:notice] = "Successfully created user."
+ redirect_to @user
+ else
+ render :action => "crop"
+ end
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @user = User.find(params[:id])
+ end
+
+ def update
+ @user = User.find(params[:id])
+ if @user.update_attributes(params[:user])
+ if params[:user][:avatar].blank?
+ flash[:notice] = "Successfully updated user."
+ redirect_to @user
+ else
+ render :action => "crop"
+ end
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @user = User.find(params[:id])
+ @user.destroy
+ flash[:notice] = "Successfully destroyed user."
+ redirect_to users_url
+ end
+end
@@ -0,0 +1,3 @@
+# Methods added to this helper will be available to all templates in the application.
+module ApplicationHelper
+end
@@ -0,0 +1,22 @@
+# These helper methods can be called in your template to set variables to be used in the layout
+# This module should be included in all views globally,
+# to do so you may need to add this line to your ApplicationController
+# helper :layout
+module LayoutHelper
+ def title(page_title, show_title = true)
+ @content_for_title = page_title.to_s
+ @show_title = show_title
+ end
+
+ def show_title?
+ @show_title
+ end
+
+ def stylesheet(*args)
+ content_for(:head) { stylesheet_link_tag(*args) }
+ end
+
+ def javascript(*args)
+ content_for(:head) { javascript_include_tag(*args) }
+ end
+end
@@ -0,0 +1,2 @@
+module UsersHelper
+end
@@ -0,0 +1,20 @@
+class User < ActiveRecord::Base
+ has_attached_file :avatar, :styles => { :small => "100x100#", :large => "500x500>" }, :processors => [:cropper]
+ attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
+ after_update :reprocess_avatar, :if => :cropping?
+
+ def cropping?
+ !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
+ end
+
+ def avatar_geometry(style = :original)
+ @geometry ||= {}
+ @geometry[style] ||= Paperclip::Geometry.from_file(avatar.path(style))
+ end
+
+ private
+
+ def reprocess_avatar
+ avatar.reprocess!
+ end
+end
@@ -0,0 +1,23 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html>
+ <head>
+ <title><%= h(yield(:title) || "Untitled") %></title>
+ <%= stylesheet_link_tag 'application' %>
+ <%= javascript_include_tag 'jquery-1.3.2.min' %>
+ <%= yield(:head) %>
+ </head>
+ <body>
+ <div id="container">
+ <%- flash.each do |name, msg| -%>
+ <%= content_tag :div, msg, :id => "flash_#{name}" %>
+ <%- end -%>
+
+ <%- if show_title? -%>
+ <h1><%=h yield(:title) %></h1>
+ <%- end -%>
+
+ <%= yield %>
+ </div>
+ </body>
+</html>
@@ -0,0 +1,12 @@
+<% form_for @user, :html => { :multipart => true } do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p>
+ <%= f.label :avatar %><br />
+ <%= f.file_field :avatar %>
+ </p>
+ <p><%= f.submit "Submit" %></p>
+<% end %>
@@ -0,0 +1,45 @@
+<% title "Crop avatar" %>
+<% content_for(:head) do %>
+<%= stylesheet_link_tag "jquery.Jcrop" %>
+<%= javascript_include_tag "jquery.Jcrop.min" %>
+<script type="text/javascript" charset="utf-8">
+$(function() {
+ $('#cropbox').Jcrop({
+ onChange: update_crop,
+ onSelect: update_crop,
+ setSelect: [0, 0, 500, 500],
+ aspectRatio: 1
+ });
+});
+
+function update_crop(coords) {
+ var rx = 100/coords.w;
+ var ry = 100/coords.h;
+ $('#preview').css({
+ width: Math.round(rx * <%= @user.avatar_geometry(:large).width %>) + 'px',
+ height: Math.round(ry * <%= @user.avatar_geometry(:large).height %>) + 'px',
+ marginLeft: '-' + Math.round(rx * coords.x) + 'px',
+ marginTop: '-' + Math.round(ry * coords.y) + 'px'
+ });
+ var ratio = <%= @user.avatar_geometry(:original).width %> / <%= @user.avatar_geometry(:large).width %>;
+ $("#crop_x").val(Math.round(coords.x * ratio));
+ $("#crop_y").val(Math.round(coords.y * ratio));
+ $("#crop_w").val(Math.round(coords.w * ratio));
+ $("#crop_h").val(Math.round(coords.h * ratio));
+}
+</script>
+<% end %>
+
+<%= image_tag @user.avatar.url(:large), :id => "cropbox" %>
+
+<h4>Preview:</h4>
+<div style="width:100px; height:100px; overflow:hidden">
+ <%= image_tag @user.avatar.url(:large), :id => "preview" %>
+</div>
+
+<% form_for @user do |f| %>
+ <% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %>
+ <%= f.hidden_field attribute, :id => attribute %>
+ <% end %>
+ <p><%= f.submit "Crop" %></p>
+<% end %>
@@ -0,0 +1,8 @@
+<% title "Edit User" %>
+
+<%= render :partial => 'form' %>
+
+<p>
+ <%= link_to "Show", @user %> |
+ <%= link_to "View All", users_path %>
+</p>
@@ -0,0 +1,17 @@
+<% title "Users" %>
+
+<table>
+ <tr>
+ <th>Name</th>
+ </tr>
+ <% for user in @users %>
+ <tr>
+ <td><%=h user.name %></td>
+ <td><%= link_to "Show", user %></td>
+ <td><%= link_to "Edit", edit_user_path(user) %></td>
+ <td><%= link_to "Destroy", user, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+ <% end %>
+</table>
+
+<p><%= link_to "New User", new_user_path %></p>
@@ -0,0 +1,5 @@
+<% title "New User" %>
+
+<%= render :partial => 'form' %>
+
+<p><%= link_to "Back to List", users_path %></p>
@@ -0,0 +1,14 @@
+<% title "User" %>
+
+<p>
+ <strong>Name:</strong>
+ <%=h @user.name %>
+</p>
+
+<p><%= image_tag @user.avatar.url(:small) %></p>
+
+<p>
+ <%= link_to "Edit", edit_user_path(@user) %> |
+ <%= link_to "Destroy", @user, :confirm => 'Are you sure?', :method => :delete %> |
+ <%= link_to "View All", users_path %>
+</p>
@@ -0,0 +1,110 @@
+# Don't change this file!
+# Configure your app in config/environment.rb and config/environments/*.rb
+
+RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
+
+module Rails
+ class << self
+ def boot!
+ unless booted?
+ preinitialize
+ pick_boot.run
+ end
+ end
+
+ def booted?
+ defined? Rails::Initializer
+ end
+
+ def pick_boot
+ (vendor_rails? ? VendorBoot : GemBoot).new
+ end
+
+ def vendor_rails?
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
+ end
+
+ def preinitialize
+ load(preinitializer_path) if File.exist?(preinitializer_path)
+ end
+
+ def preinitializer_path
+ "#{RAILS_ROOT}/config/preinitializer.rb"
+ end
+ end
+
+ class Boot
+ def run
+ load_initializer
+ Rails::Initializer.run(:set_load_path)
+ end
+ end
+
+ class VendorBoot < Boot
+ def load_initializer
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
+ Rails::Initializer.run(:install_gem_spec_stubs)
+ Rails::GemDependency.add_frozen_gem_path
+ end
+ end
+
+ class GemBoot < Boot
+ def load_initializer
+ self.class.load_rubygems
+ load_rails_gem
+ require 'initializer'
+ end
+
+ def load_rails_gem
+ if version = self.class.gem_version
+ gem 'rails', version
+ else
+ gem 'rails'
+ end
+ rescue Gem::LoadError => load_error
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
+ exit 1
+ end
+
+ class << self
+ def rubygems_version
+ Gem::RubyGemsVersion rescue nil
+ end
+
+ def gem_version
+ if defined? RAILS_GEM_VERSION
+ RAILS_GEM_VERSION
+ elsif ENV.include?('RAILS_GEM_VERSION')
+ ENV['RAILS_GEM_VERSION']
+ else
+ parse_gem_version(read_environment_rb)
+ end
+ end
+
+ def load_rubygems
+ min_version = '1.3.2'
+ require 'rubygems'
+ unless rubygems_version >= min_version
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
+ exit 1
+ end
+
+ rescue LoadError
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
+ exit 1
+ end
+
+ def parse_gem_version(text)
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
+ end
+
+ private
+ def read_environment_rb
+ File.read("#{RAILS_ROOT}/config/environment.rb")
+ end
+ end
+ end
+end
+
+# All that for this:
+Rails.boot!
Oops, something went wrong.

0 comments on commit 078e0c0

Please sign in to comment.