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

Add loading custom entities classes section #254

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions source/4.0/learn/core/structs.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,39 @@ user.full_name
# => "Jane Doe"
```

### Loading custom entities classes

When using `auto_struct` feature along with custom `Entities` classes, ROM will not detect whether constant `Entities::User` has been loaded already. If no constant is registered under provided `auto_struct` namespace, it will create custom class under that namespace automatically which may lead you to a confusion. In order to avoid it, just be sure to load all required entities constants beforehand.

The actual custom entity class will be further subclassed from yours and will be extended with additional attributes, if the query result provided more than entity expected. For instance, if the entity expects only `login` attribute and query has not been restricted to such attribute, the entity will be extended with all queried attributes:

``` ruby
module Entities
class User < ROM::Struct
attribute :login, Types::String
end
end

class UserRepo < ROM::Repository[:users]
struct_namespace Entities

def by_id(id)
users.by_pk(id).one
end
end

user = rom.relations[:users]
.changeset(:create, login: 'jane@doe.org', email: 'jane@doe.org')
.commit

repo = UserRepo.new(rom)

repo.by_id(1)
# => #<Entities::User login="jane@doe.org" id=1 login="jane@doe.org" email="jane@doe.org">
```

This is the intention of `struct_namespace` feature. If you don't want some attributes, don't query them. Otherwise your entity shape will be different than declared, but it will never omit the primarily declared attributes.

### How struct classes are determined

Mappers will look for struct classes based on `Relation#name`, but this is not restricted
Expand Down