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

Composite Resources Possible DSL #24

Open
wazery opened this issue Nov 27, 2014 · 5 comments
Open

Composite Resources Possible DSL #24

wazery opened this issue Nov 27, 2014 · 5 comments

Comments

@wazery
Copy link

wazery commented Nov 27, 2014

I opened this issue to discuss the possible DSL ideas for the Composite Resources feature.

The feature is:

Imagine the case when in user signup form user can enter company name and the company will be automatically created:

class UserWithCompanyForm < ActiveForm::Base
  self.main_model = :user

  attributes :email, :password, :company_name, presence: true

  def save
    if @model.save
      Company.create(owner_id: @model.id, name: company_name)
      true
    else
      false
    end
  end
end

Instead of code above, we could provide some API.


I just need to understand the difference between this and the association method, is this for the resources that have no relations to the main model? If so what do you imagine/suggest a good DSL to do so?

I have a very rough suggestion:

class UserWithCompanyForm < ActiveForm::Base
  self.main_model = :user

  attributes :email, :password, :company_name, presence: true

  composite :company do
    owner_id: @model.id
    name: company_name
  end
end

@kirs please if you have any ideas in mind lets discuss them here.

@kaspth
Copy link

kaspth commented Nov 27, 2014

I feel like we're getting to far into DSL land here. I'm much more of a fan of Davids original API sketch: https://gist.github.com/dhh/b0a106a00e8737280666#file-aggregator-rb-L7

To me having a method users can override to setup the relationships themselves feels like the clearer and easier way to go.

What do you guys think?

@wazery
Copy link
Author

wazery commented Nov 29, 2014

👍

@kaspth
Copy link

kaspth commented Nov 29, 2014

Alright I'll close this then. Thanks for the input 😄

@kaspth kaspth closed this as completed Nov 29, 2014
@wazery
Copy link
Author

wazery commented Nov 29, 2014

@kaspth I didn't get why you closed this issue. I liked your suggestion and in search for how to implement it, also I hope to let this issue open for any other suggestions and questions.

@kaspth
Copy link

kaspth commented Nov 29, 2014

Sorry I was under the impression this was specifically about the above DSL sample and not a general discussion.

@kaspth kaspth reopened this Nov 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants