lib/sn_foil/contexts/create_context.rb in snfoil-0.6 vs lib/sn_foil/contexts/create_context.rb in snfoil-0.7.0
- old
+ new
@@ -1,19 +1,20 @@
# frozen_string_literal: true
require 'active_support/concern'
-require_relative './setup_context'
require_relative './change_context'
module SnFoil
module Contexts
module CreateContext # rubocop:disable Metrics/ModuleLength
extend ActiveSupport::Concern
included do
- include SetupContext
+ include BuildContext
include ChangeContext
+
+ alias_method :setup_create_object, :setup_build_object
end
class_methods do
attr_reader :i_setup_create_hooks, :i_before_create_hooks, :i_after_create_hooks,
:i_after_create_success_hooks, :i_after_create_failure_hooks
@@ -50,27 +51,16 @@
(@i_after_create_failure_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
end
end
- def setup_create_object(params: {}, object: nil, **options)
- object = if object
- wrap_object(object)
- else
- klass = options.fetch(:model) { model }
- wrap_object(klass).new
- end
-
- object.attributes = params
- options.merge! object: object
- end
-
def create(**options)
options[:action] = :create
+ options = before_setup_build_object(**options)
options = before_setup_create_object(**options)
options = setup_create_object(**options)
- authorize(options[:object], :create?, **options)
+ authorize(options[:object], options.fetch(:authorize) { :create? }, **options)
options = create_hooks(**options)
options[:object]
end
def setup_create(**options)
@@ -114,16 +104,14 @@
end
private
def before_setup_create_object(**options)
- options = setup_create(**options)
- options = setup_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
options = setup_change(**options)
options = setup_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
- options = setup(**options)
- setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
+ options = setup_create(**options)
+ setup_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
end
# This method is private to help protect the order of execution of hooks
def create_hooks(options)
options = before_create_save(**options)
@@ -136,34 +124,34 @@
end
after_create_save(**options)
end
def before_create_save(**options)
- options = before_create(**options)
- options = before_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
options = before_change(**options)
- before_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
+ options = before_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
+ options = before_create(**options)
+ before_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
end
def after_create_save(**options)
- options = after_create(**options)
- options = after_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
options = after_change(**options)
- after_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
+ options = after_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
+ options = after_create(**options)
+ after_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
end
def after_create_save_success(**options)
- options = after_create_success(**options)
- options = after_create_success_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
options = after_change_success(**options)
- after_change_success_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
+ options = after_change_success_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
+ options = after_create_success(**options)
+ after_create_success_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
end
def after_create_save_failure(**options)
- options = after_create_failure(**options)
- options = after_create_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
options = after_change_failure(**options)
- after_change_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
+ options = after_change_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
+ options = after_create_failure(**options)
+ after_create_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
end
end
end
end