module Rubanok::Controller
Controller
concern. Adds `rubanok_process` method.
Public Instance Methods
# File lib/rubanok/rails/controller.rb, line 79 def implicit_plane_class "#{controller_path.classify.pluralize}Plane".safe_constantize end
Tries to infer the rubanok processor class from controller path
# File lib/rubanok/rails/controller.rb, line 51 def implicit_rubanok_class "#{controller_path.classify.pluralize}Processor".safe_constantize end
Backward compatibility
# File lib/rubanok/rails/controller.rb, line 71 def planish(*args, with: implicit_plane_class) rubanok_process(*args, with: with) end
# File lib/rubanok/rails/controller.rb, line 75 def planish_scope(*args, with: implicit_plane_class) rubanok_scope(*args, with: with) end
This method process passed data (e.g. ActiveRecord relation) using the corresponding Processor
class.
Processor
is inferred from controller name, e.g. “PostsController -> PostProcessor”.
You can specify the Processor
class explicitly via `with` option.
By default, `params` object is passed as parameters, but you can specify the params via `params` option.
# File lib/rubanok/rails/controller.rb, line 28 def rubanok_process(data, params = nil, with: nil) with_inferred_rubanok_params(with, params) do |rubanok_class, rubanok_params| rubanok_class.call(data, rubanok_params) end end
This method filters the passed params and returns the Hash (!) of the params recongnized by the processor.
Processor
is inferred from controller name, e.g. “PostsController -> PostProcessor”.
You can specify the Processor
class explicitly via `with` option.
By default, `params` object is passed as parameters, but you can specify the params via `params` option.
# File lib/rubanok/rails/controller.rb, line 44 def rubanok_scope(params = nil, with: nil) with_inferred_rubanok_params(with, params) do |rubanok_class, rubanok_params| rubanok_class.project(rubanok_params) end end
# File lib/rubanok/rails/controller.rb, line 55 def with_inferred_rubanok_params(with, params) with ||= implicit_rubanok_class if with.nil? raise ArgumentError, "Failed to find a processor class for #{self.class.name}. " \ "Please, specify the processor class explicitly via `with` option" end params ||= self.params params = params.to_unsafe_h if params.is_a?(ActionController::Parameters) yield with, params end