Created
October 6, 2015 17:30
-
-
Save SeanRoberts/729fc7dbe8e3a6ee0088 to your computer and use it in GitHub Desktop.
This file contains 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 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