From cb479ecb32349ea4a5d9fd4b2f0aab202d7dfa4a Mon Sep 17 00:00:00 2001 From: Webdev Date: Wed, 6 Apr 2016 19:31:13 -0700 Subject: [PATCH 01/78] Added home controller --- config/routes.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index 3f66539..d6ced20 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ Rails.application.routes.draw do + + get "/home" => "home#home" + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From 89a0d9d7209582e1d37fc8c2413684751c989e3e Mon Sep 17 00:00:00 2001 From: Webdev Date: Wed, 6 Apr 2016 22:48:08 -0700 Subject: [PATCH 02/78] Added about page --- config/routes.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index d6ced20..aaae8cf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,8 @@ get "/home" => "home#home" + get "/about" => "home#about" + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From 6995e995bd9144b0de8a17f0e0202d97ab5bd688 Mon Sep 17 00:00:00 2001 From: Webdev Date: Wed, 6 Apr 2016 22:49:44 -0700 Subject: [PATCH 03/78] added some untracked files back in --- app/assets/javascripts/home.coffee | 3 +++ app/assets/stylesheets/home.scss | 3 +++ app/controllers/home_controller.rb | 11 +++++++++++ app/helpers/home_helper.rb | 2 ++ app/views/home/about.html.erb | 4 ++++ app/views/home/home.html.erb | 1 + 6 files changed, 24 insertions(+) create mode 100644 app/assets/javascripts/home.coffee create mode 100644 app/assets/stylesheets/home.scss create mode 100644 app/controllers/home_controller.rb create mode 100644 app/helpers/home_helper.rb create mode 100644 app/views/home/about.html.erb create mode 100644 app/views/home/home.html.erb diff --git a/app/assets/javascripts/home.coffee b/app/assets/javascripts/home.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/home.coffee @@ -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/ diff --git a/app/assets/stylesheets/home.scss b/app/assets/stylesheets/home.scss new file mode 100644 index 0000000..f0ddc68 --- /dev/null +++ b/app/assets/stylesheets/home.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the home controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb new file mode 100644 index 0000000..06dbcc3 --- /dev/null +++ b/app/controllers/home_controller.rb @@ -0,0 +1,11 @@ +# Home and static pages go in HomeController +class HomeController < ApplicationController + def home + + end + + def about + + end + +end diff --git a/app/helpers/home_helper.rb b/app/helpers/home_helper.rb new file mode 100644 index 0000000..23de56a --- /dev/null +++ b/app/helpers/home_helper.rb @@ -0,0 +1,2 @@ +module HomeHelper +end diff --git a/app/views/home/about.html.erb b/app/views/home/about.html.erb new file mode 100644 index 0000000..e685f80 --- /dev/null +++ b/app/views/home/about.html.erb @@ -0,0 +1,4 @@ +

About Page

+ + +

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi at purus ut velit placerat egestas ut a nibh. Pellentesque pulvinar, justo quis consequat vestibulum, sapien nisi bibendum est, et molestie leo tellus vitae erat. Maecenas sed bibendum enim. Cras in libero quis eros sollicitudin commodo ac et leo. Suspendisse venenatis metus a vestibulum gravida. Pellentesque fringilla augue vel eros fringilla, eu porta nulla lobortis. Sed porta ligula ut lacus accumsan, nec facilisis neque egestas.

diff --git a/app/views/home/home.html.erb b/app/views/home/home.html.erb new file mode 100644 index 0000000..fe1d011 --- /dev/null +++ b/app/views/home/home.html.erb @@ -0,0 +1 @@ +

Home Page

From de96dbb13f9995c384e2c8fe910121fd6a4d0bf9 Mon Sep 17 00:00:00 2001 From: Webdev Date: Wed, 6 Apr 2016 22:59:38 -0700 Subject: [PATCH 04/78] Added menu links --- app/views/layouts/application.html.erb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index d0ba841..732420e 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -7,7 +7,10 @@ <%= csrf_meta_tags %> - + <%= yield %> From a5fbabb983322d0c8f9aefd8fca883369f0865c1 Mon Sep 17 00:00:00 2001 From: Webdev Date: Wed, 6 Apr 2016 23:02:41 -0700 Subject: [PATCH 05/78] Added default route --- config/routes.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index aaae8cf..299ce18 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,8 @@ get "/about" => "home#about" + root 'home#home' + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From 8d810256a2956974161b93bd77fd4bff3eba0cb7 Mon Sep 17 00:00:00 2001 From: Webdev Date: Wed, 6 Apr 2016 23:02:41 -0700 Subject: [PATCH 06/78] Added default route --- config/routes.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index aaae8cf..299ce18 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,8 @@ get "/about" => "home#about" + root 'home#home' + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From 6ca1725a9f82c17f0fb659ffdbfdc41828c70a65 Mon Sep 17 00:00:00 2001 From: Webdev Date: Sun, 10 Apr 2016 22:14:43 -0700 Subject: [PATCH 07/78] Made sure files in ./public and some personal notes stayed out of git. --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 5b61ab0..6fc8871 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ /log/* !/log/.keep /tmp +models_setup.txt +models_setup.txt~ +public/img/ From 86269cac3b5eed45791679632bf112a6a803ed8c Mon Sep 17 00:00:00 2001 From: Webdev Date: Sun, 10 Apr 2016 22:15:54 -0700 Subject: [PATCH 08/78] Added A model for Blog posts (Post), blog post comments (Comment), a cateory model for tagging (Category), and a model for email submission (Contact). --- app/models/category.rb | 2 ++ app/models/comment.rb | 2 ++ app/models/contact.rb | 2 ++ app/models/post.rb | 3 +++ db/migrate/20160411050204_create_posts.rb | 10 ++++++++++ db/migrate/20160411050207_create_comments.rb | 9 +++++++++ db/migrate/20160411050217_create_categories.rb | 9 +++++++++ db/migrate/20160411050227_create_contacts.rb | 12 ++++++++++++ 8 files changed, 49 insertions(+) create mode 100644 app/models/category.rb create mode 100644 app/models/comment.rb create mode 100644 app/models/contact.rb create mode 100644 app/models/post.rb create mode 100644 db/migrate/20160411050204_create_posts.rb create mode 100644 db/migrate/20160411050207_create_comments.rb create mode 100644 db/migrate/20160411050217_create_categories.rb create mode 100644 db/migrate/20160411050227_create_contacts.rb diff --git a/app/models/category.rb b/app/models/category.rb new file mode 100644 index 0000000..910a009 --- /dev/null +++ b/app/models/category.rb @@ -0,0 +1,2 @@ +class Category < ActiveRecord::Base +end diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..45b2d38 --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,2 @@ +class Comment < ActiveRecord::Base +end diff --git a/app/models/contact.rb b/app/models/contact.rb new file mode 100644 index 0000000..a5ee858 --- /dev/null +++ b/app/models/contact.rb @@ -0,0 +1,2 @@ +class Contact < ActiveRecord::Base +end diff --git a/app/models/post.rb b/app/models/post.rb new file mode 100644 index 0000000..e911825 --- /dev/null +++ b/app/models/post.rb @@ -0,0 +1,3 @@ +class Post < ActiveRecord::Base + +end diff --git a/db/migrate/20160411050204_create_posts.rb b/db/migrate/20160411050204_create_posts.rb new file mode 100644 index 0000000..691c505 --- /dev/null +++ b/db/migrate/20160411050204_create_posts.rb @@ -0,0 +1,10 @@ +class CreatePosts < ActiveRecord::Migration + def change + create_table :posts do |t| + t.string :title + t.text :body + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20160411050207_create_comments.rb b/db/migrate/20160411050207_create_comments.rb new file mode 100644 index 0000000..bb50905 --- /dev/null +++ b/db/migrate/20160411050207_create_comments.rb @@ -0,0 +1,9 @@ +class CreateComments < ActiveRecord::Migration + def change + create_table :comments do |t| + t.text :body + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20160411050217_create_categories.rb b/db/migrate/20160411050217_create_categories.rb new file mode 100644 index 0000000..38b9e13 --- /dev/null +++ b/db/migrate/20160411050217_create_categories.rb @@ -0,0 +1,9 @@ +class CreateCategories < ActiveRecord::Migration + def change + create_table :categories do |t| + t.string :title + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20160411050227_create_contacts.rb b/db/migrate/20160411050227_create_contacts.rb new file mode 100644 index 0000000..5298b22 --- /dev/null +++ b/db/migrate/20160411050227_create_contacts.rb @@ -0,0 +1,12 @@ +class CreateContacts < ActiveRecord::Migration + def change + create_table :contacts do |t| + t.string :email + t.string :name + t.string :subject + t.text :message + + t.timestamps null: false + end + end +end From 7b4930800e3dcbb1356cea1c2629fcf3ab5c85be Mon Sep 17 00:00:00 2001 From: Webdev Date: Sun, 10 Apr 2016 22:43:05 -0700 Subject: [PATCH 09/78] Added validation for Post. Post now requres the title to be required and unique --- app/models/post.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/post.rb b/app/models/post.rb index e911825..5bf1e66 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,3 +1,4 @@ class Post < ActiveRecord::Base - + # title should be required & unique + validates :title, presence: true, uniqueness: true end From 8184234772ac28f5df25094c693bfb3fc91e03f2 Mon Sep 17 00:00:00 2001 From: Webdev Date: Mon, 11 Apr 2016 18:02:39 -0700 Subject: [PATCH 10/78] Attach comments to posts --- app/models/comment.rb | 1 + app/models/contact.rb | 1 + app/models/post.rb | 1 + ...20160411055429_attach_comments_to_posts.rb | 5 ++ db/schema.rb | 48 +++++++++++++++++++ 5 files changed, 56 insertions(+) create mode 100644 db/migrate/20160411055429_attach_comments_to_posts.rb create mode 100644 db/schema.rb diff --git a/app/models/comment.rb b/app/models/comment.rb index 45b2d38..d7a9351 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,2 +1,3 @@ class Comment < ActiveRecord::Base + belongs_to :customer end diff --git a/app/models/contact.rb b/app/models/contact.rb index a5ee858..26a68e5 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -1,2 +1,3 @@ class Contact < ActiveRecord::Base + end diff --git a/app/models/post.rb b/app/models/post.rb index 5bf1e66..0f67153 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,4 +1,5 @@ class Post < ActiveRecord::Base # title should be required & unique validates :title, presence: true, uniqueness: true + has_many :comments, dependent: :destroy end diff --git a/db/migrate/20160411055429_attach_comments_to_posts.rb b/db/migrate/20160411055429_attach_comments_to_posts.rb new file mode 100644 index 0000000..8651677 --- /dev/null +++ b/db/migrate/20160411055429_attach_comments_to_posts.rb @@ -0,0 +1,5 @@ +class AttachCommentsToPosts < ActiveRecord::Migration + def change + add_reference :comments, :post + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..5e52224 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,48 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20160411055429) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "categories", force: :cascade do |t| + t.string "title" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "comments", force: :cascade do |t| + t.text "body" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "post_id" + end + + create_table "contacts", force: :cascade do |t| + t.string "email" + t.string "name" + t.string "subject" + t.text "message" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "posts", force: :cascade do |t| + t.string "title" + t.text "body" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end From 08b15ccce4ea874812fd87d4973b2cb1cdd46bee Mon Sep 17 00:00:00 2001 From: Webdev Date: Mon, 11 Apr 2016 18:28:33 -0700 Subject: [PATCH 11/78] added controller for blog posts --- app/assets/javascripts/blog_posts.coffee | 3 +++ app/assets/stylesheets/blog_posts.scss | 3 +++ app/controllers/blog_posts_controller.rb | 2 ++ app/helpers/blog_posts_helper.rb | 2 ++ 4 files changed, 10 insertions(+) create mode 100644 app/assets/javascripts/blog_posts.coffee create mode 100644 app/assets/stylesheets/blog_posts.scss create mode 100644 app/controllers/blog_posts_controller.rb create mode 100644 app/helpers/blog_posts_helper.rb diff --git a/app/assets/javascripts/blog_posts.coffee b/app/assets/javascripts/blog_posts.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/blog_posts.coffee @@ -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/ diff --git a/app/assets/stylesheets/blog_posts.scss b/app/assets/stylesheets/blog_posts.scss new file mode 100644 index 0000000..5b7c942 --- /dev/null +++ b/app/assets/stylesheets/blog_posts.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the blog_posts controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/blog_posts_controller.rb b/app/controllers/blog_posts_controller.rb new file mode 100644 index 0000000..133889c --- /dev/null +++ b/app/controllers/blog_posts_controller.rb @@ -0,0 +1,2 @@ +class BlogPostsController < ApplicationController +end diff --git a/app/helpers/blog_posts_helper.rb b/app/helpers/blog_posts_helper.rb new file mode 100644 index 0000000..48c3d5f --- /dev/null +++ b/app/helpers/blog_posts_helper.rb @@ -0,0 +1,2 @@ +module BlogPostsHelper +end From 63a82bcf4bcfda38b178de6a4985422b001bbc67 Mon Sep 17 00:00:00 2001 From: Webdev Date: Mon, 11 Apr 2016 18:30:36 -0700 Subject: [PATCH 12/78] Added routes for blog posts --- config/routes.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index 299ce18..1f4dd18 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,8 @@ get "/about" => "home#about" + resources :blog_posts + root 'home#home' # The priority is based upon order of creation: first created -> highest priority. From 6eb0da929e1da9baa55c3ca30d6c24a443a0c36d Mon Sep 17 00:00:00 2001 From: Webdev Date: Mon, 11 Apr 2016 18:44:43 -0700 Subject: [PATCH 13/78] Added faker gem and used to create a seed for blog posts --- Gemfile | 2 +- Gemfile.lock | 3 +++ db/seeds.rb | 5 +++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index b38d1b2..ac39f10 100644 --- a/Gemfile +++ b/Gemfile @@ -35,6 +35,7 @@ gem 'sdoc', '~> 0.4.0', group: :doc group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' + gem 'faker' end group :development do @@ -44,4 +45,3 @@ group :development do # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' end - diff --git a/Gemfile.lock b/Gemfile.lock index b0631ce..c922b44 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -52,6 +52,8 @@ GEM debug_inspector (0.0.2) erubis (2.7.0) execjs (2.6.0) + faker (1.6.3) + i18n (~> 0.5) globalid (0.3.6) activesupport (>= 4.1.0) i18n (0.7.0) @@ -145,6 +147,7 @@ PLATFORMS DEPENDENCIES byebug coffee-rails (~> 4.1.0) + faker jbuilder (~> 2.0) jquery-rails pg (~> 0.15) diff --git a/db/seeds.rb b/db/seeds.rb index 4edb1e8..ec21594 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,8 @@ # # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) + +100.times do + Post.create title: Faker::Company.bs, + body: Faker::Lorem.paragraph +end From a7f7bc9fcd5c792656db11607c59982f0ff1c386 Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 11:00:43 -0700 Subject: [PATCH 14/78] Added blog post index with view and controller method --- app/controllers/blog_posts_controller.rb | 3 +++ app/views/blog_posts/index.html.erb | 10 ++++++++++ 2 files changed, 13 insertions(+) create mode 100644 app/views/blog_posts/index.html.erb diff --git a/app/controllers/blog_posts_controller.rb b/app/controllers/blog_posts_controller.rb index 133889c..c204929 100644 --- a/app/controllers/blog_posts_controller.rb +++ b/app/controllers/blog_posts_controller.rb @@ -1,2 +1,5 @@ class BlogPostsController < ApplicationController + def index + @posts = Post.all + end end diff --git a/app/views/blog_posts/index.html.erb b/app/views/blog_posts/index.html.erb new file mode 100644 index 0000000..1d31066 --- /dev/null +++ b/app/views/blog_posts/index.html.erb @@ -0,0 +1,10 @@ +

Blog Posts

+ +<% @posts.each do |p| %> +
+ <%= link_to p.title, blog_post_path(p) %> +
+
+ <%= p.body %> +
+<% end %> From 9e21189b8de4f49926f7c9d7219c1b2e55895dcc Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 17:06:53 -0700 Subject: [PATCH 15/78] renamed blog posts controller to just posts --- .../javascripts/{blog_posts.coffee => posts.coffee} | 0 app/assets/stylesheets/{blog_posts.scss => posts.scss} | 2 +- app/controllers/blog_posts_controller.rb | 5 ----- app/controllers/posts_controller.rb | 9 +++++++++ app/helpers/blog_posts_helper.rb | 2 -- app/helpers/posts_helper.rb | 2 ++ app/views/{blog_posts => posts}/index.html.erb | 2 +- config/routes.rb | 2 +- 8 files changed, 14 insertions(+), 10 deletions(-) rename app/assets/javascripts/{blog_posts.coffee => posts.coffee} (100%) rename app/assets/stylesheets/{blog_posts.scss => posts.scss} (62%) delete mode 100644 app/controllers/blog_posts_controller.rb create mode 100644 app/controllers/posts_controller.rb delete mode 100644 app/helpers/blog_posts_helper.rb create mode 100644 app/helpers/posts_helper.rb rename app/views/{blog_posts => posts}/index.html.erb (70%) diff --git a/app/assets/javascripts/blog_posts.coffee b/app/assets/javascripts/posts.coffee similarity index 100% rename from app/assets/javascripts/blog_posts.coffee rename to app/assets/javascripts/posts.coffee diff --git a/app/assets/stylesheets/blog_posts.scss b/app/assets/stylesheets/posts.scss similarity index 62% rename from app/assets/stylesheets/blog_posts.scss rename to app/assets/stylesheets/posts.scss index 5b7c942..1a7e153 100644 --- a/app/assets/stylesheets/blog_posts.scss +++ b/app/assets/stylesheets/posts.scss @@ -1,3 +1,3 @@ -// Place all the styles related to the blog_posts controller here. +// Place all the styles related to the posts controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/blog_posts_controller.rb b/app/controllers/blog_posts_controller.rb deleted file mode 100644 index c204929..0000000 --- a/app/controllers/blog_posts_controller.rb +++ /dev/null @@ -1,5 +0,0 @@ -class BlogPostsController < ApplicationController - def index - @posts = Post.all - end -end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb new file mode 100644 index 0000000..c0658b3 --- /dev/null +++ b/app/controllers/posts_controller.rb @@ -0,0 +1,9 @@ +class PostsController < ApplicationController + def index + @posts = Post.all + end + + def new + @post = Post.new + end +end diff --git a/app/helpers/blog_posts_helper.rb b/app/helpers/blog_posts_helper.rb deleted file mode 100644 index 48c3d5f..0000000 --- a/app/helpers/blog_posts_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module BlogPostsHelper -end diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb new file mode 100644 index 0000000..a7b8cec --- /dev/null +++ b/app/helpers/posts_helper.rb @@ -0,0 +1,2 @@ +module PostsHelper +end diff --git a/app/views/blog_posts/index.html.erb b/app/views/posts/index.html.erb similarity index 70% rename from app/views/blog_posts/index.html.erb rename to app/views/posts/index.html.erb index 1d31066..f48db64 100644 --- a/app/views/blog_posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -2,7 +2,7 @@ <% @posts.each do |p| %>
- <%= link_to p.title, blog_post_path(p) %> + <%= link_to p.title, post_path(p) %>
<%= p.body %> diff --git a/config/routes.rb b/config/routes.rb index 1f4dd18..3385744 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,7 @@ get "/about" => "home#about" - resources :blog_posts + resources :posts root 'home#home' From 2e627aeea15fc36371624ca5ae455499d7d0f3d4 Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 18:23:43 -0700 Subject: [PATCH 16/78] added in slim templating gem --- Gemfile | 2 ++ Gemfile.lock | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/Gemfile b/Gemfile index ac39f10..94108a9 100644 --- a/Gemfile +++ b/Gemfile @@ -32,6 +32,8 @@ gem 'sdoc', '~> 0.4.0', group: :doc # Use Capistrano for deployment # gem 'capistrano-rails', group: :development + gem 'slim' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' diff --git a/Gemfile.lock b/Gemfile.lock index c922b44..4f9f5ce 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -118,6 +118,9 @@ GEM sdoc (0.4.1) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) + slim (3.0.6) + temple (~> 0.7.3) + tilt (>= 1.3.3, < 2.1) spring (1.6.4) sprockets (3.6.0) concurrent-ruby (~> 1.0) @@ -126,6 +129,7 @@ GEM actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) + temple (0.7.6) thor (0.19.1) thread_safe (0.3.5) tilt (2.0.2) @@ -154,6 +158,7 @@ DEPENDENCIES rails (= 4.2.6) sass-rails (~> 5.0) sdoc (~> 0.4.0) + slim spring turbolinks uglifier (>= 1.3.0) From 49c591b164ca4d88c84f6d2f897fbcfc38fe0a9c Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 18:35:05 -0700 Subject: [PATCH 17/78] added in form for creating or editing blog posts and the relevent views --- app/controllers/posts_controller.rb | 34 ++++++++++++++++++++++++++++ app/views/posts/_blog_form.html.slim | 9 ++++++++ app/views/posts/edit.html.slim | 3 +++ app/views/posts/new.html.slim | 3 +++ 4 files changed, 49 insertions(+) create mode 100644 app/views/posts/_blog_form.html.slim create mode 100644 app/views/posts/edit.html.slim create mode 100644 app/views/posts/new.html.slim diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index c0658b3..73af170 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,4 +1,8 @@ class PostsController < ApplicationController + + before_action(:find_post, {only: [:show, :edit, :update, :destroy]}) + + def index @posts = Post.all end @@ -6,4 +10,34 @@ def index def new @post = Post.new end + + def create + @post = Post.create{post_params} + if @post.save + flash[:notice] = "Post Created" + redirect_to post_path(post_params) + else + flash[:alert] = "Error! Post not created" + render :new + end + end + + def edit + end + + def update + if @post.update post_params + redirect_to post_path(@post), notice: "Question updated!" + else + render :edit + end + end + + def find_post + @post = Post.find params[:id] + end + + def post_params + params.require(:post).permit([:title, :body]) + end end diff --git a/app/views/posts/_blog_form.html.slim b/app/views/posts/_blog_form.html.slim new file mode 100644 index 0000000..ea3b14f --- /dev/null +++ b/app/views/posts/_blog_form.html.slim @@ -0,0 +1,9 @@ += form_for @post do |f| + div + = f.label :title + = f.text_field :title + div + = f.label :body + = f.text_area :body + div + = f.submit diff --git a/app/views/posts/edit.html.slim b/app/views/posts/edit.html.slim new file mode 100644 index 0000000..9793752 --- /dev/null +++ b/app/views/posts/edit.html.slim @@ -0,0 +1,3 @@ +h2 Edit Blog Post + += render "blog_form" diff --git a/app/views/posts/new.html.slim b/app/views/posts/new.html.slim new file mode 100644 index 0000000..8fa1e63 --- /dev/null +++ b/app/views/posts/new.html.slim @@ -0,0 +1,3 @@ +h2 New Blog Post + += render "blog_form" From 7e93059d3ea3a9156f1e095e2ed0796533ff6f84 Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 19:10:45 -0700 Subject: [PATCH 18/78] added a show view for an indevidual blog post --- app/controllers/posts_controller.rb | 3 +++ app/views/posts/show.html.slim | 5 +++++ 2 files changed, 8 insertions(+) create mode 100644 app/views/posts/show.html.slim diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 73af170..666e56b 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -33,6 +33,9 @@ def update end end + def show + end + def find_post @post = Post.find params[:id] end diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim new file mode 100644 index 0000000..3435e05 --- /dev/null +++ b/app/views/posts/show.html.slim @@ -0,0 +1,5 @@ +div + h2 + = @post.title +div + = @post.body From eeaaf378b7b5c144d82cbb02b9abc8cd5a1250a2 Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 19:11:48 -0700 Subject: [PATCH 19/78] changed helper functions (find_post, post_params) to private --- app/controllers/posts_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 666e56b..aa4eb61 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -33,9 +33,11 @@ def update end end - def show + def show end + private + def find_post @post = Post.find params[:id] end From 226401c8a70374e6f5c00d28cbf8109f43a7d5ff Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 19:25:07 -0700 Subject: [PATCH 20/78] added delete blog post functionality, with UI in posts show view --- app/controllers/posts_controller.rb | 7 ++++++- app/views/posts/show.html.slim | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index aa4eb61..70c3fe4 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -36,8 +36,13 @@ def update def show end + def destroy + @post.destroy + redirect_to posts_path, notice: "The blog post #{@post.title} was deleted." + end + private - + def find_post @post = Post.find params[:id] end diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index 3435e05..c5a9f6c 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -3,3 +3,6 @@ div = @post.title div = @post.body + +div + = link_to "Delete", post_path(@post), method: :delete, data: {confirm: "Are you sure?"} From 458000603e6d82ae7935833ab8cc979d393ce80b Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 19:28:12 -0700 Subject: [PATCH 21/78] added edit UI to blog post#show --- app/views/posts/show.html.slim | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index c5a9f6c..65fbc0d 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -4,5 +4,7 @@ div div = @post.body +div + = link_to "Edit", edit_post_path(@post) div = link_to "Delete", post_path(@post), method: :delete, data: {confirm: "Are you sure?"} From fd51ac2fd4fe3946e89440242f1bb9e252b670d4 Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 19:31:03 -0700 Subject: [PATCH 22/78] Minor UI changes --- app/views/posts/edit.html.slim | 2 +- app/views/posts/show.html.slim | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/posts/edit.html.slim b/app/views/posts/edit.html.slim index 9793752..50c2da8 100644 --- a/app/views/posts/edit.html.slim +++ b/app/views/posts/edit.html.slim @@ -1,3 +1,3 @@ -h2 Edit Blog Post +h2 Editing Blog Post = render "blog_form" diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index 65fbc0d..c3e95ce 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -7,4 +7,4 @@ div div = link_to "Edit", edit_post_path(@post) div - = link_to "Delete", post_path(@post), method: :delete, data: {confirm: "Are you sure?"} + = link_to "Delete", post_path(@post), method: :delete, data: {confirm: "Are you sure you want to delete this post?"} From 84a0430eb7c380b1c2c0a9994e8112b3133a776e Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 20:00:36 -0700 Subject: [PATCH 23/78] added Blog Posts index link to menu bar --- app/views/layouts/application.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 732420e..3a419ff 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,6 +10,7 @@ <%= yield %> From 49a7f5aaaf92246dfa61dd4fe4c3e2c3d163239b Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 22:03:29 -0700 Subject: [PATCH 24/78] added coments controller --- app/assets/javascripts/comments.coffee | 3 +++ app/assets/stylesheets/comments.scss | 3 +++ app/controllers/comments_controller.rb | 2 ++ app/helpers/comments_helper.rb | 2 ++ app/views/comments/_comments_form.html.slim | 9 +++++++++ app/views/comments/new.html.slim | 1 + 6 files changed, 20 insertions(+) create mode 100644 app/assets/javascripts/comments.coffee create mode 100644 app/assets/stylesheets/comments.scss create mode 100644 app/controllers/comments_controller.rb create mode 100644 app/helpers/comments_helper.rb create mode 100644 app/views/comments/_comments_form.html.slim create mode 100644 app/views/comments/new.html.slim diff --git a/app/assets/javascripts/comments.coffee b/app/assets/javascripts/comments.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/comments.coffee @@ -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/ diff --git a/app/assets/stylesheets/comments.scss b/app/assets/stylesheets/comments.scss new file mode 100644 index 0000000..3722c12 --- /dev/null +++ b/app/assets/stylesheets/comments.scss @@ -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/ diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..7669955 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,2 @@ +class CommentsController < ApplicationController +end diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 0000000..0ec9ca5 --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,2 @@ +module CommentsHelper +end diff --git a/app/views/comments/_comments_form.html.slim b/app/views/comments/_comments_form.html.slim new file mode 100644 index 0000000..ea3b14f --- /dev/null +++ b/app/views/comments/_comments_form.html.slim @@ -0,0 +1,9 @@ += form_for @post do |f| + div + = f.label :title + = f.text_field :title + div + = f.label :body + = f.text_area :body + div + = f.submit diff --git a/app/views/comments/new.html.slim b/app/views/comments/new.html.slim new file mode 100644 index 0000000..8fa69db --- /dev/null +++ b/app/views/comments/new.html.slim @@ -0,0 +1 @@ +h2 New Comment From 8f08b318446d0401ae7dba02c6c25314025170dc Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 22:05:46 -0700 Subject: [PATCH 25/78] added comments route --- config/routes.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/routes.rb b/config/routes.rb index 3385744..af81d03 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ get "/about" => "home#about" resources :posts + resources :comments root 'home#home' From 8d623ee0bcdef42390b809e91b87033ac7ecabd5 Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 22:17:38 -0700 Subject: [PATCH 26/78] Filled in comments controller (full sweep) --- app/controllers/comments_controller.rb | 52 ++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 7669955..19670d0 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,2 +1,54 @@ class CommentsController < ApplicationController + + before_action(:find_comment, {only: [:show, :edit, :update, :destroy]}) + + def index + @comments = comment.all + end + + def new + @comment = comment.new + end + + def create + @comment = comment.create{comment_params} + if @comment.save + flash[:notice] = "Comment Created" + redirect_to comment_path(comment_params) + else + flash[:alert] = "Error! Comment not created" + render :new + end + end + + def edit + end + + def update + if @comment.update comment_params + redirect_to comment_path(@comment), notice: "Comment updated!" + else + render :edit + end + end + + def show + end + + def destroy + @comment.destroy + redirect_to comments_path, notice: "Your comment was deleted." + end + + + private + + def find_comment + @comment = comment.find params[:id] + end + + def comment_params + params.require(:comment).permit([:title, :body]) + end + end From 01efa8760a1f0603620ef4f84905fa2f76170809 Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 22:24:17 -0700 Subject: [PATCH 27/78] Added comment edit and new forms --- app/controllers/comments_controller.rb | 8 ++++---- app/views/comments/_comment_form.html.slim | 6 ++++++ app/views/comments/_comments_form.html.slim | 9 --------- app/views/comments/new.html.slim | 2 ++ app/views/posts/new.html.slim | 4 ++-- 5 files changed, 14 insertions(+), 15 deletions(-) create mode 100644 app/views/comments/_comment_form.html.slim delete mode 100644 app/views/comments/_comments_form.html.slim diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 19670d0..405f0df 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -3,15 +3,15 @@ class CommentsController < ApplicationController before_action(:find_comment, {only: [:show, :edit, :update, :destroy]}) def index - @comments = comment.all + @comments = Comment.all end def new - @comment = comment.new + @comment = Comment.new end def create - @comment = comment.create{comment_params} + @comment = Comment.create{comment_params} if @comment.save flash[:notice] = "Comment Created" redirect_to comment_path(comment_params) @@ -44,7 +44,7 @@ def destroy private def find_comment - @comment = comment.find params[:id] + @comment = Comment.find params[:id] end def comment_params diff --git a/app/views/comments/_comment_form.html.slim b/app/views/comments/_comment_form.html.slim new file mode 100644 index 0000000..7a79413 --- /dev/null +++ b/app/views/comments/_comment_form.html.slim @@ -0,0 +1,6 @@ += form_for @comment do |f| + div + = f.label :body + = f.text_area :body + div + = f.submit diff --git a/app/views/comments/_comments_form.html.slim b/app/views/comments/_comments_form.html.slim deleted file mode 100644 index ea3b14f..0000000 --- a/app/views/comments/_comments_form.html.slim +++ /dev/null @@ -1,9 +0,0 @@ -= form_for @post do |f| - div - = f.label :title - = f.text_field :title - div - = f.label :body - = f.text_area :body - div - = f.submit diff --git a/app/views/comments/new.html.slim b/app/views/comments/new.html.slim index 8fa69db..a776154 100644 --- a/app/views/comments/new.html.slim +++ b/app/views/comments/new.html.slim @@ -1 +1,3 @@ h2 New Comment + += render "comment_form" diff --git a/app/views/posts/new.html.slim b/app/views/posts/new.html.slim index 8fa1e63..a776154 100644 --- a/app/views/posts/new.html.slim +++ b/app/views/posts/new.html.slim @@ -1,3 +1,3 @@ -h2 New Blog Post +h2 New Comment -= render "blog_form" += render "comment_form" From 8bb1bb1d7479f0f82f3eae8ea958ed4dcbfc46be Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 22:51:56 -0700 Subject: [PATCH 28/78] fix over lax input field permit --- app/controllers/comments_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 405f0df..c550ed5 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -48,7 +48,7 @@ def find_comment end def comment_params - params.require(:comment).permit([:title, :body]) + params.require(:comment).permit([:body]) end end From 60e2adc485bdaaf95fcb604dec2b51b75c7a33af Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 12 Apr 2016 22:52:27 -0700 Subject: [PATCH 29/78] added edid, index and show views for comments. index probably won't be used later --- app/views/comments/edit.html.slim | 3 +++ app/views/comments/index.html.slim | 5 +++++ app/views/comments/show.html.slim | 1 + 3 files changed, 9 insertions(+) create mode 100644 app/views/comments/edit.html.slim create mode 100644 app/views/comments/index.html.slim create mode 100644 app/views/comments/show.html.slim diff --git a/app/views/comments/edit.html.slim b/app/views/comments/edit.html.slim new file mode 100644 index 0000000..79977c3 --- /dev/null +++ b/app/views/comments/edit.html.slim @@ -0,0 +1,3 @@ +h2 Edit Comment + += render "comment_form" diff --git a/app/views/comments/index.html.slim b/app/views/comments/index.html.slim new file mode 100644 index 0000000..af48bec --- /dev/null +++ b/app/views/comments/index.html.slim @@ -0,0 +1,5 @@ +h2 All Comments List + +- @comments.each do |c| + div + = c.body diff --git a/app/views/comments/show.html.slim b/app/views/comments/show.html.slim new file mode 100644 index 0000000..268dd5e --- /dev/null +++ b/app/views/comments/show.html.slim @@ -0,0 +1 @@ += @comment.body From 76083baa11cf6580126c2c5b36d7582045cc768f Mon Sep 17 00:00:00 2001 From: Webdev Date: Wed, 13 Apr 2016 22:56:19 -0700 Subject: [PATCH 30/78] fixed new blog post and new comments. mostly in the respective controllers --- app/controllers/comments_controller.rb | 6 +++--- app/controllers/posts_controller.rb | 2 +- app/views/comments/new.html.slim | 2 +- app/views/posts/_blog_form.html.slim | 9 --------- app/views/posts/new.html.slim | 4 ++-- 5 files changed, 7 insertions(+), 16 deletions(-) delete mode 100644 app/views/posts/_blog_form.html.slim diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index c550ed5..f4c7145 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -11,10 +11,10 @@ def new end def create - @comment = Comment.create{comment_params} + @comment = Comment.new(comment_params) if @comment.save flash[:notice] = "Comment Created" - redirect_to comment_path(comment_params) + redirect_to comment_path(@comment) else flash[:alert] = "Error! Comment not created" render :new @@ -48,7 +48,7 @@ def find_comment end def comment_params - params.require(:comment).permit([:body]) + params.require(:comment).permit(:body) end end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 70c3fe4..472a6cc 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -12,7 +12,7 @@ def new end def create - @post = Post.create{post_params} + @post = Post.new{post_params} if @post.save flash[:notice] = "Post Created" redirect_to post_path(post_params) diff --git a/app/views/comments/new.html.slim b/app/views/comments/new.html.slim index a776154..9557503 100644 --- a/app/views/comments/new.html.slim +++ b/app/views/comments/new.html.slim @@ -1,3 +1,3 @@ h2 New Comment -= render "comment_form" += render "comments/comment_form" diff --git a/app/views/posts/_blog_form.html.slim b/app/views/posts/_blog_form.html.slim deleted file mode 100644 index ea3b14f..0000000 --- a/app/views/posts/_blog_form.html.slim +++ /dev/null @@ -1,9 +0,0 @@ -= form_for @post do |f| - div - = f.label :title - = f.text_field :title - div - = f.label :body - = f.text_area :body - div - = f.submit diff --git a/app/views/posts/new.html.slim b/app/views/posts/new.html.slim index a776154..1d07049 100644 --- a/app/views/posts/new.html.slim +++ b/app/views/posts/new.html.slim @@ -1,3 +1,3 @@ -h2 New Comment +h2 New Blog Post -= render "comment_form" += render "posts/post_form" From 3b4f610b7de9ff4ab2213761630e409296cb76e5 Mon Sep 17 00:00:00 2001 From: Webdev Date: Wed, 13 Apr 2016 23:09:00 -0700 Subject: [PATCH 31/78] fixed new blog post controller action and added flash messages in --- app/controllers/posts_controller.rb | 6 +++--- app/views/comments/_comment_form.html.slim | 2 ++ app/views/posts/_post_form.html.slim | 10 ++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 app/views/posts/_post_form.html.slim diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 472a6cc..44f6eb3 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,6 +1,6 @@ class PostsController < ApplicationController - before_action(:find_post, {only: [:show, :edit, :update, :destroy]}) + before_action(:find_post, {only: [:show, :edit, :update, :destroy]}) def index @@ -12,10 +12,10 @@ def new end def create - @post = Post.new{post_params} + @post = Post.new(post_params) if @post.save flash[:notice] = "Post Created" - redirect_to post_path(post_params) + redirect_to post_path(@post) else flash[:alert] = "Error! Post not created" render :new diff --git a/app/views/comments/_comment_form.html.slim b/app/views/comments/_comment_form.html.slim index 7a79413..4f6b7b7 100644 --- a/app/views/comments/_comment_form.html.slim +++ b/app/views/comments/_comment_form.html.slim @@ -1,3 +1,5 @@ += notice || alert + = form_for @comment do |f| div = f.label :body diff --git a/app/views/posts/_post_form.html.slim b/app/views/posts/_post_form.html.slim new file mode 100644 index 0000000..4a2c62c --- /dev/null +++ b/app/views/posts/_post_form.html.slim @@ -0,0 +1,10 @@ += notice || alert += form_for @post do |f| + div + = f.label :title + = f.text_field :title + div + = f.label :body + = f.text_area :body + div + = f.submit From 306fe4c3695a45d15e6569498f4c835bf4a61e59 Mon Sep 17 00:00:00 2001 From: Webdev Date: Wed, 13 Apr 2016 23:21:08 -0700 Subject: [PATCH 32/78] .gitignore ed bybug history --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 6fc8871..6867b3b 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ models_setup.txt models_setup.txt~ public/img/ +.byebug_history From 4da107643f5208d786256b9d52f274ba00019f14 Mon Sep 17 00:00:00 2001 From: Webdev Date: Wed, 13 Apr 2016 23:31:55 -0700 Subject: [PATCH 33/78] set up blog project for rspec testing, including posts controllers test file, factories, and faker --- .rspec | 2 + Gemfile | 2 + Gemfile.lock | 25 ++++++ spec/controllers/posts_controller_spec.rb | 66 ++++++++++++++++ spec/factories/posts.rb | 6 ++ spec/rails_helper.rb | 57 ++++++++++++++ spec/spec_helper.rb | 92 +++++++++++++++++++++++ 7 files changed, 250 insertions(+) create mode 100644 .rspec create mode 100644 spec/controllers/posts_controller_spec.rb create mode 100644 spec/factories/posts.rb create mode 100644 spec/rails_helper.rb create mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..83e16f8 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--require spec_helper diff --git a/Gemfile b/Gemfile index 94108a9..ef18c37 100644 --- a/Gemfile +++ b/Gemfile @@ -37,7 +37,9 @@ gem 'sdoc', '~> 0.4.0', group: :doc group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' + gem 'rspec-rails' gem 'faker' + gem 'factory_girl_rails' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 4f9f5ce..a4cae71 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -50,8 +50,14 @@ GEM coffee-script-source (1.10.0) concurrent-ruby (1.0.1) debug_inspector (0.0.2) + diff-lcs (1.2.5) erubis (2.7.0) execjs (2.6.0) + factory_girl (4.7.0) + activesupport (>= 3.0.0) + factory_girl_rails (4.7.0) + factory_girl (~> 4.7.0) + railties (>= 3.0.0) faker (1.6.3) i18n (~> 0.5) globalid (0.3.6) @@ -108,6 +114,23 @@ GEM rake (11.1.2) rdoc (4.2.2) json (~> 1.4) + rspec-core (3.4.4) + rspec-support (~> 3.4.0) + rspec-expectations (3.4.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.4.0) + rspec-mocks (3.4.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.4.0) + rspec-rails (3.4.2) + actionpack (>= 3.0, < 4.3) + activesupport (>= 3.0, < 4.3) + railties (>= 3.0, < 4.3) + rspec-core (~> 3.4.0) + rspec-expectations (~> 3.4.0) + rspec-mocks (~> 3.4.0) + rspec-support (~> 3.4.0) + rspec-support (3.4.1) sass (3.4.22) sass-rails (5.0.4) railties (>= 4.0.0, < 5.0) @@ -151,11 +174,13 @@ PLATFORMS DEPENDENCIES byebug coffee-rails (~> 4.1.0) + factory_girl_rails faker jbuilder (~> 2.0) jquery-rails pg (~> 0.15) rails (= 4.2.6) + rspec-rails sass-rails (~> 5.0) sdoc (~> 0.4.0) slim diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb new file mode 100644 index 0000000..8bcdee7 --- /dev/null +++ b/spec/controllers/posts_controller_spec.rb @@ -0,0 +1,66 @@ +require 'rails_helper' + +RSpec.describe PostsController, type: :controller do + + before {get :new} + + describe "#new" do + it "renders the template" do + expect(response).to render_template(:new) + end + it "assigns a post object" do + expect(assigns(:post)).to be_a_new(Post) + end + end + + describe "#create" do + describe "with valid attributes" do + def valid_request + post :create, post: FactoryGirl.attributes_for(:post) + end + + it "saves a record to the database" do + count_before = Post.count + valid_request + count_after = Post.count + expect(count_after).to eq(count_before + 1) + end + + it "redirects_to the posts show page" do + valid_request + expect(response).to redirect_to post_path(Post.last) + end + + it "sets a flash message" do + valid_request + expect(flash[:notice]).to be + end + end + + describe "with invalid attributes" do + def invalid_request + post :create, post: {hello: "world"} + end + + it "renders the new template" do + invalid_request + expect(response).to render_template(:new) + end + + it "sets an alert message" do + invalid_request + expect(flash[:alert]).to be + end + + it "doesn't save a record to the database" do + count_before = Post.count + invalid_request + count_after = Post.count + expect(count_after).to eq(count_before) + end + + end + + end + +end diff --git a/spec/factories/posts.rb b/spec/factories/posts.rb new file mode 100644 index 0000000..5ab4ff8 --- /dev/null +++ b/spec/factories/posts.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :post do + sequence(:title) { |n| "#{Faker::Commerce.department}"} + body {Faker::Lorem.paragraph} + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..6f1ab14 --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,57 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../../config/environment', __FILE__) +# Prevent database truncation if the environment is production +abort("The Rails environment is running in production mode!") if Rails.env.production? +require 'spec_helper' +require 'rspec/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } + +# Checks for pending migration and applies them before tests are run. +# If you are not using ActiveRecord, you can remove this line. +ActiveRecord::Migration.maintain_test_schema! + +RSpec.configure do |config| + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true + + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, :type => :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! + + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..61e2738 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,92 @@ +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# The `.rspec` file also contains a few flags that are not defaults but that +# users commonly want. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # These two settings work together to allow you to limit a spec run + # to individual examples or groups you care about by tagging them with + # `:focus` metadata. When nothing is tagged with `:focus`, all examples + # get run. + config.filter_run :focus + config.run_all_when_everything_filtered = true + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = 'doc' + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end From 186f8275578603f6d27b743ab1ffa4fb42fef046 Mon Sep 17 00:00:00 2001 From: Webdev Date: Thu, 14 Apr 2016 19:24:23 -0700 Subject: [PATCH 34/78] Added blog post model rspec test --- app/models/post.rb | 8 +++++- spec/factories/posts.rb | 4 +-- spec/models/post_spec.rb | 53 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 spec/models/post_spec.rb diff --git a/app/models/post.rb b/app/models/post.rb index 0f67153..12ed89a 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,5 +1,11 @@ class Post < ActiveRecord::Base # title should be required & unique - validates :title, presence: true, uniqueness: true + validates :title, presence: true, uniqueness: true, length: {minimum: 7} + validates :body, presence: true has_many :comments, dependent: :destroy + + + def body_snippet + body.length > 99 ? "#{self.body[0...99]}..." : body + end end diff --git a/spec/factories/posts.rb b/spec/factories/posts.rb index 5ab4ff8..47ed04d 100644 --- a/spec/factories/posts.rb +++ b/spec/factories/posts.rb @@ -1,6 +1,6 @@ FactoryGirl.define do factory :post do - sequence(:title) { |n| "#{Faker::Commerce.department}"} - body {Faker::Lorem.paragraph} + sequence(:title) { |n| "#{Faker::Company.bs}-#{n}"} + body { Faker::Hipster.paragraph } end end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb new file mode 100644 index 0000000..8644684 --- /dev/null +++ b/spec/models/post_spec.rb @@ -0,0 +1,53 @@ +require 'rails_helper' + +RSpec.describe Post, type: :model do + # pending "add some examples to (or delete) #{__FILE__}" + + describe "validations" do + it "requires a title" do + # p0 = FactoryGirl.create(:post, title: nil) + p0 = Post.new(title: nil) + p0.valid? + expect(p0.errors).to have_key(:title) + end + + it "must have a title longer than 7 characters" do + # p0 = FactoryGirl.create(:post, title: "1234") + p0 = Post.new(title: "1234") + p0.valid? + expect(p0.errors).to have_key(:title) + + end + + it "requires a unique title" do + # p0 = FactoryGirl.create(:post, title: "same_title") + # p0.save + Post.create(title: "same_title", body: "df") + # p1 = FactoryGirl.create(:post, title: "same_title") + p1 = Post.new(title: "same_title", body: "af") + p1.valid? + expect(p1.errors).to have_key :title + + end + + it "requires a body" do + # p0 = FactoryGirl.create(:post, body: nil) + p0 = Post.new(body: nil) + p0.valid? + expect(p0.errors).to have_key(:body) + end + + it "must have a method, body_snippet, that returns a shortened body" do + body = Faker::Lorem.characters(99) + p0 = FactoryGirl.create(:post, body: "#{body}abcd") + expect(p0.body_snippet).to eq("#{body}...") + end + + it "must have a method, body_snippet, that doesn't change small body strings " do + body = Faker::Lorem.characters(99) + p0 = FactoryGirl.create(:post, body: body) + expect(p0.body_snippet).to eq(body) + end + + end +end From 9546afc3b44b12ea22b0e2a6dd1e48b78e5b39f3 Mon Sep 17 00:00:00 2001 From: Webdev Date: Fri, 15 Apr 2016 18:05:10 -0700 Subject: [PATCH 35/78] refectored rspec test for post --- spec/models/post_spec.rb | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 8644684..8be3eef 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -4,48 +4,46 @@ # pending "add some examples to (or delete) #{__FILE__}" describe "validations" do + def valid_attributes + {title: "Real Title", body: "A body goes here"} + end + it "requires a title" do - # p0 = FactoryGirl.create(:post, title: nil) - p0 = Post.new(title: nil) + p0 = Post.create(valid_attributes.merge title: nil) p0.valid? expect(p0.errors).to have_key(:title) end it "must have a title longer than 7 characters" do - # p0 = FactoryGirl.create(:post, title: "1234") - p0 = Post.new(title: "1234") + p0 = Post.new(valid_attributes.merge(title: "123")) p0.valid? expect(p0.errors).to have_key(:title) end it "requires a unique title" do - # p0 = FactoryGirl.create(:post, title: "same_title") - # p0.save - Post.create(title: "same_title", body: "df") - # p1 = FactoryGirl.create(:post, title: "same_title") - p1 = Post.new(title: "same_title", body: "af") + Post.create(valid_attributes) + p1 = Post.new(valid_attributes) p1.valid? - expect(p1.errors).to have_key :title + expect(p1.errors).to have_key :title end it "requires a body" do - # p0 = FactoryGirl.create(:post, body: nil) - p0 = Post.new(body: nil) + p0 = Post.new valid_attributes.merge body: nil p0.valid? expect(p0.errors).to have_key(:body) end it "must have a method, body_snippet, that returns a shortened body" do body = Faker::Lorem.characters(99) - p0 = FactoryGirl.create(:post, body: "#{body}abcd") + p0 = Post.new(title:"title", body: "#{body}abcd") expect(p0.body_snippet).to eq("#{body}...") end it "must have a method, body_snippet, that doesn't change small body strings " do body = Faker::Lorem.characters(99) - p0 = FactoryGirl.create(:post, body: body) + p0 = Post.new(title:"title", body: body) expect(p0.body_snippet).to eq(body) end From 57152607017926f130d4380cd385d4c93469e64e Mon Sep 17 00:00:00 2001 From: Webdev Date: Fri, 15 Apr 2016 19:06:31 -0700 Subject: [PATCH 36/78] added a comment --- spec/models/post_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 8be3eef..09b24d7 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -37,6 +37,7 @@ def valid_attributes it "must have a method, body_snippet, that returns a shortened body" do body = Faker::Lorem.characters(99) + # I appended "abcd" to push it over 100 characters and so I could experiment manualy p0 = Post.new(title:"title", body: "#{body}abcd") expect(p0.body_snippet).to eq("#{body}...") end From 632ac8d433a3a6e15ef5e83c2694d3915d4f0576 Mon Sep 17 00:00:00 2001 From: Webdev Date: Fri, 15 Apr 2016 19:26:31 -0700 Subject: [PATCH 37/78] comments now display under blog posts and can be created there --- app/controllers/comments_controller.rb | 4 +++- app/controllers/posts_controller.rb | 1 + app/models/comment.rb | 2 ++ app/views/posts/show.html.slim | 15 +++++++++++++++ config/routes.rb | 5 +++-- 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index f4c7145..1e4b06e 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -11,10 +11,12 @@ def new end def create + @post = Post.find params[:post_id] @comment = Comment.new(comment_params) + @comment.post = @post if @comment.save flash[:notice] = "Comment Created" - redirect_to comment_path(@comment) + redirect_to post_path(@post) else flash[:alert] = "Error! Comment not created" render :new diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 44f6eb3..36453f7 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -34,6 +34,7 @@ def update end def show + @comment = Comment.new end def destroy diff --git a/app/models/comment.rb b/app/models/comment.rb index d7a9351..79a946f 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,3 +1,5 @@ class Comment < ActiveRecord::Base belongs_to :customer + belongs_to :post + end diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index c3e95ce..2ff8836 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -8,3 +8,18 @@ div = link_to "Edit", edit_post_path(@post) div = link_to "Delete", post_path(@post), method: :delete, data: {confirm: "Are you sure you want to delete this post?"} + += @comment.errors.full_messages.join(", ") + += form_for @comment, url: post_comments_path(@post) do |f| + = form_for [@post, @comment] do |form_helper| + = form_helper.text_area :body + = form_helper.submit + +h3 Comments +- @post.comments.each do |comment| + = by comment.email_or_unknown + = comment.body + = link_to "delete", post_comment_path(@post, comment), + data: {confirm: "Are you sure?"}, + method: :delete diff --git a/config/routes.rb b/config/routes.rb index af81d03..90f2bbc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,8 +4,9 @@ get "/about" => "home#about" - resources :posts - resources :comments + resources :posts do + resources :comments + end root 'home#home' From 9fc29d860923d8b8ba58027ee2ee5f8329d8c212 Mon Sep 17 00:00:00 2001 From: Webdev Date: Sun, 17 Apr 2016 11:42:55 -0700 Subject: [PATCH 38/78] waiting til later to introduce users --- app/views/posts/show.html.slim | 1 - 1 file changed, 1 deletion(-) diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index 2ff8836..3e21df8 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -18,7 +18,6 @@ div h3 Comments - @post.comments.each do |comment| - = by comment.email_or_unknown = comment.body = link_to "delete", post_comment_path(@post, comment), data: {confirm: "Are you sure?"}, From abd7d875541a13edfb1c112cc556a74b9058831d Mon Sep 17 00:00:00 2001 From: Webdev Date: Sun, 17 Apr 2016 11:58:23 -0700 Subject: [PATCH 39/78] added body validation --- app/models/comment.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/comment.rb b/app/models/comment.rb index 79a946f..848ede1 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -2,4 +2,5 @@ class Comment < ActiveRecord::Base belongs_to :customer belongs_to :post + validates :body, presence: :true end From ef548424dac2d6aecfa372661e3cdee2374ea41e Mon Sep 17 00:00:00 2001 From: Webdev Date: Sun, 17 Apr 2016 12:21:03 -0700 Subject: [PATCH 40/78] added category model tests --- spec/factories/categories.rb | 5 +++++ spec/models/category_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 spec/factories/categories.rb create mode 100644 spec/models/category_spec.rb diff --git a/spec/factories/categories.rb b/spec/factories/categories.rb new file mode 100644 index 0000000..d597d71 --- /dev/null +++ b/spec/factories/categories.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :category do + + end +end diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb new file mode 100644 index 0000000..ac6ae09 --- /dev/null +++ b/spec/models/category_spec.rb @@ -0,0 +1,20 @@ +require 'rails_helper' + +RSpec.describe Category, type: :model do + describe "validations" do + it "requires a title" do + c = category.new(title: nil) + c.valid? + expect(c.errors).to have_key :title + end + it "must be a unique category" do + Campaign.create(title: "sample title") + c = Campaign.new(title: "sample title") + + c.valid? + + expect(c.errors).to have_key :title + + end + end +end From e6926a600f735936579f9df88ab8ebba8622ed5b Mon Sep 17 00:00:00 2001 From: Webdev Date: Sun, 17 Apr 2016 14:27:50 -0700 Subject: [PATCH 41/78] Added in categories for posts --- app/models/category.rb | 3 +++ app/models/post.rb | 4 +++- app/views/posts/_post_form.html.slim | 3 +++ .../20160417212436_add_category_references_to_posts.rb | 5 +++++ db/schema.rb | 10 +++++++--- spec/models/category_spec.rb | 6 +++--- 6 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20160417212436_add_category_references_to_posts.rb diff --git a/app/models/category.rb b/app/models/category.rb index 910a009..9dc026c 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,2 +1,5 @@ class Category < ActiveRecord::Base + has_many :posts, dependent: :nullify + + validates :title, presence: true, uniqueness: true end diff --git a/app/models/post.rb b/app/models/post.rb index 12ed89a..d430eeb 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -2,7 +2,9 @@ class Post < ActiveRecord::Base # title should be required & unique validates :title, presence: true, uniqueness: true, length: {minimum: 7} validates :body, presence: true - has_many :comments, dependent: :destroy + + has_many :comments, dependent: :destroy + belongs_to :category def body_snippet diff --git a/app/views/posts/_post_form.html.slim b/app/views/posts/_post_form.html.slim index 4a2c62c..299ae44 100644 --- a/app/views/posts/_post_form.html.slim +++ b/app/views/posts/_post_form.html.slim @@ -6,5 +6,8 @@ div = f.label :body = f.text_area :body + div + = f.label :category + = f.collection_select :category_id, Category.order(:title), :id, :title div = f.submit diff --git a/db/migrate/20160417212436_add_category_references_to_posts.rb b/db/migrate/20160417212436_add_category_references_to_posts.rb new file mode 100644 index 0000000..e3e34bf --- /dev/null +++ b/db/migrate/20160417212436_add_category_references_to_posts.rb @@ -0,0 +1,5 @@ +class AddCategoryReferencesToPosts < ActiveRecord::Migration + def change + add_reference :posts, :category, index: true, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 5e52224..fc0c362 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160411055429) do +ActiveRecord::Schema.define(version: 20160417212436) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -41,8 +41,12 @@ create_table "posts", force: :cascade do |t| t.string "title" t.text "body" - 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.integer "category_id" end + add_index "posts", ["category_id"], name: "index_posts_on_category_id", using: :btree + + add_foreign_key "posts", "categories" end diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index ac6ae09..3d4c264 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -3,13 +3,13 @@ RSpec.describe Category, type: :model do describe "validations" do it "requires a title" do - c = category.new(title: nil) + c = Category.new(title: nil) c.valid? expect(c.errors).to have_key :title end it "must be a unique category" do - Campaign.create(title: "sample title") - c = Campaign.new(title: "sample title") + Category.create(title: "sample title") + c = Category.new(title: "sample title") c.valid? From 4f03df12c5b375992839937c579344cef12d59ca Mon Sep 17 00:00:00 2001 From: Webdev Date: Sun, 17 Apr 2016 14:32:52 -0700 Subject: [PATCH 42/78] moved notice || alert display to layout instead of restricted to individual views --- app/views/comments/_comment_form.html.slim | 2 -- app/views/layouts/application.html.erb | 4 ++++ app/views/posts/_post_form.html.slim | 1 - 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/views/comments/_comment_form.html.slim b/app/views/comments/_comment_form.html.slim index 4f6b7b7..7a79413 100644 --- a/app/views/comments/_comment_form.html.slim +++ b/app/views/comments/_comment_form.html.slim @@ -1,5 +1,3 @@ -= notice || alert - = form_for @comment do |f| div = f.label :body diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 3a419ff..6af250b 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -12,6 +12,10 @@ <%= link_to("About", about_path, {class: "menu-item"}) %> <%= link_to("Blog Posts", posts_path, {class: "menu-item"}) %>
+
+ <%= notice || alert %> +
+ <%= yield %> diff --git a/app/views/posts/_post_form.html.slim b/app/views/posts/_post_form.html.slim index 4a2c62c..ea3b14f 100644 --- a/app/views/posts/_post_form.html.slim +++ b/app/views/posts/_post_form.html.slim @@ -1,4 +1,3 @@ -= notice || alert = form_for @post do |f| div = f.label :title From 38a4cfb9a03369cee01eb2998a8746f26cdd3f2e Mon Sep 17 00:00:00 2001 From: Webdev Date: Sun, 17 Apr 2016 15:53:19 -0700 Subject: [PATCH 43/78] fixed wrong path to partial --- app/views/posts/edit.html.slim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/posts/edit.html.slim b/app/views/posts/edit.html.slim index 50c2da8..960edf8 100644 --- a/app/views/posts/edit.html.slim +++ b/app/views/posts/edit.html.slim @@ -1,3 +1,3 @@ h2 Editing Blog Post -= render "blog_form" += render "posts/post_form" From 8d89f0043c07cc2a586b3eaf3ec727696fc614d6 Mon Sep 17 00:00:00 2001 From: Webdev Date: Sun, 17 Apr 2016 16:09:40 -0700 Subject: [PATCH 44/78] added in category support, for blog posts, in views and controller --- app/controllers/posts_controller.rb | 3 ++- app/models/post.rb | 5 +++++ app/views/posts/index.html.erb | 3 +++ app/views/posts/show.html.slim | 3 +++ db/seeds.rb | 4 ++++ spec/factories/categories.rb | 2 +- 6 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 36453f7..a48d295 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -13,6 +13,7 @@ def new def create @post = Post.new(post_params) + if @post.save flash[:notice] = "Post Created" redirect_to post_path(@post) @@ -49,6 +50,6 @@ def find_post end def post_params - params.require(:post).permit([:title, :body]) + params.require(:post).permit([:title, :body, :category_id]) end end diff --git a/app/models/post.rb b/app/models/post.rb index d430eeb..7d397e7 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -10,4 +10,9 @@ class Post < ActiveRecord::Base def body_snippet body.length > 99 ? "#{self.body[0...99]}..." : body end + + def category_or_unknown + category ? category.title : "Unknown Category" + end + end diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index f48db64..9d1eace 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -4,6 +4,9 @@
<%= link_to p.title, post_path(p) %>
+
+ Category: <%= p.category_or_unknown %> +
<%= p.body %>
diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index 3e21df8..5e355ce 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -1,6 +1,9 @@ div h2 = @post.title +div + | Category: + = @post.category_or_unknown div = @post.body diff --git a/db/seeds.rb b/db/seeds.rb index ec21594..b3666e5 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -6,7 +6,11 @@ # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) +# B log post seed 100.times do Post.create title: Faker::Company.bs, body: Faker::Lorem.paragraph end + +# category seed. shouldn't run too often +# 10.times { FactoryGirl.create(:category) } diff --git a/spec/factories/categories.rb b/spec/factories/categories.rb index d597d71..938cb1d 100644 --- a/spec/factories/categories.rb +++ b/spec/factories/categories.rb @@ -1,5 +1,5 @@ FactoryGirl.define do factory :category do - + sequence(:title) {|n| "#{n}-#{Faker::Hacker.adjective}" } end end From 6c063dbb08f09949445d9e8d250c2402e9b9e7a9 Mon Sep 17 00:00:00 2001 From: Webdev Date: Mon, 18 Apr 2016 17:00:49 -0700 Subject: [PATCH 45/78] added user model --- Gemfile | 2 +- Gemfile.lock | 2 ++ app/models/comment.rb | 1 + app/models/post.rb | 1 + db/schema.rb | 18 +++++++++++++++++- 5 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index ef18c37..1775fa5 100644 --- a/Gemfile +++ b/Gemfile @@ -24,7 +24,7 @@ gem 'jbuilder', '~> 2.0' gem 'sdoc', '~> 0.4.0', group: :doc # Use ActiveModel has_secure_password -# gem 'bcrypt', '~> 3.1.7' +gem 'bcrypt', '~> 3.1.7' # Use Unicorn as the app server # gem 'unicorn' diff --git a/Gemfile.lock b/Gemfile.lock index a4cae71..6eec5ff 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -37,6 +37,7 @@ GEM thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) arel (6.0.3) + bcrypt (3.1.11) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) builder (3.2.2) @@ -172,6 +173,7 @@ PLATFORMS ruby DEPENDENCIES + bcrypt (~> 3.1.7) byebug coffee-rails (~> 4.1.0) factory_girl_rails diff --git a/app/models/comment.rb b/app/models/comment.rb index 848ede1..c5ab164 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,6 +1,7 @@ class Comment < ActiveRecord::Base belongs_to :customer belongs_to :post + belongs_to :user validates :body, presence: :true end diff --git a/app/models/post.rb b/app/models/post.rb index 7d397e7..0e1baec 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -5,6 +5,7 @@ class Post < ActiveRecord::Base has_many :comments, dependent: :destroy belongs_to :category + belongs_to :user def body_snippet diff --git a/db/schema.rb b/db/schema.rb index fc0c362..d450b54 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160417212436) do +ActiveRecord::Schema.define(version: 20160418235427) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -27,8 +27,11 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "post_id" + t.integer "contact_id" end + add_index "comments", ["contact_id"], name: "index_comments_on_contact_id", using: :btree + create_table "contacts", force: :cascade do |t| t.string "email" t.string "name" @@ -44,9 +47,22 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "category_id" + t.integer "contact_id" end add_index "posts", ["category_id"], name: "index_posts_on_category_id", using: :btree + add_index "posts", ["contact_id"], name: "index_posts_on_contact_id", using: :btree + + create_table "users", force: :cascade do |t| + t.string "first_name" + t.string "last_name" + t.string "email" + t.string "password_digest" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + add_foreign_key "comments", "contacts" add_foreign_key "posts", "categories" + add_foreign_key "posts", "contacts" end From cc7cc2b23fabea271dd37e6c26ffd496b23c05fc Mon Sep 17 00:00:00 2001 From: Webdev Date: Mon, 18 Apr 2016 17:07:32 -0700 Subject: [PATCH 46/78] add user references to posts and comments models --- app/models/user.rb | 5 +++++ db/migrate/20160418235427_create_users.rb | 12 ++++++++++++ .../20160419000523_add_user_references_to_posts.rb | 5 +++++ ...20160419000537_add_user_references_to_comments.rb | 5 +++++ db/schema.rb | 8 +++++++- spec/factories/users.rb | 8 ++++++++ spec/models/user_spec.rb | 5 +++++ 7 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 app/models/user.rb create mode 100644 db/migrate/20160418235427_create_users.rb create mode 100644 db/migrate/20160419000523_add_user_references_to_posts.rb create mode 100644 db/migrate/20160419000537_add_user_references_to_comments.rb create mode 100644 spec/factories/users.rb create mode 100644 spec/models/user_spec.rb diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..6714974 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,5 @@ +class User < ActiveRecord::Base + has_many :posts + has_many :comments + +end diff --git a/db/migrate/20160418235427_create_users.rb b/db/migrate/20160418235427_create_users.rb new file mode 100644 index 0000000..e22193e --- /dev/null +++ b/db/migrate/20160418235427_create_users.rb @@ -0,0 +1,12 @@ +class CreateUsers < ActiveRecord::Migration + def change + create_table :users do |t| + t.string :first_name + t.string :last_name + t.string :email + t.string :password_digest + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20160419000523_add_user_references_to_posts.rb b/db/migrate/20160419000523_add_user_references_to_posts.rb new file mode 100644 index 0000000..a74b33b --- /dev/null +++ b/db/migrate/20160419000523_add_user_references_to_posts.rb @@ -0,0 +1,5 @@ +class AddUserReferencesToPosts < ActiveRecord::Migration + def change + add_reference :posts, :user, index: true, foreign_key: true + end +end diff --git a/db/migrate/20160419000537_add_user_references_to_comments.rb b/db/migrate/20160419000537_add_user_references_to_comments.rb new file mode 100644 index 0000000..9d6a655 --- /dev/null +++ b/db/migrate/20160419000537_add_user_references_to_comments.rb @@ -0,0 +1,5 @@ +class AddUserReferencesToComments < ActiveRecord::Migration + def change + add_reference :comments, :user, index: true, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index d450b54..5263e53 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160418235427) do +ActiveRecord::Schema.define(version: 20160419000537) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -28,9 +28,11 @@ t.datetime "updated_at", null: false t.integer "post_id" t.integer "contact_id" + t.integer "user_id" end add_index "comments", ["contact_id"], name: "index_comments_on_contact_id", using: :btree + add_index "comments", ["user_id"], name: "index_comments_on_user_id", using: :btree create_table "contacts", force: :cascade do |t| t.string "email" @@ -48,10 +50,12 @@ t.datetime "updated_at", null: false t.integer "category_id" t.integer "contact_id" + t.integer "user_id" end add_index "posts", ["category_id"], name: "index_posts_on_category_id", using: :btree add_index "posts", ["contact_id"], name: "index_posts_on_contact_id", using: :btree + add_index "posts", ["user_id"], name: "index_posts_on_user_id", using: :btree create_table "users", force: :cascade do |t| t.string "first_name" @@ -63,6 +67,8 @@ end add_foreign_key "comments", "contacts" + add_foreign_key "comments", "users" add_foreign_key "posts", "categories" add_foreign_key "posts", "contacts" + add_foreign_key "posts", "users" end diff --git a/spec/factories/users.rb b/spec/factories/users.rb new file mode 100644 index 0000000..0c122b0 --- /dev/null +++ b/spec/factories/users.rb @@ -0,0 +1,8 @@ +FactoryGirl.define do + factory :user do + first_name "MyString" + last_name "MyString" + email "MyString" + password_digest "MyString" + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 0000000..47a31bb --- /dev/null +++ b/spec/models/user_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe User, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From a6ef34986e8606ef5c7982c1a136b90939f760dc Mon Sep 17 00:00:00 2001 From: Webdev Date: Mon, 18 Apr 2016 17:30:03 -0700 Subject: [PATCH 47/78] add user validations and fixed user model password check with has_secure password --- app/models/user.rb | 4 +++- spec/models/user_spec.rb | 51 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 6714974..30f5bd4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,7 @@ class User < ActiveRecord::Base has_many :posts has_many :comments - + + has_secure_password + end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 47a31bb..08b4841 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,5 +1,54 @@ require 'rails_helper' RSpec.describe User, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + + describe "validations" do + def valid_attributes + {first_name: "Joe", last_name: "Blogs", email: "email@hotmail.com", password_digest: "supersecret"} + end + + it "requires a first_name" do + u = User.create(valid_attributes.merge first_name: nil) + u.valid? + expect(u.errors).to have_key(:name) + end + + it "requires a last_name" do + u = User.create(valid_attributes.merge last_name: nil) + u.valid? + expect(u.errors).to have_key(:name) + end + + it "requires a email" do + u = User.create(valid_attributes.merge email: nil) + u.valid? + expect(u.errors).to have_key(:email) + end + it "requires a email with an @" do + u = User.create(valid_attributes.merge email: "not_a_real_email") + u.valid? + expect(u.errors).to have_key(:email) + end + it "requires a unique email" do + User.create(valid_attributes) + e = User.new(valid_attributes) + e.valid? + + expect(p1.errors).to have_key :title + end + + it "requires a password" do + u = User.create(valid_attributes.merge password: nil) + u.valid? + expect(u.errors).to have_key(:password) + end + + it "hashes the password" do + pass = "supersecret" + u = User.create(valid_attributes.merge password: pass) + u.valid? + expect(u.password).not_to eq(pass) + end + end + end From 6096642dd317f6b20b363871d8e13d47d98d9144 Mon Sep 17 00:00:00 2001 From: Webdev Date: Mon, 18 Apr 2016 17:38:40 -0700 Subject: [PATCH 48/78] fixed up user validations rspec test --- spec/models/user_spec.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 08b4841..74cde14 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -4,28 +4,28 @@ describe "validations" do def valid_attributes - {first_name: "Joe", last_name: "Blogs", email: "email@hotmail.com", password_digest: "supersecret"} + {first_name: "Joe", last_name: "Blogs", email: "email@hotmail.com", password: "supersecret"} end it "requires a first_name" do - u = User.create(valid_attributes.merge first_name: nil) + u = User.new(valid_attributes.merge first_name: nil) u.valid? expect(u.errors).to have_key(:name) end it "requires a last_name" do - u = User.create(valid_attributes.merge last_name: nil) + u = User.new(valid_attributes.merge last_name: nil) u.valid? expect(u.errors).to have_key(:name) end it "requires a email" do - u = User.create(valid_attributes.merge email: nil) + u = User.new(valid_attributes.merge email: nil) u.valid? expect(u.errors).to have_key(:email) end it "requires a email with an @" do - u = User.create(valid_attributes.merge email: "not_a_real_email") + u = User.new(valid_attributes.merge email: "not_a_real_email") u.valid? expect(u.errors).to have_key(:email) end @@ -38,15 +38,14 @@ def valid_attributes end it "requires a password" do - u = User.create(valid_attributes.merge password: nil) + u = User.new(valid_attributes.merge password: nil) u.valid? expect(u.errors).to have_key(:password) end it "hashes the password" do pass = "supersecret" - u = User.create(valid_attributes.merge password: pass) - u.valid? + u = User.new(valid_attributes.merge password: pass) expect(u.password).not_to eq(pass) end end From 67a7074d7b8328df59e6025dabde5285223b9609 Mon Sep 17 00:00:00 2001 From: Webdev Date: Mon, 18 Apr 2016 17:52:10 -0700 Subject: [PATCH 49/78] added user controller and filled in a create user and edit user forms for views --- app/assets/javascripts/users.coffee | 3 +++ app/assets/stylesheets/users.scss | 3 +++ app/controllers/users_controller.rb | 2 ++ app/helpers/users_helper.rb | 2 ++ app/models/user.rb | 8 ++++++ app/views/users/_users_form.html.erb | 33 +++++++++++++++++++++++ app/views/users/edit.html.slim | 3 +++ app/views/users/new.html.slim | 3 +++ spec/controllers/users_controller_spec.rb | 5 ++++ spec/helpers/users_helper_spec.rb | 15 +++++++++++ spec/models/user_spec.rb | 8 +++--- 11 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 app/assets/javascripts/users.coffee create mode 100644 app/assets/stylesheets/users.scss create mode 100644 app/controllers/users_controller.rb create mode 100644 app/helpers/users_helper.rb create mode 100644 app/views/users/_users_form.html.erb create mode 100644 app/views/users/edit.html.slim create mode 100644 app/views/users/new.html.slim create mode 100644 spec/controllers/users_controller_spec.rb create mode 100644 spec/helpers/users_helper_spec.rb diff --git a/app/assets/javascripts/users.coffee b/app/assets/javascripts/users.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/users.coffee @@ -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/ diff --git a/app/assets/stylesheets/users.scss b/app/assets/stylesheets/users.scss new file mode 100644 index 0000000..1efc835 --- /dev/null +++ b/app/assets/stylesheets/users.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the users controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..3e74dea --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,2 @@ +class UsersController < ApplicationController +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 0000000..2310a24 --- /dev/null +++ b/app/helpers/users_helper.rb @@ -0,0 +1,2 @@ +module UsersHelper +end diff --git a/app/models/user.rb b/app/models/user.rb index 30f5bd4..c963e8e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,4 +4,12 @@ class User < ActiveRecord::Base has_secure_password + validates :first_name, presence: true + validates :last_name, presence: true + + VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i + validates :email, presence: true, uniqueness: true, format: VALID_EMAIL_REGEX + + validates :password, presence: true + end diff --git a/app/views/users/_users_form.html.erb b/app/views/users/_users_form.html.erb new file mode 100644 index 0000000..ee1d063 --- /dev/null +++ b/app/views/users/_users_form.html.erb @@ -0,0 +1,33 @@ +<% if @user.errors.any? %> +
    + <% @user.errors.full_messages.each do |msg| %> +
  • <%= msg %>
  • + <% end %> +
+<% end %> + +<%= form_for @user do |f| %> +
+ <%= f.label :first_name %> + <%= f.text_field :first_name %> +
+
+ <%= f.label :last_name %> + <%= f.text_field :last_name %> +
+
+ <%= f.label :email %> + <%= f.email_field :email %> +
+
+ <%= f.label :password %> + <%= f.password_field :password %> +
+
+ <%= f.label :password_confirmation %> + <%= f.password_field :password_confirmation %> +
+
+ <%= f.submit "Create or modify an Account" %> +
+<% end %> diff --git a/app/views/users/edit.html.slim b/app/views/users/edit.html.slim new file mode 100644 index 0000000..4362912 --- /dev/null +++ b/app/views/users/edit.html.slim @@ -0,0 +1,3 @@ +h1 Edit User + += render "users/users_form" diff --git a/app/views/users/new.html.slim b/app/views/users/new.html.slim new file mode 100644 index 0000000..3d84bc0 --- /dev/null +++ b/app/views/users/new.html.slim @@ -0,0 +1,3 @@ +h1 Create User + += render "users/users_form" diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb new file mode 100644 index 0000000..e2c3d3b --- /dev/null +++ b/spec/controllers/users_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe UsersController, type: :controller do + +end diff --git a/spec/helpers/users_helper_spec.rb b/spec/helpers/users_helper_spec.rb new file mode 100644 index 0000000..b2e3444 --- /dev/null +++ b/spec/helpers/users_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the UsersHelper. For example: +# +# describe UsersHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe UsersHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 74cde14..8fe9b7c 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -10,13 +10,13 @@ def valid_attributes it "requires a first_name" do u = User.new(valid_attributes.merge first_name: nil) u.valid? - expect(u.errors).to have_key(:name) + expect(u.errors).to have_key(:first_name) end it "requires a last_name" do u = User.new(valid_attributes.merge last_name: nil) u.valid? - expect(u.errors).to have_key(:name) + expect(u.errors).to have_key(:last_name) end it "requires a email" do @@ -34,7 +34,7 @@ def valid_attributes e = User.new(valid_attributes) e.valid? - expect(p1.errors).to have_key :title + expect(e.errors).to have_key :email end it "requires a password" do @@ -46,7 +46,7 @@ def valid_attributes it "hashes the password" do pass = "supersecret" u = User.new(valid_attributes.merge password: pass) - expect(u.password).not_to eq(pass) + expect(u.password_digest).not_to eq(pass) end end From 5d2f918584e1b1eae4e94c01e3b515d223e5810a Mon Sep 17 00:00:00 2001 From: Webdev Date: Mon, 18 Apr 2016 19:22:30 -0700 Subject: [PATCH 50/78] added ability to add a user --- app/controllers/application_controller.rb | 15 +++++++++++++++ app/controllers/posts_controller.rb | 2 +- app/controllers/users_controller.rb | 19 +++++++++++++++++++ config/routes.rb | 2 ++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..69cb8bb 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,19 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + + # def authenticate_user! + # redirect_to new_session_path, notice: "Please sign in!" unless user_signed_in? + # end + # + # def user_signed_in? + # session[:user_id].present? + # end + # helper_method :user_signed_in? + # + # def current_user + # @current_user ||= User.find session[:user_id] if user_signed_in? + # end + # helper_method :current_user + end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index a48d295..31784ed 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -28,7 +28,7 @@ def edit def update if @post.update post_params - redirect_to post_path(@post), notice: "Question updated!" + redirect_to post_path(@post), notice: "Blog Post updated!" else render :edit end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3e74dea..e8002ce 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,2 +1,21 @@ class UsersController < ApplicationController + + def new + @user = User.new + end + + def create + user_params = params.require(:user).permit(:first_name, :last_name, :email, + :password, :password_confirmation) + @user = User.new user_params + if @user.save + # we log the user in by setting the session :user_id to the user's id + # in our database so we can identify the user who is logged in by their id + session[:user_id] = @user.id + redirect_to root_path, notice: "Account created!" + else + render :new + end + end + end diff --git a/config/routes.rb b/config/routes.rb index 90f2bbc..4c55f2f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,8 @@ resources :comments end + resources :users + root 'home#home' # The priority is based upon order of creation: first created -> highest priority. From 30cab77131e6cb415973b50097f8e1b58298720b Mon Sep 17 00:00:00 2001 From: Webdev Date: Mon, 18 Apr 2016 19:51:11 -0700 Subject: [PATCH 51/78] added in login page --- app/assets/javascripts/sessions.coffee | 3 +++ app/assets/stylesheets/sessions.scss | 3 +++ app/controllers/sessions_controller.rb | 15 +++++++++++++++ app/helpers/sessions_helper.rb | 2 ++ config/routes.rb | 4 +++- spec/controllers/sessions_controller_spec.rb | 5 +++++ spec/helpers/sessions_helper_spec.rb | 15 +++++++++++++++ 7 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/sessions.coffee create mode 100644 app/assets/stylesheets/sessions.scss create mode 100644 app/controllers/sessions_controller.rb create mode 100644 app/helpers/sessions_helper.rb create mode 100644 spec/controllers/sessions_controller_spec.rb create mode 100644 spec/helpers/sessions_helper_spec.rb diff --git a/app/assets/javascripts/sessions.coffee b/app/assets/javascripts/sessions.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/sessions.coffee @@ -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/ diff --git a/app/assets/stylesheets/sessions.scss b/app/assets/stylesheets/sessions.scss new file mode 100644 index 0000000..7bef9cf --- /dev/null +++ b/app/assets/stylesheets/sessions.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the sessions controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..9adfe1d --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,15 @@ +class SessionsController < ApplicationController + def new + end + + def create + user = User.find_by_email params[:email] + if user && user.authenticate(params[:password]) + session[:user_id] = user.id + redirect_to root_path, notice: "Signed In!" + else + flash[:alert] = "Wrong Credentials!" + render :new + end + end +end diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb new file mode 100644 index 0000000..309f8b2 --- /dev/null +++ b/app/helpers/sessions_helper.rb @@ -0,0 +1,2 @@ +module SessionsHelper +end diff --git a/config/routes.rb b/config/routes.rb index 4c55f2f..accac9b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,7 +8,9 @@ resources :comments end - resources :users + resources :users #, only: [:new, :create] + + resources :sessions, only: [:new, :create] root 'home#home' diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb new file mode 100644 index 0000000..003bede --- /dev/null +++ b/spec/controllers/sessions_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe SessionsController, type: :controller do + +end diff --git a/spec/helpers/sessions_helper_spec.rb b/spec/helpers/sessions_helper_spec.rb new file mode 100644 index 0000000..9484198 --- /dev/null +++ b/spec/helpers/sessions_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the SessionsHelper. For example: +# +# describe SessionsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe SessionsHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end From 46fb16a31992adddb92e73f2fede7c8c5f04f747 Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 19 Apr 2016 17:34:20 -0700 Subject: [PATCH 52/78] fixes up login paged --- app/views/sessions/new.html.slim | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 app/views/sessions/new.html.slim diff --git a/app/views/sessions/new.html.slim b/app/views/sessions/new.html.slim new file mode 100644 index 0000000..2a0ed2a --- /dev/null +++ b/app/views/sessions/new.html.slim @@ -0,0 +1,11 @@ +h1 Sign In + += form_tag sessions_path + div + = label_tag :email + = email_field_tag :email + div + = label_tag :password + = password_field_tag :password + = submit_tag "Sign In" + From fe04a7734b973e725703dc094131f3f3d54e6b33 Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 19 Apr 2016 18:21:02 -0700 Subject: [PATCH 53/78] added ability to log in and out. Had to implement destory method in the sessions controller --- app/controllers/application_controller.rb | 8 ++++---- app/controllers/sessions_controller.rb | 6 ++++++ app/views/layouts/application.html.erb | 6 ++++++ config/routes.rb | 6 ++++-- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 69cb8bb..72b4a24 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -7,10 +7,10 @@ class ApplicationController < ActionController::Base # redirect_to new_session_path, notice: "Please sign in!" unless user_signed_in? # end # - # def user_signed_in? - # session[:user_id].present? - # end - # helper_method :user_signed_in? + def user_signed_in? + session[:user_id].present? + end + helper_method :user_signed_in? # # def current_user # @current_user ||= User.find session[:user_id] if user_signed_in? diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 9adfe1d..93d3599 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -12,4 +12,10 @@ def create render :new end end + + def destroy + session[:user_id] = nil + redirect_to root_path, notice: "Logged out" + end + end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 6af250b..f9adbad 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,6 +11,12 @@ <%= link_to("Home", home_path, {class: "menu-item"}) %> <%= link_to("About", about_path, {class: "menu-item"}) %> <%= link_to("Blog Posts", posts_path, {class: "menu-item"}) %> + <% if user_signed_in? %> + <%= link_to("Log Out", sessions_path, {class: "menu-item", method: :delete}) %> + <% else %> + <%= link_to "Sign In", new_session_path %> + <%= link_to "Sign Up", new_user_path %> + <% end %>
<%= notice || alert %> diff --git a/config/routes.rb b/config/routes.rb index accac9b..2e6f338 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,9 +8,11 @@ resources :comments end - resources :users #, only: [:new, :create] + resources :users - resources :sessions, only: [:new, :create] + resources :sessions, only: [:new, :create] do + delete :destroy, on: :collection + end root 'home#home' From 195727d012c2f45934956993d38d11cc01c68c36 Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 19 Apr 2016 18:39:30 -0700 Subject: [PATCH 54/78] added user authentication for backend --- app/controllers/application_controller.rb | 6 +++--- app/controllers/comments_controller.rb | 1 + app/controllers/posts_controller.rb | 1 + 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 72b4a24..0e1a228 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,9 +3,9 @@ class ApplicationController < ActionController::Base # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception - # def authenticate_user! - # redirect_to new_session_path, notice: "Please sign in!" unless user_signed_in? - # end + def authenticate_user! + redirect_to new_session_path, notice: "Please sign in!" unless user_signed_in? + end # def user_signed_in? session[:user_id].present? diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 1e4b06e..48a84bb 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,5 +1,6 @@ class CommentsController < ApplicationController + before_action :authenticate_user! before_action(:find_comment, {only: [:show, :edit, :update, :destroy]}) def index diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 31784ed..9b7b6c8 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,5 +1,6 @@ class PostsController < ApplicationController + before_action :authenticate_user! before_action(:find_post, {only: [:show, :edit, :update, :destroy]}) From eadae0e29d64fbc86d8ee71d92d11125112f1f7a Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 19 Apr 2016 18:48:02 -0700 Subject: [PATCH 55/78] don't show edit or delete buttons if not logged in --- app/controllers/application_controller.rb | 2 +- app/controllers/posts_controller.rb | 2 +- app/views/posts/show.html.slim | 17 +++++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0e1a228..d018790 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,7 +6,7 @@ class ApplicationController < ActionController::Base def authenticate_user! redirect_to new_session_path, notice: "Please sign in!" unless user_signed_in? end - # + def user_signed_in? session[:user_id].present? end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 9b7b6c8..a9812cb 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,6 +1,6 @@ class PostsController < ApplicationController - before_action :authenticate_user! + before_action :authenticate_user!, except: [:index, :show] before_action(:find_post, {only: [:show, :edit, :update, :destroy]}) diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index 5e355ce..0d12292 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -6,11 +6,11 @@ div = @post.category_or_unknown div = @post.body - -div - = link_to "Edit", edit_post_path(@post) -div - = link_to "Delete", post_path(@post), method: :delete, data: {confirm: "Are you sure you want to delete this post?"} +- if user_signed_in? + div + = link_to "Edit", edit_post_path(@post) + div + = link_to "Delete", post_path(@post), method: :delete, data: {confirm: "Are you sure you want to delete this post?"} = @comment.errors.full_messages.join(", ") @@ -22,6 +22,7 @@ div h3 Comments - @post.comments.each do |comment| = comment.body - = link_to "delete", post_comment_path(@post, comment), - data: {confirm: "Are you sure?"}, - method: :delete + - if user_signed_in? + = link_to "delete", post_comment_path(@post, comment), + data: {confirm: "Are you sure?"}, + method: :delete From e830f12d85e5b0cff38ebbfafdb5dc194596c09e Mon Sep 17 00:00:00 2001 From: Webdev Date: Tue, 19 Apr 2016 19:03:02 -0700 Subject: [PATCH 56/78] redirect to post when comment is delete insetead of non exsistant index page for comments --- app/controllers/comments_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 48a84bb..71bdf2c 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -39,8 +39,9 @@ def show end def destroy + @post = Post.find params[:post_id] @comment.destroy - redirect_to comments_path, notice: "Your comment was deleted." + redirect_to post_path(@post), notice: "Your comment was deleted." end From 6174a5f597ce5afe71b54a5e1f4d8516a72c74d9 Mon Sep 17 00:00:00 2001 From: Webdev Date: Wed, 20 Apr 2016 09:02:23 -0700 Subject: [PATCH 57/78] start on adding per user permissons by restricting posts passed by controller to thouse owned by the user --- app/controllers/application_controller.rb | 10 +++++----- app/controllers/posts_controller.rb | 7 ++++++- db/schema.rb | 2 ++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d018790..0f232fd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,10 +11,10 @@ def user_signed_in? session[:user_id].present? end helper_method :user_signed_in? - # - # def current_user - # @current_user ||= User.find session[:user_id] if user_signed_in? - # end - # helper_method :current_user + + def current_user + @current_user ||= User.find session[:user_id] if user_signed_in? + end + helper_method :current_user end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index a9812cb..35de22e 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,7 +1,8 @@ class PostsController < ApplicationController before_action :authenticate_user!, except: [:index, :show] - before_action(:find_post, {only: [:show, :edit, :update, :destroy]}) + before_action(:find_post, {only: [:show]}) + before_action(:find_owned_post, {only: [:edit, :update, :destroy]}) def index @@ -46,6 +47,10 @@ def destroy private + def find_owned_post + @post = current_user.posts.find params[:id] + end + def find_post @post = Post.find params[:id] end diff --git a/db/schema.rb b/db/schema.rb index 5263e53..894b08d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,3 +1,5 @@ + + # encoding: UTF-8 # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to From c05082ab4dec6b5144dbd87436571541aca5a516 Mon Sep 17 00:00:00 2001 From: Webdev Date: Wed, 20 Apr 2016 09:02:37 -0700 Subject: [PATCH 58/78] Revert "start on adding per user permissons by restricting posts passed by controller to thouse owned by the user" Decided not to use this approch for even basic user permissions. This reverts commit 6174a5f597ce5afe71b54a5e1f4d8516a72c74d9. --- app/controllers/application_controller.rb | 10 +++++----- app/controllers/posts_controller.rb | 7 +------ db/schema.rb | 2 -- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0f232fd..d018790 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,10 +11,10 @@ def user_signed_in? session[:user_id].present? end helper_method :user_signed_in? - - def current_user - @current_user ||= User.find session[:user_id] if user_signed_in? - end - helper_method :current_user + # + # def current_user + # @current_user ||= User.find session[:user_id] if user_signed_in? + # end + # helper_method :current_user end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 35de22e..a9812cb 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,8 +1,7 @@ class PostsController < ApplicationController before_action :authenticate_user!, except: [:index, :show] - before_action(:find_post, {only: [:show]}) - before_action(:find_owned_post, {only: [:edit, :update, :destroy]}) + before_action(:find_post, {only: [:show, :edit, :update, :destroy]}) def index @@ -47,10 +46,6 @@ def destroy private - def find_owned_post - @post = current_user.posts.find params[:id] - end - def find_post @post = Post.find params[:id] end diff --git a/db/schema.rb b/db/schema.rb index 894b08d..5263e53 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,5 +1,3 @@ - - # encoding: UTF-8 # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to From aefb5389eb7656c576e14ef671b39a78fca750c3 Mon Sep 17 00:00:00 2001 From: Webdev Date: Fri, 22 Apr 2016 19:12:51 -0700 Subject: [PATCH 59/78] Add cancancan and use to manage blog post permissions --- Gemfile | 2 + Gemfile.lock | 2 + app/controllers/application_controller.rb | 11 ++--- app/controllers/posts_controller.rb | 6 +++ app/models/ability.rb | 51 +++++++++++++++++++++++ app/views/posts/show.html.slim | 3 +- 6 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 app/models/ability.rb diff --git a/Gemfile b/Gemfile index 1775fa5..b5ec699 100644 --- a/Gemfile +++ b/Gemfile @@ -34,6 +34,8 @@ gem 'bcrypt', '~> 3.1.7' gem 'slim' + gem 'cancancan' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' diff --git a/Gemfile.lock b/Gemfile.lock index 6eec5ff..8831f3e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -42,6 +42,7 @@ GEM debug_inspector (>= 0.0.1) builder (3.2.2) byebug (8.2.2) + cancancan (1.13.1) coffee-rails (4.1.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.1.x) @@ -175,6 +176,7 @@ PLATFORMS DEPENDENCIES bcrypt (~> 3.1.7) byebug + cancancan coffee-rails (~> 4.1.0) factory_girl_rails faker diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d018790..0b8eb52 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,10 +11,11 @@ def user_signed_in? session[:user_id].present? end helper_method :user_signed_in? - # - # def current_user - # @current_user ||= User.find session[:user_id] if user_signed_in? - # end - # helper_method :current_user + + # Cancancan's ability.rb will use this if it exsists, I think -- IDFS + def current_user + @current_user ||= User.find session[:user_id] if user_signed_in? + end + helper_method :current_user end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index a9812cb..bcfbeb5 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -2,6 +2,8 @@ class PostsController < ApplicationController before_action :authenticate_user!, except: [:index, :show] before_action(:find_post, {only: [:show, :edit, :update, :destroy]}) + before_action :authorize_post, only: [:edit, :update, :destroy] + def index @@ -46,6 +48,10 @@ def destroy private + def authorize_question + redirect_to root_path unless can? :manage, @post + end + def find_post @post = Post.find params[:id] end diff --git a/app/models/ability.rb b/app/models/ability.rb new file mode 100644 index 0000000..4151513 --- /dev/null +++ b/app/models/ability.rb @@ -0,0 +1,51 @@ +class Ability + include CanCan::Ability + + def initialize(user) + + user ||= User.new # guest user (not logged in) + + # TODO Add admin. Commented out until admin column is added to users table + # if user.admin? + # can :manage, :all + # else + # can :read, :all + # end + + can :manage, Post do |post| + post.user == user + end + + # can :manage, Answers do |ans| + # ans.user == user + # end + + + # Define abilities for the passed in user here. For example: + # + # user ||= User.new # guest user (not logged in) + # if user.admin? + # can :manage, :all + # else + # can :read, :all + # end + # + # The first argument to `can` is the action you are giving the user + # permission to do. + # If you pass :manage it will apply to every action. Other common actions + # here are :read, :create, :update and :destroy. + # + # The second argument is the resource the user can perform the action on. + # If you pass :all it will apply to every resource. Otherwise pass a Ruby + # class of the resource. + # + # The third argument is an optional hash of conditions to further filter the + # objects. + # For example, here the user can only update published articles. + # + # can :update, Article, :published => true + # + # See the wiki for details: + # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities + end +end diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index 0d12292..477fef8 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -6,9 +6,10 @@ div = @post.category_or_unknown div = @post.body -- if user_signed_in? +- if can? :edit, @post div = link_to "Edit", edit_post_path(@post) +- if can? :destroy, @post div = link_to "Delete", post_path(@post), method: :delete, data: {confirm: "Are you sure you want to delete this post?"} From fbd299deb3a573fc9542c04c832f510e37b2470b Mon Sep 17 00:00:00 2001 From: Webdev Date: Fri, 22 Apr 2016 19:26:43 -0700 Subject: [PATCH 60/78] Rename confusing method name. Is purly a refactor --- app/controllers/posts_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index bcfbeb5..89a259a 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -42,13 +42,13 @@ def show end def destroy - @post.destroy + @post.destroyapp/controllers/posts_controller.rb redirect_to posts_path, notice: "The blog post #{@post.title} was deleted." end private - def authorize_question + def authorize_post redirect_to root_path unless can? :manage, @post end From 6b956baa9ff9a586ce66fe1e5c1a922a9176439f Mon Sep 17 00:00:00 2001 From: Webdev Date: Fri, 22 Apr 2016 23:52:46 -0700 Subject: [PATCH 61/78] fixes flash alert and wierd pasting typo --- app/controllers/posts_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 89a259a..2c7de72 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -42,13 +42,14 @@ def show end def destroy - @post.destroyapp/controllers/posts_controller.rb + @post.destroy redirect_to posts_path, notice: "The blog post #{@post.title} was deleted." end private def authorize_post + flash[:alert] ="You do not have permission to delete the blog post #{@post.title}" redirect_to root_path unless can? :manage, @post end From 457ffe5b4c334e2dc3d13b6f63300a3f13628710 Mon Sep 17 00:00:00 2001 From: Webdev Date: Fri, 22 Apr 2016 23:59:11 -0700 Subject: [PATCH 62/78] add cancancan authoriztion for comments --- app/controllers/comments_controller.rb | 8 ++++++++ app/models/ability.rb | 6 +++--- app/views/posts/show.html.slim | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 71bdf2c..7eb7d8b 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -2,6 +2,9 @@ class CommentsController < ApplicationController before_action :authenticate_user! before_action(:find_comment, {only: [:show, :edit, :update, :destroy]}) + before_action :authorize_comment, only: [:edit, :update, :destroy] + + def index @comments = Comment.all @@ -51,6 +54,11 @@ def find_comment @comment = Comment.find params[:id] end + def authorize_comment + flash[:alert] "You do not have permission to delete this comment" + redirect_to root_path unless can? :manage, @pcomment + end + def comment_params params.require(:comment).permit(:body) end diff --git a/app/models/ability.rb b/app/models/ability.rb index 4151513..ed6d3e4 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -16,9 +16,9 @@ def initialize(user) post.user == user end - # can :manage, Answers do |ans| - # ans.user == user - # end + can :manage, Comment do |comment| + comment.user == user || comment.post.user == user + end # Define abilities for the passed in user here. For example: diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index 477fef8..058ed2e 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -23,7 +23,7 @@ div h3 Comments - @post.comments.each do |comment| = comment.body - - if user_signed_in? + - if can? :delete, @post = link_to "delete", post_comment_path(@post, comment), data: {confirm: "Are you sure?"}, method: :delete From 9f4ad1666cd7655bd31da5fc0c2f787bf4862210 Mon Sep 17 00:00:00 2001 From: Webdev Date: Wed, 27 Apr 2016 17:53:06 -0700 Subject: [PATCH 63/78] unfinished users rspec tests --- spec/controllers/users_controller_spec.rb | 26 +++++++++++++++++++++++ spec/factories/users.rb | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index e2c3d3b..92119de 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -2,4 +2,30 @@ RSpec.describe UsersController, type: :controller do + # describe "#new" do + # get :new + # end + + describe "#create" do + def valid_request + post :create, user: FactoryGirl.attributes_for(:user) + end + + describe "with valid attributes" do + it "adds user to session" + + it "redirects_to the root path" do + valid_request + expect(response).to redirect_to root_path + end + end + + describe "with invalid attributes" do + it "doesn't add a user to session" + + it "redirects_to the root path" do + expect(response).to render_template :new + end + end + end end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 0c122b0..5ac0268 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -2,7 +2,7 @@ factory :user do first_name "MyString" last_name "MyString" - email "MyString" + email "MyString@email.com" password_digest "MyString" end end From 6f68c92334b4c0875458a743662aef481533ec62 Mon Sep 17 00:00:00 2001 From: Webdev Date: Wed, 27 Apr 2016 18:11:47 -0700 Subject: [PATCH 64/78] add a user factory --- spec/factories/users.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 5ac0268..d6cf6ee 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -1,8 +1,8 @@ FactoryGirl.define do factory :user do - first_name "MyString" - last_name "MyString" - email "MyString@email.com" - password_digest "MyString" + first_name { Faker::Name.first_name } + last_name { Faker::Name.last_name } + sequence(:email) { |n| Faker::Internet.email.gsub("@", "-#{n}@") } + password { Faker::Internet.password } end end From ff5079170733f744b9c23584bb772c11757ce7ea Mon Sep 17 00:00:00 2001 From: Webdev Date: Wed, 27 Apr 2016 18:28:41 -0700 Subject: [PATCH 65/78] add rspec test for users controller --- spec/controllers/users_controller_spec.rb | 50 +++++++++++++++++++---- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 92119de..d6ac064 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -2,9 +2,17 @@ RSpec.describe UsersController, type: :controller do - # describe "#new" do - # get :new - # end + describe "#new" do + it "renders the new template" do + get :new + expect(response).to render_template(:new) + end + + it "assigns a new user variable" do + get :new + expect(assigns(:user)).to be_a_new(User) + end + end describe "#create" do def valid_request @@ -12,20 +20,46 @@ def valid_request end describe "with valid attributes" do - it "adds user to session" - it "redirects_to the root path" do + it "redirects to the home page" do + valid_request + expect(response).to redirect_to(root_path) + end + + it "adds the new user to session" do + valid_request + expect(session[:user_id]).to eq(User.last.id) + end + + it "adds a record to the database" do + expect { valid_request }.to change { User.count }.by(1) + end + + it "redirects_to the home page" do valid_request expect(response).to redirect_to root_path end end describe "with invalid attributes" do - it "doesn't add a user to session" + def invalid_request + post :create, user: FactoryGirl.attributes_for(:user).merge(email: nil) + end - it "redirects_to the root path" do - expect(response).to render_template :new + it "doesn't add a user to session" do + invalid_request + expect(session[:user_id]).to eq(nil) end + + it "doesn't add a record to the database" do + expect { invalid_request }.to change { User.count }.by(0) + end + + it "renders the new template" do + invalid_request + expect(response).to render_template(:new) + end + end end end From c99be8edb3b4c5459f0312ea3e6d0cd65025847f Mon Sep 17 00:00:00 2001 From: Ian Simpson Date: Sun, 15 May 2016 11:34:35 -0700 Subject: [PATCH 66/78] add feature to allow a user favourite blog posts and to view all their favourites. Used a _partial so that ajax refresh could be implimented later --- app/assets/javascripts/favourites.coffee | 3 +++ app/assets/stylesheets/favourites.scss | 3 +++ app/controllers/favourites_controller.rb | 26 +++++++++++++++++++ app/helpers/favourites_helper.rb | 2 ++ app/models/favourite.rb | 6 +++++ app/models/post.rb | 10 +++++++ app/models/user.rb | 2 ++ app/views/favourites/_favourite.html.erb | 5 ++++ app/views/favourites/index.html.slim | 5 ++++ app/views/layouts/application.html.erb | 1 + app/views/posts/show.html.slim | 2 ++ config/routes.rb | 2 ++ .../20160515170755_create_favourites.rb | 10 +++++++ db/schema.rb | 20 +++++++++----- .../controllers/favourites_controller_spec.rb | 5 ++++ spec/factories/favourites.rb | 6 +++++ spec/helpers/favourites_helper_spec.rb | 15 +++++++++++ spec/models/favourite_spec.rb | 5 ++++ 18 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 app/assets/javascripts/favourites.coffee create mode 100644 app/assets/stylesheets/favourites.scss create mode 100644 app/controllers/favourites_controller.rb create mode 100644 app/helpers/favourites_helper.rb create mode 100644 app/models/favourite.rb create mode 100644 app/views/favourites/_favourite.html.erb create mode 100644 app/views/favourites/index.html.slim create mode 100644 db/migrate/20160515170755_create_favourites.rb create mode 100644 spec/controllers/favourites_controller_spec.rb create mode 100644 spec/factories/favourites.rb create mode 100644 spec/helpers/favourites_helper_spec.rb create mode 100644 spec/models/favourite_spec.rb diff --git a/app/assets/javascripts/favourites.coffee b/app/assets/javascripts/favourites.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/favourites.coffee @@ -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/ diff --git a/app/assets/stylesheets/favourites.scss b/app/assets/stylesheets/favourites.scss new file mode 100644 index 0000000..66cc292 --- /dev/null +++ b/app/assets/stylesheets/favourites.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the favourites controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/favourites_controller.rb b/app/controllers/favourites_controller.rb new file mode 100644 index 0000000..8948288 --- /dev/null +++ b/app/controllers/favourites_controller.rb @@ -0,0 +1,26 @@ +class FavouritesController < ApplicationController + before_action :authenticate_user! + + def create + favourite = Favourite.new + post = Post.find params[:post_id] + favourite.post = post + favourite.user = current_user + if favourite.save + redirect_to post_path(post), notice: "Thanks for favouriting!" + else + redirect_to post_path(post), alert: "Can't favourite! Have you already favourited?" + end + end + + def destroy + post = Post.find params[:post_id] + favourite = current_user.favourites.find params[:id] + favourite.destroy + redirect_to post, notice: "Favourite removed!" + end + + def index + @favourites = current_user.favourites.all + end +end diff --git a/app/helpers/favourites_helper.rb b/app/helpers/favourites_helper.rb new file mode 100644 index 0000000..907962e --- /dev/null +++ b/app/helpers/favourites_helper.rb @@ -0,0 +1,2 @@ +module FavouritesHelper +end diff --git a/app/models/favourite.rb b/app/models/favourite.rb new file mode 100644 index 0000000..40ae00c --- /dev/null +++ b/app/models/favourite.rb @@ -0,0 +1,6 @@ +class Favourite < ActiveRecord::Base + belongs_to :user + belongs_to :post + + validates :post_id, uniqueness: {scope: :user_id} +end diff --git a/app/models/post.rb b/app/models/post.rb index 0e1baec..61dd28a 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -7,6 +7,8 @@ class Post < ActiveRecord::Base belongs_to :category belongs_to :user + has_many :favourites, dependent: :destroy + has_many :favouriting_users, through: :favourites, source: :users def body_snippet body.length > 99 ? "#{self.body[0...99]}..." : body @@ -16,4 +18,12 @@ def category_or_unknown category ? category.title : "Unknown Category" end + def favourite_for(user) + favourites.find_by_user_id(user.id) + end + + def favourited_by?(user) + favourites.find_by_user_id(user.id).present? + end + end diff --git a/app/models/user.rb b/app/models/user.rb index c963e8e..788b374 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -12,4 +12,6 @@ class User < ActiveRecord::Base validates :password, presence: true + has_many :favourites, dependent: :destroy + has_many :favourite_posts, through: :favourites, source: :post end diff --git a/app/views/favourites/_favourite.html.erb b/app/views/favourites/_favourite.html.erb new file mode 100644 index 0000000..ab7dfb4 --- /dev/null +++ b/app/views/favourites/_favourite.html.erb @@ -0,0 +1,5 @@ +<% if user_signed_in? && @post.favourited_by?(current_user)%> + <%= link_to "Remove Favourite", post_favourite_path(@post, @post.favourite_for(current_user)), method: :delete %> +<% else %> + <%= link_to "Favourite Product", post_favourites_path(@post), method: :post %> +<% end %> diff --git a/app/views/favourites/index.html.slim b/app/views/favourites/index.html.slim new file mode 100644 index 0000000..5fa22d7 --- /dev/null +++ b/app/views/favourites/index.html.slim @@ -0,0 +1,5 @@ +h1 Your Favourite Products + +- @favourites.each do |favourite| + div + = link_to favourite.post.title, post_path(favourite.post) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index f9adbad..983fe77 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -12,6 +12,7 @@ <%= link_to("About", about_path, {class: "menu-item"}) %> <%= link_to("Blog Posts", posts_path, {class: "menu-item"}) %> <% if user_signed_in? %> + <%= link_to "Favourite Posts", favourites_path, {class: "menu-item"} %> <%= link_to("Log Out", sessions_path, {class: "menu-item", method: :delete}) %> <% else %> <%= link_to "Sign In", new_session_path %> diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index 058ed2e..4a831df 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -20,6 +20,8 @@ div = form_helper.text_area :body = form_helper.submit += render 'favourites/favourite' + h3 Comments - @post.comments.each do |comment| = comment.body diff --git a/config/routes.rb b/config/routes.rb index 2e6f338..237bf73 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,7 +6,9 @@ resources :posts do resources :comments + resources :favourites, only: [:create, :destroy] end + resources :favourites, only: [:index] resources :users diff --git a/db/migrate/20160515170755_create_favourites.rb b/db/migrate/20160515170755_create_favourites.rb new file mode 100644 index 0000000..bf98360 --- /dev/null +++ b/db/migrate/20160515170755_create_favourites.rb @@ -0,0 +1,10 @@ +class CreateFavourites < ActiveRecord::Migration + def change + create_table :favourites do |t| + t.references :user, index: true, foreign_key: true + t.references :post, index: true, foreign_key: true + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 5263e53..840f3ab 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160419000537) do +ActiveRecord::Schema.define(version: 20160515170755) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -27,11 +27,9 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "post_id" - t.integer "contact_id" t.integer "user_id" end - add_index "comments", ["contact_id"], name: "index_comments_on_contact_id", using: :btree add_index "comments", ["user_id"], name: "index_comments_on_user_id", using: :btree create_table "contacts", force: :cascade do |t| @@ -43,18 +41,26 @@ t.datetime "updated_at", null: false end + create_table "favourites", force: :cascade do |t| + t.integer "user_id" + t.integer "post_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "favourites", ["post_id"], name: "index_favourites_on_post_id", using: :btree + add_index "favourites", ["user_id"], name: "index_favourites_on_user_id", using: :btree + create_table "posts", force: :cascade do |t| t.string "title" t.text "body" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "category_id" - t.integer "contact_id" t.integer "user_id" end add_index "posts", ["category_id"], name: "index_posts_on_category_id", using: :btree - add_index "posts", ["contact_id"], name: "index_posts_on_contact_id", using: :btree add_index "posts", ["user_id"], name: "index_posts_on_user_id", using: :btree create_table "users", force: :cascade do |t| @@ -66,9 +72,9 @@ t.datetime "updated_at", null: false end - add_foreign_key "comments", "contacts" add_foreign_key "comments", "users" + add_foreign_key "favourites", "posts" + add_foreign_key "favourites", "users" add_foreign_key "posts", "categories" - add_foreign_key "posts", "contacts" add_foreign_key "posts", "users" end diff --git a/spec/controllers/favourites_controller_spec.rb b/spec/controllers/favourites_controller_spec.rb new file mode 100644 index 0000000..b163a08 --- /dev/null +++ b/spec/controllers/favourites_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe FavouritesController, type: :controller do + +end diff --git a/spec/factories/favourites.rb b/spec/factories/favourites.rb new file mode 100644 index 0000000..aa92c59 --- /dev/null +++ b/spec/factories/favourites.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :favourite do + user nil + post nil + end +end diff --git a/spec/helpers/favourites_helper_spec.rb b/spec/helpers/favourites_helper_spec.rb new file mode 100644 index 0000000..8c67e2c --- /dev/null +++ b/spec/helpers/favourites_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the FavouritesHelper. For example: +# +# describe FavouritesHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe FavouritesHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/favourite_spec.rb b/spec/models/favourite_spec.rb new file mode 100644 index 0000000..271aef4 --- /dev/null +++ b/spec/models/favourite_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Favourite, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From 4b9d4ebb63f2bf3d8edc82c4f13138d87d4335ce Mon Sep 17 00:00:00 2001 From: Ian Simpson Date: Sun, 15 May 2016 14:02:27 -0700 Subject: [PATCH 67/78] fix comment permissions and make respond propery to an ivalid comment attempt --- app/controllers/comments_controller.rb | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 7eb7d8b..f867714 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -5,7 +5,6 @@ class CommentsController < ApplicationController before_action :authorize_comment, only: [:edit, :update, :destroy] - def index @comments = Comment.all end @@ -23,20 +22,20 @@ def create redirect_to post_path(@post) else flash[:alert] = "Error! Comment not created" - render :new + render "/posts/show" end end def edit end - def update - if @comment.update comment_params - redirect_to comment_path(@comment), notice: "Comment updated!" - else - render :edit - end - end + # def update + # if @comment.update comment_params + # redirect_to comment_path(@comment), notice: "Comment updated!" + # else + # render :edit + # end + # end def show end @@ -55,8 +54,8 @@ def find_comment end def authorize_comment - flash[:alert] "You do not have permission to delete this comment" - redirect_to root_path unless can? :manage, @pcomment + flash[:alert] = "You do not have permission to manage this comment" + redirect_to root_path unless can? :manage, @comment end def comment_params From ceb1a891cf0dc1ff6d90b310cc15ffb0ab21f18c Mon Sep 17 00:00:00 2001 From: Ian Simpson Date: Sun, 15 May 2016 13:22:03 -0700 Subject: [PATCH 68/78] add bootsrap gem and enable --- Gemfile | 2 ++ Gemfile.lock | 8 +++++++- app/assets/stylesheets/application.css | 15 --------------- app/assets/stylesheets/application.scss | 2 ++ 4 files changed, 11 insertions(+), 16 deletions(-) delete mode 100644 app/assets/stylesheets/application.css create mode 100644 app/assets/stylesheets/application.scss diff --git a/Gemfile b/Gemfile index b5ec699..16ee7fe 100644 --- a/Gemfile +++ b/Gemfile @@ -36,6 +36,8 @@ gem 'bcrypt', '~> 3.1.7' gem 'cancancan' + gem 'bootstrap-sass' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' diff --git a/Gemfile.lock b/Gemfile.lock index 8831f3e..3c82d3a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -37,9 +37,14 @@ GEM thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) arel (6.0.3) + autoprefixer-rails (6.3.6) + execjs bcrypt (3.1.11) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) + bootstrap-sass (3.3.6) + autoprefixer-rails (>= 5.2.1) + sass (>= 3.3.4) builder (3.2.2) byebug (8.2.2) cancancan (1.13.1) @@ -175,6 +180,7 @@ PLATFORMS DEPENDENCIES bcrypt (~> 3.1.7) + bootstrap-sass byebug cancancan coffee-rails (~> 4.1.0) @@ -194,4 +200,4 @@ DEPENDENCIES web-console (~> 2.0) BUNDLED WITH - 1.11.2 + 1.12.2 diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css deleted file mode 100644 index f9cd5b3..0000000 --- a/app/assets/stylesheets/application.css +++ /dev/null @@ -1,15 +0,0 @@ -/* - * This is a manifest file that'll be compiled into application.css, which will include all the files - * listed below. - * - * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, - * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path. - * - * You're free to add application-wide styles to this file and they'll appear at the bottom of the - * compiled file so the styles you add here take precedence over styles defined in any styles - * defined in the other CSS/SCSS files in this directory. It is generally better to create a new - * file per style scope. - * - *= require_tree . - *= require_self - */ diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss new file mode 100644 index 0000000..40987ec --- /dev/null +++ b/app/assets/stylesheets/application.scss @@ -0,0 +1,2 @@ +@import "bootstrap-sprockets"; +@import "bootstrap" From f590f3b160f7373f895fdd2d173e1f3844f854a4 Mon Sep 17 00:00:00 2001 From: Ian Simpson Date: Sun, 15 May 2016 13:43:04 -0700 Subject: [PATCH 69/78] add bootstrap styling to navbar and put the everything in a container. --- app/assets/stylesheets/application.css | 15 +++++++ ...tion.scss => bootstrap_and_overrides.scss} | 2 + app/views/layouts/application.html.erb | 41 +++++++++++-------- app/views/posts/index.html.erb | 1 - 4 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 app/assets/stylesheets/application.css rename app/assets/stylesheets/{application.scss => bootstrap_and_overrides.scss} (63%) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css new file mode 100644 index 0000000..f9cd5b3 --- /dev/null +++ b/app/assets/stylesheets/application.css @@ -0,0 +1,15 @@ +/* + * This is a manifest file that'll be compiled into application.css, which will include all the files + * listed below. + * + * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, + * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path. + * + * You're free to add application-wide styles to this file and they'll appear at the bottom of the + * compiled file so the styles you add here take precedence over styles defined in any styles + * defined in the other CSS/SCSS files in this directory. It is generally better to create a new + * file per style scope. + * + *= require_tree . + *= require_self + */ diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/bootstrap_and_overrides.scss similarity index 63% rename from app/assets/stylesheets/application.scss rename to app/assets/stylesheets/bootstrap_and_overrides.scss index 40987ec..c80c55e 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/bootstrap_and_overrides.scss @@ -1,2 +1,4 @@ +body { padding-top: 50px; } + @import "bootstrap-sprockets"; @import "bootstrap" diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 983fe77..a021efb 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -7,23 +7,30 @@ <%= csrf_meta_tags %> - -
- <%= notice || alert %> -
- -<%= yield %> +
+ +
+ <%= notice || alert %> +
+ <%= yield %> +
diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 9d1eace..7477a92 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -1,5 +1,4 @@

