Sha256: c88a3f9eef0ececf123fd93dfb1f6c40129220cb4de2f59fbb7b34a922ab2870

Contents?: true

Size: 1.67 KB

Versions: 31

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module Renalware
  module Patients
    class BookmarksController < BaseController
      include Concerns::Pageable
      before_action :load_patient, only: :create

      # Display the user's bookmarks
      def index
        search = BookmarksQuery.new(
          default_relation: Patients.cast_user(current_user).bookmarks,
          params: params[:q]
        ).search

        bookmarks = search.result.page(page).per(per_page)
        authorize bookmarks
        render locals: { bookmarks: bookmarks, search: search }
      end

      # idempotent
      def create
        Bookmark.find_or_create_by!(user: user, patient: patient) do |bookmark|
          bookmark.assign_attributes(bookmark_params)
        end
        redirect_back(fallback_location: patient_path(patient),
                      notice: success_msg_for("bookmark"))
      end

      # idempotent
      def destroy
        bookmark = user.bookmarks.find_by(id: params[:id])
        patient = bookmark&.patient
        if bookmark.present?
          authorize bookmark
          bookmark.destroy
        else
          skip_authorization
        end
        fallback_location = patient.presence || root_path
        redirect_back(fallback_location: patient_path(fallback_location),
                      notice: success_msg_for("bookmark"))
      end

      private

      def user
        @user ||= Renalware::Patients.cast_user(current_user)
      end

      def bookmark_params
        params.require(:patients_bookmark).permit(:notes, :urgent, :tags)
      end

      def search_params
        hash = params[:q] || {}
        hash[:s] ||= "created_at desc"
        hash
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
renalware-core-2.0.147 app/controllers/renalware/patients/bookmarks_controller.rb
renalware-core-2.0.146 app/controllers/renalware/patients/bookmarks_controller.rb
renalware-core-2.0.145 app/controllers/renalware/patients/bookmarks_controller.rb
renalware-core-2.0.144 app/controllers/renalware/patients/bookmarks_controller.rb
renalware-core-2.0.143 app/controllers/renalware/patients/bookmarks_controller.rb
renalware-core-2.0.142 app/controllers/renalware/patients/bookmarks_controller.rb
renalware-core-2.0.141 app/controllers/renalware/patients/bookmarks_controller.rb
renalware-core-2.0.140 app/controllers/renalware/patients/bookmarks_controller.rb
renalware-core-2.0.139 app/controllers/renalware/patients/bookmarks_controller.rb
renalware-core-2.0.138 app/controllers/renalware/patients/bookmarks_controller.rb
renalware-core-2.0.137 app/controllers/renalware/patients/bookmarks_controller.rb