Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
adding episode 253
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Feb 13, 2011
1 parent b482bb8 commit ea9ff12
Show file tree
Hide file tree
Showing 77 changed files with 10,308 additions and 0 deletions.
10 changes: 10 additions & 0 deletions episode-253/README
@@ -0,0 +1,10 @@
Railscasts Episode #253: CarrierWave File Uploads

http://railscasts.com/episodes/253

Commands

bundle
rails g uploader image
rails g migration add_image_to_paintings image:string
rake db:migrate
Binary file added episode-253/paintings/burney_falls.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added episode-253/paintings/crooked_river.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added episode-253/paintings/quiet_place.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added episode-253/paintings/tree_on_the_bank.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added episode-253/paintings/union_creek.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added episode-253/paintings/what_lies_beneath.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions episode-253/picturesque/.gitignore
@@ -0,0 +1,5 @@
.bundle
db/*.sqlite3
log/*.log
tmp/**/*
public/uploads
9 changes: 9 additions & 0 deletions episode-253/picturesque/Gemfile
@@ -0,0 +1,9 @@
source 'http://rubygems.org'

gem 'rails', '3.0.3'
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
gem 'nifty-generators'
gem 'mocha', :group => :test

gem "rmagick"
gem "carrierwave"
83 changes: 83 additions & 0 deletions episode-253/picturesque/Gemfile.lock
@@ -0,0 +1,83 @@
GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionmailer (3.0.3)
actionpack (= 3.0.3)
mail (~> 2.2.9)
actionpack (3.0.3)
activemodel (= 3.0.3)
activesupport (= 3.0.3)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.4)
rack (~> 1.2.1)
rack-mount (~> 0.6.13)
rack-test (~> 0.5.6)
tzinfo (~> 0.3.23)
activemodel (3.0.3)
activesupport (= 3.0.3)
builder (~> 2.1.2)
i18n (~> 0.4)
activerecord (3.0.3)
activemodel (= 3.0.3)
activesupport (= 3.0.3)
arel (~> 2.0.2)
tzinfo (~> 0.3.23)
activeresource (3.0.3)
activemodel (= 3.0.3)
activesupport (= 3.0.3)
activesupport (3.0.3)
arel (2.0.8)
builder (2.1.2)
carrierwave (0.5.1)
activesupport (~> 3.0)
erubis (2.6.6)
abstract (>= 1.0.0)
i18n (0.5.0)
mail (2.2.15)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
mocha (0.9.10)
rake
nifty-generators (0.4.4)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack (>= 1.0.0)
rack-test (0.5.7)
rack (>= 1.0)
rails (3.0.3)
actionmailer (= 3.0.3)
actionpack (= 3.0.3)
activerecord (= 3.0.3)
activeresource (= 3.0.3)
activesupport (= 3.0.3)
bundler (~> 1.0)
railties (= 3.0.3)
railties (3.0.3)
actionpack (= 3.0.3)
activesupport (= 3.0.3)
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.8.7)
rmagick (2.13.1)
sqlite3-ruby (1.2.5)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.24)

PLATFORMS
ruby

DEPENDENCIES
carrierwave
mocha
nifty-generators
rails (= 3.0.3)
rmagick
sqlite3-ruby (= 1.2.5)
7 changes: 7 additions & 0 deletions episode-253/picturesque/Rakefile
@@ -0,0 +1,7 @@
# 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.expand_path('../config/application', __FILE__)
require 'rake'

Picturesque::Application.load_tasks
@@ -0,0 +1,3 @@
class ApplicationController < ActionController::Base
protect_from_forgery
end
44 changes: 44 additions & 0 deletions episode-253/picturesque/app/controllers/galleries_controller.rb
@@ -0,0 +1,44 @@
class GalleriesController < ApplicationController
def index
@galleries = Gallery.all
end

def show
@gallery = Gallery.find(params[:id])
end

def new
@gallery = Gallery.new
end

def create
@gallery = Gallery.new(params[:gallery])
if @gallery.save
flash[:notice] = "Successfully created gallery."
redirect_to @gallery
else
render :action => 'new'
end
end

def edit
@gallery = Gallery.find(params[:id])
end

def update
@gallery = Gallery.find(params[:id])
if @gallery.update_attributes(params[:gallery])
flash[:notice] = "Successfully updated gallery."
redirect_to gallery_url
else
render :action => 'edit'
end
end

def destroy
@gallery = Gallery.find(params[:id])
@gallery.destroy
flash[:notice] = "Successfully destroyed gallery."
redirect_to galleries_url
end
end
36 changes: 36 additions & 0 deletions episode-253/picturesque/app/controllers/paintings_controller.rb
@@ -0,0 +1,36 @@
class PaintingsController < ApplicationController
def new
@painting = Painting.new(:gallery_id => params[:gallery_id])
end

def create
@painting = Painting.new(params[:painting])
if @painting.save
flash[:notice] = "Successfully created painting."
redirect_to @painting.gallery
else
render :action => 'new'
end
end

def edit
@painting = Painting.find(params[:id])
end

def update
@painting = Painting.find(params[:id])
if @painting.update_attributes(params[:painting])
flash[:notice] = "Successfully updated painting."
redirect_to @painting.gallery
else
render :action => 'edit'
end
end

def destroy
@painting = Painting.find(params[:id])
@painting.destroy
flash[:notice] = "Successfully destroyed painting."
redirect_to @painting.gallery
end
end
2 changes: 2 additions & 0 deletions episode-253/picturesque/app/helpers/application_helper.rb
@@ -0,0 +1,2 @@
module ApplicationHelper
end
23 changes: 23 additions & 0 deletions episode-253/picturesque/app/helpers/error_messages_helper.rb
@@ -0,0 +1,23 @@
module ErrorMessagesHelper
# Render error messages for the given objects. The :message and :header_message options are allowed.
def error_messages_for(*objects)
options = objects.extract_options!
options[:header_message] ||= "Invalid Fields"
options[:message] ||= "Correct the following errors and try again."
messages = objects.compact.map { |o| o.errors.full_messages }.flatten
unless messages.empty?
content_tag(:div, :class => "error_messages") do
list_items = messages.map { |msg| content_tag(:li, msg) }
content_tag(:h2, options[:header_message]) + content_tag(:p, options[:message]) + content_tag(:ul, list_items.join.html_safe)
end
end
end

module FormBuilderAdditions
def error_messages(options = {})
@template.error_messages_for(@object, options)
end
end
end

ActionView::Helpers::FormBuilder.send(:include, ErrorMessagesHelper::FormBuilderAdditions)
2 changes: 2 additions & 0 deletions episode-253/picturesque/app/helpers/galleries_helper.rb
@@ -0,0 +1,2 @@
module GalleriesHelper
end
22 changes: 22 additions & 0 deletions episode-253/picturesque/app/helpers/layout_helper.rb
@@ -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) { h(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
2 changes: 2 additions & 0 deletions episode-253/picturesque/app/helpers/paintings_helper.rb
@@ -0,0 +1,2 @@
module PaintingsHelper
end
4 changes: 4 additions & 0 deletions episode-253/picturesque/app/models/gallery.rb
@@ -0,0 +1,4 @@
class Gallery < ActiveRecord::Base
attr_accessible :name
has_many :paintings
end
5 changes: 5 additions & 0 deletions episode-253/picturesque/app/models/painting.rb
@@ -0,0 +1,5 @@
class Painting < ActiveRecord::Base
attr_accessible :gallery_id, :name, :image, :remote_image_url
belongs_to :gallery
mount_uploader :image, ImageUploader
end
47 changes: 47 additions & 0 deletions episode-253/picturesque/app/uploaders/image_uploader.rb
@@ -0,0 +1,47 @@
# encoding: utf-8

class ImageUploader < CarrierWave::Uploader::Base

# Include RMagick or ImageScience support:
include CarrierWave::RMagick
# include CarrierWave::ImageScience

# Choose what kind of storage to use for this uploader:
storage :file
# storage :s3

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end

# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end

# Create different versions of your uploaded files:
version :thumb do
process :resize_to_limit => [200, 200]
end

# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_white_list
# %w(jpg jpeg gif png)
# end

# Override the filename of the uploaded files:
# def filename
# "something.jpg" if original_filename
# end

end
8 changes: 8 additions & 0 deletions episode-253/picturesque/app/views/galleries/_form.html.erb
@@ -0,0 +1,8 @@
<%= form_for @gallery do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p><%= f.submit %></p>
<% end %>
8 changes: 8 additions & 0 deletions episode-253/picturesque/app/views/galleries/edit.html.erb
@@ -0,0 +1,8 @@
<% title "Edit Gallery" %>
<%= render 'form' %>

<p>
<%= link_to "Show", @gallery %> |
<%= link_to "View All", galleries_path %>
</p>
10 changes: 10 additions & 0 deletions episode-253/picturesque/app/views/galleries/index.html.erb
@@ -0,0 +1,10 @@
<% title "Galleries" %>
<% for gallery in @galleries %>
<div class="gallery">
<h2><%= link_to gallery.name, gallery %></h2>
Paintings: <%= gallery.paintings.size %>
</div>
<% end %>

<p><%= link_to "New Gallery", new_gallery_path %></p>
5 changes: 5 additions & 0 deletions episode-253/picturesque/app/views/galleries/new.html.erb
@@ -0,0 +1,5 @@
<% title "New Gallery" %>
<%= render 'form' %>

<p><%= link_to "Back to List", galleries_path %></p>
21 changes: 21 additions & 0 deletions episode-253/picturesque/app/views/galleries/show.html.erb
@@ -0,0 +1,21 @@
<% title @gallery.name %>

<div id="paintings">
<% for painting in @gallery.paintings %>
<div class="painting">
<%= image_tag painting.image_url(:thumb) if painting.image? %>
<div class="name"><%= painting.name %></div>
<div class="actions">
<%= link_to "edit", edit_painting_path(painting) %> |
<%= link_to "remove", painting, :confirm => 'Are you sure?', :method => :delete %>
</div>
</div>
<% end %>
<div class="clear"></div>
</div>

<p>
<%= link_to "Add a Painting", new_painting_path(:gallery_id => @gallery) %> |
<%= link_to "Remove Gallery", @gallery, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "View Galleries", galleries_path %>
</p>
19 changes: 19 additions & 0 deletions episode-253/picturesque/app/views/layouts/application.html.erb
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title><%= content_for?(:title) ? yield(:title) : "Untitled" %></title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
<%= yield(:head) %>
</head>
<body>
<div id="container">
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
<%= content_tag :h1, yield(:title) if show_title? %>
<%= yield %>
</div>
</body>
</html>

0 comments on commit ea9ff12

Please sign in to comment.