限制邮箱会话在特定用户之间进行

时间:2016-02-25 18:15:39

标签: ruby-on-rails ruby ruby-on-rails-4 mailboxer

我已经按照这里的教程:http://www.sitepoint.com/messaging-rails-mailboxer/将mailboxer gem实现到我的应用程序中,并且它应该正常工作。但是在实现之后,我现在需要以某种方式限制用户之间的对话:

  • 如果当前用户是学生(枚举:1),则它只能向一个用户发送消息,而该用户只能在配对表中与之配对。
  • 如果当前用户是主管(枚举:2),则只能向配对表中与之配对的学生用户发送消息。

我想我需要对messages_helper模块进行更改,但我不知道究竟要做什么。

Model User.rb

<div class="container">    
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>      
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>  
            <div class="row">           
                <asp:LinkButton ID="BookAppointment" runat="server" Text="Book Appointment" OnClick="BookAppointment_Click"></asp:LinkButton>   
                <asp:LinkButton ID="ViewBookings" runat="server" Text="View Bookings" OnClick="ViewBookings_Click"></asp:LinkButton>   
                <asp:LinkButton ID="CancelBooking" runat="server" Text="Cancel Booking" OnClick="CancelBooking_Click"></asp:LinkButton> 
                <asp:LinkButton ID="SearchDoctor" runat="server" Text="Search Doctor" OnClick="SearchDoctor_Click" ></asp:LinkButton>                            
            </div>
            <div>
                <div style="width: 870px; float:right;"> 
                    <asp:Panel ID="Panel1" runat="server">
                    <asp:Label ID="Label5" runat="server" Text="Book Appointment : "></asp:Label>
                    <br />
                    <br />
                    <asp:Button ID="Button2" runat="server" Text="9.00 - 9.30" CssClass="btn btn-primary" Width="120px"  OnClick="Button2_Click"></asp:Button>  <asp:Button ID="Button3" runat="server" Text="9.30 - 10.00" CssClass="btn btn-primary" Width="120px"  OnClick="Button3_Click"></asp:Button>  <asp:Button ID="Button4" runat="server" Text="10.00 - 10.30" CssClass="btn btn-primary" Width="120px"  OnClick="Button4_Click"></asp:Button>  <asp:Button ID="Button5" runat="server" Text="10.30 - 11.00" CssClass="btn btn-primary" Width="120px"  OnClick="Button5_Click"></asp:Button>  <asp:Button ID="Button6" runat="server" Text="11.00 - 11.30" CssClass="btn btn-primary" Width="120px"  OnClick="Button6_Click"></asp:Button>  
                    <asp:Button ID="Button7" runat="server" Text="11.30 - 12.00" CssClass="btn btn-primary" Width="120px"  OnClick="Button7_Click"></asp:Button>
                    <br />
                    <br/>
                    <asp:Button ID="Button14" runat="server" Text="12.00 - 1.30 Break" CssClass="btn btn-danger" Enabled="False" Width="840px"></asp:Button>
                    <br />
                    <br />
                    <asp:Button ID="Button8" runat="server" Text="1.30 - 2.00" CssClass="btn btn-primary" Width="120px"  OnClick="Button8_Click"></asp:Button>  <asp:Button ID="Button9" runat="server" Text="2.00 - 2.30" CssClass="btn btn-primary" Width="120px"  OnClick="Button9_Click"></asp:Button>  <asp:Button ID="Button10" runat="server" Text="2.30 - 3.00" CssClass="btn btn-primary" Width="120px"  OnClick="Button10_Click"></asp:Button>  <asp:Button ID="Button11" runat="server" Text="3.00 - 3.30" CssClass="btn btn-primary" Width="120px"  OnClick="Button11_Click"></asp:Button>  <asp:Button ID="Button12" runat="server" Text="3.30 - 4.00" CssClass="btn btn-primary" Width="120px"  OnClick="Button12_Click"></asp:Button>  
                    <asp:Button ID="Button13" runat="server" Text="4.00 - 4.30" CssClass="btn btn-primary" Width="120px"  OnClick="Button13_Click"></asp:Button>
                    <br/>
                    <asp:Button ID="Button21" runat="server" Text="04.30 - 6.30 Break" CssClass="btn btn-danger" Enabled="False" Width="840px"></asp:Button>
                    <br />
                    <br />
                    <asp:Button ID="Button22" runat="server" Text="6.30 - 7.00" CssClass="btn btn-primary" Width="120px"  OnClick="Button22_Click"></asp:Button>  <asp:Button ID="Button23" runat="server" Text="7.00 - 7.30" CssClass="btn btn-primary" Width="120px"  OnClick="Button23_Click"></asp:Button>  <asp:Button ID="Button24" runat="server" Text="7.30 - 8.00" CssClass="btn btn-primary" Width="120px"  OnClick="Button24_Click"></asp:Button>  <asp:Button ID="Button25" runat="server" Text="8.00 - 8.30" CssClass="btn btn-primary" Width="120px"  OnClick="Button25_Click"></asp:Button>  <asp:Button ID="Button26" runat="server" Text="8.30 - 9.00" CssClass="btn btn-primary" Width="120px"  OnClick="Button26_Click"></asp:Button>  
                    <asp:Button ID="Button27" runat="server" Text="9.00 - 9.30" CssClass="btn btn-primary" Width="120px"  OnClick="Button27_Click"></asp:Button>
                    <br />
                    <br />
                </div>
            </div>
        </ContentTemplate> 
    </asp:UpdatePanel>
