-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Calculations): Able to show calculations on show page (#235)
Co-authored-by: Zach Daniel <[email protected]>
- Loading branch information
1 parent
56a731f
commit e563097
Showing
5 changed files
with
222 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
defmodule Demo.Calculations.Concat do | ||
use Ash.Resource.Calculation | ||
|
||
@impl true | ||
def init(opts) do | ||
if opts[:keys] && is_list(opts[:keys]) && Enum.all?(opts[:keys], &is_atom/1) do | ||
{:ok, opts} | ||
else | ||
{:error, "Expected a `keys` option for which keys to concat"} | ||
end | ||
end | ||
|
||
@impl true | ||
def load(_query, opts, _context) do | ||
opts[:keys] | ||
end | ||
|
||
@impl true | ||
def calculate(records, opts, %{arguments: %{separator: separator}}) when is_bitstring(separator) do | ||
Enum.map(records, fn record -> | ||
Enum.map_join(opts[:keys], separator, fn key -> | ||
to_string(Map.get(record, key)) | ||
end) | ||
end) | ||
end | ||
|
||
def calculate(_records, _opts, _context) do | ||
{:error, "Argument separator invalid"} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters