Skip to content

Instantly share code, notes, and snippets.

@jayroh
Created October 31, 2012 20:30
Show Gist options
  • Save jayroh/3989630 to your computer and use it in GitHub Desktop.
Save jayroh/3989630 to your computer and use it in GitHub Desktop.
small refactor
class Dashboards::MessagesController < ApplicationController
include DashboardsHelper
include ApplicationHelper
layout 'dashboard'
before_filter :user_must_be_signed_in
before_filter :must_be_approved_or_incomplete_if_coach
before_filter :set_whether_coach_client
def create
@coach = Coach.find(params[:message].delete(:coach))
converser = User.find_by_id(params[:message].delete(:converser))
message = @coach.messages.build(params[:message])
message.from_user = current_user
if converser.present?
message.to_user = converser
else
message.to_user = @coach.user
end
redirect_to :back, notice: save_and_give_notice_for(message)
end
def index
@recipients = User.find((Message.where(to_user_id: current_user.id).map(&:from_user_id) + Message.where(from_user_id: current_user.id).map(&:to_user_id)).uniq)
end
def show
@converser = User.find(params[:id])
@messages = Message.between_users(current_user, @converser).order("updated_at ASC")
@new_message = current_user.sent_messages.build
@coach = @messages.first.messageable
end
# these two could go into application_controller.rb so that
# they're available across the app.
def is_coach?
@is_coach ||= !current_user.coaches.blank?
end
def is_client?
@is_client ||= current_user.client
end
helper_method :is_coach?
helper_method :is_client?
# * * *
private
def save_and_give_notice_for(message)
if message.save!
"Your message will be delivered shortly. Thank you!"
else
"There was a problem sending your message. Please try again."
end
end
# def set_whether_coach_client
# @is_coach = !current_user.coaches.blank?
# @is_client = current_user.client
# end
def must_be_approved_or_incomplete_if_coach
@user = current_user
if current_user.coaches.count > 0 && !has_active_or_incomplete_coach(current_user)
redirect_to coach_applications_page_one_path
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment