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

KG - Response Filter Updates #1398

Merged
merged 4 commits into from
Jul 11, 2018
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
12 changes: 12 additions & 0 deletions app/assets/javascripts/surveyor/responses.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,15 @@ $(document).ready ->
$("#for-Form").addClass('hidden')
$("#for-Form .selectpicker").selectpicker('deselectAll')
$("#for-SystemSurvey").removeClass('hidden')

$(document).on 'change', '#filterrific_with_state', ->
selected = $(this).find('option:selected')

if selected.length == 1
selected_value = selected.val()

$(".survey-select option:not([data-active='#{selected_value}'])").prop('disabled', true)
$('.survey-select').selectpicker('refresh')
else
$('.survey-select option').prop('disabled', false)
$('.survey-select').selectpicker('refresh')
37 changes: 29 additions & 8 deletions app/controllers/surveyor/responses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,26 @@ def index
}
) || return

@type = @filterrific.of_type.constantize.yaml_klass
@responses =
if @type == Survey.name
@filterrific.find.eager_load(:survey, :question_responses, :identity)
else
@filterrific.find.eager_load(:survey, :question_responses, :identity).
where(survey: Form.for(current_user))
end
@type = @filterrific.of_type.constantize.yaml_klass

respond_to do |format|
format.html
format.js
format.json {
@responses =
if @type == Survey.name
@filterrific.find.eager_load(:survey, :question_responses, :identity)
else
existing_responses = @filterrific.find.eager_load(:survey, :question_responses, :identity).
where(survey: Form.for(current_user))

if @filterrific.include_incomplete == 'false'
existing_responses
else
incomplete_responses = get_incomplete_form_responses
existing_responses + incomplete_responses
end
end
preload_responses
}
# format.xlsx
Expand Down Expand Up @@ -174,4 +181,18 @@ def determine_type_rights

types
end

def get_incomplete_form_responses
responses = []

Protocol.eager_load(sub_service_requests: [:responses, :service_forms, :organization_forms]).distinct.each do |p|
p.sub_service_requests.each do |ssr|
ssr.forms_to_complete.each do |f|
responses << Response.new(survey: f,respondable: ssr)
end
end
end

responses
end
end
4 changes: 2 additions & 2 deletions app/helpers/surveyor/responses_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def response_options(response)
def view_response_button(response)
link_to(
content_tag(:span, '', class: 'glyphicon glyphicon-search', aria: { hidden: 'true' }),
surveyor_response_path(response),
response.new_record? ? '' : surveyor_response_path(response),
remote: true,
class: ['btn btn-info view-response', response.completed? ? '' : 'disabled']
)
Expand All @@ -45,7 +45,7 @@ def view_response_button(response)
def edit_response_button(response)
link_to(
content_tag(:span, '', class: 'glyphicon glyphicon-edit', aria: { hidden: 'true' }),
edit_surveyor_response_path(response),
response.new_record? ? '' : edit_surveyor_response_path(response),
remote: true,
class: ['btn btn-warning edit-response', response.completed? ? '' : 'disabled']
)
Expand Down
13 changes: 7 additions & 6 deletions app/models/sub_service_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class SubServiceRequest < ApplicationRecord
has_many :notifications, :dependent => :destroy
has_many :subsidies
has_many :responses, as: :respondable, dependent: :destroy
has_many :service_forms, -> { active }, through: :services, source: :forms
has_many :organization_forms, -> { active }, through: :organization, source: :forms
has_one :approved_subsidy, :dependent => :destroy
has_one :pending_subsidy, :dependent => :destroy

Expand Down Expand Up @@ -409,22 +411,21 @@ def generate_approvals current_user, params
### FORMS ###
#############
def forms_to_complete
Form.where(surveyable: self.services).where.not(id: self.responses.pluck(:survey_id)).active +
Form.where(surveyable: self.organization).where.not(id: self.responses.pluck(:survey_id)).active
completed_ids = self.responses.pluck(:survey_id)

(self.service_forms + self.organization_forms).select{ |f| !completed_ids.include?(f.id) }
end

def form_completed?(form)
self.responses.where(survey: form).any?
end

def has_completed_forms?
Response.where(respondable: self, survey: Form.where(surveyable: self.services).active.ids + Form.where(surveyable: self.organization).active.ids).any?
self.responses.where(survey: self.service_forms + self.organization_forms).any?
end

def all_forms_completed?
form_ids = Form.where(surveyable: self.services).active.ids +
Form.where(surveyable: self.organization).active.ids
Response.where(respondable: self, survey_id: form_ids).count == form_ids.count
(self.service_forms + self.organization_forms).count == self.responses.count
end

##########################
Expand Down
16 changes: 14 additions & 2 deletions app/models/survey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,21 @@ class Survey < ApplicationRecord
'Multiple Dropdown': 'multiple_dropdown'
}

def self.for_dropdown_select
def self.for_dropdown_select(filtered_states=nil)
self.all.order(:title).group_by(&:title).map{ |title, surveys|
[title, surveys.map{ |survey| ["Version #{survey.version} (#{survey.active ? I18n.t(:surveyor)[:response_filters][:fields][:state_filters][:active] : I18n.t(:surveyor)[:response_filters][:fields][:state_filters][:inactive]})", survey.id] }]
[
title,
surveys.map{ |survey|
[
"Version #{survey.version} (#{survey.active ? I18n.t(:surveyor)[:response_filters][:fields][:state_filters][:active] : I18n.t(:surveyor)[:response_filters][:fields][:state_filters][:inactive]})",
survey.id,
{
disabled: filtered_states && !filtered_states.include?(survey.active ? 1 : 0),
data: { active: survey.active ? '1' : '0' }
}
]
}
]
}
end

Expand Down
4 changes: 2 additions & 2 deletions app/views/surveyor/responses/_filter_responses_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
.form-group.row{ id: "for-#{Form.name}", class: filterrific.of_type == Form.name ? "" : "hidden" }
= form.label :with_survey, Form.name, class: 'col-lg-3 control-label text-left'
.col-lg-9
= form.select :with_survey, grouped_options_for_select(Form.for(current_user).for_dropdown_select, filterrific.with_survey), {}, class: 'form-control selectpicker', multiple: true
= form.select :with_survey, grouped_options_for_select(Form.for(current_user).for_dropdown_select(filterrific.with_state), filterrific.with_survey), {}, class: 'form-control selectpicker survey-select', multiple: true, data: { hide_disabled: 'true' }
.form-group.row{ id: "for-#{SystemSurvey.name}", class: filterrific.of_type == SystemSurvey.name ? "" : "hidden" }
= form.label :with_survey, Survey.name, class: 'col-lg-3 control-label text-left'
.col-lg-9
= form.select :with_survey, grouped_options_for_select(SystemSurvey.unscoped.for_dropdown_select, filterrific.with_survey), {}, class: 'form-control selectpicker', multiple: true
= form.select :with_survey, grouped_options_for_select(SystemSurvey.unscoped.for_dropdown_select(filterrific.with_state), filterrific.with_survey), {}, class: 'form-control selectpicker survey-select', multiple: true, data: { hide_disabled: 'true' }
.form-group.row
.col-sm-12
%label
Expand Down
2 changes: 1 addition & 1 deletion app/views/surveyor/responses/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
#filter-responses
= render 'surveyor/responses/filter_responses_form', filterrific: @filterrific, type: @type
.col-lg-9
= render 'surveyor/responses/table', responses: @responses, type: @type
= render 'surveyor/responses/table', type: @type
2 changes: 1 addition & 1 deletion app/views/surveyor/responses/index.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# 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.
$('#filter-responses').html("<%= j render 'surveyor/responses/filter_responses_form', filterrific: @filterrific, type: @type %>")
$('#responses-panel').replaceWith("<%= j render 'surveyor/responses/table', responses: @responses, type: @type %>")
$('#responses-panel').replaceWith("<%= j render 'surveyor/responses/table', type: @type %>")
$('#responses-table').bootstrapTable()
$('.selectpicker').selectpicker()
$(".datetimepicker:not(.time)").datetimepicker(format: 'MM/DD/YYYY', allowInputToggle: true)
2 changes: 1 addition & 1 deletion app/views/surveyor/responses/index.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ json.(@responses) do |response|
json.srid srid == 'N/A' ? 'N/A' : link_to(srid, dashboard_protocol_path(srid.to_s.split('-').first), target: :blank)
json.short_title response.try(:respondable).try(:protocol).try(:short_title) || 'N/A'
json.primary_pi response.try(:respondable).try(:protocol).try(:primary_principal_investigator).try(:full_name) || 'N/A'
json.title response.survey.title
json.title response.survey.full_title
json.by response.identity.try(:full_name) || 'N/A'
json.complete complete_display(response)
json.completion_date format_date(response.created_at)
Expand Down
22 changes: 1 addition & 21 deletions spec/controllers/surveyor/responses/get_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@
expect(assigns(:type)).to eq('Survey')
end

it 'should assign @responses' do
@resp = create(:response, survey: create(:system_survey))
create(:question_response, response: @resp)

get :index, params: {}, format: :html

expect(assigns(:responses)).to be_a(ActiveRecord::Relation)
expect(assigns(:responses).first).to eq(@resp)
end

it 'should respond ok' do
get :index, params: {}, format: :html

Expand Down Expand Up @@ -97,16 +87,6 @@
expect(assigns(:type)).to eq('Survey')
end

it 'should assign @responses' do
@resp = create(:response, survey: create(:system_survey))
create(:question_response, response: @resp)

get :index, params: {}, format: :js, xhr: true

expect(assigns(:responses)).to be_a(ActiveRecord::Relation)
expect(assigns(:responses).first).to eq(@resp)
end

it 'should respond ok' do
get :index, params: {}, format: :js, xhr: true

Expand Down Expand Up @@ -176,7 +156,7 @@
create(:question_response, response: resp2)
create(:super_user, identity: logged_in_user, organization: org1)

get :index, params: { type: 'Form' }
get :index, params: { type: 'Form' }, format: :json

expect(assigns(:responses).count).to eq(1)
expect(assigns(:responses).first).to eq(resp1)
Expand Down
34 changes: 26 additions & 8 deletions spec/features/surveyor/responses/user_filters_responses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@
describe 'incomplete filter' do
before :each do
@other_survey = create(:system_survey, title: 'Hollywoo Stars and Celebrities. Do they know things? What do they know? Let\'s find out', active: true)
survey_response = create(:response, survey: @survey, updated_at: Time.now - 5.days)
other_response = create(:response, survey: @other_survey, updated_at: Time.now + 5.days)
survey_response = create(:response, survey: @survey)
other_response = create(:response, survey: @other_survey)
create(:question_response, response: survey_response)
# other_response is incomplete
end
Expand All @@ -213,13 +213,31 @@
end

context 'user filters including incomplete responses' do
scenario 'and sees both complete and incomplete responses' do
find('#filterrific_include_incomplete').click
click_button I18n.t(:actions)[:filter]
wait_for_javascript_to_finish
context 'for surveys' do
scenario 'and sees both complete and incomplete responses' do
find('#filterrific_include_incomplete').click
click_button I18n.t(:actions)[:filter]
wait_for_javascript_to_finish

expect(page).to have_selector('td', text: @survey.title)
expect(page).to have_selector('td', text: @other_survey.title)
end
end

expect(page).to have_selector('td', text: @survey.title)
expect(page).to have_selector('td', text: @other_survey.title)
context 'for forms' do
scenario 'and sees both complete and incomplete responses' do
@other_form = create(:form, surveyable: @organization, title: 'Formula One', active: true)
other_response = create(:response, survey: @other_form)
ssr = create(:sub_service_request, organization: @organization)

bootstrap_select '#filterrific_of_type', Form.name
find('#filterrific_include_incomplete').click
click_button I18n.t(:actions)[:filter]
wait_for_javascript_to_finish

expect(page).to have_selector('td', text: @form.title)
expect(page).to have_selector('td', text: @other_form.title)
end
end
end
end
Expand Down