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

fixing error with empty note creation, and refactoring bad code [#130… #668

Merged
merged 2 commits into from
Sep 19, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions app/assets/javascripts/document_management.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ $(document).ready ->
$(this).hide()

$("#save_request_note").click ->
data = note:
notable_id: $(this).data('notable-id')
notable_type: $(this).data('notable-type')
body: $("#new_note_text").val()
data =
in_proper: true
note:
notable_id: $(this).data('notable-id')
notable_type: $(this).data('notable-type')
body: $("#new_note_text").val()
$("#new_note_text").val("")
$.ajax
type: 'POST'
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/dashboard/notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ def new
end

def create
if note_params[:body].present? # don't create empty notes
@note = Note.create(note_params.merge(identity_id: current_user.id))
@note = Note.new(note_params.merge(identity_id: current_user.id))
if @note.save
@selector = "#{@note.unique_selector}_notes"
else
@errors = @note.errors
end

@in_proper = params[:in_proper]
@notes = @notable.notes
end

Expand Down
1 change: 1 addition & 0 deletions app/views/dashboard/notes/_index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
%h4.modal-title.text-center
= notable_type.underscore.humanize.titleize + t(:dashboard)[:notes][:index][:header]
.modal-body
#modal_errors
- if notes.any?
.details-table
.details-header.text-center
Expand Down
27 changes: 18 additions & 9 deletions app/views/dashboard/notes/create.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
if $(".modal-content").length > 0
# dashboard notes modals
$("#modal_place").html("<%= escape_javascript(render(partial: 'index', locals: { notable: @notable, notes: @notes, notable_id: @notable_id, notable_type: @notable_type })) %>")
$("span#<%= @selector %>").html("<%= escape_javascript(@notes.count.to_s) %>")
else
#sparc proper service request notes in step 3
$("#request_notes_table").html("<%= escape_javascript(render(partial: '/notes/request_notes', locals: { notes: @notes, thead_class: 'orange-provider' } )) %>")
$('.new_request_note_button').show()
$('#note_form').hide()

<% if @in_proper %>
#sparc proper service request notes in step 3
$("#request_notes_table").html("<%= escape_javascript(render(partial: '/notes/request_notes', locals: { notes: @notes, thead_class: 'orange-provider' } )) %>")
$('.new_request_note_button').show()
$('#note_form').hide()
<% else %>

# dashboard notes modals
<% if @errors %>
$("#modal_errors").html("<%= escape_javascript(render(partial: 'shared/modal_errors', locals: {errors: @errors})) %>")
<% else %>

$("#modal_place").html("<%= escape_javascript(render(partial: 'index', locals: { notable: @notable, notes: @notes, notable_id: @notable_id, notable_type: @notable_type })) %>")
$("span#<%= @selector %>").html("<%= escape_javascript(@notes.count.to_s) %>")
<% end %>

<% end %>
33 changes: 3 additions & 30 deletions spec/controllers/dashboard/notes/post_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,6 @@ def self.find(id); end # notable types should respond to #find
def notes; end # instance of notable types have Notes
end

context "params[:note][:body] empty" do
before(:each) do
@logged_in_user = build_stubbed(:identity)

@notable_obj = findable_stub(MyNotableType) do
MyNotableType.new(2)
end
allow(@notable_obj).to receive(:notes).and_return("all my notes")

allow(Note).to receive(:create)

log_in_dashboard_identity(obj: @logged_in_user)
xhr :get, :create, note: { identity_id: "-1", notable_type: "MyNotableType",
notable_id: "2", body: "" }
end

it "should not create a new Note" do
expect(Note).not_to have_received(:create)
end

it "should assign @notes to notable object's Notes" do
expect(assigns(:notes)).to eq("all my notes")
end

it { is_expected.to render_template "dashboard/notes/create" }
it { is_expected.to respond_with :ok }
end

context "params[:note][:body] not empty" do
before(:each) do
@logged_in_user = build_stubbed(:identity)
Expand All @@ -68,15 +40,16 @@ def notes; end # instance of notable types have Notes
@note = build_stubbed(:note)
allow(@notable_obj).to receive(:notes).and_return("all my notes")

allow(Note).to receive(:create).and_return(@note)
allow(Note).to receive(:new).and_return(@note)
allow(@note).to receive(:save)

log_in_dashboard_identity(obj: @logged_in_user)
xhr :get, :create, note: { identity_id: "-1", notable_type: "MyNotableType",
notable_id: "2", body: "this was notable" }
end

it "should create a new Note for current user with params[:note]" do
expect(Note).to have_received(:create).with({
expect(Note).to have_received(:new).with({
identity_id: @logged_in_user.id,
"notable_type" => "MyNotableType",
"notable_id" => "2",
Expand Down