Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Media upload #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@
/tmp

config/database.yml
features/.DS_Store
public/uploads/*
.DS_Store
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ gem 'devise'
gem 'jquery-rails'
gem 'rails', '3.2.8'
gem 'pg'
gem 'carrierwave'
gem 'mini_magick'

# Gems used only for assets and not required
# in production environments by default.
Expand All @@ -23,6 +25,7 @@ end
group :test, :development do
gem 'fabrication'
gem 'rspec-rails'
gem 'capybara-mechanize'
end

group :test do
Expand Down
48 changes: 39 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,23 @@ 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)
capybara-mechanize (0.3.0)
capybara (~> 1.1)
mechanize (~> 2.3)
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 +56,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 All @@ -66,6 +73,8 @@ GEM
railties (~> 3.1)
warden (~> 1.2.1)
diff-lcs (1.1.3)
domain_name (0.5.4)
unf (~> 0.0.3)
erubis (2.7.0)
execjs (1.4.0)
multi_json (~> 1.0)
Expand All @@ -88,9 +97,22 @@ GEM
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mechanize (2.5.1)
domain_name (~> 0.5, >= 0.5.1)
mime-types (~> 1.17, >= 1.17.2)
net-http-digest_auth (~> 1.1, >= 1.1.1)
net-http-persistent (~> 2.5, >= 2.5.2)
nokogiri (~> 1.4)
ntlm-http (~> 0.1, >= 0.1.1)
webrobots (~> 0.0, >= 0.0.9)
mime-types (1.19)
multi_json (1.3.6)
mini_magick (3.4)
subexec (~> 0.2.1)
multi_json (1.3.7)
net-http-digest_auth (1.2.1)
net-http-persistent (2.8)
nokogiri (1.5.5)
ntlm-http (0.1.1)
orm_adapter (0.4.0)
pg (0.14.1)
polyglot (0.3.3)
Expand Down Expand Up @@ -134,12 +156,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 @@ -154,17 +176,22 @@ GEM
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
subexec (0.2.2)
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)
unf (0.0.5)
unf_ext
unf_ext (0.0.5)
warden (1.2.1)
rack (>= 1.0)
webrobots (0.0.13)
xpath (0.1.4)
nokogiri (~> 1.3)

Expand All @@ -173,13 +200,16 @@ PLATFORMS

DEPENDENCIES
bourbon
capybara-mechanize
carrierwave
coffee-rails (~> 3.2.1)
cucumber-rails
database_cleaner
devise
fabrication
jquery-rails
launchy
mini_magick
pg
rails (= 3.2.8)
rails-boilerplate
Expand Down
Binary file added app/.DS_Store
Binary file not shown.
Binary file added app/models/.DS_Store
Binary file not shown.
19 changes: 17 additions & 2 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
require 'carrierwave/processing/mini_magick'

class Post < ActiveRecord::Base
attr_accessible :body, :title
attr_accessible :body, :title, :image, :video_title, :video_url

validates_presence_of :title, :body, :author

validates :video_url, :presence => true, :if => :video_title_present?
validates :video_title, :presence => true, :if => :video_url_present?

def video_title_present?
!self.video_title.blank?
end

def video_url_present?
!self.video_url.blank?
end

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
12 changes: 12 additions & 0 deletions app/validators/url_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class UrlValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || "must be a valid URL") unless url_valid?(value)
end

# a URL may be technically well-formed but may
# not actually be valid, so this checks for both.
def url_valid?(url)
url = URI.parse(url) rescue false
url.kind_of?(URI::HTTP) || url.kind_of?(URI::HTTPS)
end
end
10 changes: 8 additions & 2 deletions app/views/posts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<%= form_for @post do |f| %>
<%= form_for @post, html: {:multipart => true} do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :body %>
<%= f.text_field :body %>
<%= f.text_field :body %><br \>
<%= f.label :video_title %>
<%= f.text_field :video_title %>
<%= f.label :video_url %>
<%= f.text_field :video_url %>
<%= f.label :image %>
<%= f.file_field :image %>
<%= f.submit submit_text %>
<%- end %>
9 changes: 8 additions & 1 deletion app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
<%= link_to "View All Posts", :root %>
<%= render @post %>
<%= link_to "Edit Post", edit_post_path(@post) %>
<% if @post.video_title %>
<div class="video">
<%= link_to @post.video_title, @post.video_url.to_s %>
</div>
<div class="clear"></div>
<% end %>
<%= link_to "Edit Post", edit_post_path(@post), :id => "video" %>
<hr />
<ul><%= render @post.parent_comments %></ul>
<hr />
<h2>Have a thought? Comment here!</h2>
<%= render partial: 'comments/form', locals: { submit_text: "Create Comment", comment: @comment } %>

2 changes: 2 additions & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

# Initialize the rails application
Weeblog::Application.initialize!


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
6 changes: 6 additions & 0 deletions db/migrate/20121108173114_add_video_to_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddVideoToPosts < ActiveRecord::Migration
def change
add_column :posts, :video_title, :string
add_column :posts, :video_url, :string
end
end
26 changes: 23 additions & 3 deletions 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 => 20121108173114) do

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

create_table "posts", :force => true do |t|
create_table "images", :force => true do |t|
t.string "title"
t.text "body"
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"
t.string "image"
t.string "video_title"
t.string "video_url"
end

create_table "users", :force => true do |t|
Expand Down
19 changes: 19 additions & 0 deletions features/step_definitions/interaction_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,22 @@
field_value = (field.tag_name == 'textarea') ? field.text : field.value
field_value.should == text
end

And /^I attach "(.*?)" to "(.*?)"$/ do |attached_file, locator|
page.attach_file(locator, attached_file)
#attach_file('Image', '/path/to/image.jpg')
end


Then /^I should see my video$/ do
page.has_content?('View Video')
end

Then /^I should see the image "(.*?)" in "(.*?)" the CSS$/ do |content, css|
find('img')['src'].should == "http://www.google.com/logo.png"
end

Then /^I should see the video "(.*?)"$/ do |content|
page.find_link(content).visible?
#find('a')['href'].should == content find_link(locator)
end
Loading