This repository has been archived by the owner on Feb 13, 2018. It is now read-only.
Add notes to history #54
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
05eccbf
Add note model and unit test
bishboria dc6705e
Add Notes controller and functional test
bishboria 3db9e21
Updating presenters and decorators to return notes
bishboria f283911
Give note object in map block a better name
bishboria 792afeb
Use head method instead of render
bishboria 8f40854
defaulting notes parameter in method signature
bishboria 14eb77c
Move notes assertions into a separate test
bishboria 776e9d4
Use created_at for default order, rather than _id
bishboria f64b487
Add validations to note model
bishboria 974e800
Show validation errors in response
bishboria File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class NotesController < ApplicationController | ||
def create | ||
note = Note.new(filtered_params) | ||
if note.save | ||
head :created | ||
else | ||
error 422, message: :invalid_attributes, errors: note.errors.full_messages | ||
end | ||
end | ||
|
||
private | ||
|
||
def filtered_params | ||
params.except(:action, :controller) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Note | ||
include Mongoid::Document | ||
include Mongoid::Timestamps | ||
|
||
field :text, type: String | ||
field :need_id, type: Integer | ||
field :author, type: Hash | ||
field :revision, type: String | ||
|
||
default_scope order_by([:created_at, :desc]) | ||
|
||
validates_presence_of :text, :need_id, :author | ||
validate :validate_need_id | ||
|
||
def save | ||
need = Need.where(need_id: need_id).first | ||
self.revision = need.revisions.first.id if need | ||
super | ||
end | ||
|
||
private | ||
|
||
def validate_need_id | ||
need = Need.where(need_id: need_id).first | ||
if need.nil? | ||
errors.add(:need_id, "A note must have a valid need_id") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require_relative '../test_helper' | ||
|
||
class NotesControllerTest < ActionController::TestCase | ||
|
||
setup do | ||
login_as_stub_user | ||
@need = FactoryGirl.create(:need) | ||
@note = { | ||
text: "test", | ||
need_id: @need.need_id, | ||
author: { | ||
name: stub_user.name, | ||
email: stub_user.email | ||
} | ||
} | ||
end | ||
|
||
context "POST create" do | ||
should "save the note" do | ||
post :create, @note | ||
|
||
note = Note.first | ||
|
||
assert_equal 201, response.status | ||
assert_equal "test", note.text | ||
assert_equal @need.need_id, note.need_id | ||
assert_equal stub_user.name, note.author["name"] | ||
assert_equal stub_user.email, note.author["email"] | ||
end | ||
|
||
should "show errors if a note is not valid" do | ||
post :create, @note.except(:text) | ||
|
||
body = JSON.parse(response.body) | ||
assert_equal 422, response.status | ||
assert_equal "invalid_attributes", body["_response_info"]["status"] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require_relative '../test_helper' | ||
|
||
class NoteTest < ActiveSupport::TestCase | ||
setup do | ||
@need = FactoryGirl.create(:need) | ||
@atts = { | ||
text: "test", | ||
need_id: @need.need_id, | ||
author: { | ||
name: "Winston Smith-Churchill", | ||
email: "winston@alphagov.co.uk", | ||
uid: "win5t0n" | ||
} | ||
} | ||
end | ||
|
||
should "be able to create a note" do | ||
Need.expects(:where).twice.returns([@need]) | ||
|
||
note = Note.new(@atts) | ||
note.save | ||
note.reload | ||
|
||
assert_equal "test", note.text | ||
assert_equal "Winston Smith-Churchill", note.author["name"] | ||
assert_equal @need.revisions.first.id.to_s, note.revision | ||
end | ||
|
||
should "not save a note without a need_id" do | ||
note = Note.new(@atts.except(:need_id)) | ||
|
||
refute note.save | ||
assert note.errors.has_key?(:need_id) | ||
end | ||
|
||
should "not save a note without a valid need_id" do | ||
note = Note.new(@atts.merge(need_id: :foo)) | ||
|
||
refute note.save | ||
assert note.errors.has_key?(:need_id) | ||
end | ||
|
||
should "not save a note without text" do | ||
note = Note.new(@atts.except(:text)) | ||
|
||
refute note.save | ||
assert note.errors.has_key?(:text) | ||
end | ||
|
||
should "not save a note without an author" do | ||
note = Note.new(@atts.except(:author)) | ||
|
||
refute note.save | ||
assert note.errors.has_key?(:author) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,20 @@ class ChangesetPresenterTest < ActiveSupport::TestCase | |
created_at: Time.parse("2013-01-01") | ||
) | ||
|
||
note = OpenStruct.new( | ||
text: "test", | ||
author: { | ||
name: "Sir John Anderson", | ||
email: "jock@alphagov.co.uk", | ||
uid: "j0hn" | ||
}, | ||
created_at: Time.parse("2013-01-02") | ||
) | ||
|
||
@changeset = OpenStruct.new( | ||
current: current, | ||
changes: { role: [ "user", "home owner" ] } | ||
changes: { role: [ "user", "home owner" ] }, | ||
notes: [note] | ||
) | ||
end | ||
|
||
|
@@ -35,4 +46,15 @@ class ChangesetPresenterTest < ActiveSupport::TestCase | |
|
||
assert_equal ["user", "home owner"], response[:changes][:role] | ||
end | ||
|
||
should "return notes as part of the changset" do | ||
response = ChangesetPresenter.new(@changeset).present | ||
|
||
note = response[:notes][0] | ||
assert_equal "test", note[:text] | ||
assert_equal "Sir John Anderson", note[:author][:name] | ||
assert_equal "jock@alphagov.co.uk", note[:author][:email] | ||
assert_equal "j0hn", note[:author][:uid] | ||
assert_equal Time.parse("2013-01-02"), note[:created_at] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you split these note assertions into a separate should block ? i feel notes cannot be considered as basic attributes of a need. |
||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this class should have some validations