简体   繁体   English

在 ruby​​ on rails 应用程序中重用模型

[英]Reusing model in ruby on rails application

I'm currently in the process of adding comments to one of my models MaintenanceRequest and wondering how I should go about this.我目前正在向我的一个模型MaintenanceRequest添加评论,并想知道我应该如何处理这个问题。

I have a model Offer which already has comments built and it is set up with ActionCable as follows我有一个模型Offer已经建立了评论,它是用 ActionCable 设置的,如下所示

# models/comment.rb

class Comment < ApplicationRecord
  belongs_to :user
  belongs_to :offer
end
# channels/comment_channel.rb

class CommentChannel < ApplicationCable::Channel
  def subscribed
    offer = Offer.find params[:offer]
    stream_for offer
  end
end

and the comment controller looks as并且评论控制器看起来像

# controllers/comments_controller.rb

class CommentsController < ApplicationController
  before_action :authenticate_user!
  before_action :is_valid_offer

  def create
    offer = Offer.find(comment_params[:offer_id])

    if comment_params[:content].blank?
      # return redirect_to request.referrer, alert: 'Comment cannot be empty.'
      return render json: {success: false}
    end

    if offer.user_id != current_user.id && offer.landlord_id != current_user.id
      return render json: {success: false}
    end

    @comment = Comment.new(
      user_id: current_user.id,
      offer_id: offer.id,
      content: comment_params[:content],
    )

    if @comment.save
      # redirect_to request.referrer, notice: 'Comment sent.'
      CommentChannel.broadcast_to offer, message: render_comment(@comment)
      render json: {success: true}
    else
      redirect_to request.referrer, alert: "Couldn't send comment."
      render json: {success: true}
    end
  end
end

My question is that is there a way I could use the same model for comments belonging to MaintenanceRequest or should I just create new model and controller for there comments?我的问题是,有没有一种方法可以对属于MaintenanceRequest评论使用相同的模型,还是应该为评论创建新模型和控制器? It would seem that this would get quite messy if I tried to reuse the current comment model.如果我尝试重用当前的评论模型,这似乎会变得非常混乱。 What would be the "best practice" here?这里的“最佳实践”是什么?

If you want to make a resuable assocation you want to use a polymorphic assocation:如果要创建可重用关联,则要使用多态关联:

class Comment < ApplicationRecord
  belongs_to :user
  belongs_to :commentable, 
    polymorphic: true

  validates :content, 
    presence: true
end

Instead of using a single column holding an integer pointing to a fixed table this "cheats" the relation database model by using a one column for the other tables primary key and storing the class name of the entitity in a string column (in this example called commentable_type ).不是使用包含指向固定表的整数的单个列,而是通过使用一列作为其他表的主键并将实体的类名存储在字符串列中(在本例中称为commentable_type )。

You can create the needed columns by passing the polymorphic option when generating the migration (or model):您可以通过在生成迁移(或模型)时传递多态选项来创建所需的列:

rails g migration add_commentable_to_comments commentable:belongs_to{polymorphic}

However that controller action is a bit of train wreck.然而,控制器的动作有点像火车失事。 It should look more like this:它应该看起来更像这样:

class CommentsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_offer
  before_action :authorize_user

  def create
    @comment = @offer.comments.new(comment_params) do |c|
      c.user = current_user
    end

    if @comment.save
      # redirect_to request.referrer, notice: 'Comment sent.'
      CommentChannel.broadcast_to offer, message: render_comment(@comment)
      # rendering json here is actually just stupid. Just use a HTTP status code 
      # as a response if you're not actually returning the rendered entity.
      render json: { success: true }, 
        status: :created,
        location: @comment
    else
      # either return JSON which is actually useful or just a status code
      render json: { success: false }, 
        status: :unprocessable_entity
    end
  end

  private

  def set_offer
    @offer = Offer.find(params[:offer_id])
  end

  # this should be extracted to an authorization layer such as Pundit
  def authorize_offer
    if @offer.user_id != current_user.id && @offer.landlord_id != current_user.id 
      render json: { success: false },
        status: :unauthorized
    end
  end
end

There are two way to resuse this controller for different types of "commentables".有两种方法可以将此控制器重用于不同类型的“评论”。 You can either check for the presence of the offer_id or maintenance_request_id in a single controller and use that to deduce the class or you can use inheritance to destribute the responsibilities better:您可以检查单个控制器中是否存在offer_idmaintenance_request_id并使用它来推断类,或者您可以使用继承来更好地分配职责:

# routes.rb

resources :offers do
  resources :comments, 
    only: :create,
    module: :offers
end

resources :maintainence_requests do
  resources :comments, 
    only: :create,
    module: :maintainence_requests
end
class CommentsController
  before_action :set_commentable, only: [:new, :create, :index]
  before_action :authenticate_user!
  attr_reader :commentable

  def create
    @comment = commentable.comments.new(comment_params) do |c|
      c.user = current_user
    end
    if @comment.save
      # simple extendability feature that lets you "tap into" the flow 
      # by passing a block when calling super in subclasses
      yield @comment if block_given?
      render json: @comment,
             status: :created
    else
      render json: { errors: @comment.errors.full_messages },
             status: :unprocessable_entity
    end
  end

  
  private

  # Uses simple heuristics based on module nesting to guess the name of the model
  # that is being commented upon. Overide if needed.
  # @example Foos::BarsController -> Foo
  def commentable_class
    @commentable_class ||= self.class.module_parent.name.singularize.constantize
  end 

  def commentable_param_key
    commentable_class.model_name.param_key
  end

  def set_commentable
    @commentable = commentable_class.find("#{commentable_param_key}_id")
  end

  def comment_params
    params.require(:comment)
          .permit(:content)
  end
end
module Offers
  class CommentsController < ::CommentsController
    # POST /offers/:offer_id/comments
    def create
      # block is called after a comment is succesfully saved but before 
      # the response is set
      super do |comment|
        CommentChannel.broadcast_to comment.offer, 
          message: render_comment(comment) 
      end
    end
  end
end
module MaintainenceRequests
  class CommentsController < ::CommentsController
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM