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

jl & wth - Redcap API #225

Merged
merged 1 commit into from
Apr 21, 2017
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
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@
//= require surveyor/surveys
//= require sweetalert.min
//= require update_visit_group
//= require feedback
23 changes: 23 additions & 0 deletions app/assets/javascripts/feedback.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
$.fn.clear_form_errors = () ->
this.find('.form-group').removeClass('has-error')
this.find('span.help-block').remove()

$.fn.render_form_errors = (model_name, errors) ->
form = this
this.clear_form_errors()

$.each(errors, (field, messages) ->
input = form.find('input, select, textarea').filter(->
name = $(this).attr('name')
if name
name.match(new RegExp(model_name + '\\[' + field + '\\(?'))
)
input.closest('.form-group').addClass('has-error')
input.parent().append('<span class="help-block">' + $.map(messages, (m) -> m.charAt(0).toUpperCase() + m.slice(1)).join('<br />') + '</span>')
)

$ ->

$('#modal_place').on 'shown.bs.modal', ->
$('.new_feedback').ajaxError (e, data) ->
$('.new_feedback').render_form_errors('feedback', JSON.parse(data.responseText))
13 changes: 8 additions & 5 deletions app/controllers/feedback_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ def new

def create
@feedback = Feedback.new(feedback_params)
if @feedback.valid?
emitter = RedcapSurveyEmitter.new(@feedback)
emitter.send_form
else
render :new
respond_to do |format|
if @feedback.valid?
emitter = RedcapSurveyEmitter.new(@feedback)
emitter.send_form
format.js
else
format.json { render json: @feedback.errors, status: :unprocessable_entity }
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions app/views/feedback/create.js.coffee
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<% if @feedback.valid? %>
$('#modal_place').modal('hide')
<% end %>
2 changes: 1 addition & 1 deletion lib/redcap_survey_emitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def send_form
:letters => Digest::SHA1.hexdigest(Time.now.usec.to_s)[0..16],
:name => @feedback.name,
:email => @feedback.email,
:date => @feedback.date.to_datetime.strftime("%Y/%m/%d").gsub('/', '-'),
:date => Date.strptime(@feedback.date[0..9], '%m/%d/%Y').strftime("%Y/%m/%d").gsub('/', '-'),
:typeofrequest => @feedback.typeofrequest,
:priority => @feedback.priority,
:browser => @feedback.browser,
Expand Down