Skip to content

Commit

Permalink
Improve docs for using the AggregateMapper#query method
Browse files Browse the repository at this point in the history
  • Loading branch information
sskirby committed Mar 12, 2020
1 parent a3ef7bc commit 3e06d12
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,12 @@ It also does not do some things that you might expect from other ORMs:

**A.** Create a method on a [Repository](http://martinfowler.com/eaaCatalog/repository.html)! They have full access to the DB/ORM so you can use [Arel](https://github.com/rails/arel) and go [crazy](http://asciicasts.com/episodes/239-activerecord-relation-walkthrough) or use direct SQL if you want.

For example:
For example, use the [#query](https://rubydoc.info/github/nulogy/vorpal/master/Vorpal/AggregateMapper#query-instance_method) method on the [AggregateMapper](https://rubydoc.info/github/nulogy/vorpal/master/Vorpal/AggregateMapper) to access the underyling [ActiveRecordRelation](https://api.rubyonrails.org/classes/ActiveRecord/Relation.html):

```ruby
def find_all
@mapper.query.load_all # use the mapper to load all the aggregates
def find_special_ones
# use `load_all` or `load_one` to convert from ActiveRecord objects to domain POROs.
@mapper.query.where(special: true).load_all
end
```

Expand Down
11 changes: 11 additions & 0 deletions lib/vorpal/aggregate_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ def engine
@engine
end

# Returns a 'Vorpal-aware' [ActiveRecord::Relation](https://api.rubyonrails.org/classes/ActiveRecord/Relation.html)
# for the ActiveRecord object underlying the domain entity mapped by this mapper.
#
# This method allows you to easily access the power of ActiveRecord::Relation to do more complex
# queries in your repositories.
#
# The ActiveRecord::Relation is 'Vorpal-aware' because it has the {#load_one} and {#load_many} methods
# mixed in so that you can get the POROs from your domain model instead of the ActiveRecord
# objects normally returned by ActiveRecord::Relation.
#
# @return [ActiveRecord::Relation]
def query
@engine.query(@domain_class)
end
Expand Down

0 comments on commit 3e06d12

Please sign in to comment.