lib/trailblazer/operation/controller.rb in trailblazer-0.3.3 vs lib/trailblazer/operation/controller.rb in trailblazer-1.0.0.rc1
- old
+ new
@@ -1,89 +1,77 @@
require "trailblazer/endpoint"
module Trailblazer::Operation::Controller
private
- def form(operation_class, params=self.params) # consider private.
- process_params!(params)
-
- @operation = operation_class.present(params)
- @form = @operation.contract
- @model = @operation.model
-
- yield @operation if block_given?
+ def form(*args)
+ present(*args).tap do |op|
+ op.contract.prepopulate! # equals to @form.prepopulate!
+ end
end
- # Doesn't run #process.
+ # Provides the operation instance, model and contract without running #process.
+ # Returns the operation.
def present(operation_class, params=self.params)
res, op = operation!(operation_class, params) { [true, operation_class.present(params)] }
-
- yield op if block_given?
- # respond_with op
- # TODO: implement respond(present: true)
+ op
end
- def collection(operation_class, params=self.params)
- # TODO: merge with #present.
- res, op = operation!(operation_class, params) { [true, operation_class.present(params)] }
- @collection = @model
-
- yield op if block_given?
- end
-
- # full-on Op[]
- # Note: this is not documented on purpose as this concept is experimental. I don't like it too much and prefer
- # returns in the valid block.
- class Else
- def initialize(op, run)
- @op = op
- @run = run
+ def collection(*args)
+ present(*args).tap do |op|
+ @collection = op.model
end
-
- def else
- yield @op if @run
- end
end
- # Endpoint::Invocation
def run(operation_class, params=self.params, &block)
res, op = operation!(operation_class, params) { operation_class.run(params) }
yield op if res and block_given?
Else.new(op, !res)
end
# The block passed to #respond is always run, regardless of the validity result.
- def respond(operation_class, params=self.params, respond_options = {}, &block)
- res, op = operation!(operation_class, params) { operation_class.run(params) }
+ def respond(operation_class, options={}, params=self.params, &block)
+ res, op = operation!(operation_class, params, options) { operation_class.run(params) }
+ namespace = options.delete(:namespace) || []
- return respond_with op, respond_options if not block_given?
- respond_with op, respond_options, &Proc.new { |formats| block.call(op, formats) } if block_given?
+ return respond_with *namespace, op, options if not block_given?
+ respond_with *namespace, op, options, &Proc.new { |formats| block.call(op, formats) } if block_given?
end
+private
+
def process_params!(params)
end
# Normalizes parameters and invokes the operation (including its builders).
- def operation!(operation_class, params, &block)
- Trailblazer::Endpoint.new(self, operation_class, params, request, self.class._operation).(&block)
+ def operation!(operation_class, params, options={}, &block)
+ # Per default, only treat :html as non-document.
+ options[:is_document] ||= request.format == :html ? false : true
+
+ process_params!(params)
+ res, op = Trailblazer::Endpoint.new(operation_class, params, request, options).(&block)
+ setup_operation_instance_variables!(op)
+
+ [res, op]
end
def setup_operation_instance_variables!(operation)
@operation = operation
@form = operation.contract
@model = operation.model
end
- def self.included(includer)
- includer.extend Uber::InheritableAttr
- includer.inheritable_attr :_operation
- includer._operation = {document_formats: {}}
- includer.extend ClassMethods
- end
- module ClassMethods
- def operation(options)
- _operation[:document_formats][options[:document_formats]] = true
+ # Note: this is not documented on purpose as this concept is experimental. I don't like it too much and prefer
+ # returns in the valid block.
+ class Else
+ def initialize(op, run)
+ @op = op
+ @run = run
+ end
+
+ def else
+ yield @op if @run
end
end
end