Reputation: 6981
What is the best approach here? I'm trying to clean up some code and I'm wondering if the controller is the best place for this variety of logic:
if user_signed_in?
if current_user.try(:admin?)
@docs = Doc.chronologic.page(params[:page]).per(5)
@orders = Order.chronologic.page(params[:page]).per(5)
else
@docs = Doc.chronologic.where(:user_id => current_user.ftp, :retired => "active").page(params[:page]).per(5)
@orders = Order.chronologic.where(:user => current_user.ftp).page(params[:page]).per(5)
end
respond_to do |format|
format.html
format.json { render json: @docs }
end
else
redirect_to new_user_session_path
end
If there's a better location for it, where would it be?
Thanks!
Edit: it's far worse for methods like pdf
which has line after line of instructions for Prawn, but I can't seem to get send_data
to work from the model.
Upvotes: 0
Views: 84
Reputation:
This is basically what mu said, but here's my take.
In your app controller:
def require_logged_in
redirect_to new_user_session_path unless user_signed_in?
end
In your controller
before_filter :require_logged_in
def some_action
@docs = Doc.chronologic.for_user(current_user).page(params[:page]).per(5)
@orders = Order.chronologic.for_user(current_user).page(params[:page]).per(5)
respond_to do |format|
format.html
format.json { render json: @docs }
end
end
In your Doc model
scope :for_user, lambda do |user|
where(:user_id => user.ftp, :retired => "active") unless user.admin?
end
And something similar in your Order model.
Per your edit, definitely don't do send_data
from your model.
Upvotes: 2