Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[#6] Add avatars to friends

  • Loading branch information...
commit 8773776d50cc23b2e1a22a67f105039bdd7484cd 1 parent 2ef79cc
@harlow harlow authored
View
4 .gitignore
@@ -7,9 +7,13 @@
# Ignore bundler config
/.bundle
+# Ignore images uploaded in Test and Development environments
+/public/system
+
# Ignore the default SQLite database.
/db/*.sqlite3
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp
+*.swp
View
8 app/models/friend.rb
@@ -1,3 +1,9 @@
class Friend < ActiveRecord::Base
- attr_accessible :name
+ attr_accessible :name, :avatar
+ validates_presence_of :name
+
+ has_attached_file :avatar, styles: {
+ thumb: '100x100>',
+ medium: '300x300>'
+ }
end
View
4 app/views/friends/_form.html.erb
@@ -15,6 +15,10 @@
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
+ <div class="field">
+ <%= f.label :avatar %><br />
+ <%= f.file_field :avatar %>
+ </div>
<div class="actions">
<%= f.submit %>
</div>
View
2  app/views/friends/index.html.erb
@@ -2,6 +2,7 @@
<table>
<tr>
+ <th>Avatar</th>
<th>Name</th>
<th></th>
<th></th>
@@ -10,6 +11,7 @@
<% @friends.each do |friend| %>
<tr>
+ <td><%= image_tag friend.avatar.url(:thumb) %>
<td><%= friend.name %></td>
<td><%= link_to 'Show', friend %></td>
<td><%= link_to 'Edit', edit_friend_path(friend) %></td>
View
1  app/views/friends/show.html.erb
@@ -1,6 +1,7 @@
<p id="notice"><%= notice %></p>
<p>
+ <%= image_tag @friend.avatar.url(:medium) %><br />
<b>Name:</b>
<%= @friend.name %>
</p>
View
9 db/migrate/20120802191046_add_avatar_to_friends.rb
@@ -0,0 +1,9 @@
+class AddAvatarToFriends < ActiveRecord::Migration
+ def self.up
+ add_attachment :friends, :avatar
+ end
+
+ def self.down
+ remove_attachment :friends, :avatar
+ end
+end
View
10 db/schema.rb
@@ -11,12 +11,16 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120802190047) do
+ActiveRecord::Schema.define(:version => 20120802191046) do
create_table "friends", :force => true do |t|
t.string "name"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.string "avatar_file_name"
+ t.string "avatar_content_type"
+ t.integer "avatar_file_size"
+ t.datetime "avatar_updated_at"
end
end
View
14 spec/acceptance/visitor_adds_new_friend_spec.rb
@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+feature 'visitor adds a new friend' do
+ scenario 'add friend with image' do
+ visit root_path
+ click_link 'New Friend'
+ fill_in 'Name', with: 'Mona Lisa'
+ attach_file 'Avatar', Rails.root.join('spec', 'support', 'mona_lisa.jpg')
+ click_button 'Create Friend'
+
+ page.should have_css('p', text: 'Mona Lisa')
+ page.should have_xpath("//img[contains(@src, 'mona_lisa.jpg')]")
+ end
+end
View
5 spec/models/friend_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Friend do
+ it { should validate_presence_of :name }
+end
View
BIN  spec/support/mona_lisa.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Please sign in to comment.
Something went wrong with that request. Please try again.