Skip to content

Commit

Permalink
wth - RMID functionalities part 3
Browse files Browse the repository at this point in the history
Make AJAX request to RMID API to get short_title, long_title and pi_name
from entered RMID record. Fields are disabled upon successful request.
Implemented Gon gem to have access to our environment variables in our
assets. [#136746539]
  • Loading branch information
William Holt committed Jan 13, 2017
1 parent 23cb953 commit 2c64e0b
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ gem 'dynamic_form'
gem 'execjs'
gem 'exception_notification'
gem 'filterrific'
gem 'gon', '~> 6.1'
gem 'grape', '0.7.0'
gem 'grape-entity', '~> 0.4.4'
gem 'grouped_validations', :git => 'https://github.com/jleonardw9/grouped_validations.git', branch: 'master'
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ GEM
actionpack (>= 3.0)
globalid (0.3.6)
activesupport (>= 4.1.0)
gon (6.1.0)
actionpack (>= 3.0)
json
multi_json
request_store (>= 1.0)
grape (0.7.0)
activesupport
builder
Expand Down Expand Up @@ -371,6 +376,7 @@ GEM
rake (11.3.0)
redcarpet (3.3.3)
remotipart (1.2.1)
request_store (1.3.2)
responders (2.3.0)
railties (>= 4.2.0, < 5.1)
rest-client (1.8.0)
Expand Down Expand Up @@ -540,6 +546,7 @@ DEPENDENCIES
factory_girl_rails
faker
filterrific
gon (~> 6.1)
grape (= 0.7.0)
grape-entity (~> 0.4.4)
grouped_validations!
Expand Down
32 changes: 32 additions & 0 deletions app/assets/javascripts/protocol_form.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
# 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.

toggleFields = (fields, state) ->
$(fields).prop('disabled', state)

resetRmIdFields = (fields, value) ->
$(fields).val(value)

$(document).ready ->

$(document).on 'click', '.human-subjects', ->
Expand All @@ -28,6 +34,32 @@ $(document).ready ->
$('.rm-id').addClass('required-field')
$('.has-human-subject-info').val('true')

$(document).on 'blur', '.research-master-field', ->
rmId = $('.research-master-field').val()
unless $(this).val() == ''
$.ajax
url: "#{gon.rm_id_api_url}research_masters/#{rmId}.json"
type: 'GET'
headers: {"Authorization": "Token token=\"#{gon.rm_id_api_token}\""}
success: (data) ->
$('#protocol_short_title').val(data.short_title)
$('#protocol_title').val(data.long_title)
$('#protocol_project_roles_attributes_0_identity_id').val(data.pi_name)
toggleFields('.rm-locked-fields', true)
error: ->
swal("Error", "Research Master Record not found", "error")
resetRmIdFields('.rm-id-dependent', '')
toggleFields('.rm-locked-fields', false)

$(document).on 'change', '.research-master-field', ->
if $(this).val() == ''
resetRmIdFields('.rm-id-dependent', '')
toggleFields('.rm-locked-fields', false)

$('#new_protocol').bind 'submit', ->
$(this).find(':input').prop('disabled', false)


# Protocol Edit Begin
$(document).on 'click', '#protocol-type-button', ->
protocol_id = $(this).data('protocol-id')
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/dashboard/protocols_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def new
@protocol.requester_id = current_user.id
@protocol.populate_for_edit
session[:protocol_type] = params[:protocol_type]
gon.rm_id_api_url = RESEARCH_MASTER_API
gon.rm_id_api_token = RMID_API_TOKEN
end

def create
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/protocols_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def new
@protocol.requester_id = current_user.id
@service_request = ServiceRequest.find(params[:srid])
@protocol.populate_for_edit
gon.rm_id_api_url = RESEARCH_MASTER_API
gon.rm_id_api_token = RMID_API_TOKEN
end

def create
Expand Down
4 changes: 2 additions & 2 deletions app/models/protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ def has_human_subject_info?
end

validate :existing_rm_id,
if: -> record { record.has_human_subject_info? && !record.research_master_id.nil? }
if: -> record { !record.research_master_id.nil? }

validate :unique_rm_id_to_protocol,
if: -> record { record.has_human_subject_info? && !record.research_master_id.nil? }
if: -> record { !record.research_master_id.nil? }

def existing_rm_id
rm_ids = HTTParty.get(RESEARCH_MASTER_API + 'research_masters.json', headers: {'Content-Type' => 'application/json', 'Authorization' => "Token token=\"#{RMID_API_TOKEN}\""})
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
= csrf_meta_tags
:javascript
window.I18n = #{current_translations.to_json.html_safe};
= Gon::Base.render_data

%body#body
- if @service_request
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/dashboard/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
= csrf_meta_tags
:javascript
window.I18n = #{current_translations.to_json.html_safe};
= Gon::Base.render_data

%body#body
#container
Expand Down
2 changes: 1 addition & 1 deletion app/views/protocols/form/_project_roles_fields.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
.form-group.row
= pr_form.label :identity_id, t(:protocols)[:authorized_users][:primary_pi], class: 'col-lg-2 control-label required'
.col-lg-10
= pr_form.text_field :identity_id, placeholder: t(:constants)[:prompts][:search_for_user], class: 'form-control'
= pr_form.text_field :identity_id, placeholder: t(:constants)[:prompts][:search_for_user], class: 'form-control rm-id-dependent'
%label#primary_pi_name.hidden
%a.btn.btn-xs.btn-danger.glyphicon.glyphicon-remove#user-select-clear-icon{ href: "javascript:void(0);", style: 'display: none; vertical-align: middle;' }
= pr_form.hidden_field :identity_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@
= form.label :research_master_id, class: 'col-lg-2 control-label rm-id' do
= link_to t(:protocols)[:studies][:information][:research_master_id], RESEARCH_MASTER_LINK
.col-lg-10
= form.text_field :research_master_id, class: 'form-control'
= form.text_field :research_master_id, class: 'form-control research-master-field'
= form.hidden_field :has_human_subject_info, value: false, class: 'has-human-subject-info'

.form-group.row
= form.label :short_title, t(:protocols)[:studies][:information][:short_title], class: 'col-lg-2 control-label required'
.col-lg-10
= form.text_field :short_title, class: 'form-control'
= form.text_field :short_title, class: 'form-control rm-id-dependent rm-locked-fields'

.form-group.row
= form.label :title, t(:protocols)[:studies][:information][:title], class: 'col-lg-2 control-label required'
.col-lg-10
= form.text_field :title, class: 'form-control'
= form.text_field :title, class: 'form-control rm-id-dependent rm-locked-fields'

.form-group.row#funding-status
= form.label :funding_status, t(:protocols)[:studies][:information][:funding_status], class: 'col-lg-2 control-label required'
Expand Down

0 comments on commit 2c64e0b

Please sign in to comment.