testdouble / ecto_resource Goto Github PK
View Code? Open in Web Editor NEWA simple module to clear up the boilerplate of CRUD resources in Phoenix context files.
License: Apache License 2.0
A simple module to clear up the boilerplate of CRUD resources in Phoenix context files.
License: Apache License 2.0
Hello,
First of all, you did a great job on this library. Thanks so much for sharing it.
I have a question related to using multiple schemas in a single context. Is it possible? The documentation states that it's doable, but there isn't any example on how to accomplish this.
To give an example of what I'm trying to accomplish:
Let's say we have an Account context backed by two schemas, User and Profile.
Can both be exposed as resources using this library, on the Account context?
So we'd get:
Account.all_users
Account.create_user
#...
Account.all_profiles
Account.create_profile
#...
If this isn't doable or out of the scope of this library, it's totally ok. I'm just starting to get my head around Elixir and it's conventions, and maybe I'm thinking about this problem in the wrong way.
Thanks again for the great work! This lib is really usefull to reduce boilerplate.
I have a Books
Context with the Book
resource. On the BookController
I have this code.
def new(conn, _params) do
changeset = Books.change_book(%{})
render(conn, "new.html", changeset: changeset)
end
But calling this throws
iex(7)> IndiePaper.Books.change_book(%{})
** (FunctionClauseError) no function clause matching in Ecto.Changeset.cast/4
The following arguments were given to Ecto.Changeset.cast/4:
# 1
%{}
# 2
%{}
# 3
[:title, :description]
# 4
[]
Attempted function clauses (showing 5 out of 5):
def cast(_data, %{__struct__: _} = params, _permitted, _opts)
def cast({data, types}, params, permitted, opts) when is_map(data)
def cast(%Ecto.Changeset{types: nil}, _params, _permitted, _opts)
def cast(%Ecto.Changeset{changes: changes, data: data, types: types, empty_values: empty_values} = changeset, params, permitted, opts)
def cast(%{__struct__: module} = data, params, permitted, opts)
(ecto 3.5.6) lib/ecto/changeset.ex:466: Ecto.Changeset.cast/4
(indie_paper 0.1.0) lib/indie_paper/books/book.ex:17: IndiePaper.Books.Book.changeset/2
Any idea on how to fix this ?
Here is my Books Context for reference
defmodule IndiePaper.Books do
alias IndiePaper.{Repo}
use EctoResource
using_repo(Repo) do
resource(IndiePaper.Books.Book)
end
end
Here is the Book
Schema
defmodule IndiePaper.Books.Book do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "books" do
field :title, :string
field :description, :string
timestamps()
end
@doc false
def changeset(book, attrs \\ %{}) do
book
|> cast(attrs, [:title, :description])
|> validate_required([:title, :description])
|> unique_constraint(:title)
end
end
Thanks for making such an awesome library. I had always been put off by the amount of boilerplate that I had to work into Phoenix contexts for all the simple crud operations. This library cleaned up my entire context in a jiffy. Thanks so much for making such a nice and awesome library. It's not every day you find something that does what you want to do the way it's supposed to be done and nothing more.
Thanks so much for making this and more for making it available to the world. Have a great day (every day) and a great life. Cheers :D
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.