trailblazer / trailblazer-loader

Require concept files, models and more without any autoloading magic.
MIT License
17 stars 23 forks source link

Non-namespaced operations fail to load #18

Closed philsturgeon closed 6 years ago

philsturgeon commented 7 years ago

This may well be user error, but I've hit a roadblock. When using this folder structure in a Rails 5.1 app, operations cannot be found.

app
├── concepts
│   ├── policy
│   │   ├── operation
│   │   │   ├── index.rb

Simple boring controller method like this:

  def index
    result = run Policy::Index
    render_json_response :ok, result["data"]
  end

The policy index.rb just looked like this:

class Policy::Index < Trailblazer::Operation
  step :data!

  def data!(options, *)
    options["data"] = blaaa
  end
end

That was failing to run, giving NameError (uninitialized constant Policy::Index):, until I namespaced them:

module Policy::Operation
  class Index < Trailblazer::Operation
    step :data!

    def data!(options, *)
      options["data"] = Permissions::Policy.list
    end
  end
end

Changing the reference Policy::Index to Policy::Operations::Index to match solves this entirely.

The README says it should work either way, but im only able to do it namespaced.

apotonick commented 7 years ago

Phiiiil! :beers:

I remember we had the same problem with Contract::Create because contract and policy are "special" "names". Can you rename Policy to Bla or something and see if it helps? If not, try require_dependency the files directly.

We will push a new loader version soon where this kind of stuff can be easily overridden!

philsturgeon commented 6 years ago

Ah, that makes sense. Ok I called it something else. Thank you :)