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 5 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
16 changes: 16 additions & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,6 +48,21 @@ def destroy

private

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

def add_categories_to_article
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
Copy link
Member

Choose a reason for hiding this comment

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

Could you please see if this solution work?

@article.categories.push category unless  @article.categories.include? category

end
end

def article_params
params[:article].permit(:title, :content)
end
Expand Down
1 change: 1 addition & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Article < ApplicationRecord
validates :title, :content, 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 :artilces
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] }, {}, {multipe: true, 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
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
21 changes: 20 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_18_155404) 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 @@ -20,6 +20,22 @@
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 "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|
Expand All @@ -45,6 +61,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
Expand Down
13 changes: 13 additions & 0 deletions features/step_definitions/category_steps.rb
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

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

I sense that you are mixing styles. Most of the step definitions omit the parathesis, here you have them. Stick to one style, please.

end
Empty file.
Empty file.
5 changes: 2 additions & 3 deletions features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
end

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

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


Capybara.register_driver :selenium do |app|
options = Selenium::WebDriver::Chrome::Options.new(
implicit_wait: 60,
args: %w(disable-popup-blocking disable-infobars),
binary: '/Applications/Google Chrome 2.app/Contents/MacOS/Google Chrome'
args: %w( headless disable-popup-blocking disable-infobars)
)

Capybara::Selenium::Driver.new(
Expand Down
29 changes: 29 additions & 0 deletions features/user_can_asign_categories_to_articles.feature
Original file line number Diff line number Diff line change
@@ -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 |
Copy link
Member

Choose a reason for hiding this comment

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

Why do you have this background step here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wasn't sure how we would make it write the test in the beginning. Will remove it!

And the following user exists
| email | password |
| [email protected] | OsloOslo123 |
And the following categories exists
Copy link
Member

Choose a reason for hiding this comment

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

Please separate steps in the background with an empty line.

| 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 "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"
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
4 changes: 4 additions & 0 deletions spec/models/article_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
18 changes: 18 additions & 0 deletions spec/models/category_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'rails_helper'

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

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
4 changes: 2 additions & 2 deletions spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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