Skip to content
Browse files

Upload de imagens - primeira parte da setima aula...

  • Loading branch information...
1 parent 3025948 commit d64038d46fe50e16bdab7f83532f4b259bd8dbc3 @wfsneto committed Nov 24, 2011
View
2 Gemfile
@@ -7,6 +7,8 @@ gem 'rails', '3.0.10'
gem 'sqlite3'
+gem 'paperclip', '2.4.5'
+
# Use unicorn as the web server
# gem 'unicorn'
View
21 Gemfile.lock
@@ -30,17 +30,24 @@ GEM
activesupport (3.0.10)
arel (2.0.10)
builder (2.1.2)
+ cocaine (0.2.0)
erubis (2.6.6)
abstract (>= 1.0.0)
i18n (0.5.0)
+ json (1.6.1)
mail (2.2.19)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
- mime-types (1.16)
- polyglot (0.3.2)
- rack (1.2.3)
+ mime-types (1.17.2)
+ paperclip (2.4.5)
+ activerecord (>= 2.3.0)
+ activesupport (>= 2.3.2)
+ cocaine (>= 0.0.2)
+ mime-types
+ polyglot (0.3.3)
+ rack (1.2.4)
rack-mount (0.6.14)
rack (>= 1.0.0)
rack-test (0.5.7)
@@ -59,18 +66,20 @@ GEM
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (~> 0.14.4)
- rake (0.9.2)
- rdoc (3.9.4)
+ rake (0.9.2.2)
+ rdoc (3.11)
+ json (~> 1.4)
sqlite3 (1.3.4)
thor (0.14.6)
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
- tzinfo (0.3.29)
+ tzinfo (0.3.31)
PLATFORMS
ruby
DEPENDENCIES
+ paperclip (= 2.4.5)
rails (= 3.0.10)
sqlite3
View
40 app/controllers/admin/assets_controller.rb
@@ -0,0 +1,40 @@
+class Admin::AssetsController < Admin::AdminController
+ #menu_item :assets
+
+ def index
+ @assets = Asset.all
+ respond_with @assets
+ end
+
+ def new
+ @asset = Asset.new
+ respond_with @asset
+ end
+
+ def edit
+ @asset = Asset.find(params[:id])
+ end
+
+ def show
+ @asset = Asset.find(params[:id])
+ respond_with @asset
+ end
+
+ def create
+ @asset = Asset.new(params[:asset])
+ flash[:notice] = 'Asset was successfully created.' if @asset.save
+ respond_with @asset, :location => [:admin, @asset]
+ end
+
+ def update
+ @asset = Asset.find(params[:id])
+ flash[:notice] = 'Asset was successfully updated.' if @asset.update_attributes(params[:asset])
+ respond_with @asset, :location => [:admin, @asset]
+ end
+
+ def destroy
+ @asset = Asset.find(params[:id])
+ @asset.destroy
+ respond_with @asset, :location => admin_assets_path
+ end
+end
View
2 app/helpers/assets_helper.rb
@@ -0,0 +1,2 @@
+module AssetsHelper
+end
View
14 app/models/asset.rb
@@ -0,0 +1,14 @@
+class Asset < ActiveRecord::Base
+
+ validates_presence_of :title
+
+ has_attached_file :document, :styles => {
+ :medium => "300x300#",
+ :thumb => "50x50#"
+ }
+
+ validates_attachment_presence :document
+ validates_attachment_size :document, :less_than => 5.megabyte
+ validates_attachment_content_type :document, :content_type => %w(image/png image/jpeg image/pjpeg image/gif image/tiff)
+
+end
View
18 app/views/admin/assets/_form.html.erb
@@ -0,0 +1,18 @@
+<%= form_for [:admin, @asset], :html => { :multipart => true } do |f| %>
+ <%= error_messages_for @asset %>
+
+ <div class="field">
+ <%= f.text_field :title, :placeholder => "Title" %>
+ </div>
+
+ <div class="field">
+ <%= f.file_field :document %>
+ </div>
+
+ <hr class="thick" />
+
+ <p>
+ <%= f.submit :class => "button" %> or <%= link_to "cancel", admin_assets_path %>
+ </p>
+<% end %>
+
View
3 app/views/admin/assets/edit.html.erb
@@ -0,0 +1,3 @@
+<h2>Editing Asset</h2>
+
+<%= render 'form' %>
View
32 app/views/admin/assets/index.html.erb
@@ -0,0 +1,32 @@
+<h2>Assets</h2>
+
+<%- if @assets.present? -%>
+
+ <table class="grid">
+ <% @assets.each do |asset| %>
+ <%= content_tag_for :tr, asset, :class => cycle(:odd, :even) do %>
+ <td style="width:10%">
+ <%= link_to image_tag(asset.document.url(:thumb), :class => "middle"), [:admin, asset] %>
+ </td>
+ <td>
+ <%= link_to asset.title, [:admin, asset] %>
+ </td>
+ <td style="width:15%; text-align:right;">
+ <%= link_to_edit [:edit, :admin, asset] %>
+ <%= link_to_destroy [:admin, asset] %>
+ </td>
+ <% end %>
+ <% end %>
+ </table>
+
+<%- else -%>
+ <p>You don't have any asset yet.</p>
+<%- end -%>
+
+<hr class="thick" />
+
+<div class="bar">
+ <div class="right">
+ <%= link_to 'New asset', new_admin_asset_path, :class => "button" %>
+ </div>
+</div>
View
3 app/views/admin/assets/new.html.erb
@@ -0,0 +1,3 @@
+<h1>New asset</h1>
+
+<%= render 'form' %>
View
23 app/views/admin/assets/show.html.erb
@@ -0,0 +1,23 @@
+<div class="asset">
+ <div class="preview">
+ <%= link_to image_tag(@asset.document.url(:medium)), @asset.document.url, :target => "_blank" %>
+ </div>
+
+ <div class="info">
+ <p>
+ <b>Title:</b>
+ <%= @asset.title %>
+ <span class="hint">
+ (<%= number_to_human_size @asset.document_file_size %>)
+ <%= @asset.document_content_type %>
+ </span>
+ </p>
+
+ <hr class="thick" />
+
+ <p>
+ <%= link_to 'Edit', edit_admin_asset_path(@asset), :class => "button" %>
+ <%= link_to 'Back', admin_assets_path, :class => "button" %>
+ </p>
+ </div>
+</div>
View
2 app/views/layouts/admin.html.erb
@@ -3,7 +3,7 @@
<head>
<title>Producer</title>
<%= stylesheet_link_tag 'admin', 'scaffold' %>
- <%= javascript_include_tag :defaults %>
+ <%= javascript_include_tag "vendor/jquery", "rails" %>
<%= csrf_meta_tag %>
</head>
<body>
View
2 app/views/layouts/application.html.erb
@@ -2,7 +2,7 @@
<head>
<title>Producer</title>
<%= stylesheet_link_tag "application", "scaffold" %>
- <%= javascript_include_tag :defaults %>
+ <%= javascript_include_tag "vendor/jquery", "rails" %>
<%= csrf_meta_tag %>
</head>
<body>
View
3 config/routes.rb
@@ -1,10 +1,13 @@
Producer::Application.routes.draw do
+ resources :assets
+
root :to => "pages#index"
namespace :admin do
resources :posts
resources :categories, :except => [:show]
+ resources :assets
end
resources :categories
View
14 db/migrate/20111124210240_create_assets.rb
@@ -0,0 +1,14 @@
+class CreateAssets < ActiveRecord::Migration
+ def self.up
+ create_table :assets do |t|
+ t.string :title
+ t.string :description
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :assets
+ end
+end
View
15 db/migrate/20111124210336_add_attachment_document_to_asset.rb
@@ -0,0 +1,15 @@
+class AddAttachmentDocumentToAsset < ActiveRecord::Migration
+ def self.up
+ add_column :assets, :document_file_name, :string
+ add_column :assets, :document_content_type, :string
+ add_column :assets, :document_file_size, :integer
+ add_column :assets, :document_updated_at, :datetime
+ end
+
+ def self.down
+ remove_column :assets, :document_file_name
+ remove_column :assets, :document_content_type
+ remove_column :assets, :document_file_size
+ remove_column :assets, :document_updated_at
+ end
+end
View
13 db/schema.rb
@@ -10,7 +10,18 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20111007024719) do
+ActiveRecord::Schema.define(:version => 20111124210336) do
+
+ create_table "assets", :force => true do |t|
+ t.string "title"
+ t.string "description"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ t.string "document_file_name"
+ t.string "document_content_type"
+ t.integer "document_file_size"
+ t.datetime "document_updated_at"
+ end
create_table "categories", :force => true do |t|
t.string "name"
View
18 public/stylesheets/scaffold.css
@@ -12,6 +12,10 @@ pre {
font-size: 11px;
}
+a { color: #000; }
+a:visited { color: #666; }
+a:hover { color: #fff; background-color:#000; }
+
div.field, div.actions {
margin-bottom: 10px;
}
@@ -20,16 +24,14 @@ div.field, div.actions {
color: green;
}
-#alert {
- color: red;
-}
-
-.field_with_errors input, .field_with_errors textarea{
- background-color: #FFFFE6;
- border: 1px solid red;
+.field_with_errors {
+ padding: 2px;
+ background-color: red;
+ display: table;
}
#error_explanation {
+ width: 450px;
border: 2px solid red;
padding: 7px;
padding-bottom: 0;
@@ -50,5 +52,5 @@ div.field, div.actions {
#error_explanation ul li {
font-size: 12px;
- list-style: none;
+ list-style: square;
}
View
BIN public/system/documents/1/medium/Earth-Upload-icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN public/system/documents/1/original/Earth-Upload-icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN public/system/documents/1/thumb/Earth-Upload-icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
9 test/fixtures/assets.yml
@@ -0,0 +1,9 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+ title: MyString
+ description: MyString
+
+two:
+ title: MyString
+ description: MyString
View
39 test/functional/assets_controller_test.rb
@@ -0,0 +1,39 @@
+class Admin::AssetsController < Admin::AdminController
+ menu_item :assets
+
+ def index
+ @assets = Asset.all
+ respond_with @assets
+ end
+
+ def new
+ @asset = Asset.new
+ respond_with @asset
+ end
+
+ def edit
+ @asset = Asset.find(params[:id])
+ end
+
+ def show
+ @asset = Asset.find(params[:id])
+ respond_with @asset
+ end
+
+ def create
+ @asset = Asset.new(params[:asset])
+ flash[:notice] = 'Asset was successfully created.' if @asset.save
+ respond_with @asset, :location => [:admin, @asset]
+ end
+
+ def update
+ @asset = Asset.find(params[:id])
+ flash[:notice] = 'Asset was successfully updated.' if @asset.update_attributes(params[:asset])
+ respond_with @asset, :location => [:admin, @asset]
+ end
+
+ def destroy
+ @asset = Asset.find(params[:id])
+ @asset.destroy
+ respond_with @asset, :location => admin_assets_path
+ end
View
8 test/unit/asset_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class AssetTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
4 test/unit/helpers/assets_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class AssetsHelperTest < ActionView::TestCase
+end

0 comments on commit d64038d

Please sign in to comment.
Something went wrong with that request. Please try again.