将 Mailboxer 对话限制在特定用户之间
Limiting Mailboxer Conversations to be between specific users
我已按照此处的教程进行操作:http://www.sitepoint.com/messaging-rails-mailboxer/ 将邮箱程序 gem 实施到我的应用程序中并使其正常工作。然而,在实施之后,我现在需要以某种方式限制用户之间的对话:
- 如果当前用户是学生(枚举:1),它只能向配对中与之配对的一个用户发送消息 table。
- 如果当前用户是主管(枚举:2),它只能向配对中与之配对的学生用户发送消息 table。
我想我需要对 messages_helper 模块进行更改,但我不知道具体要做什么。
型号User.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
型号Pairing.rb
class Pairing < ActiveRecord::Base
has_and_belongs_to_many :supervisor, class_name: 'User'
belongs_to :student, class_name: 'User'
end
这些模型的架构(省略不相关的字段)
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_controller.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
messages_helper.rb
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
new.html.erb(发送消息的视图)
<% header "Start Conversation" %>
<%= 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 %>
通过进行这些更改解决
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
我已按照此处的教程进行操作:http://www.sitepoint.com/messaging-rails-mailboxer/ 将邮箱程序 gem 实施到我的应用程序中并使其正常工作。然而,在实施之后,我现在需要以某种方式限制用户之间的对话:
- 如果当前用户是学生(枚举:1),它只能向配对中与之配对的一个用户发送消息 table。
- 如果当前用户是主管(枚举:2),它只能向配对中与之配对的学生用户发送消息 table。
我想我需要对 messages_helper 模块进行更改,但我不知道具体要做什么。
型号User.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
型号Pairing.rb
class Pairing < ActiveRecord::Base
has_and_belongs_to_many :supervisor, class_name: 'User'
belongs_to :student, class_name: 'User'
end
这些模型的架构(省略不相关的字段)
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_controller.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
messages_helper.rb
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
new.html.erb(发送消息的视图)
<% header "Start Conversation" %>
<%= 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 %>
通过进行这些更改解决
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