Blog Posts

- <% @posts.each do |p| %>
<%= link_to p.title, post_path(p) %> From f492423d5854f8d2491fb72687d67087b9099510 Mon Sep 17 00:00:00 2001 From: Ian Simpson Date: Sun, 15 May 2016 13:50:05 -0700 Subject: [PATCH 70/78] add styling to flash notices --- app/assets/stylesheets/bootstrap_and_overrides.scss | 2 +- app/views/layouts/application.html.erb | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/assets/stylesheets/bootstrap_and_overrides.scss b/app/assets/stylesheets/bootstrap_and_overrides.scss index c80c55e..d3e0995 100644 --- a/app/assets/stylesheets/bootstrap_and_overrides.scss +++ b/app/assets/stylesheets/bootstrap_and_overrides.scss @@ -1,4 +1,4 @@ -body { padding-top: 50px; } +body { padding-top: 70px; } @import "bootstrap-sprockets"; @import "bootstrap" diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index a021efb..1b90b46 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -27,7 +27,11 @@
- <%= notice || alert %> + <% if notice %> +
<%= notice %>
+ <% elsif alert %> +
<%= alert %>
+ <% end %>
<%= yield %> From 39cd95836da8ac44d5d54416dc998a2a400d9d88 Mon Sep 17 00:00:00 2001 From: Ian Simpson Date: Sun, 15 May 2016 14:34:40 -0700 Subject: [PATCH 71/78] assign the signed in user to be the creator of a post --- app/controllers/comments_controller.rb | 1 + app/controllers/posts_controller.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index f867714..3160d05 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -17,6 +17,7 @@ def create @post = Post.find params[:post_id] @comment = Comment.new(comment_params) @comment.post = @post + @comment.user = current_user if @comment.save flash[:notice] = "Comment Created" redirect_to post_path(@post) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 2c7de72..9f6c165 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -16,6 +16,7 @@ def new def create @post = Post.new(post_params) + @post.user = current_user if @post.save flash[:notice] = "Post Created" From 657b74432fdf86d2e909a44091dd28d0fc418130 Mon Sep 17 00:00:00 2001 From: Ian Simpson Date: Sun, 15 May 2016 14:40:13 -0700 Subject: [PATCH 72/78] add display of user for each comment and fix comment styling --- app/views/favourites/_favourite.html.erb | 2 +- app/views/posts/show.html.slim | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/views/favourites/_favourite.html.erb b/app/views/favourites/_favourite.html.erb index ab7dfb4..eb6d11c 100644 --- a/app/views/favourites/_favourite.html.erb +++ b/app/views/favourites/_favourite.html.erb @@ -1,5 +1,5 @@ <% if user_signed_in? && @post.favourited_by?(current_user)%> <%= link_to "Remove Favourite", post_favourite_path(@post, @post.favourite_for(current_user)), method: :delete %> <% else %> - <%= link_to "Favourite Product", post_favourites_path(@post), method: :post %> + <%= link_to "Favourite Post", post_favourites_path(@post), method: :post %> <% end %> diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index 4a831df..65472c8 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -12,19 +12,23 @@ div - if can? :destroy, @post div = link_to "Delete", post_path(@post), method: :delete, data: {confirm: "Are you sure you want to delete this post?"} - -= @comment.errors.full_messages.join(", ") += render 'favourites/favourite' + +h3 Comments += @comment.errors.full_messages.join(", ") = form_for @comment, url: post_comments_path(@post) do |f| = form_for [@post, @comment] do |form_helper| - = form_helper.text_area :body + = form_helper.text_area :body, class: "form-control" = form_helper.submit -= render 'favourites/favourite' -h3 Comments - @post.comments.each do |comment| - = comment.body + div + p + = comment.body + | by + = comment.user.email - if can? :delete, @post = link_to "delete", post_comment_path(@post, comment), data: {confirm: "Are you sure?"}, From 4cbbc676ce5573f987f2cf80eb41f51a9906efd2 Mon Sep 17 00:00:00 2001 From: Ian Simpson Date: Sun, 15 May 2016 15:26:50 -0700 Subject: [PATCH 73/78] add icons to edit delete action for comments and posts. Will remove text later but was checking to see if could just be hidden --- app/models/ability.rb | 2 +- app/views/favourites/_favourite.html.erb | 4 ++-- app/views/posts/show.html.slim | 18 +++++++++++------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index ed6d3e4..8d60438 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -17,7 +17,7 @@ def initialize(user) end can :manage, Comment do |comment| - comment.user == user || comment.post.user == user + comment.user == user || comment.post.user == user end diff --git a/app/views/favourites/_favourite.html.erb b/app/views/favourites/_favourite.html.erb index eb6d11c..fe1dfba 100644 --- a/app/views/favourites/_favourite.html.erb +++ b/app/views/favourites/_favourite.html.erb @@ -1,5 +1,5 @@ <% if user_signed_in? && @post.favourited_by?(current_user)%> - <%= link_to "Remove Favourite", post_favourite_path(@post, @post.favourite_for(current_user)), method: :delete %> + <%= link_to "Unfavourite", post_favourite_path(@post, @post.favourite_for(current_user)), method: :delete, class: "glyphicon glyphicon-star" %> <% else %> - <%= link_to "Favourite Post", post_favourites_path(@post), method: :post %> + <%= link_to "Favourite", post_favourites_path(@post), method: :post, class: "glyphicon glyphicon-star-empty" %> <% end %> diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index 65472c8..c868b08 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -8,10 +8,10 @@ div = @post.body - if can? :edit, @post div - = link_to "Edit", edit_post_path(@post) + = link_to "Edit", edit_post_path(@post), class: "glyphicon glyphicon-edit" - if can? :destroy, @post div - = link_to "Delete", post_path(@post), method: :delete, data: {confirm: "Are you sure you want to delete this post?"} + = link_to "Delete", post_path(@post), method: :delete, data: {confirm: "Are you sure you want to delete this post?"}, class: "glyphicon glyphicon-trash" = render 'favourites/favourite' @@ -25,11 +25,15 @@ h3 Comments - @post.comments.each do |comment| div - p + span = comment.body | by = comment.user.email - - if can? :delete, @post - = link_to "delete", post_comment_path(@post, comment), - data: {confirm: "Are you sure?"}, - method: :delete + | + span + - if can? :delete, comment + = link_to "delete", post_comment_path(@post, comment), + data: {confirm: "Are you sure?"}, + method: :delete, + class: "glyphicon glyphicon-remove" + From 5e140d764cc76794ce171ebbb392f56343be7e0b Mon Sep 17 00:00:00 2001 From: Ian Simpson Date: Sun, 15 May 2016 15:45:45 -0700 Subject: [PATCH 74/78] add new comment link to navbar and minor styling. I'll change to simple for later and improve the styling then --- app/views/layouts/application.html.erb | 1 + app/views/posts/_post_form.html.slim | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 1b90b46..c45cb01 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -17,6 +17,7 @@