This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
77 changed files
with
10,308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.bundle | ||
db/*.sqlite3 | ||
log/*.log | ||
tmp/**/* | ||
public/uploads |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
3 changes: 3 additions & 0 deletions
3
episode-253/picturesque/app/controllers/application_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class ApplicationController < ActionController::Base | ||
protect_from_forgery | ||
end |
44 changes: 44 additions & 0 deletions
44
episode-253/picturesque/app/controllers/galleries_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
36
episode-253/picturesque/app/controllers/paintings_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module ApplicationHelper | ||
end |
23 changes: 23 additions & 0 deletions
23
episode-253/picturesque/app/helpers/error_messages_helper.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module GalleriesHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module PaintingsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Gallery < ActiveRecord::Base | ||
attr_accessible :name | ||
has_many :paintings | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
10
episode-253/picturesque/app/views/galleries/index.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<% title "New Gallery" %> | ||
<%= render 'form' %> | ||
|
||
<p><%= link_to "Back to List", galleries_path %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
19
episode-253/picturesque/app/views/layouts/application.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
Oops, something went wrong.