lib/sn_foil/contexts/create_context.rb in snfoil-0.3.0 vs lib/sn_foil/contexts/create_context.rb in snfoil-0.4.0

- old
+ new

@@ -13,17 +13,24 @@ include SetupContext include ChangeContext end class_methods do - attr_reader :i_before_create_hooks, :i_after_create_hooks, :i_after_create_success_hooks, :i_after_create_failure_hooks + attr_reader :i_setup_create_hooks, :i_before_create_hooks, :i_after_create_hooks, + :i_after_create_success_hooks, :i_after_create_failure_hooks def create(params:, user: nil, **options) new(user).create(**options, params: params) end + def setup_create(method = nil, **options, &block) + raise ArgumentError, '#setup_create requires either a method name or a block' if method.nil? && block.nil? + + (@i_setup_create_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] } + end + def before_create(method = nil, **options, &block) - raise ArgumentError, '#on_create requires either a method name or a block' if method.nil? && block.nil? + raise ArgumentError, '#before_create requires either a method name or a block' if method.nil? && block.nil? (@i_before_create_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] } end def after_create(method = nil, **options, &block) @@ -57,11 +64,11 @@ options.merge! object: object end def create(**options) options[:action] = :create - options = setup_change(setup_create(**options)) + options = before_setup_create_object(**options) options = setup_create_object(**options) authorize(options[:object], :create?, **options) options = create_hooks(**options) unwrap_object(options[:object]) end @@ -84,10 +91,14 @@ def after_create_failure(**options) options end + def setup_create_hooks + self.class.i_setup_create_hooks || [] + end + def before_create_hooks self.class.i_before_create_hooks || [] end def after_create_hooks @@ -101,9 +112,18 @@ def after_create_failure_hooks self.class.i_after_create_failure_hooks || [] 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) } + end # This method is private to help protect the order of execution of hooks def create_hooks(options) options = before_create_save(**options) options = if options[:object].save