Skip to content

Commit

Permalink
separate image model rabbit trail
Browse files Browse the repository at this point in the history
  • Loading branch information
sam452 committed Nov 8, 2012
1 parent 3baff56 commit 13ac356
Show file tree
Hide file tree
Showing 25 changed files with 185 additions and 10 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gem 'devise'
gem 'jquery-rails'
gem 'rails', '3.2.8'
gem 'pg'
gem 'carrierwave'

# Gems used only for assets and not required
# in production environments by default.
Expand Down
23 changes: 14 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ GEM
addressable (2.3.2)
arel (3.0.2)
bcrypt-ruby (3.0.1)
bourbon (2.1.1)
sass (>= 3.1)
bourbon (2.1.2)
sass (>= 3.2)
thor
builder (3.0.4)
capybara (1.1.2)
capybara (1.1.3)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 0.1.4)
carrierwave (0.7.0)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
childprocess (0.3.6)
ffi (~> 1.0, >= 1.0.6)
coffee-rails (3.2.2)
Expand All @@ -49,7 +53,7 @@ GEM
coffee-script (2.2.0)
coffee-script-source
execjs
coffee-script-source (1.3.3)
coffee-script-source (1.4.0)
cucumber (1.2.1)
builder (>= 2.1.2)
diff-lcs (>= 1.1.3)
Expand Down Expand Up @@ -89,7 +93,7 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.19)
multi_json (1.3.6)
multi_json (1.3.7)
nokogiri (1.5.5)
orm_adapter (0.4.0)
pg (0.14.1)
Expand Down Expand Up @@ -134,12 +138,12 @@ GEM
railties (>= 3.0)
rspec (~> 2.11.0)
rubyzip (0.9.9)
sass (3.2.1)
sass (3.2.2)
sass-rails (3.2.5)
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
selenium-webdriver (2.25.0)
selenium-webdriver (2.26.0)
childprocess (>= 0.2.5)
libwebsocket (~> 0.1.3)
multi_json (~> 1.0)
Expand All @@ -156,10 +160,10 @@ GEM
tilt (~> 1.1, != 1.3.0)
thor (0.16.0)
tilt (1.3.3)
treetop (1.4.11)
treetop (1.4.12)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.33)
tzinfo (0.3.35)
uglifier (1.3.0)
execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2)
Expand All @@ -173,6 +177,7 @@ PLATFORMS

DEPENDENCIES
bourbon
carrierwave
coffee-rails (~> 3.2.1)
cucumber-rails
database_cleaner
Expand Down
Binary file added app/.DS_Store
Binary file not shown.
15 changes: 15 additions & 0 deletions app/controllers/images_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class ImagesController < ApplicationController
def create
@post = Post.find(params[:post_id])
@image = Image.new(params[:image])
@images.post = @post

if @image.save
flash[:notice] = "Your #{@images.title} has been posted."
redirect_to @post
else
flash[:error] = "There was a problem posting your media. #{@images.errors.full_messages.join(". ")}."
render 'posts/show'
end
end
end
1 change: 1 addition & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def index

def show
@comment = Comment.new
@image = Image.new
end

def create
Expand Down
Binary file added app/models/.DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions app/models/image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Image < ActiveRecord::Base
attr_accessible :path, :post_id, :title, :image
mount_uploader :image, ImageUploader
belongs_to :post
end
3 changes: 3 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ class Post < ActiveRecord::Base
belongs_to :author, class_name: "User"
has_many :comments
has_many :parent_comments, class_name: "Comment", conditions: { parent_comment_id: nil }
has_many :images
mount_uploader :image, ImageUploader

end
55 changes: 55 additions & 0 deletions app/uploaders/image_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# encoding: utf-8

class ImageUploader < CarrierWave::Uploader::Base

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

# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper

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

# 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
# # For Rails 3.1+ asset pipeline compatibility:
# # asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/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 :scale => [50, 50]
# 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:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end

end
8 changes: 8 additions & 0 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<%= link_to "View All Posts", :root %>
<%= render @post %>

<%= form_for @image, url: post_images_path(@post), html: { multipart: true } do |f| %>
<%= f.file_field :image %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.submit "Submit" %>
<%- end %>

<%= link_to "Edit Post", edit_post_path(@post) %>
<hr />
<ul><%= render @post.parent_comments %></ul>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

resources :posts, except: [:destroy] do
resources :comments, only: [:create]
resources :images, only: [:create]
end

# The priority is based upon order of creation:
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20121107163243_add_media_to_post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddMediaToPost < ActiveRecord::Migration
def change
add_column :posts, :media, :string
end
end
11 changes: 11 additions & 0 deletions db/migrate/20121107203118_create_images.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateImages < ActiveRecord::Migration
def change
create_table :images do |t|
t.string :title
t.integer :post_id
t.string :path

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20121107212110_add_image_id_added_to_post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddImageIdAddedToPost < ActiveRecord::Migration
def change
add_column :posts, :image_id, :integer
end
end
5 changes: 5 additions & 0 deletions db/migrate/20121108143858_add_image_to_post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddImageToPost < ActiveRecord::Migration
def change
add_column :posts, :image, :string
end
end
20 changes: 19 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20121106174159) do
ActiveRecord::Schema.define(:version => 20121108143858) do

create_table "comments", :force => true do |t|
t.integer "post_id"
Expand All @@ -22,12 +22,30 @@
t.integer "parent_comment_id"
end

create_table "images", :force => true do |t|
t.string "title"
t.integer "post_id"
t.string "path"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "media", :force => true do |t|
t.string "title"
t.string "path"
t.string "media"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "posts", :force => true do |t|
t.string "title"
t.text "body"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "author_id"
t.integer "image_id"

This comment has been minimized.

Copy link
@elizabrock

elizabrock Nov 14, 2012

Member

I don't think you need both image_id and image added to the post. Which one is actually necessary?

t.string "image"
end

create_table "users", :force => true do |t|
Expand Down
32 changes: 32 additions & 0 deletions features/user_creates_post_and_uploads_media.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@wip
Feature: User creates post and uploads media
As a user
In order to share my thoughts with the world
I want to publish a blog post with media

Acceptance Criteria:
* Post must have title and body
* Post must be publicly visible once saved
* Post must show media in the post


Background:
Given I am signed in

Scenario: Happy Path
Given I am on the homepage
And I click "Write Post"
Then I should see "Fill in your blog post here"
When I fill in "My thoughts are grand" for "Title"
And I fill in "Actually, they aren't" for "Body"
And I press "Publish"
Then I should see "Your post has been published"
And I should see the title "My thoughts are grand"
And I should see "Actually, they aren't"
And show me the page
And I attach "/Users/sam/Desktop/myfile.mov" to "media_media"
And I fill in "My video" for "Title"
Then I should see "Your post has been published"
And I should see the title "My thoughts are grand"
And I should see "Actually, they aren't"
And I should see my video
Binary file added public/uploads/media/media/1/Picture_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/media/media/2/myfile.mov
Binary file not shown.
Binary file added public/uploads/media/media/3/myfile.mov
Binary file not shown.
Binary file added public/uploads/media/media/4/myfile.mov
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/media/media/6/myfile.mov
Binary file not shown.
Loading
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 spec/models/image_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe Image do
pending "add some examples to (or delete) #{__FILE__}"
end

1 comment on commit 13ac356

@elizabrock
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't be committing those temporary uploaded files. You need to remove the items in public/uploads and add public/uploads to the .gitignore.

Please sign in to comment.