Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Journalist can assign an article to a category #26

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
before_action :find_articles_and_categories, only: [:show, :update, :edit, :destroy]

def new
@article = Article.new
end

def create
@article = Article.new(article_params)
add_categories_to_article
if @article.save
flash[:success] = "Article was successfully created."
redirect_to @article
Expand All @@ -18,12 +17,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
Expand All @@ -33,19 +27,27 @@ 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
end

private

def find_articles_and_categories
@article = Article.find(params[:id])
@categories = Category.all
end

def add_categories_to_article
if !params[:article][:categories].nil?
category_id = params[:article][:categories]
category = Category.find_by(id: category_id)
@article.categories << category unless @article.categories.include?(category)
end
end

def article_params
params[:article].permit(:title, :content)
end
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class CategoriesController < ApplicationController
def show
@category = Category.find_by(id: params[:id])
end
end
16 changes: 7 additions & 9 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ class HomeController < ApplicationController
before_action :get_coordinates, only: [:index]

def index
if [email protected]?
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
if [email protected]?
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
Expand Down Expand Up @@ -44,5 +43,4 @@ def update_user_location
current_user.save
end
end

end
5 changes: 3 additions & 2 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -1,7 +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
3 changes: 3 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Category < ApplicationRecord
has_and_belongs_to_many :articles
end
4 changes: 4 additions & 0 deletions app/views/articles/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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] }, {}, {id: 'categories'})
2 changes: 2 additions & 0 deletions app/views/articles/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions app/views/categories/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
%h1= @category.name

- if @category.articles
- @category.articles.each do |article|
%h3= article.title
%p= article.content
9 changes: 7 additions & 2 deletions app/views/home/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
- if @local_articles
%h3 Local Articles
- @local_articles.each do |article|
%p= link_to "#{article.title}", article_path(article)
%h3= link_to "#{article.title}", article_path(article)
%p= sanitize(truncate(article.content, length: 150))

- if @articles
%h3 All Articles
- @articles.each do |article|
%p= link_to "#{article.title}", article_path(article)
%h3= link_to "#{article.title}", article_path(article)
%p= sanitize(truncate(article.content, length: 150))

- if @categories
%h3 Categories
- @categories.each do |category|
%p= link_to "#{category.name}", category_path(category)
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
root controller: :home, action: :index

devise_for :users
resources :categories, only: [:show]
resources :home, only: [:create, :index]
resources :subscription, only: [:new, :create]
resources :articles do
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20180322195300_create_categories.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
15 changes: 14 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_03_21_152001) 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"
Expand All @@ -25,6 +25,19 @@
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
t.datetime "updated_at", null: false
end

create_table "comments", force: :cascade do |t|
t.string "email"
t.text "content"
Expand Down
7 changes: 6 additions & 1 deletion features/create_articles.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
26 changes: 26 additions & 0 deletions features/step_definitions/category_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Given("the following categories exists") do |table|
table.hashes.each do |category|
create(:category, category)
end
end

Given("the following article are assigned to categories") do |table|
table.hashes.each do |article|
category = Category.find_by(name: article[:category])
article = Article.find_by(title: article[:title])
article.categories.push category
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not do this in the step definition where you create articles?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made more since to us. didn't know if that would mess up all the other test that are using that step so we decided to make a new one.


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

Then("I should be on the {string} page") do |category_name|
category = Category.find_by(name: category_name)
expect(page.current_path).to eq category_path(category)
end
2 changes: 1 addition & 1 deletion features/step_definitions/create_articles_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion features/step_definitions/edit_articles_steps.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Empty file.
4 changes: 2 additions & 2 deletions features/step_definitions/log_out_steps.rb
Original file line number Diff line number Diff line change
@@ -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
end
2 changes: 1 addition & 1 deletion features/step_definitions/sign_in_steps.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Given("following user exist") do |table|
table.hashes.each do |user|
FactoryBot.create(:user, user)
create(:user, user)
end
end
2 changes: 1 addition & 1 deletion features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
end

Cucumber::Rails::Database.javascript_strategy = :truncation

World(FactoryBot::Syntax::Methods)
Chromedriver.set_version '2.36'


Expand Down
27 changes: 27 additions & 0 deletions features/user_can_asign_categories_to_articles.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 the following user exists
| email | password |
| [email protected] | OsloOslo123 |

And the following categories exists
| name |
| Fashion |
| Tech |

And I am logged in as "[email protected]"

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 "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 "Tech"
34 changes: 34 additions & 0 deletions features/user_can_visit_category_page.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Feature: User should be able to visit a category page
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 the following categories exists
| 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 |
| A Whole New Article | Tech |

Scenario: User visits the Fashion page
Given I visit the Homepage
When I click on the "Fashion" button
Then I should be on the "Fashion" page
And I should see "A Whole New World"
And I should see "A new fantastic point of view"

Scenario: User visits the Tech page
Given I visit the Homepage
When I click on the "Tech" button
Then I should be on the "Tech" page
And I should see "A Whole New Article"
And I should see "A new fantastic article"
3 changes: 2 additions & 1 deletion spec/factories/articles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
content "MyText"
latitude 1.0
longitude 2.6
address "pontus 1"
address "pontus 1"
categories {[create(:category)]}
end
end
5 changes: 5 additions & 0 deletions spec/factories/categories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :category do
name "MyString"
end
end
5 changes: 5 additions & 0 deletions spec/models/article_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
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 }
it { is_expected.to validate_presence_of :categories }
end
end
Loading