Возможно, вы могли бы сделать что-то вроде:
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