Sha256: baba8c3ac5e4df8bed3cf8cb18b21b32a1bc62788acb3f1555b1b23314ca21e0

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require 'active_support/concern'
require_relative './setup_context_concern'
require_relative './change_context_concern'

module SnFoil
  module Contexts
    module BuildContextConcern
      extend ActiveSupport::Concern

      included do
        include SetupContextConcern
        include ChangeContextConcern
      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

1 entries across 1 versions & 1 rubygems

Version Path
snfoil-0.1.0 lib/sn_foil/contexts/build_context_concern.rb