From 42de4d099a77f5d04b6c6942f4b343e0ccfcf533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Br=C3=BCder?= Date: Wed, 30 Sep 2015 13:09:30 +0200 Subject: [PATCH] refatored Post into Article --- .gitignore | 3 +- Gemfile | 6 +- Gemfile.lock | 79 ++++++++++--------- app.rb | 68 ++++++++-------- ...st.rb => 20140609093929_create_article.rb} | 4 +- db/schema.rb | 2 +- features/article.feature | 44 +++++++++++ features/post.feature | 44 ----------- features/step_definitions/web_steps.rb | 14 ++-- features/support/paths.rb | 4 +- spec/models/article_spec.rb | 14 ++++ spec/models/post_spec.rb | 14 ---- spec/spec_helper.rb | 11 +-- views/articles/edit.erb | 22 ++++++ views/articles/index.erb | 13 +++ views/articles/new.erb | 13 +++ views/articles/show.erb | 2 + views/posts/edit.erb | 22 ------ views/posts/index.erb | 13 --- views/posts/new.erb | 13 --- views/posts/show.erb | 2 - 21 files changed, 199 insertions(+), 208 deletions(-) rename db/migrate/{20140609093929_create_post.rb => 20140609093929_create_article.rb} (53%) create mode 100644 features/article.feature delete mode 100644 features/post.feature create mode 100644 spec/models/article_spec.rb delete mode 100644 spec/models/post_spec.rb create mode 100644 views/articles/edit.erb create mode 100644 views/articles/index.erb create mode 100644 views/articles/new.erb create mode 100644 views/articles/show.erb delete mode 100644 views/posts/edit.erb delete mode 100644 views/posts/index.erb delete mode 100644 views/posts/new.erb delete mode 100644 views/posts/show.erb diff --git a/.gitignore b/.gitignore index 926abf7..4f8a5c1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store -*.sqlite3 \ No newline at end of file +*.sqlite3 +Gemfile.lock \ No newline at end of file diff --git a/Gemfile b/Gemfile index ee36edc..82b5ca6 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source 'https://rubygems.org/' -gem "sinatra", "1.4.5" +gem "sinatra", "1.4.6" gem "sqlite3", "1.3.10" gem "activerecord", "4.2.0" gem "sinatra-activerecord", "2.0.5" @@ -15,7 +15,3 @@ group :development do gem "shotgun", "0.9" # So we don't need to restart the server every time gem "tux", "0.3.0" # A ruby console for testing end - -group :production do - gem "mysql", "2.9.1" -end diff --git a/Gemfile.lock b/Gemfile.lock index 9413cb1..cc21ed1 100755 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,29 +1,29 @@ GEM remote: https://rubygems.org/ specs: - activemodel (4.1.1) - activesupport (= 4.1.1) + activemodel (4.2.0) + activesupport (= 4.2.0) builder (~> 3.1) - activerecord (4.1.1) - activemodel (= 4.1.1) - activesupport (= 4.1.1) - arel (~> 5.0.0) - activesupport (4.1.1) - i18n (~> 0.6, >= 0.6.9) + activerecord (4.2.0) + activemodel (= 4.2.0) + activesupport (= 4.2.0) + arel (~> 6.0) + activesupport (4.2.0) + i18n (~> 0.7) json (~> 1.7, >= 1.7.7) minitest (~> 5.1) - thread_safe (~> 0.1) + thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) - arel (5.0.1.20140414130214) + arel (6.0.3) bond (0.5.1) builder (3.2.2) - capybara (2.4.1) + capybara (2.4.4) mime-types (>= 1.16) nokogiri (>= 1.3.3) rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) - cucumber (1.3.17) + cucumber (1.3.18) builder (>= 2.1.2) diff-lcs (>= 1.1.3) gherkin (~> 2.12) @@ -31,28 +31,27 @@ GEM multi_test (>= 0.1.1) cucumber-sinatra (0.5.0) templater (>= 1.0.0) - database_cleaner (1.3.0) + database_cleaner (1.4.0) diff-lcs (1.2.5) extlib (0.9.16) gherkin (2.12.2) multi_json (~> 1.3) highline (1.6.21) - i18n (0.6.9) - json (1.8.1) - mime-types (2.3) - mini_portile (0.6.0) - minitest (5.3.4) - multi_json (1.10.1) - multi_test (0.1.1) + i18n (0.7.0) + json (1.8.3) + mime-types (2.6.2) + mini_portile (0.6.2) + minitest (5.8.1) + multi_json (1.11.2) + multi_test (0.1.2) mysql (2.9.1) - nokogiri (1.6.3.1) - mini_portile (= 0.6.0) + nokogiri (1.6.6.2) + mini_portile (~> 0.6.0) rack (1.5.2) rack-protection (1.5.3) rack rack-test (0.6.2) rack (>= 1.0) - rake (10.3.2) ripl (0.7.1) bond (~> 0.5.1) ripl-multi_line (0.3.1) @@ -75,22 +74,22 @@ GEM rack (~> 1.4) rack-protection (~> 1.4) tilt (~> 1.3, >= 1.3.4) - sinatra-activerecord (2.0.2) + sinatra-activerecord (2.0.5) activerecord (>= 3.2) sinatra (~> 1.0) - sqlite3 (1.3.9) + sqlite3 (1.3.10) templater (1.0.0) diff-lcs (>= 1.1.2) extlib (>= 0.9.5) highline (>= 1.4.0) - thread_safe (0.3.4) + thread_safe (0.3.5) tilt (1.4.1) tux (0.3.0) ripl (>= 0.3.5) ripl-multi_line (>= 0.2.4) ripl-rack (>= 0.2.0) sinatra (>= 1.2.1) - tzinfo (1.2.1) + tzinfo (1.2.2) thread_safe (~> 0.1) xpath (2.0.0) nokogiri (~> 1.3) @@ -99,16 +98,18 @@ PLATFORMS ruby DEPENDENCIES - activerecord - capybara - cucumber - cucumber-sinatra - database_cleaner - mysql - rake + activerecord (= 4.2.0) + capybara (= 2.4.4) + cucumber (= 1.3.18) + cucumber-sinatra (= 0.5.0) + database_cleaner (= 1.4.0) + mysql (= 2.9.1) rspec (= 2.99.0) - shotgun - sinatra - sinatra-activerecord - sqlite3 - tux + shotgun (= 0.9) + sinatra (= 1.4.5) + sinatra-activerecord (= 2.0.5) + sqlite3 (= 1.3.10) + tux (= 0.3.0) + +BUNDLED WITH + 1.10.0 diff --git a/app.rb b/app.rb index dbb30e6..68284d3 100644 --- a/app.rb +++ b/app.rb @@ -5,70 +5,68 @@ class Blog < Sinatra::Application end -class Post < ActiveRecord::Base - +class Article < ActiveRecord::Base def number_of_characters self.text.length end - end get '/' do - @posts = Post.order("created_at DESC") - erb :"posts/index" + @articles = Article.order("created_at DESC") + erb :"articles/index" end get '/about' do erb :"about/index" end -# The New Post form sends a POST request (storing data) here -# where we try to create the post it sent in its params hash. -# If successful, redirect to that post. Otherwise, render the "posts/new" -# template where the @post object will have the incomplete data that the +# The New Article form sends a POST request (storing data) here +# where we try to create the article it sent in its params hash. +# If successful, redirect to that article. Otherwise, render the "posts/new" +# template where the @article object will have the incomplete data that the # user can modify and resubmit. -post "/posts" do - @post = Post.new(params[:post]) - if @post.save +post "/articles" do + @article = Article.new(params[:article]) + if @article.save redirect "/" else - erb :"posts/new" + erb :"articles/new" end end -# Get the New Post form -get '/posts/new' do - @post = Post.new - erb :"posts/new" +# Get the New Article form +get '/articles/new' do + @article = Article.new + erb :"articles/new" end -# Get the individual page of the post with this ID. -get "/posts/:id" do - @post = Post.find(params[:id]) - erb :"posts/show" +# Get the individual page of the article with this ID. +get "/articles/:id" do + @article = Article.find(params[:id]) + erb :"articles/show" end -# Get the Edit Post form of the post with this ID. -get "/posts/:id/edit" do - @post = Post.find(params[:id]) - erb :"posts/edit" +# Get the Edit Article form of the article with this ID. +get "/articles/:id/edit" do + @article = Article.find(params[:id]) + erb :"articles/edit" end -# The Edit Post form sends a PUT request (modifying data) here. -# If the post is updated successfully, redirect to it. Otherwise, -# render the edit form again with the failed @post object still in memory +# The Edit Article form sends a PUT request (modifying data) here. +# If the article is updated successfully, redirect to it. Otherwise, +# render the edit form again with the failed @article object still in memory # so they can retry. -put "/posts/:id" do - @post = Post.find(params[:id]) - if @post.update_attributes(params[:post]) +put "/articles/:id" do + @article = Article.find(params[:id]) + if @article.update_attributes(params[:article]) redirect "/" else - erb :"posts/edit" + erb :"articles/edit" end end -# Deletes the post with this ID and redirects to homepage. -delete "/posts/:id" do - @post = Post.find(params[:id]).destroy +# Deletes the article with this ID and redirects to homepage. +delete "/articles/:id" do + @article = Article.find(params[:id]).destroy redirect "/" end diff --git a/db/migrate/20140609093929_create_post.rb b/db/migrate/20140609093929_create_article.rb similarity index 53% rename from db/migrate/20140609093929_create_post.rb rename to db/migrate/20140609093929_create_article.rb index b3bc79f..55f1e24 100644 --- a/db/migrate/20140609093929_create_post.rb +++ b/db/migrate/20140609093929_create_article.rb @@ -1,6 +1,6 @@ -class CreatePost < ActiveRecord::Migration +class CreateArticle < ActiveRecord::Migration def change - create_table :posts do |t| + create_table :articles do |t| t.string :title t.text :text t.timestamps diff --git a/db/schema.rb b/db/schema.rb index 85128a1..017078b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -13,7 +13,7 @@ ActiveRecord::Schema.define(version: 20140609093929) do - create_table "posts", force: true do |t| + create_table "articles", force: :cascade do |t| t.string "title" t.text "text" t.datetime "created_at" diff --git a/features/article.feature b/features/article.feature new file mode 100644 index 0000000..7acbe70 --- /dev/null +++ b/features/article.feature @@ -0,0 +1,44 @@ +Feature: Article + + Background: + Given I have an article titled "An article about coffee" with the text "Lorem ipsum dolor samet" + + Scenario: Accessing new article form + Given I am on the home page + When I follow "Create new article" + Then I should be on the create new article page + And I should see "New article" + + Scenario: Creating an article + Given I am on the create new article page + When I fill in "article_title" with "My article title" + And I fill in "article_text" with "Lorem ipsum dolor samet" + And I press "Save" + Then I should be on the home page + And I should see "My article title" + + Scenario: Showing an article + Given I am on the home page + When I follow "An article about coffee" + Then I should see "An article about coffee" + And I should see "Lorem ipsum dolor samet" + + Scenario: Editing an article + Given I am on the home page + When I follow "Edit" + Then I should see "Edit - An article about coffee" + And I should see "Lorem ipsum dolor samet" + When I fill in "article_title" with "An article about Red Bull" + And I press "Update" + Then I should be on the home page + And I should see "An article about Red Bull" + + Scenario: Deleting an article + Given I am editing an article titled "An article about coffee" + When I press "Delete" + Then I should be on the home page + And I should not see "My new article title" + + Scenario: Show the number of characters used in article + Given I am on the home page + Then I should see the number of characters of the article displayed diff --git a/features/post.feature b/features/post.feature deleted file mode 100644 index 9424bcf..0000000 --- a/features/post.feature +++ /dev/null @@ -1,44 +0,0 @@ -Feature: Post - - Background: - Given I have a post titled "A post about coffee" with the text "Lorem ipsum dolor samet" - - Scenario: Accessing new post form - Given I am on the home page - When I follow "Create new post" - Then I should be on the create new post page - And I should see "New Post" - - Scenario: Creating a post - Given I am on the create new post page - When I fill in "post_title" with "My post title" - And I fill in "post_text" with "Lorem ipsum dolor samet" - And I press "Save" - Then I should be on the home page - And I should see "My post title" - - Scenario: Showing a post - Given I am on the home page - When I follow "A post about coffee" - Then I should see "A post about coffee" - And I should see "Lorem ipsum dolor samet" - - Scenario: Editing a post - Given I am on the home page - When I follow "Edit" - Then I should see "Edit - A post about coffee" - And I should see "Lorem ipsum dolor samet" - When I fill in "post_title" with "A post about Red Bull" - And I press "Update" - Then I should be on the home page - And I should see "A post about Red Bull" - - Scenario: Deleting a post - Given I am editing a post titled "A post about coffee" - When I press "Delete" - Then I should be on the home page - And I should not see "My new post title" - - Scenario: Show the number of characters used in post - Given I am on the home page - Then I should see the number of characters of the post displayed diff --git a/features/step_definitions/web_steps.rb b/features/step_definitions/web_steps.rb index dc01128..0e3d6f8 100644 --- a/features/step_definitions/web_steps.rb +++ b/features/step_definitions/web_steps.rb @@ -18,18 +18,18 @@ def with_scope(locator) # Own Steps -Given(/^I have a post titled "(.*?)" with the text "(.*?)"$/) do |arg1, arg2| - Post.create(title: arg1, text: arg2) +Given(/^I have an article titled "(.*?)" with the text "(.*?)"$/) do |arg1, arg2| + Article.create(title: arg1, text: arg2) end -Given(/^I am editing a post titled "(.*?)"$/) do |post_title| - post = Post.find_by_title(post_title) - visit "/posts/#{post.id}/edit" +Given(/^I am editing an article titled "(.*?)"$/) do |article_title| + article = Article.find_by_title(article_title) + visit "/articles/#{article.id}/edit" end -Then(/^I should see the number of characters of the post displayed$/) do - page.should have_xpath('//*', :text => Post.first.number_of_characters) +Then(/^I should see the number of characters of the article displayed$/) do + page.should have_xpath('//*', :text => Article.first.number_of_characters) end # end diff --git a/features/support/paths.rb b/features/support/paths.rb index b264729..13b34da 100644 --- a/features/support/paths.rb +++ b/features/support/paths.rb @@ -16,8 +16,8 @@ def path_to(page_name) when /the about\s?page/ '/about' - when /create new post\s?page/ - '/posts/new' + when /create new article\s?page/ + '/articles/new' else raise "Can't find mapping from \"#{page_name}\" to a path.\n" + diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb new file mode 100644 index 0000000..a5c84c3 --- /dev/null +++ b/spec/models/article_spec.rb @@ -0,0 +1,14 @@ +require 'spec_helper' + +describe Article do + + before(:each) do + @text = "This is a longer article text. This way, we make sure that we get a different number." + @article = Article.create(title: "This is my article", text: @text) + end + + it "returns the number of characters used in the text" do + expect(@article.number_of_characters).to eq(@text.length) + end + +end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb deleted file mode 100644 index c5b3dd2..0000000 --- a/spec/models/post_spec.rb +++ /dev/null @@ -1,14 +0,0 @@ -require 'spec_helper' - -describe Post do - - before(:each) do - @text = "This is a longer post text. This way, we make sure that we get a different number." - @post = Post.create(title: "This is my post", text: @text) - end - - it "returns the number of characters used in the text" do - expect(@post.number_of_characters).to eq(@text.length) - end - -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5448bfe..0e29c9c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,17 +1,12 @@ -require File.join(File.dirname(__FILE__), '..', 'app.rb') +ENV['RACK_ENV'] = 'test' -require 'sinatra' +require File.join(File.dirname(__FILE__), '..', 'app.rb') require 'rack/test' -set :environment, :test -set :run, false -set :raise_errors, true -set :logging, false - def app Sinatra::Application end RSpec.configure do |config| config.include Rack::Test::Methods -end +end \ No newline at end of file diff --git a/views/articles/edit.erb b/views/articles/edit.erb new file mode 100644 index 0000000..b48690c --- /dev/null +++ b/views/articles/edit.erb @@ -0,0 +1,22 @@ +

Edit - <%= @article.title %>

+ +
+ + +
+ +
+ +
+ +
+ + +
+ +

Delete this article

+ +
+ + +
diff --git a/views/articles/index.erb b/views/articles/index.erb new file mode 100644 index 0000000..318876a --- /dev/null +++ b/views/articles/index.erb @@ -0,0 +1,13 @@ +Create new article + +<% @articles.each do |article| %> +
+
"> +

+ <%= article.title %> +

+ Length: <%= article.number_of_characters %> + Edit +
+
+<% end %> diff --git a/views/articles/new.erb b/views/articles/new.erb new file mode 100644 index 0000000..b5f3931 --- /dev/null +++ b/views/articles/new.erb @@ -0,0 +1,13 @@ +

New article

+ +
+
+ +
+ +
+ +
+ + +
diff --git a/views/articles/show.erb b/views/articles/show.erb new file mode 100644 index 0000000..5b56509 --- /dev/null +++ b/views/articles/show.erb @@ -0,0 +1,2 @@ +

<%= @article.title %>

+

<%= @article.text %>

diff --git a/views/posts/edit.erb b/views/posts/edit.erb deleted file mode 100644 index 4db3314..0000000 --- a/views/posts/edit.erb +++ /dev/null @@ -1,22 +0,0 @@ -

Edit - <%= @post.title %>

- -
- - -
- -
- -
- -
- - -
- -

Delete this post

- -
- - -
diff --git a/views/posts/index.erb b/views/posts/index.erb deleted file mode 100644 index 21655c9..0000000 --- a/views/posts/index.erb +++ /dev/null @@ -1,13 +0,0 @@ -Create new post - -<% @posts.each do |post| %> -
-
"> -

- <%= post.title %> -

- Length: <%= post.number_of_characters %> - Edit -
-
-<% end %> diff --git a/views/posts/new.erb b/views/posts/new.erb deleted file mode 100644 index 68071e4..0000000 --- a/views/posts/new.erb +++ /dev/null @@ -1,13 +0,0 @@ -

New Post

- -
-
- -
- -
- -
- - -
diff --git a/views/posts/show.erb b/views/posts/show.erb deleted file mode 100644 index 48035f8..0000000 --- a/views/posts/show.erb +++ /dev/null @@ -1,2 +0,0 @@ -

<%= @post.title %>

-

<%= @post.text %>