Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix slice specific repository commands from being nil #14

Open
bkuhlmann opened this issue Sep 29, 2024 · 0 comments
Open

Fix slice specific repository commands from being nil #14

bkuhlmann opened this issue Sep 29, 2024 · 0 comments

Comments

@bkuhlmann
Copy link

bkuhlmann commented Sep 29, 2024

Why

Hello. 👋 When using ROM commands within a slice, they end up as nil objects. My initial guess is something is broken with object inheritance because commands work if used with repository objects within the application slice (i.e. the main slice).

How

In my case, I have a Tasks slice which uses ROM commands for create, update, and delete:

Source Code
# frozen_string_literal: true

module Tasks
  module Repositories
    # Defines the tasks repository.
    class Task < DB::Repository
      ORDER = proc { created_at.asc }

      # TODO: Enable once Hanami 2.2.0 supports commands.
      # commands :create, update: :by_pk, delete: :by_pk

      def initialize(order: ORDER, **)
        @order = order
        super(**)
      end

      def find(id) = tasks.combine(:user).by_pk(id).one

      def find_by_description text
        tasks.combine(:user)
             .where { description.ilike "%#{text}%" }
             .to_a
      end

      def all = tasks.combine(:user).order(&order).to_a

      # TODO: Remove once Hanami 2.2.0 supports commands.
      def create(attributes) = tasks.changeset(:create, attributes).commit

      # TODO: Remove once Hanami 2.2.0 supports commands.
      def update(id, **) = tasks.by_pk(id).update(**)

      # TODO: Remove once Hanami 2.2.0 supports commands.
      def delete(id, **) = tasks.by_pk(id).delete(**)

      private

      attr_reader :order
    end
  end
end

Notice that I've disabled (commented) the commands macro in favor of creating the equivalent methods for create, update, and delete. That's the workaround. ...but I'd prefer to use only the commands macro and delete the additional methods.

This appears to work when working within a normal Hanami 2.2.0, Beta 2 application with no slices.

Steps to Recreate

  1. Clone the 220 branch of my Hemo application.
  2. Run bin/setup.
  3. Open the Task repository object.
  4. Uncomment the commands macro and comment the create, update, and delete methods.
  5. Run the test suite to see nil exceptions being thrown. You can revert the changes to make the specs pass again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant