首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rails,重复代码重构

Rails,重复代码重构
EN

Stack Overflow用户
提问于 2014-11-01 18:38:13
回答 3查看 999关注 0票数 1

我如何在rails控制器中重构类似的代码片段?

app/控制器/相册_控制器.…:58…62 <>

代码语言:javascript
运行
复制
def set_album
  if current_user.admin?
    @album = User.find(params[:user_id]).albums.find(params[:id])
  else
    @album = current_user.albums.find(params[:id])
  end
end

app/控制器/文章_Controller.rb:45…49 <>

代码语言:javascript
运行
复制
def set_article
  if current_user.admin?
    @article = User.find(params[:user_id]).articles.find(params[:id])
  else
    @article = current_user.articles.find(params[:id])
  end
end

app/控制器/照片_控制器.…:55…59 <>

代码语言:javascript
运行
复制
def set_photo
  if current_user.admin?
    @photo = User.find(params[:user_id]).photos.find(params[:id])
  else
    @photo = current_user.photos.find(params[:id])
  end
end
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-11-01 20:30:02

控制器/关注点/user_Resource.rb

代码语言:javascript
运行
复制
module UserResource
  extend ActiveSupport::Concern

  included do
    before_action :set_resource , only: [:edit, :update, :destroy]
    before_action :signed_in_user, only: [:new, :edit, :update, :destroy]
    before_action :correct_user, only: [:edit, :update, :destroy]
  end

  def set_resource
    association = controller_name.classify.downcase
    resource = current_user.admin? ? User.find(params[:user_id]) : current_user
    resource = resource.send(association.to_s.pluralize).find(params[:id])
    instance_variable_set("@#{association}", resource)
  end

  def correct_user
    association = controller_name.classify.downcase
    redirect_to root_path unless admin_or_current?(instance_variable_get("@#{association}").user)
  end
end

然后,在{照片、相册、文章}_controller.rb中

代码语言:javascript
运行
复制
include UserResource
票数 2
EN

Stack Overflow用户

发布于 2014-11-01 20:11:39

一种方法是创建一个新的控制器:

代码语言:javascript
运行
复制
class ResourceController < ApplicationController
  before_filter :set_resource, only: [:show, :edit, :update, :destroy]

  private

  def set_resource
    user = current_user.admin? ? User.find(params[:user_id]) : current_user
    resource = user.send(controller_name.to_sym).find(params[:id])
    instance_variable_set("@#{controller_name.singularize}", resource)
  end
end

然后你的albums_controller.rb:

代码语言:javascript
运行
复制
class AlbumsController < ResourceController
  # use @album in show, edit, update, and destroy
end

articles_controller.rb:

代码语言:javascript
运行
复制
class ArticlesController < ResourceController
  # use @article in show, edit, update, and destroy    
end

photos_controller.rb:

代码语言:javascript
运行
复制
class PhotosController < ResourceController
  # use @photo in show, edit, update, and destroy
end
票数 1
EN

Stack Overflow用户

发布于 2014-11-01 19:04:33

在这里使用元编程是个好主意,我的意思是:

代码语言:javascript
运行
复制
def set_resource(association_singular) # e.g. :photo
  resource = current_user.admin? ? User.find(params[:user_id]) : current_user
  resource = resource.send(association.to_s.pluralize).find(params[:id]) )
  instance_variable_set("@#{association}", resource)
end

然后,在控制器( before_filter only: [:action]

代码语言:javascript
运行
复制
def action
  # ...
  set_resource(:photo)
  # ...
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26691748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档