Skip to content

Commit

Permalink
user can comment on pictures
Browse files Browse the repository at this point in the history
  • Loading branch information
vickymg committed Mar 6, 2016
1 parent e676c1f commit 44a714d
Show file tree
Hide file tree
Showing 59 changed files with 120 additions and 9 deletions.
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ group :development, :test do
gem 'byebug'
gem 'rspec-rails'
gem 'capybara'
gem 'shoulda'
end

group :development do
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ GEM
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
shoulda (3.5.0)
shoulda-context (~> 1.0, >= 1.0.1)
shoulda-matchers (>= 1.4.1, < 3.0)
shoulda-context (1.2.1)
shoulda-matchers (2.8.0)
activesupport (>= 3.0.0)
spring (1.6.4)
sprockets (3.5.2)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -195,6 +201,7 @@ DEPENDENCIES
rspec-rails
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
shoulda
spring
turbolinks
uglifier (>= 1.3.0)
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/comments.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/comments.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the comments controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
17 changes: 17 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class CommentsController < ApplicationController

def new
@photo = Photo.find(params[:photo_id])
@comment = Comment.new
end

def create
@photo = Photo.find(params[:photo_id])
@photo.comments.create(comment_params)
redirect_to photos_path
end

def comment_params
params.require(:comment).permit(:comment)
end
end
3 changes: 3 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Comment < ActiveRecord::Base
belongs_to :photo
end
3 changes: 3 additions & 0 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class Photo < ActiveRecord::Base

has_many :comments, dependent: :destroy

has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
4 changes: 4 additions & 0 deletions app/views/comments/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= form_for [@photo, @comment] do |f|
= f.label :comment
= f.text_area :comment
= f.submit 'Leave comment'
11 changes: 9 additions & 2 deletions app/views/photos/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
%meta(charset="UTF-8")
%h2 Instagram
- if @photos.any?
- @photos.each do | photo |
= image_tag photo.image.url(:thumb)
= image_tag photo.image.url(:medium)
= link_to "Comment", new_photo_comment_path(photo)
- if photo.comments.any?
%ul
- photo.comments.each do |comment|
%li
= comment.comment
- else
No photos yet!

%a(href='/photos/new') Add a photo
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Rails.application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
resources :photos
resources :photos, shallow: true do
resources :comments
end
# You can have the root of your site routed with "root"
# root 'welcome#index'
root to: "photos#index"
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20160306110145_create_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.text :comment

t.timestamps null: false
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20160306110738_add_photo_id_to_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPhotoIdToComments < ActiveRecord::Migration
def change
add_reference :comments, :photo, index: true, foreign_key: true
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160305213846) do
ActiveRecord::Schema.define(version: 20160306110738) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "comments", force: :cascade do |t|
t.text "comment"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "photo_id"
end

add_index "comments", ["photo_id"], name: "index_comments_on_photo_id", using: :btree

create_table "photos", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
Expand All @@ -25,4 +34,5 @@
t.datetime "image_updated_at"
end

add_foreign_key "comments", "photos"
end
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed spec/.DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions spec/controllers/comments_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe CommentsController, type: :controller do

end
19 changes: 19 additions & 0 deletions spec/features/comments_feature_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'rails_helper'
require_relative 'web_spec_helper'


feature 'comments' do
before { add_photo }

scenario 'allows users to leave a comment' do
visit '/photos'
click_link 'Comment'
fill_in "Comment", with: "Great picture"
click_button 'Leave comment'

expect(current_path).to eq '/photos'
expect(page).to have_content('Great picture')
end


end
6 changes: 6 additions & 0 deletions spec/features/web_spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def add_photo
visit '/photos'
click_link 'Add a photo'
attach_file 'Image', Rails.root.join('spec','features','img.jpg')
click_button 'Create Photo'
end
5 changes: 5 additions & 0 deletions spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Comment, type: :model do
it { should belong_to :photo }
end
12 changes: 7 additions & 5 deletions spec/models/photo_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# require 'rails_helper'
#
# RSpec.describe Photo, type: :model do
# pending "add some examples to (or delete) #{__FILE__}"
# end
require 'rails_helper'

RSpec.describe Photo, type: :model do

it { is_expected.to have_many(:comments).dependent(:destroy) }

end

0 comments on commit 44a714d

Please sign in to comment.