Sha256: f40e0dbb8d7d229a56344f3c115706e432d7530a752399a83095efc67f7a74a3

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require 'active_support/concern'
require_relative './setup_context'
require_relative './change_context'

module SnFoil
  module Contexts
    module BuildContext
      extend ActiveSupport::Concern

      included do
        include SetupContext
        include ChangeContext
      end

      class_methods do
        def build(params:, user: nil, **options)
          new(user).build(**options, params: params)
        end
      end

      def setup_build_object(params: {}, object: nil, **options)
        SnFoil.logger.info 'Warning: Using build bypasses authorize.  It is safer to interact with models through create' unless ENV['ISTEST']
        return wrap_object(object) if object

        klass = options.fetch(:model) { model }
        options.merge! object: wrap_object(klass).new(**params)
      end

      def build(**options)
        options[:action] = :build
        options = setup_build(setup_change(**options))
        options = setup_build_object(**options)
        unwrap_object(options[:object])
      end

      def setup_build(**options)
        options
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
snfoil-0.4.3 lib/sn_foil/contexts/build_context.rb
snfoil-0.4.2 lib/sn_foil/contexts/build_context.rb
snfoil-0.4.1 lib/sn_foil/contexts/build_context.rb
snfoil-0.4.0 lib/sn_foil/contexts/build_context.rb
snfoil-0.3.0 lib/sn_foil/contexts/build_context.rb