Create_for_*


#1

Hi!

If I have a /budget/create action, how should I make a
/budget/create_for_customer without repeating myself?
I could create a “create_for_customer” action and call “create” from
there, but stripping the customer select_tag somehow, since customer_id
will be passed from /customer/show.

How do you do the create_for_something stuff?

Thanks,
Rodrigo.


#2

Am Donnerstag, den 23.02.2006, 17:21 +0100 schrieb Rodrigo A.:

If I have a /budget/create action, how should I make a
/budget/create_for_customer without repeating myself?
I could create a “create_for_customer” action and call “create” from
there, but stripping the customer select_tag somehow, since customer_id
will be passed from /customer/show.

How do you do the create_for_something stuff?

Just to understand you right: The code in your create action looks very
similar to the code in your create_for_customer action, right?

You have different choices to solve this:

  1. Have only one create action and try to decide what to do by examining
    the coming in parameters.

  2. Abstract your create logic and make a separate method from it. You
    can then call this method from every action you need the logic.

  3. Why not have two or more different actions, that look similar?
    Perhaps you are going to make one more complex in the future and an
    abstraction is not practicable anymore.

  4. Sometime it makes sense to have a data load method called from
    different actions, to ensure some necessary instance variables are
    available, like for select tags, that appear in different views.

What i would try to avoid, is calling one action from another, because
you cannot pass parameters to it.

I hope that helped.


Norman T.

http://blog.inlet-media.de


#3

Norman T. wrote:

Am Donnerstag, den 23.02.2006, 17:21 +0100 schrieb Rodrigo A.:

If I have a /budget/create action, how should I make a
/budget/create_for_customer without repeating myself?
I could create a “create_for_customer” action and call “create” from
there, but stripping the customer select_tag somehow, since customer_id
will be passed from /customer/show.

How do you do the create_for_something stuff?

Just to understand you right: The code in your create action looks very
similar to the code in your create_for_customer action, right?

Exactly

You have different choices to solve this:

  1. Have only one create action and try to decide what to do by examining
    the coming in parameters.

Yes, that was one of the approach, to keep it DRY.

  1. Abstract your create logic and make a separate method from it. You
    can then call this method from every action you need the logic.

Um, that’s interesting.

  1. Why not have two or more different actions, that look similar?
    Perhaps you are going to make one more complex in the future and an
    abstraction is not practicable anymore.

Yes, it’s not a bad idea at all, but as said above, i would like to
repeat the less possible.

  1. Sometime it makes sense to have a data load method called from
    different actions, to ensure some necessary instance variables are
    available, like for select tags, that appear in different views.
    Yes, i alway use that. I make a private method called populate_form

What i would try to avoid, is calling one action from another, because
you cannot pass parameters to it.

Now i think that the best approach is to have more than one action:
create, create_for_customer, but try to “extract” common code to a
private create_budget method, something like a helper.

I hope that helped.

Of course, thanks!

Regards,
Rodrigo.