</div>

Model Pairing.rb

class User < ActiveRecord::Base

  enum role: [:student, :supervisor, :admin]
  after_initialize :set_default_role, :if => :new_record?

  has_many :students, class_name: "User",
                      foreign_key: "supervisor_id"

  belongs_to :supervisor, class_name: "User"

  has_and_belongs_to_many :pairings

  def set_default_role
    self.role ||= :student
  end

  def mailboxer_email(object)
    email
  end

  acts_as_messageable

end

这些模型的模式(省略了不相关的字段)

class Pairing < ActiveRecord::Base

  has_and_belongs_to_many :supervisor, class_name: 'User'
  belongs_to :student, class_name: 'User'

end

messages_controller.rb

create_table "pairings", force: :cascade do |t|
    t.integer  "supervisor_id"
    t.integer  "student_id"
    t.string   "project_title"
  end

  add_index "pairings", ["student_id"], name: "index_pairings_on_student_id", unique: true
  add_index "pairings", ["supervisor_id"], name: "index_pairings_on_supervisor_id"

create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "email",                  default: "", null: false
    t.integer  "role"
  end

messages_helper.rb

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

new.html.erb(发送消息的视图)

module MessagesHelper
  def recipients_options(chosen_recipient = nil)
    s = ''
    User.all.each do |user|
      s << "<option value='#{user.id}' #{'selected' if user == chosen_recipient}>#{user.name}</option>"
    end
    s.html_safe
  end
end

1 个答案:

答案 0 :(得分:0)

通过进行这些更改来解决

module MessagesHelper
  def recipients_options(chosen_recipient = nil)
    s = ''
    current_id = current_user.id
    if current_user.try(:student?)
        User.joins('INNER JOIN "pairings" ON "pairings"."supervisor_id" = "users"."id"').where("pairings.student_id" => current_id).each do |user|
            s << "<option value='#{user.id}' #{'selected' if user == chosen_recipient}>#{user.name}</option>"
        end
    elsif current_user.try(:supervisor?)
        User.joins('INNER JOIN "pairings" ON "pairings"."student_id" = "users"."id"').where("pairings.supervisor_id" => current_id).each do |user|
            s << "<option value='#{user.id}' #{'selected' if user == chosen_recipient}>#{user.name}</option>"
        end
    else
        User.all.each do |user|
            s << "<option value='#{user.id}' #{'selected' if user == chosen_recipient}>#{user.name}</option>"
        end

    end

    s.html_safe
  end

end