Skip to content

Commit

Permalink
Shorten methods in MedicationsHelper and Input component, add Google …
Browse files Browse the repository at this point in the history
…Calendar reminder to print_reminders (#1219)

* Shorten methods in MedicationsHelper

* Shorten Input component

* Readd medication_weekly_dosage

* Add Google Calendar reminder to print_reminders
  • Loading branch information
julianguyen authored Oct 30, 2018
1 parent 3ff7666 commit b2877c6
Show file tree
Hide file tree
Showing 17 changed files with 345 additions and 271 deletions.
6 changes: 4 additions & 2 deletions app/helpers/medication_refill_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ def save_refill_to_google_calendar(medication)
end

def calendar_uploader_params(medication)
{ summary: "Refill for #{medication.name}",
{
summary: "Refill for #{medication.name}",
date: medication.refill,
access_token: current_user.google_access_token,
email: current_user.email }
email: current_user.email
}
end

private
Expand Down
285 changes: 158 additions & 127 deletions app/helpers/medications_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true

# rubocop:disable ModuleLength
module MedicationsHelper
include FormHelper
Expand All @@ -18,120 +17,151 @@ def medication_form_inputs
current_user.google_oauth2_enabled? ? google_fields : common_fields
end

# rubocop:disable MethodLength
def medication_basic_props(field)
{
id: "medication_#{field}",
name: "medication[#{field}]",
dark: true
}
end

def refill_reminder(field, info, checked, label = nil)
{
id: "medication_#{field}",
type: 'checkbox',
label: label || t("medications.#{field}"),
info: info,
name: "medication[#{field}][active]",
checked: checked
}.merge(dark: true, uncheckedValue: false, value: true)
end

def medication_refill_reminder
refill_reminder(
'refill_reminder_attributes',
t('medications.form.refill_reminder_hint'),
@medication&.refill_reminder&.active,
t('medications.refill_reminder')
)
end

def medication_take_medication_reminder_attributes
refill_reminder(
'take_medication_reminder_attributes',
t('medications.form.daily_reminder_hint'),
@medication&.take_medication_reminder&.active,
t('common.daily_reminder')
)
end

def hidden_fields
[
hidden_props(
'refill_reminder_attributes',
@medication&.refill_reminder&.id
),
hidden_props(
'take_medication_reminder_attributes',
@medication&.take_medication_reminder&.id
)
]
end

def reminder_fields
[
medication_refill_reminder,
medication_take_medication_reminder_attributes
].concat(hidden_fields)
end

def hidden_props(field, value)
{
id: "medication_#{field}_id",
name: "medication[#{field}][id]",
type: 'hidden',
value: value
}
end

def medication_weekly_dosage
{
type: 'checkboxGroup',
checkboxes: days_checkbox,
label: t('common.days'),
info: t('medications.form.weekly_dosage_hint'),
required: true
}.merge(medication_basic_props('weekly_dosage'))
end

def extra_fields
[medication_weekly_dosage, medication_refill, medication_comments]
end

def common_fields
[
{
id: 'medication_name',
type: 'text',
name: 'medication[name]',
label: t('common.name'),
value: @medication.name || nil,
required: true,
info: t('categories.form.name_hint'),
dark: true
},
medication_name,
medication_field('strength'),
{
id: 'medication_strength_unit',
type: 'select',
name: 'medication[strength_unit]',
value: @medication.strength_unit || t('medications.units.mg'),
dark: true,
options: [
{
id: 'medication_strength_unit_mg',
label: t('medications.units.mg'),
value: t('medications.units.mg')
},
{
id: 'medication_strength_unit_ml',
label: t('medications.units.ml'),
value: t('medications.units.ml')
}
]
},
medication_strength_unit,
medication_field('total'),
medication_unit_field('total'),
medication_field('dosage'),
medication_unit_field('dosage'),
{
id: 'medication_refill',
type: 'date',
name: 'medication[refill]',
label: t('medications.form.refill'),
value: @medication.refill || nil,
info: t('medications.form.refill_hint'),
required: true,
dark: true
},
{
id: 'medication_comments',
type: 'textarea',
name: 'medication[comments]',
label: t('comment.plural'),
value: @medication.comments || nil,
info: t('medications.form.comments_hint'),
dark: true
},
{
id: 'medication_refill_reminder',
type: 'checkbox',
dark: true,
label: t('medications.refill_reminder'),
info: t('medications.form.refill_reminder_hint'),
name: 'medication[refill_reminder_attributes][active]',
checked: @medication&.refill_reminder&.active,
uncheckedValue: false,
value: true
},
{
id: 'medication_take_medication_reminder',
type: 'checkbox',
dark: true,
label: t('common.daily_reminder'),
info: t('medications.form.daily_reminder_hint'),
name: 'medication[take_medication_reminder_attributes][active]',
checked: @medication&.take_medication_reminder&.active,
uncheckedValue: false,
value: true
},
{
id: 'medication_refill_reminder_attributes_id',
name: 'medication[refill_reminder_attributes][id]',
type: 'hidden',
value: @medication&.refill_reminder&.id
},
{
id: 'medication_take_medication_reminder_attributes_id',
name: 'medication[take_medication_reminder_attributes][id]',
type: 'hidden',
value: @medication&.take_medication_reminder&.id
}
]
medication_unit_field('dosage')
].concat(extra_fields).concat(reminder_fields)
end

def medication_comments
{
type: 'textarea',
label: t('comment.plural'),
value: @medication.comments || nil,
info: t('medications.form.comments_hint')
}.merge(medication_basic_props('comments'))
end

def medication_strength_unit
{
type: 'select',
value: @medication.strength_unit || t('medications.units.mg'),
options: [
medication_type_unit_mg('strength'),
medication_type_unit_ml('strength')
]
}.merge(medication_basic_props('strength_unit'))
end

def medication_name
{
type: 'text',
label: t('common.name'),
value: @medication.name || nil,
info: t('categories.form.name_hint'),
required: true
}.merge(medication_basic_props('name'))
end

def medication_refill
{
type: 'date',
label: t('medications.form.refill'),
value: @medication.refill || nil,
info: t('medications.form.refill_hint'),
required: true
}.merge(medication_basic_props('refill'))
end
# rubocop:enable MethodLength

# rubocop:disable MethodLength
def google_fields
common_fields.push(
id: 'medication_add_to_google_cal',
common_fields.push({
type: 'checkbox',
dark: true,
label: t('medications.form.add_to_google_cal'),
info: t('medications.form.google_cal_hint'),
name: 'medication[add_to_google_cal]',
checked: @medication.add_to_google_cal,
checked: @medication.add_to_google_cal || nil,
uncheckedValue: false,
value: true
)
}.merge(medication_basic_props('add_to_google_cal')))
end
# rubocop:enable MethodLength

# rubocop:disable MethodLength
def days_checkbox
week_days = 0.upto(6)
week_days.map do |i|
0.upto(6).map do |i|
{
id: "medication_weekly_dosage_#{i}",
type: 'checkbox',
Expand All @@ -142,52 +172,53 @@ def days_checkbox
}
end
end
# rubocop:enable MethodLength

# rubocop:disable MethodLength
def medication_type_unit_tablets(type)
{
id: "medication_#{type}_unit_tablets",
label: t('medications.units.tablets.other'),
value: t('medications.units.tablets.other')
}
end

def medication_type_unit_mg(type)
{
id: "medication_#{type}_unit_mg",
label: t('medications.units.mg'),
value: t('medications.units.mg')
}
end

def medication_type_unit_ml(type)
{
id: "medication_#{type}_unit_ml",
label: t('medications.units.ml'),
value: t('medications.units.ml')
}
end

def medication_unit_field(type)
{
id: "medication_#{type}_unit",
type: 'select',
name: "medication[#{type}_unit]",
dark: true,
value: @medication["#{type}_unit"] ||
t('medications.units.tablets.other'),
options: [
{
id: "medication_#{type}_unit_tablets",
label: t('medications.units.tablets.other'),
value: t('medications.units.tablets.other')
},
{
id: "medication_#{type}_unit_mg",
label: t('medications.units.mg'),
value: t('medications.units.mg')
},
{
id: "medication_#{type}_unit_ml",
label: t('medications.units.ml'),
value: t('medications.units.ml')
}
medication_type_unit_tablets(type),
medication_type_unit_mg(type),
medication_type_unit_ml(type)
]
}
}.merge(medication_basic_props("#{type}_unit"))
end
# rubocop:enable MethodLength

# rubocop:disable MethodLength
def medication_field(type)
{
id: "medication_#{type}",
type: 'number',
name: "medication[#{type}]",
label: t("medications.form.#{type}"),
value: @medication[type.to_s] || nil,
required: true,
info: t("medications.form.#{type}_hint"),
placeholder: t("medications.form.#{type}_placeholder"),
dark: true
}
required: true
}.merge(medication_basic_props(type))
end
# rubocop:enable MethodLength
end
# rubocop:enable ModuleLength
20 changes: 11 additions & 9 deletions app/helpers/reminder_helper.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
# frozen_string_literal: true

module ReminderHelper
def print_reminders(data)
return '' unless active_reminders?(data)
return '' unless reminders?(data)

names_arr = data.active_reminders.map(&:name)
reminders = format_reminders(names_arr)
reminders.html_safe
if data[:add_to_google_cal]
names_arr.push(t('medications.form.add_to_google_cal'))
end
format_reminders(names_arr).html_safe
end

private

def reminders?(data)
active_reminders?(data) || data[:add_to_google_cal]
end

def active_reminders?(data)
data.methods.include?(:active_reminders) && data.active_reminders&.any?
end

def format_reminders(reminder_names)
reminders = '<div>'
reminders += '<i class="fa fa-bell smallMarginRight"></i>'
reminders += join_names(reminder_names)
reminders += '</div>'
reminders
reminders = join_names(reminder_names)
"<div><i class=\"fa fa-bell smallMarginRight\"></i>#{reminders}</div>"
end

def join_names(reminder_names)
Expand Down
Loading

0 comments on commit b2877c6

Please sign in to comment.