Ability to set object for blueprint #198

Merged
merged 3 commits into from Sep 26, 2012

3 participants

@Baltazore

It can be given with model_object option in #fields_for.
Useful for setting defaults and initializing associated objects.

@lest
Collaborator

I have the same question as in #104. Could you please provide some use cases when this feature makes things easier? Thanks!

@joxxoxo

well, i think it's better than ft.fields_for :co_worker, ft.object.co_worker || ft.object.build_co_worker, because
this view won't be reusable in other places, with other logic.
Also I think that #link_to_add should be configurable with default associated object to add ability to set defaults (which will go to the blueprint).
It couldn't be done with subject.fields_for(:tasks, subject.object.tasks.presence || subject.object.tasks.build(:name => 'for check')) {|f| f.object.name }, cuz blueprint is generated with Task.new.
I don't like the idea of setting defaults with after_initialize or some gem neither, because it'll spread all over the app and i need it only on one form.

Another option is add parameter like model_object_attributes, which will be given to the .new call, but i'd rather prefer model_object.

Examples,
class Task
def self.default
new(:title => 'New task', :co_workers => User.current.team, :assignee => User.current, :estimate => 1.week.from_now)
end
end

#view
...
<%= f.link_to_add :tasks, :model_object => Task.default %>
...

or something like
<%= f.link_to_add :tasks, :model_object => TaskBuilder.default_for(current_user) %>

@lest lest merged commit dd20ee2 into ryanb:master Sep 26, 2012

1 check failed

Details default The Travis build failed
@lest
Collaborator

Thanks! Could you please add docs about this option? E.g. like in #104.

@Baltazore

Thank you for merge my request.
I created a page on Wiki.

P.S.
Доклад на RailsClubMoscow был замечательный, очень помогло с faye:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment