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 Box#build #34

Closed
paulcsmith opened this issue Dec 15, 2017 · 4 comments
Closed

Add Box#build #34

paulcsmith opened this issue Dec 15, 2017 · 4 comments
Labels
feature request A new requested feature / option

Comments

@paulcsmith
Copy link
Member

Make it so that you can generate an unpersisted model from the form fields

@paulcsmith paulcsmith transferred this issue from luckyframework/lucky_record Feb 2, 2019
@jwoertink jwoertink added the feature request A new requested feature / option label Jul 26, 2019
@jwoertink
Copy link
Member

For anyone looking to take this on, you can start here:

https://github.com/luckyframework/avram/blob/master/spec/support/base_box.cr and https://github.com/luckyframework/avram/blob/master/spec/support/boxes/user_box.cr#L8

These are examples that already do this. Just make it native.

@paulcsmith
Copy link
Member Author

I think what build would do is manually set the id. I'm not 100% sure the best way to do this but open to PRs to see what people think of

@jwoertink
Copy link
Member

Started working on this, but then got blocked. The current idea of the Avram model is that it's a representation of data from a database. @matthewmcgarvey brought up the point that this idea would break that convention.

Aside from that, just from a technical aspect, we now have view models which don't have the same default columns that a table model has. Since a model instance requires certain values to be set, it's difficult to know what a "random" value might be. For example, what if your primary key is named custom_id and it's a String? Or what if you don't have the timestamps (or they're renamed).

Here's an example of how this idea was originally used:

# Taken from Avram specs
it "returns the value from params for updates" do
    user = UserFactory.build
    params = {"name" => "New Name From Params"}
    avram_params = Avram::Params.new(params)

    operation = SaveUser.new(user, avram_params)

    operation.name.value.should eq params["name"]
    operation.nickname.value.should eq user.nickname
    operation.age.value.should eq user.age
end

Where the idea here is that you're testing the SaveOperation, but have no need for the data to be persisted. This idea may just be an internal "hack" to allow us to test some internals. I'm not sure that a standard user of Avram would need to do this.

For now, I'm going put this PR on hold, but if anyone has another real-world example of where this could be helpful, please put your example here and we can revisit it.

@jwoertink
Copy link
Member

After some thought, I've decided to close out this feature request. You can actually just call UserFactory.new if you need, or if you're testing operations, just SaveUser.new.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request A new requested feature / option
Projects
None yet
Development

No branches or pull requests

2 participants