Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp

# Ignore coverage directory
coverage
7 changes: 7 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class PostsController < ApplicationController

def show
render json: Post.find(params[:ids])
end

end
1 change: 1 addition & 0 deletions app/models/group.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Group < ActiveRecord::Base
has_many :tweets
has_many :posts
end
3 changes: 3 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Post < ActiveRecord::Base
belongs_to :group
end
1 change: 1 addition & 0 deletions app/serializers/group_serializer.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions app/serializers/post_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class PostSerializer < ActiveModel::Serializer
attributes :id, :title
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DsmcodeApi::Application.routes.draw do
resources :groups
resource :tweets

resource :posts
end
9 changes: 9 additions & 0 deletions db/migrate/20131013172125_create_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20131013174840_add_group_id_to_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddGroupIdToPosts < ActiveRecord::Migration
def change
add_column :posts, :group_id, :integer
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
9 changes: 6 additions & 3 deletions test/controllers/groups_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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

Expand Down
20 changes: 20 additions & 0 deletions test/controllers/posts_controller_test.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions test/controllers/tweets_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
]
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/posts.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions test/models/post_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class PostTest < ActiveSupport::TestCase
end