Rails:在传递给模型之前根据用户类型修改参数的设计模式

桑杰

有多个控制器操作接收 startDate 作为参数并将其传递给相关模型以获取数据。基于用户属性,必须对该参数应用验证规则。验证不仅会失败,而且会分配特定于该用户属性的默认值。

startDate 上所需的修改在所有操作中都是通用的,应应用于所有情况。所以,我认为这个逻辑不应该在每个模型上重复。相反,控制器的 before_action 过滤器,像filtered_pa​​rams 听起来是正确的地方。

before_action :filtered_params

def filtered_params
   params.require(:query).permit(:start_date)
   user = User.instance
   if(user.type == 'student') {} # startDate should be Greater than or equal to 12-03-2018. modify params.startDate with the logic
   elsif(user.type == 'professor') {} // startDate should be Greater than equal to 01-01-2018
   else {} // do nothing
   end
end

上述方法有效,但我不想陷入 if else 循环。有没有更好的方法来做到这一点?

维利安

也许你可以这样做:

before_action :filtered_params

def filtered_params
  @filtered_params ||= get_filtered_params
end

def get_filtered_params
  # not sure about this user assignment, but let's go with it...
  user = User.instance
  begin
    "ParamsFilter::#{user.type.camelize}Service".constantize.call params
  rescue NameError => e
    # I'm guessing at the default behavior here. You would need to 
    # modify to meet your requirements.
    return params.require(:query).permit(:start_date)
  end
end

那么你需要类似的东西:

app
 |- services
 |   |- params_filter
 |   |   |- service_base.rb
 |   |   |- student_service.rb
 |   |   |- professor_service.rb
 |   |- service_base.rb

服务可能看起来像

class ParamsFilter::StudentService < ParamsFilter::ServiceBase

  MIN_START_DATE = '12-03-2018'

  #======================================================================
  # Instance Methods
  #======================================================================

    def call
      # In this case, given how ServiceBase is defined, the params will 
      # be received as `args`, so you'll need to work with the `args` 
      # variable, which will contain `params`.

      # You could do stuff unique to `student`:
      student_stuff

      # and then do some common stuff by calling `super`.
      super

    end

  private

    def student_stuff
    end

end

哪里ParamsFilter::ServiceBase可能看起来像:

class ParamsFilter::ServiceBase < ServiceBase

  #======================================================================
  # Instance Methods
  #======================================================================

    def call
      args[:start_date] = self.class::MIN_START_DATE unless start_date_okay?
    end

  private 

    def start_date_okay?
      args[:start_date] >= self.class::MIN_START_DATE
    end

end

哪里service_base.rb可能看起来像:

class ServiceBase

  attr_accessor *%w(
    args
  ).freeze

  class << self

    def call(args=nil)
      new(args).call
    end

  end # Class Methods

  #======================================================================
  # Instance Methods
  #======================================================================

  def initialize(args)
    @args = args
  end

end

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

作为库作者,期望用户根据情况将null传递给方法参数是否设计不当?

R模式修改传递给泛型函数的参数

修改Doskey宏如何根据传递给它的参数将参数传递给命令

AngularJs,根据用户输入将可选参数传递给URL

将数组参数传递给Kaitai Struct用户定义类型

将参数传递给模型?

DryIoC根据泛型类型参数将参数传递给开放泛型服务的构造函数

如何以只读模式与其他设计用户共享 Rails 模型?

如何将我的Ruby on Rails模型中的参数(例如,ID,用户名,密码)传递给我的PHP Codeigniter控制器

Vue axios将数组作为参数传递给Rails并更新所有内容(批量修改)

将控制器的参数传递给用户模型中的方法

通过 Ajax 将强类型视图模型作为参数传递给控制器

类型错误:传递给Controller :: show()的参数2必须是模式的实例,给定的字符串

使用主干并将参数传递给模型

MvxCachingFragmentStatePagerAdapter 将参数传递给视图模型

传递给Dictionary的模型项类型错误

将参数传递给交互模式

将参数传递给命令模式

设计用户存在错误,将数据从员工传递给用户

将自定义参数传递给Rails事件模型回调?

在Rails中将模型传递给方法

Ruby on Rails:将值传递给模型

如何将用户定义的类型变量作为参数传递给函数?

如果用户将原始类型参数传递给println(),那么幕后究竟发生了什么?

如何在 SQL 中循环用户定义的数据类型并将参数传递给程序

将IEnumerable <string>作为用户定义的表类型参数传递给SQL Server存储过程

在传递给解析器之前如何修改父节点?

将用户参数传递给SSIS包

修改传递给函数内部可变参数函数的参数