These are chat archives for dry-rb/chat

22nd
Jul 2018
siassaj
@siassaj
Jul 22 2018 09:01
feels a bit ... redundant to me if you have search & replace available
Jaromír Červenka
@Cervajz
Jul 22 2018 09:16
@swinner2 I do this:
  # Merges errors from Dry::Validation & attributes from the form to ActiveRecord model. To make it compatible with
  # inline errors presented by simple_form (to emulate validations on model itself.)
  def merge_errors_attrs(model, errors, attributes)
    model.assign_attributes(attributes) if attributes.present?

    errors.each do |attribute, attr_errors|
      attr_errors.each { |error| model.errors.add attribute, error }
    end

    model
  end
It's in my ApplicationController
Then I use it like this:
def create
    Users::Create.new.call(user_params.to_h) do |result|
      result.success { redirect_to users_path, notice: t('users.notices.create') }

      result.failure do |errors|
        render :new, locals: { user: merge_errors_attrs(repository.new, errors, user_params) }
      end
    end
  end
I am not sure if this is the best way either :D
Nikita Shilnikov
@flash-gordon
Jul 22 2018 11:23
@siassaj you won't do search & replace in, say, tests
siassaj
@siassaj
Jul 22 2018 12:11
I think maybe it's a concept that pays off more as the size of the project grouws
yeah, maybe I'll try it and see how it affects my codebase
Nikita Shilnikov
@flash-gordon
Jul 22 2018 12:14
you should start with understanding ioc and DI, once you get it you'll understand container is just a tool, rather simple one
siassaj
@siassaj
Jul 22 2018 12:38
we use DI in many cases, never needed a container before
i hadn't considered their value
Nikita Shilnikov
@flash-gordon
Jul 22 2018 12:42
then take a look at dry-auto_inject, it uses dry-container under the hood
siassaj
@siassaj
Jul 22 2018 12:54
yeah makes sense
not valuable in my small apps
but can be massively valuable in big ones