From 0895310f8d92b0a0959c9acf1774b989a29a85fe Mon Sep 17 00:00:00 2001 From: Holger_Tidemand Date: Thu, 22 Mar 2018 21:02:32 +0100 Subject: [PATCH 01/12] started with feature test --- app/models/category.rb | 2 ++ .../20180322195300_create_categories.rb | 9 ++++++ db/schema.rb | 14 ++++++++- features/step_definitions/category_steps.rb | 13 +++++++++ .../step_definitions/delete_article_steps.rb | 0 features/support/env.rb | 2 +- ...r_can_asign_categories_to_articles.feature | 29 +++++++++++++++++++ spec/factories/categories.rb | 5 ++++ spec/models/category_spec.rb | 5 ++++ 9 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 app/models/category.rb create mode 100644 db/migrate/20180322195300_create_categories.rb create mode 100644 features/step_definitions/category_steps.rb delete mode 100644 features/step_definitions/delete_article_steps.rb create mode 100644 features/user_can_asign_categories_to_articles.feature create mode 100644 spec/factories/categories.rb create mode 100644 spec/models/category_spec.rb diff --git a/app/models/category.rb b/app/models/category.rb new file mode 100644 index 0000000..54cb6ae --- /dev/null +++ b/app/models/category.rb @@ -0,0 +1,2 @@ +class Category < ApplicationRecord +end diff --git a/db/migrate/20180322195300_create_categories.rb b/db/migrate/20180322195300_create_categories.rb new file mode 100644 index 0000000..6ccc391 --- /dev/null +++ b/db/migrate/20180322195300_create_categories.rb @@ -0,0 +1,9 @@ +class CreateCategories < ActiveRecord::Migration[5.2] + def change + create_table :categories do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 9591048..ad852e6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_03_18_155404) do +ActiveRecord::Schema.define(version: 2018_03_22_195300) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -20,6 +20,15 @@ t.text "content" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.float "latitude" + t.float "longitude" + t.string "address" + end + + create_table "categories", force: :cascade do |t| + t.string "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "comments", force: :cascade do |t| @@ -45,6 +54,9 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "subscriber", default: false + t.float "latitude" + t.float "longitude" + t.text "address" t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end diff --git a/features/step_definitions/category_steps.rb b/features/step_definitions/category_steps.rb new file mode 100644 index 0000000..1bf4e1b --- /dev/null +++ b/features/step_definitions/category_steps.rb @@ -0,0 +1,13 @@ +Given("the following categories exists") do |table| + table.hashes.each do |category| + create(:category, category) + end +end + +Given("I am on the Create Article page") do + visit new_article_path +end + +Then("I select {string} from {string}") do |category, select_box| + select(category, from: select_box) +end diff --git a/features/step_definitions/delete_article_steps.rb b/features/step_definitions/delete_article_steps.rb deleted file mode 100644 index e69de29..0000000 diff --git a/features/support/env.rb b/features/support/env.rb index 2c94f64..0b989c8 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -12,7 +12,7 @@ end Cucumber::Rails::Database.javascript_strategy = :truncation - +World(FactoryBot::Syntax::Methods) Chromedriver.set_version '2.36' diff --git a/features/user_can_asign_categories_to_articles.feature b/features/user_can_asign_categories_to_articles.feature new file mode 100644 index 0000000..f15e504 --- /dev/null +++ b/features/user_can_asign_categories_to_articles.feature @@ -0,0 +1,29 @@ +Feature: Better orginized websitet with categories + As a subscriber + In order to be able to find content of interest + I would like to be able to filter articles by category + + + +Background: + Given following article exists + | title | content | + | A Whole New World | A new fantastic point of view | + And the following user exists + | email | password | + | harald@norge.no | OsloOslo123 | + And the following categories exists + | name | + | Fashion | + And I am logged in as "harald@norge.no" + +Scenario: User creates a new article and asign it to a category + Given I am on the Create Article page + Then I fill in "Title" with "A Whole New Article" + And I fill in "Content" with "A new fantastic Article" + And I select "Fashion" from "categories" + When I click "Create Article" button + Then I should be on "A Whole New Article" page + And I should see "A Whole New Article" + And I should see "A new fantastic Article" + And I should see "Fashion" diff --git a/spec/factories/categories.rb b/spec/factories/categories.rb new file mode 100644 index 0000000..0524aca --- /dev/null +++ b/spec/factories/categories.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :category do + name "MyString" + end +end diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb new file mode 100644 index 0000000..0c9fefa --- /dev/null +++ b/spec/models/category_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Category, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From f786baac1b3a7d9ea64741de1d073cf03af93706 Mon Sep 17 00:00:00 2001 From: Holger_Tidemand Date: Thu, 22 Mar 2018 22:27:24 +0100 Subject: [PATCH 02/12] adds ability to add and category to an article --- app/controllers/articles_controller.rb | 12 ++++++++++++ app/models/article.rb | 1 + app/models/category.rb | 1 + app/views/articles/new.html.haml | 4 ++++ app/views/articles/show.html.haml | 2 ++ ...22210143_create_join_table_article_category.rb | 8 ++++++++ db/schema.rb | 9 ++++++++- spec/models/article_spec.rb | 4 ++++ spec/models/category_spec.rb | 15 ++++++++++++++- spec/models/comment_spec.rb | 4 ++-- 10 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20180322210143_create_join_table_article_category.rb diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 4c40e52..70345c6 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -10,6 +10,7 @@ def new def create @article = Article.new(article_params) + add_categories_to_article if @article.save flash[:success] = "Article was successfully created." redirect_to @article @@ -47,6 +48,17 @@ def destroy private + def add_categories_to_article + binding.pry + categories = [] + categories << params[:article][:categories] + categories.each do |category_id| + category = Category.find_by(id: category_id) + @article.categories.include?(category) ? next : @article.categories << category + end + binding.pry + end + def article_params params[:article].permit(:title, :content) end diff --git a/app/models/article.rb b/app/models/article.rb index 1977a87..64455d1 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,4 +1,5 @@ class Article < ApplicationRecord validates :title, :content, presence: true has_many :comments, dependent: :destroy + has_and_belongs_to_many :categories end diff --git a/app/models/category.rb b/app/models/category.rb index 54cb6ae..0e5ecaa 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,2 +1,3 @@ class Category < ApplicationRecord + has_and_belongs_to_many :artilces end diff --git a/app/views/articles/new.html.haml b/app/views/articles/new.html.haml index f2168b1..0f4907b 100644 --- a/app/views/articles/new.html.haml +++ b/app/views/articles/new.html.haml @@ -9,3 +9,7 @@ = form.text_area :content, id: :article_content %p = form.submit value: "Create Article" + %p + = form.label :categories + %br/ + = form.select(:categories, Category.all.collect{|category| [category.name, category.id] }, {}, {multipe: true, id: 'categories'}) diff --git a/app/views/articles/show.html.haml b/app/views/articles/show.html.haml index c064069..fe8e125 100644 --- a/app/views/articles/show.html.haml +++ b/app/views/articles/show.html.haml @@ -16,6 +16,8 @@ = form.text_area :content, cols: 50, rows: 5, id: 'comment_content' %p= form.submit 'Submit comment' +- @article.categories.each do |category| + %p= category.name %p = link_to 'Edit Article', edit_article_path(@article) = link_to 'Delete Article', article_path(@article), method: :delete diff --git a/db/migrate/20180322210143_create_join_table_article_category.rb b/db/migrate/20180322210143_create_join_table_article_category.rb new file mode 100644 index 0000000..09291ae --- /dev/null +++ b/db/migrate/20180322210143_create_join_table_article_category.rb @@ -0,0 +1,8 @@ +class CreateJoinTableArticleCategory < ActiveRecord::Migration[5.2] + def change + create_join_table :articles, :categories do |t| + # t.index [:article_id, :category_id] + # t.index [:category_id, :article_id] + end + end +end diff --git a/db/schema.rb b/db/schema.rb index ad852e6..70602a8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_03_22_195300) do +ActiveRecord::Schema.define(version: 2018_03_22_210143) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -25,6 +25,13 @@ t.string "address" end + create_table "articles_categories", id: false, force: :cascade do |t| + t.bigint "article_id", null: false + t.bigint "category_id", null: false + t.index ["article_id", "category_id"], name: "index_articles_categories_on_article_id_and_category_id" + t.index ["category_id", "article_id"], name: "index_articles_categories_on_category_id_and_article_id" + end + create_table "categories", force: :cascade do |t| t.string "name" t.datetime "created_at", null: false diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index 63084a4..df57b2f 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -12,6 +12,10 @@ end end + describe "Assosiation" do + it {is_expected.to have_and_belong_to_many :categories} + end + describe 'Validations' do it { is_expected.to validate_presence_of :title } it { is_expected.to validate_presence_of :content } diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 0c9fefa..c98f4d9 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -1,5 +1,18 @@ require 'rails_helper' RSpec.describe Category, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + describe "Factory" do + it "should have a valid factory" do + article = create(:category) + expect(create(:category)).to be_valid + end + end + + describe "Assosiation" do + it {is_expected.to have_and_belong_to_many :articles} + end + + describe 'DB columns' do + it { is_expected.to have_db_column :name } + end end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index c7994c2..6e2ee48 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -13,8 +13,8 @@ describe "Factory" do it "should have a valid factory" do - article = FactoryBot.create(:article) - expect(FactoryBot.create(:comment, article: article)).to be_valid + article = create(:article) + expect(create(:comment, article: article)).to be_valid end end end From 80b55c8a8a0ae98daa74c454ac5fc219799fdd9e Mon Sep 17 00:00:00 2001 From: Holger_Tidemand Date: Fri, 23 Mar 2018 11:07:29 +0100 Subject: [PATCH 03/12] working on adding more then one category to an article --- app/controllers/articles_controller.rb | 8 ++++++-- app/views/articles/new.html.haml | 2 +- .../20180322210143_create_join_table_article_category.rb | 4 ++-- features/user_can_asign_categories_to_articles.feature | 4 ++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 70345c6..747f6bc 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -48,15 +48,19 @@ def destroy private + def find_articles_and_categories + @article = Article.find(params[:id]) + @categories = Category.all + end + def add_categories_to_article - binding.pry categories = [] categories << params[:article][:categories] + categories.each do |category_id| category = Category.find_by(id: category_id) @article.categories.include?(category) ? next : @article.categories << category end - binding.pry end def article_params diff --git a/app/views/articles/new.html.haml b/app/views/articles/new.html.haml index 0f4907b..feccdfd 100644 --- a/app/views/articles/new.html.haml +++ b/app/views/articles/new.html.haml @@ -12,4 +12,4 @@ %p = form.label :categories %br/ - = form.select(:categories, Category.all.collect{|category| [category.name, category.id] }, {}, {multipe: true, id: 'categories'}) + = form.select(:categories, Category.all.collect {|category| [category.name, category.id] }, {}, {multipe: true, id: 'categories'}) diff --git a/db/migrate/20180322210143_create_join_table_article_category.rb b/db/migrate/20180322210143_create_join_table_article_category.rb index 09291ae..2a4e795 100644 --- a/db/migrate/20180322210143_create_join_table_article_category.rb +++ b/db/migrate/20180322210143_create_join_table_article_category.rb @@ -1,8 +1,8 @@ class CreateJoinTableArticleCategory < ActiveRecord::Migration[5.2] def change create_join_table :articles, :categories do |t| - # t.index [:article_id, :category_id] - # t.index [:category_id, :article_id] + t.index [:article_id, :category_id] + t.index [:category_id, :article_id] end end end diff --git a/features/user_can_asign_categories_to_articles.feature b/features/user_can_asign_categories_to_articles.feature index f15e504..e1f1c57 100644 --- a/features/user_can_asign_categories_to_articles.feature +++ b/features/user_can_asign_categories_to_articles.feature @@ -3,8 +3,6 @@ Feature: Better orginized websitet with categories In order to be able to find content of interest I would like to be able to filter articles by category - - Background: Given following article exists | title | content | @@ -15,6 +13,7 @@ Background: And the following categories exists | name | | Fashion | + | Tech | And I am logged in as "harald@norge.no" Scenario: User creates a new article and asign it to a category @@ -22,6 +21,7 @@ Scenario: User creates a new article and asign it to a category Then I fill in "Title" with "A Whole New Article" And I fill in "Content" with "A new fantastic Article" And I select "Fashion" from "categories" + And I select "Tech" from "categories" When I click "Create Article" button Then I should be on "A Whole New Article" page And I should see "A Whole New Article" From cc6c2f679e9fd14f581e2637a8216bf020f040a7 Mon Sep 17 00:00:00 2001 From: Holger_Tidemand Date: Sat, 24 Mar 2018 15:21:42 +0100 Subject: [PATCH 04/12] refactored home controller for better readability --- app/controllers/home_controller.rb | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 063c2e8..4e2e2ea 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,24 +1,14 @@ class HomeController < ApplicationController before_action :get_coordinates, only: [:index] + GOTHENBURG = [57.700501, 11.975463] def index - if !@coordinates.empty? - user = create_guest_user - location = (current_user ? current_user.address : user.address) - @local_articles = Article.near(location, 20) - @articles = Article.all - else - @articles = Article.all - end - end - - def get_location - user = create_guest_user - current_user ? current_user.address : user.address + @local_articles = Article.near(current_user.address, 20) if !@coordinates.empty? + @articles = Article.all end def set_edition - if User.near([57.700501, 11.975463], 50).include? current_user + if User.near(GOTHENBURG, 50).include? current_user @edition = 'Gothenburg Edition' else @edition = 'Rest of Sweden Edition' @@ -38,11 +28,8 @@ def get_coordinates end def update_user_location - if current_user - current_user.latitude = @coordinates.values.first - current_user.longitude = @coordinates.values.second - current_user.save - end + current_user.latitude = @coordinates.values.first + current_user.longitude = @coordinates.values.second + current_user.save end - end From 40541016e4dd779fa309f8c1bb546104faa066f4 Mon Sep 17 00:00:00 2001 From: Holger_Tidemand Date: Sat, 24 Mar 2018 15:57:53 +0100 Subject: [PATCH 05/12] refactor add categories to article method refactor test for assigning articles to categories --- app/controllers/articles_controller.rb | 23 +++++-------------- ...r_can_asign_categories_to_articles.feature | 5 ++-- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index e5a09bb..db3609b 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -1,4 +1,5 @@ class ArticlesController < ApplicationController + before_action :find_articles_and_categories, only: [:show, :update, :edit, :destroy] def index @articles = Article.all end @@ -19,12 +20,7 @@ def create end end - def show - @article = Article.find(params[:id]) - end - def update - @article = Article.find(params[:id]) if @article.update_attributes(article_params) flash[:success] = 'Article successfully updated.' redirect_to @article @@ -34,12 +30,7 @@ def update end end - def edit - @article = Article.find(params[:id]) - end - def destroy - @article = Article.find(params[:id]) @article.destroy flash[:success] = "Article successfully deleted." redirect_to root_path @@ -48,17 +39,15 @@ def destroy private def find_articles_and_categories - @article = Article.find(params[:id]) - @categories = Category.all + @article = Article.find(params[:id]) + @categories = Category.all end def add_categories_to_article - categories = [] - categories << params[:article][:categories] - - categories.each do |category_id| + if !params[:article][:categories].nil? + category_id = params[:article][:categories] category = Category.find_by(id: category_id) - @article.categories.include?(category) ? next : @article.categories << category + @article.categories << category unless @article.categories.include?(category) end end diff --git a/features/user_can_asign_categories_to_articles.feature b/features/user_can_asign_categories_to_articles.feature index e1f1c57..87d3c54 100644 --- a/features/user_can_asign_categories_to_articles.feature +++ b/features/user_can_asign_categories_to_articles.feature @@ -4,7 +4,7 @@ Feature: Better orginized websitet with categories I would like to be able to filter articles by category Background: - Given following article exists + Given the following article exists | title | content | | A Whole New World | A new fantastic point of view | And the following user exists @@ -20,10 +20,9 @@ Scenario: User creates a new article and asign it to a category Given I am on the Create Article page Then I fill in "Title" with "A Whole New Article" And I fill in "Content" with "A new fantastic Article" - And I select "Fashion" from "categories" And I select "Tech" from "categories" When I click "Create Article" button Then I should be on "A Whole New Article" page And I should see "A Whole New Article" And I should see "A new fantastic Article" - And I should see "Fashion" + And I should see "Tech" From 39c6547027bfef72838b2c9af7d2f27d1a05043a Mon Sep 17 00:00:00 2001 From: Holger_Tidemand Date: Sat, 24 Mar 2018 16:04:14 +0100 Subject: [PATCH 06/12] cleaned up in unit test for category --- spec/models/category_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index c98f4d9..036e03e 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -3,7 +3,6 @@ RSpec.describe Category, type: :model do describe "Factory" do it "should have a valid factory" do - article = create(:category) expect(create(:category)).to be_valid end end From 0d90de13cbb341ec9e52689104b3d840615db395 Mon Sep 17 00:00:00 2001 From: Holger_Tidemand Date: Sat, 24 Mar 2018 16:17:40 +0100 Subject: [PATCH 07/12] recatored new article form to not allow multiple category choose --- app/views/articles/new.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/articles/new.html.haml b/app/views/articles/new.html.haml index feccdfd..aba37fd 100644 --- a/app/views/articles/new.html.haml +++ b/app/views/articles/new.html.haml @@ -12,4 +12,4 @@ %p = form.label :categories %br/ - = form.select(:categories, Category.all.collect {|category| [category.name, category.id] }, {}, {multipe: true, id: 'categories'}) + = form.select(:categories, Category.all.collect {|category| [category.name, category.id] }, {}, {id: 'categories'}) From ef8065bf4ea248e44469de7ad4aacffe17d5cf8d Mon Sep 17 00:00:00 2001 From: Holger_Tidemand Date: Sat, 24 Mar 2018 18:26:36 +0100 Subject: [PATCH 08/12] removes index method from article controller --- app/controllers/articles_controller.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index db3609b..a2033ea 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -1,8 +1,5 @@ class ArticlesController < ApplicationController before_action :find_articles_and_categories, only: [:show, :update, :edit, :destroy] - def index - @articles = Article.all - end def new @article = Article.new From 4f431e3f627862c84df25479f6bc8df65d68e48e Mon Sep 17 00:00:00 2001 From: Holger_Tidemand Date: Sun, 25 Mar 2018 11:44:47 +0200 Subject: [PATCH 09/12] refactored requested changes --- app/models/article.rb | 4 ++-- features/step_definitions/category_steps.rb | 2 +- features/step_definitions/create_articles_steps.rb | 2 +- features/step_definitions/edit_articles_steps.rb | 2 +- features/step_definitions/log_out_steps.rb | 4 ++-- features/step_definitions/sign_in_steps.rb | 2 +- features/user_can_asign_categories_to_articles.feature | 7 +++---- spec/factories/articles.rb | 3 ++- spec/models/article_spec.rb | 1 + 9 files changed, 14 insertions(+), 13 deletions(-) diff --git a/app/models/article.rb b/app/models/article.rb index e7d08c3..5c38f2b 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,8 +1,8 @@ class Article < ApplicationRecord after_validation :reverse_geocode # auto-fetch address reverse_geocoded_by :latitude, :longitude - - validates :title, :content, presence: true + + validates :title, :content, :categories, presence: true has_many :comments, dependent: :destroy has_and_belongs_to_many :categories end diff --git a/features/step_definitions/category_steps.rb b/features/step_definitions/category_steps.rb index f3b8211..23badfe 100644 --- a/features/step_definitions/category_steps.rb +++ b/features/step_definitions/category_steps.rb @@ -17,7 +17,7 @@ end Then("I select {string} from {string}") do |category, select_box| - select(category, from: select_box) + select category, from: select_box end Then("I should be on the {string} page") do |category_name| diff --git a/features/step_definitions/create_articles_steps.rb b/features/step_definitions/create_articles_steps.rb index e4894ce..29a405e 100644 --- a/features/step_definitions/create_articles_steps.rb +++ b/features/step_definitions/create_articles_steps.rb @@ -11,7 +11,7 @@ end Then("I fill in {string} with {string}") do |input, value| - fill_in(input, with: value) + fill_in input, with: value end And("I click {string} button") do |string| diff --git a/features/step_definitions/edit_articles_steps.rb b/features/step_definitions/edit_articles_steps.rb index d50b402..f8dadbe 100644 --- a/features/step_definitions/edit_articles_steps.rb +++ b/features/step_definitions/edit_articles_steps.rb @@ -1,6 +1,6 @@ Given("the following article exists") do |table| table.hashes.each do |article| - FactoryBot.create(:article, article) + create(:article, article) end end diff --git a/features/step_definitions/log_out_steps.rb b/features/step_definitions/log_out_steps.rb index 19649c0..bf8c3c7 100644 --- a/features/step_definitions/log_out_steps.rb +++ b/features/step_definitions/log_out_steps.rb @@ -1,10 +1,10 @@ Given("the following user exists") do |table| table.hashes.each do |user| - FactoryBot.create(:user, user) + create(:user, user) end end Given("I am logged in as {string}") do |email| user = User.find_by(email: email) login_as(user, scope: :user) -end \ No newline at end of file +end diff --git a/features/step_definitions/sign_in_steps.rb b/features/step_definitions/sign_in_steps.rb index 7448b4f..f8b71b0 100644 --- a/features/step_definitions/sign_in_steps.rb +++ b/features/step_definitions/sign_in_steps.rb @@ -1,5 +1,5 @@ Given("following user exist") do |table| table.hashes.each do |user| - FactoryBot.create(:user, user) + create(:user, user) end end diff --git a/features/user_can_asign_categories_to_articles.feature b/features/user_can_asign_categories_to_articles.feature index 87d3c54..91117ce 100644 --- a/features/user_can_asign_categories_to_articles.feature +++ b/features/user_can_asign_categories_to_articles.feature @@ -4,16 +4,15 @@ Feature: Better orginized websitet with categories I would like to be able to filter articles by category Background: - Given the following article exists - | title | content | - | A Whole New World | A new fantastic point of view | - And the following user exists + Given the following user exists | email | password | | harald@norge.no | OsloOslo123 | + And the following categories exists | name | | Fashion | | Tech | + And I am logged in as "harald@norge.no" Scenario: User creates a new article and asign it to a category diff --git a/spec/factories/articles.rb b/spec/factories/articles.rb index 3d86cd1..e78f4cc 100644 --- a/spec/factories/articles.rb +++ b/spec/factories/articles.rb @@ -4,6 +4,7 @@ content "MyText" latitude 1.0 longitude 2.6 - address "pontus 1" + address "pontus 1" + categories {[create(:category)]} end end diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index c66f191..94db94d 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -22,5 +22,6 @@ describe 'Validations' do it { is_expected.to validate_presence_of :title } it { is_expected.to validate_presence_of :content } + it { is_expected.to validate_presence_of :categories } end end From da05695cb1761006d7fe004881dcd6c60b463f5e Mon Sep 17 00:00:00 2001 From: Holger_Tidemand Date: Sun, 25 Mar 2018 11:48:39 +0200 Subject: [PATCH 10/12] refactor old test to pass with new validation rules --- features/create_articles.feature | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/features/create_articles.feature b/features/create_articles.feature index eb8a553..6fc7f0c 100644 --- a/features/create_articles.feature +++ b/features/create_articles.feature @@ -4,12 +4,17 @@ Feature: Create articles I would like to be able to create articles Background: - Given I visit the Homepage + Given the following categories exists + | name | + | Fashion | + + And I visit the Homepage Scenario: Successfully create an article When I click "New Article" link Then I fill in "Title" with "A Whole New World" And I fill in "Content" with "A new fantastic point of view" + And I select "Fashion" from "categories" And I click "Create Article" button Then I should be on "A Whole New World" page And I should see "Article was successfully created." From a1c6e5069fa20905b96338085222d455873b0225 Mon Sep 17 00:00:00 2001 From: Holger_Tidemand Date: Mon, 26 Mar 2018 15:04:37 +0200 Subject: [PATCH 11/12] removed changes nor related to this PR --- app/controllers/home_controller.rb | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index d7d0e7a..c2a5e24 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,15 +1,23 @@ class HomeController < ApplicationController before_action :get_coordinates, only: [:index] - GOTHENBURG = [57.700501, 11.975463] def index - @local_articles = Article.near(current_user.address, 20) if !@coordinates.empty? - @articles = Article.all - @categories = Category.all + if !@coordinates.empty? + user = create_guest_user + location = (current_user ? current_user.address : user.address) + @local_articles = Article.near(location, 20) + end + @articles = Article.all + @categories = Category.all + end + + def get_location + user = create_guest_user + current_user ? current_user.address : user.address end def set_edition - if User.near(GOTHENBURG, 50).include? current_user + if User.near([57.700501, 11.975463], 50).include? current_user @edition = 'Gothenburg Edition' else @edition = 'Rest of Sweden Edition' @@ -29,8 +37,10 @@ def get_coordinates end def update_user_location - current_user.latitude = @coordinates.values.first - current_user.longitude = @coordinates.values.second - current_user.save + if current_user + current_user.latitude = @coordinates.values.first + current_user.longitude = @coordinates.values.second + current_user.save + end end end From 1b9cf5813d8c6d86ad6c9ce5801245b7c4174ded Mon Sep 17 00:00:00 2001 From: Holger_Tidemand Date: Mon, 26 Mar 2018 15:10:17 +0200 Subject: [PATCH 12/12] refactors feature test for category view page --- features/user_can_visit_category_page.feature | 2 ++ 1 file changed, 2 insertions(+) diff --git a/features/user_can_visit_category_page.feature b/features/user_can_visit_category_page.feature index 7c7100c..e5c2b6b 100644 --- a/features/user_can_visit_category_page.feature +++ b/features/user_can_visit_category_page.feature @@ -8,10 +8,12 @@ Background: | name | | Fashion | | Tech | + And the following article exists | title | content | | A Whole New World | A new fantastic point of view | | A Whole New Article | A new fantastic article | + And the following article are assigned to categories | title | category | | A Whole New World | Fashion |