Skip to content

Instantly share code, notes, and snippets.

@SeanRoberts
Created October 6, 2015 17:30
Show Gist options
  • Save SeanRoberts/729fc7dbe8e3a6ee0088 to your computer and use it in GitHub Desktop.
Save SeanRoberts/729fc7dbe8e3a6ee0088 to your computer and use it in GitHub Desktop.
class DrugFinder
attr_accessor :user, :scope
def initialize(user, scope = Drug.all)
@user = user
@scope = scope
end
def drugs
if user.is_admin?
drugs_for_admin
else
drugs_for_regular_user
end
end
private
def drugs_for_admin
scope
end
def drugs_for_regular_user
scope.where(province_id: user.province_id)
end
end
# MEANWHILE, IN THE CONTROLLER
def index
@drugs = DrugFinder.new(current_user).drugs
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment