module SnFoil::Contexts::UpdateContext

Public Instance Methods

after_update(method = nil, **options, &block) click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 37
def after_update(method = nil, **options, &block)
  raise ArgumentError, '#after_update requires either a method name or a block' if method.nil? && block.nil?

  (@i_after_update_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
end
after_update_failure(method = nil, **options, &block) click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 49
def after_update_failure(method = nil, **options, &block)
  raise ArgumentError, '#after_update_failure requires either a method name or a block' if method.nil? && block.nil?

  (@i_after_update_failure_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
end
after_update_failure_hooks() click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 111
def after_update_failure_hooks
  self.class.i_after_update_failure_hooks || []
end
after_update_hooks() click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 103
def after_update_hooks
  self.class.i_after_update_hooks || []
end
after_update_success(method = nil, **options, &block) click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 43
def after_update_success(method = nil, **options, &block)
  raise ArgumentError, '#after_update_success requires either a method name or a block' if method.nil? && block.nil?

  (@i_after_update_success_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
end
after_update_success_hooks() click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 107
def after_update_success_hooks
  self.class.i_after_update_success_hooks || []
end
before_update(method = nil, **options, &block) click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 31
def before_update(method = nil, **options, &block)
  raise ArgumentError, '#before_update requires either a method name or a block' if method.nil? && block.nil?

  (@i_before_update_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
end
before_update_hooks() click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 99
def before_update_hooks
  self.class.i_before_update_hooks || []
end
setup_update(method = nil, **options, &block) click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 25
def setup_update(method = nil, **options, &block)
  raise ArgumentError, '#setup_update requires either a method name or a block' if method.nil? && block.nil?

  (@i_setup_update_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
end
setup_update_hooks() click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 95
def setup_update_hooks
  self.class.i_setup_update_hooks || []
end
setup_update_object(params: {}, id: nil, object: nil, **options) click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 56
def setup_update_object(params: {}, id: nil, object: nil, **options)
  raise ArgumentError, 'one of the following keywords is required: id, object' unless id || object

  object = wrap_object(object || scope.resolve.find(id))
  authorize(object, options.fetch(:authorize, :update?), **options)

  object.attributes = params
  options.merge! object: object
end
update(id:, params:, entity: nil, **options) click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 21
def update(id:, params:, entity: nil, **options)
  new(entity).update(**options, id: id, params: params)
end

Private Instance Methods

after_update_save(options) click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 146
def after_update_save(options)
  options = after_change(**options)
  options = after_change_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
  options = after_update(**options)
  after_update_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
end
after_update_save_failure(options) click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 160
def after_update_save_failure(options)
  options = after_change_failure(**options)
  options = after_change_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
  options = after_update_failure(**options)
  after_update_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
end
after_update_save_success(options) click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 153
def after_update_save_success(options)
  options = after_change_success(**options)
  options = after_change_success_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
  options = after_update_success(**options)
  after_update_success_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
end
before_setup_update_object(**options) click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 117
def before_setup_update_object(**options)
  options = setup(**options)
  options = setup_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_update(**options)
  setup_update_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
end
before_update_save(options) click to toggle source
# File lib/sn_foil/contexts/update_context.rb, line 139
def before_update_save(options)
  options = before_change(**options)
  options = before_change_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
  options = before_update(**options)
  before_update_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
end
update_hooks(options) click to toggle source

This method is private to help protect the order of execution of hooks

# File lib/sn_foil/contexts/update_context.rb, line 127
def update_hooks(options)
  options = before_update_save(options)
  update_successful = options[:object].save
  options[:object] = unwrap_object(options[:object])
  options = if update_successful
              after_update_save_success(options)
            else
              after_update_save_failure(options)
            end
  after_update_save(options)
end