Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RoR: Enums, how to list recipients of a message based on them

I have an application that allows a user to send a message to other users. I have two user types defined as enums in user rb- teacher and student:

enum access_level: [:student, :teacher]

I am wondering how to get the desired recipients to appear in a list in the view (below) so that a teacher can only send to students or the other way round.

In my messages controller I have:

class MessagesController < ApplicationController
  before_action :authenticate_user!

  def new
    @chosen_recipient = User.find_by(id: params[:to].to_i) if params[:to]
  end

  def create
    recipients = User.where(id: params['recipients'])
    conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation
    flash[:success] = "Message has been sent!"
    redirect_to conversation_path(conversation)
  end
end

And my conversations controller:

class ConversationsController < ApplicationController
  before_action :authenticate_user!
  before_action :get_mailbox
  before_action :get_conversation, except: [:index, :empty_trash]
  before_action :get_box, only: [:index]

  def index
    if @box.eql? "inbox"
      @conversations = @mailbox.inbox
    elsif @box.eql? "sent"
      @conversations = @mailbox.sentbox
    else
      @conversations = @mailbox.trash
    end

    @conversations = @conversations.paginate(page: params[:page], per_page: 10)
  end

  def show
  end

  def mark_as_read
    @conversation.mark_as_read(current_user)
    flash[:success] = 'The conversation was marked as read.'
    redirect_to conversations_path
  end

  def reply
    current_user.reply_to_conversation(@conversation, params[:body])
    flash[:success] = 'Reply sent'
    redirect_to conversation_path(@conversation)
  end

  def destroy
    @conversation.move_to_trash(current_user)
    flash[:success] = 'The conversation was moved to trash.'
    redirect_to conversations_path
  end

  def restore
    @conversation.untrash(current_user)
    flash[:success] = 'The conversation was restored.'
    redirect_to conversations_path
  end

  def empty_trash
    @mailbox.trash.each do |conversation|
      conversation.receipts_for(current_user).update_all(deleted: true)
    end
    flash[:success] = 'Your trash was cleaned!'
    redirect_to conversations_path
  end

  private

  def get_mailbox
    @mailbox ||= current_user.mailbox
  end

  def get_conversation
    @conversation ||= @mailbox.conversations.find(params[:id])
  end

  def get_box
    if params[:box].blank? or !["inbox","sent","trash"].include?(params[:box])
      params[:box] = 'inbox'
    end
    @box = params[:box]
  end
end

My view (messages/_form.html.erb):

<%= form_tag messages_path, method: :post do %>
  <div class="form-group">
    <%= label_tag 'message[subject]', 'Subject' %>
    <%= text_field_tag 'message[subject]', nil, class: 'form-control', required: true %>
  </div>

  <div class="form-group">
    <%= label_tag 'message[body]', 'Message' %>
    <%= text_area_tag 'message[body]', nil, cols: 3, class: 'form-control', required: true %>
  </div>

  <div class="form-group">
    <%= label_tag 'recipients', 'Choose recipients' %>
    <%= select_tag 'recipients', recipients_options(@chosen_recipient), multiple: true, class: 'form-control chosen-it' %>
  </div>

  <%= submit_tag 'Send', class: 'btn btn-primary' %>
<% end %>

How would I get the list to appear based on the enum attribute associated with the user? A teacher could only see students for example.

Appreciate any guidance. Thanks.

like image 276
Reynaldo Reyes Avatar asked Nov 25 '25 08:11

Reynaldo Reyes


1 Answers

Here are the methods given by the enum,

class User < ActiveRecord::Base
  enum access_level: [ :student, :teacher ]
end


user.student!
user.student? # => true
user.access_level  # => "student"

user.teacher!
user.teacher? # => true
user.access_level  # => "teacher"

So you can use,

def new
  if params[:to].present?
   render text: params and return false
    @chosen_recipient = current_user.student? ? check_access_level('teacher') : check_access_level('student')
  end 
end

private

def check_access_level(access_level)
  User.where(id: params[:to].to_i, access_level: access_level)
end
like image 66
Sravan Avatar answered Nov 27 '25 21:11

Sravan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!