From d1436694c613ef763cbf8b62c630d554bd51508f Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Sun, 13 Oct 2013 12:26:17 -0500 Subject: [PATCH 1/2] Tech Debt: Fixed test to use the correct model ids In the tweets_controller_test.rb the ids in the assertion were the group ids when they should have been the tweet ids. They were the same value so this did not cause the test to fail but were incorrect. --- test/controllers/tweets_controller_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/controllers/tweets_controller_test.rb b/test/controllers/tweets_controller_test.rb index 54de2fd..d97ca90 100644 --- a/test/controllers/tweets_controller_test.rb +++ b/test/controllers/tweets_controller_test.rb @@ -7,11 +7,11 @@ class TweetsControllerTest < ActionController::TestCase assert_response :success assert_equal JSON.parse(@response.body), [ { - 'id' => groups(:two).id, + 'id' => tweets(:two).id, 'content' => 'Two', }, { - 'id' => groups(:one).id, + 'id' => tweets(:one).id, 'content' => 'One', }, ] From faf6e3b4e90c3df849a0bc4d82ddb244f779a9d7 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Sun, 13 Oct 2013 12:41:56 -0500 Subject: [PATCH 2/2] Feature: added post model with association to group --- .gitignore | 3 +++ app/controllers/posts_controller.rb | 7 +++++++ app/models/group.rb | 1 + app/models/post.rb | 3 +++ app/serializers/group_serializer.rb | 1 + app/serializers/post_serializer.rb | 3 +++ config/routes.rb | 2 +- db/migrate/20131013172125_create_posts.rb | 9 +++++++++ .../20131013174840_add_group_id_to_posts.rb | 5 +++++ db/schema.rb | 9 ++++++++- test/controllers/groups_controller_test.rb | 9 ++++++--- test/controllers/posts_controller_test.rb | 20 +++++++++++++++++++ test/fixtures/posts.yml | 17 ++++++++++++++++ test/models/post_test.rb | 4 ++++ 14 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 app/controllers/posts_controller.rb create mode 100644 app/models/post.rb create mode 100644 app/serializers/post_serializer.rb create mode 100644 db/migrate/20131013172125_create_posts.rb create mode 100644 db/migrate/20131013174840_add_group_id_to_posts.rb create mode 100644 test/controllers/posts_controller_test.rb create mode 100644 test/fixtures/posts.yml create mode 100644 test/models/post_test.rb diff --git a/.gitignore b/.gitignore index 25a742d..68c242b 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ # Ignore all logfiles and tempfiles. /log/*.log /tmp + +# Ignore coverage directory +coverage diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb new file mode 100644 index 0000000..baa3a61 --- /dev/null +++ b/app/controllers/posts_controller.rb @@ -0,0 +1,7 @@ +class PostsController < ApplicationController + + def show + render json: Post.find(params[:ids]) + end + +end diff --git a/app/models/group.rb b/app/models/group.rb index 3215053..588d970 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -1,3 +1,4 @@ class Group < ActiveRecord::Base has_many :tweets + has_many :posts end diff --git a/app/models/post.rb b/app/models/post.rb new file mode 100644 index 0000000..d0e08e5 --- /dev/null +++ b/app/models/post.rb @@ -0,0 +1,3 @@ +class Post < ActiveRecord::Base + belongs_to :group +end diff --git a/app/serializers/group_serializer.rb b/app/serializers/group_serializer.rb index 0f76cd1..1920258 100644 --- a/app/serializers/group_serializer.rb +++ b/app/serializers/group_serializer.rb @@ -1,4 +1,5 @@ class GroupSerializer < ActiveModel::Serializer attributes :id, :name, :website, :twitter_handle, :google_group, :facebook has_many :tweets, embed: :ids, key: :tweets + has_many :posts, embed: :ids, key: :posts end diff --git a/app/serializers/post_serializer.rb b/app/serializers/post_serializer.rb new file mode 100644 index 0000000..7e9de00 --- /dev/null +++ b/app/serializers/post_serializer.rb @@ -0,0 +1,3 @@ +class PostSerializer < ActiveModel::Serializer + attributes :id, :title +end diff --git a/config/routes.rb b/config/routes.rb index d6d016c..28ab476 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ DsmcodeApi::Application.routes.draw do resources :groups resource :tweets - + resource :posts end diff --git a/db/migrate/20131013172125_create_posts.rb b/db/migrate/20131013172125_create_posts.rb new file mode 100644 index 0000000..92e9e44 --- /dev/null +++ b/db/migrate/20131013172125_create_posts.rb @@ -0,0 +1,9 @@ +class CreatePosts < ActiveRecord::Migration + def change + create_table :posts do |t| + t.string :title + + t.timestamps + end + end +end diff --git a/db/migrate/20131013174840_add_group_id_to_posts.rb b/db/migrate/20131013174840_add_group_id_to_posts.rb new file mode 100644 index 0000000..a6eb6cb --- /dev/null +++ b/db/migrate/20131013174840_add_group_id_to_posts.rb @@ -0,0 +1,5 @@ +class AddGroupIdToPosts < ActiveRecord::Migration + def change + add_column :posts, :group_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 9621904..0eca9d3 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: 20131009012902) do +ActiveRecord::Schema.define(version: 20131013174840) do create_table "groups", force: true do |t| t.string "name" @@ -23,6 +23,13 @@ t.datetime "updated_at" end + create_table "posts", force: true do |t| + t.string "title" + t.datetime "created_at" + t.datetime "updated_at" + t.integer "group_id" + end + create_table "tweets", force: true do |t| t.string "content" t.datetime "created_at" diff --git a/test/controllers/groups_controller_test.rb b/test/controllers/groups_controller_test.rb index 52cd9da..809b04c 100644 --- a/test/controllers/groups_controller_test.rb +++ b/test/controllers/groups_controller_test.rb @@ -14,7 +14,8 @@ class GroupsControllerTest < ActionController::TestCase 'twitter_handle' => 'two_twit', 'google_group' => 'two_grp', 'facebook' => 'two_fb', - 'tweets' => [tweets(:three).id, tweets(:four).id] + 'tweets' => [tweets(:three).id, tweets(:four).id], + 'posts' => [posts(:three).id, posts(:four).id] }, { 'id' => groups(:one).id, @@ -23,7 +24,8 @@ class GroupsControllerTest < ActionController::TestCase 'twitter_handle' => 'one_twit', 'google_group' => 'one_grp', 'facebook' => 'one_fb', - 'tweets' => [tweets(:two).id, tweets(:one).id] + 'tweets' => [tweets(:two).id, tweets(:one).id], + 'posts' => [posts(:two).id, posts(:one).id] }, ], json_groups end @@ -39,7 +41,8 @@ class GroupsControllerTest < ActionController::TestCase 'twitter_handle' => 'one_twit', 'google_group' => 'one_grp', 'facebook' => 'one_fb', - 'tweets' => [tweets(:two).id, tweets(:one).id] + 'tweets' => [tweets(:two).id, tweets(:one).id], + 'posts' => [posts(:two).id, posts(:one).id] } end diff --git a/test/controllers/posts_controller_test.rb b/test/controllers/posts_controller_test.rb new file mode 100644 index 0000000..72dde12 --- /dev/null +++ b/test/controllers/posts_controller_test.rb @@ -0,0 +1,20 @@ +require 'test_helper' + +class PostsControllerTest < ActionController::TestCase + + test "should show all posts for given ids" do + get :show, :ids => [posts(:one).id, posts(:two).id] + assert_response :success + assert_equal JSON.parse(@response.body), [ + { + 'id' => posts(:two).id, + 'title' => 'Title Two', + }, + { + 'id' => posts(:one).id, + 'title' => 'Title One', + }, + ] + end + +end diff --git a/test/fixtures/posts.yml b/test/fixtures/posts.yml new file mode 100644 index 0000000..57befee --- /dev/null +++ b/test/fixtures/posts.yml @@ -0,0 +1,17 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +one: + title: 'Title One' + group: one + +two: + title: 'Title Two' + group: one + +three: + title: 'Title Three' + group: two + +four: + title: 'Title Four' + group: two diff --git a/test/models/post_test.rb b/test/models/post_test.rb new file mode 100644 index 0000000..bb4fb15 --- /dev/null +++ b/test/models/post_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class PostTest < ActiveSupport::TestCase +end