Hi there,
my short question:
Assume I have these 2 cases in my code :
============================
:name => h(agent.firstname)
[…]
:name => h(agent.sharedname)
To stay DRY, I want this to become just 1 line, by replacing
“first”/“shared” by a variable that I would set at the beginning.
How would the code of this 1 new line and the initiation of the variable
look like?
Thanks for your help!
Tom
I think that’s overdoing the DRY thing a little bit, but if you want:
name_attr = :"#{some_condition ? ‘first’ : ‘shared’}name"
:name => h(agent.send(name_attr))
On Feb 12, 2:35 pm, Tom Ha [email protected] wrote:
To stay DRY, I want this to become just 1 line, by replacing
“first”/“shared” by a variable that I would set at the beginning.
How would the code of this 1 new line and the initiation of the variable
look like?
Well you could either use send, but to be quite honest I really
wouldn’t - You’ll end up with something that is probably longer and
certainly more complicated.
Fred
On 12 February 2010 14:39, Mat B. [email protected] wrote:
I think that’s overdoing the DRY thing a little bit, but if you want:
name_attr = :“#{some_condition ? ‘first’ : ‘shared’}name”
:name => h(agent.send(name_attr))
That’s putting business logic in the view - avoid wherever possible
for sleep-filled nights…
On 12 February 2010 14:35, Tom Ha [email protected] wrote:
How would the code of this 1 new line and the initiation of the variable
look like?
Difficult to suggest an ideal solution with such a little info, but
maybe add a “first_or_shared_name” method to your Agent model, which
determines which name to show, and then call that in the views instead
of either firstname or sharedname:
agent.rb
def first_or_shared_name
case some_method_or_variable_that_determines_which_name
when :first
self. firstname
when :shared
self.sharedname
else
raise “invalid value passed to my cool new method”
end
end
view
:name => h(agent.first_or_shared_name)
then if you want/need to, you could pass the variable to the model
method from the view if it’s likely to change lots:
agent.rb
def first_or_shared_name(first_or_shared)
case first_or_shared
… etc
end
end
view
:name => h(agent.first_or_shared_name :shared)
:name => h(agent.first_or_shared_name :first)
Coming back to the point, you might just be making work for the sake
of it, but I’ve used approaches like this when, for instance, I want
to show “firstname lastname” or “title lastname” depending on whether
I have the firstname or not.
While I agree that your solution of having a method in the model that
delegates to the appropriate attribute is elegant (if the model has
access to the appropriate data to make that decision), I wonder what
reason we have to believe this is going on in the view?
On 12 February 2010 14:52, Mat B. [email protected] wrote:
While I agree that your solution of having a method in the model that
delegates to the appropriate attribute is elegant (if the model has
access to the appropriate data to make that decision), I wonder what
reason we have to believe this is going on in the view?
:name => h(agent.send(name_attr))
the call to “h” gave me a clue, but if it’s not a view (and he’s
calling view helpers in models or controllers), the principle of
keeping model logic in the model is fairly sound.
Thanks a lot for your quick help, guys !
Actually, all of your input is very helpful (some of it right away, some
of it later)!
On 12 February 2010 14:52, Mat B. [email protected] wrote:
(if the model has
access to the appropriate data to make that decision)
that’s what occurred to me as I wrote it, hence the suggestion to pass
the determination in as a parameter to the method.
Like I said, with such little info from the OP, it’s hard to be
conclusive. If we’ve given him a few different choices, one of them
might be what he needs