I have a number of models that use an aggregation to combine First Name,
Middle Initial and Last Name and they have been working fine. I am
looking to clean this up, DRY it if possible.
I have things like this in application.rb
class Clwholename
attr_reader :first_name, :middle_initial, :last_name
def initialize(first_name, middle_initial, last_name)
@first_name = first_name
@middle_initial = middle_initial
@last_name = last_name
end
def to_s
[ @first_name, @middle_initial, @last_name ].compact.join(" ")
end
end
and then in whichever model I need to use this, I have...
composed_of :clwholename,
:class_name => Clwholename,
:mapping =>
[[ :first_name, :first_name ],
[ :middle_initial, :middle_initial ],
[ :last_name, :last_name ]
]
which is repeated in a number of models. I ran into a snag when trying
to use RAV (which is very cool but gags on my Clwholename class if I
include models) and the suggestion was to put the stuff in
lib/clwholename.rb and ''require'' it in my model.
I seem to be working fine by putting the class definition for
Clwholename in lib/clwholename.rb and requiring it in my model but the
section of "composed of :clwholename" simply doesn''t work if
I put it
into helpers (I assume because that is for views and not controllers) or
in the lib/clwholename (unknown method composed_of) and I am not getting
any DRYer.
What''s the best way to handle this?
Craig
If it''s universal controller logic, put it in
application_controller.rb.
- dan
--
Dan Kohn <mailto:dan@dankohn.com>
<http://www.dankohn.com/> <tel:+1-415-233-1000>
On Jul 22, 2006, at 3:53 PM, Craig White wrote:
> I have a number of models that use an aggregation to combine First
> Name,
> Middle Initial and Last Name and they have been working fine. I am
> looking to clean this up, DRY it if possible.
>
> I have things like this in application.rb
> class Clwholename
> attr_reader :first_name, :middle_initial, :last_name
> def initialize(first_name, middle_initial, last_name)
> @first_name = first_name
> @middle_initial = middle_initial
> @last_name = last_name
> end
> def to_s
> [ @first_name, @middle_initial, @last_name ].compact.join("
")
> end
> end
>
> and then in whichever model I need to use this, I have...
> composed_of :clwholename,
> :class_name => Clwholename,
> :mapping =>
> [[ :first_name, :first_name ],
> [ :middle_initial, :middle_initial ],
> [ :last_name, :last_name ]
> ]
>
> which is repeated in a number of models. I ran into a snag when trying
> to use RAV (which is very cool but gags on my Clwholename class if I
> include models) and the suggestion was to put the stuff in
> lib/clwholename.rb and ''require'' it in my model.
>
> I seem to be working fine by putting the class definition for
> Clwholename in lib/clwholename.rb and requiring it in my model but the
> section of "composed of :clwholename" simply doesn''t
work if I put it
> into helpers (I assume because that is for views and not
> controllers) or
> in the lib/clwholename (unknown method composed_of) and I am not
> getting
> any DRYer.
>
> What''s the best way to handle this?
>
> Craig
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
I would set up an acts_as_name for the model part of it. Craig White wrote:> I have a number of models that use an aggregation to combine First Name, > Middle Initial and Last Name and they have been working fine. I am > looking to clean this up, DRY it if possible.-- Posted via http://www.ruby-forum.com/.
On Sun, 2006-07-23 at 01:28 +0200, Yehuda Katz wrote:> I would set up an acts_as_name for the model part of it. > > Craig White wrote: > > I have a number of models that use an aggregation to combine First Name, > > Middle Initial and Last Name and they have been working fine. I am > > looking to clean this up, DRY it if possible.---- that makes sense but I wouldn''t know where to start. This sounds like a module. Any point to something I could read that would give me an idea on how to accomplish this? Craig