Skip to content

Instantly share code, notes, and snippets.

@chocnut
Created January 15, 2012 23:23
Show Gist options
  • Save chocnut/1617946 to your computer and use it in GitHub Desktop.
Save chocnut/1617946 to your computer and use it in GitHub Desktop.
Multiple Filter via Scope
class Availability < ActiveRecord::Base
attr_accessible :name, :hospital_tokens, :start_at, :end_at, :shift
has_many :appointments
has_many :hospitals, :through => :appointments
belongs_to :user
has_event_calendar
attr_reader :hospital_tokens
scope :by_hospital, lambda { |hospital| selected_hospital(hospital) }
scope :by_date, lambda { |date| where("start_at >= ? AND start_at <= ?", date, date) }
scope :by_specialty, lambda { |specialty| joins(:user => [{:profile => :specialties}]).where("specialty_id = ?", specialty) }
scope :by_shift,lambda { |shift| where("shift = ?", shift) }
def hospital_tokens=(ids)
self.hospital_ids = ids.split(",")
end
private
def self.selected_hospital(hospital)
where("hospital_id IN (?)", hospital).joins(:appointments)
end
end
class Order < ActiveRecord::Base
belongs_to :hospital
belongs_to :specialty
validates_presence_of :shift, :hospital, :specialty, :shift_date
validates_presence_of :created_by, :status, :on => :create
validates_presence_of :updated_by, :status, :on => :update
end
class OrdersController < ApplicationController
load_and_authorize_resource
def index
@orders = Order.all
end
def create
params[:order][:shift_date] = Date.strptime(params[:order][:shift_date],'%m/%d/%Y')
@order = Order.create(params[:order])
@order.created_by = current_user.profile.full_name
@order.status = "Open"
if @order.save
flash[:notice] = "Order created"
redirect_to edit_order_path(@order)
else
flash[:alert] = "Please fill up required fields"
render :action => 'new'
end
end
def edit
@order = Order.find(params[:id])
@available_nurses = Availability.by_hospital(@order.hospital)
.by_date(@order.shift_date)
.by_shift(@order.shift)
.by_specialty(@order.specialty_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment