Last active
December 12, 2017 16:27
-
-
Save sirramongabriel/cb56bfb6428047b3d848f38444f25798 to your computer and use it in GitHub Desktop.
Unable to assign the medical_record_form's :id to Resident.medications, and assign Resident.medications as MedicalRecordForm.medications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MedicalRecordFormsController < ApplicationController | |
| before_action :account | |
| before_action :resident | |
| def index | |
| @medical_record_forms = resident.medical_record_forms | |
| end | |
| def new | |
| @medical_record_form = resident.medical_record_forms.build | |
| @medications = resident.medications | |
| # Prepopulates medical_record_forms/new template with Resident.medications | |
| # The attempt to store the medical_record_form_id is unsuccessful here it appears | |
| @medications.each do |med| | |
| med.medical_record_form_id = params[:id] | |
| @medical_record_form.medications << med | |
| @medical_record_form.medications.each do |m| | |
| m.medical_record_form_id = params[:id] | |
| end | |
| end | |
| # @medical_record_form.medications.build | |
| end | |
| def create | |
| # I also tried create, and build on line 27 | |
| # This throws the following error: Couldn't find Medication with ID=241 for MedicalRecordForm with ID= | |
| @medical_record_form = resident.medical_record_forms.new(medical_record_form_params) | |
| @medications = resident.medications | |
| # Another attempt to assign the medical_record_form_id to each Resident.medication | |
| @medications.each do |med| | |
| med.medical_record_form_id = params[:id] | |
| @medical_record_form.medications << med | |
| end | |
| @medical_record_form.save! | |
| if @medical_record_form.save | |
| flash[:success] = "Medical record was created!" | |
| redirect_to([resident, @medical_record_form]) | |
| else | |
| flash.now[:error] = "There was a problem with the form." | |
| render(:new) | |
| end | |
| end | |
| def show | |
| @medical_record_form = resident.medical_record_forms.find(params[:id]) | |
| @tmp_vars[:css_file] = "/../app/assets/stylesheets/nifty/bootstrap.min" | |
| pdf_file_name = "#{resident.first_name.downcase}_#{resident.last_name.downcase}_medication_record_#{@medical_record_form.created_at.month}_#{@medical_record_form.created_at.day}_#{@medical_record_form.created_at.year}" | |
| respond_to do |format| | |
| format.html | |
| format.pdf do | |
| render pdf: "show", | |
| template: 'medical_record_forms/show.pdf.erb', | |
| layout: "pdf", # or false | |
| page_size: "letter", | |
| no_background: true, | |
| title: pdf_file_name, | |
| locals: @tmp_vars | |
| end | |
| end | |
| end | |
| def edit | |
| @medical_record_form = resident.medical_record_forms.find(params[:id]) | |
| med_by_group | |
| end | |
| def update | |
| @medical_record_form = resident.medical_record_forms.find(params[:id]) | |
| if @medical_record_form.update_attributes(medical_record_form_params) | |
| flash[:success] = "Medical record updated!" | |
| redirect_to([resident, @medical_record_form]) | |
| else | |
| flash.now[:error] = "Unable to update form." | |
| end | |
| end | |
| def destroy | |
| @medical_record_form = resident.medical_record_forms.find(params[:id]) | |
| flash.now[:warning] = "You sure?" | |
| @medical_record_form.delete | |
| redirect_to(resident_medical_record_forms_path(resident)) | |
| end | |
| private | |
| def med_by_group | |
| resident.medications.each do |med| | |
| current_time = Time.zone.now | |
| if med.give_at_morning == true && med.time_at_morning?(current_time) | |
| @medications = resident.medications.where(account_id: resident.account_id, resident_id: resident.id, give_at_morning: true) | |
| elsif med.give_at_mid_day == true && med.time_at_mid_day?(current_time) | |
| @medications = resident.medications.where(account_id: resident.account_id, resident_id: resident.id, give_at_mid_day: true) | |
| elsif med.give_at_evening == true && med.time_at_evening?(current_time) | |
| @medications = resident.medications.where(account_id: resident.account_id, resident_id: resident.id, give_at_evening: true) | |
| else | |
| "This isn't bullet-proof yet, kind sire." | |
| end | |
| end | |
| end | |
| def account | |
| @account = Account.where(id: current_account.id).first | |
| end | |
| helper_method :account | |
| def employee | |
| @employee = Employee.where(account_id: current_account.id, id: resident.employee_id).first | |
| end | |
| helper_method :employee | |
| def resident | |
| @resident = Resident.where(account_id: account.id, id: params[:resident_id]).first | |
| end | |
| helper_method :resident | |
| def medications | |
| @medications = med_by_group | |
| end | |
| def list_current_time_group | |
| current_time = Time.zone.now | |
| if Medication.time_at_morning?(current_time) | |
| "Morning" | |
| elsif Medication.time_at_mid_day?(current_time) | |
| "Mid-day" | |
| elsif Medication.time_at_evening?(current_time) | |
| "Evening" | |
| else | |
| "Something isn't right, the 3 groups should cover a 24 hour period" | |
| end | |
| end | |
| helper_method :list_current_time_group | |
| def medical_record_form_params | |
| medical_record_form_params = params[:medical_record_form] | |
| medical_record_form_params ? medical_record_form_params.permit( | |
| :account_id, | |
| :employee_id, | |
| :resident_id, | |
| :date_of_record, | |
| medications_attributes: | |
| [ | |
| :account_id, | |
| :employee_id, | |
| :resident_id, | |
| :medical_record_form_id, | |
| :id, | |
| :_destroy, | |
| :name, | |
| :dosage, | |
| :time_taken, | |
| :give_at_morning, | |
| :give_at_mid_day, | |
| :give_at_evening, | |
| :instructions, | |
| :usage_type, | |
| :date_of_record | |
| ] | |
| ) : { } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment