Forum: Ruby on Rails function to be used in model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cad27a22166c3a46e34da899476ec897?d=identicon&s=25 ryan (Guest)
on 2006-06-07 22:57
Where is the best place to put a function that will be used in both
several controller and several models?
6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2006-06-08 00:03
ryan wrote:
> Where is the best place to put a function that will be used in both
> several controller and several models?
You *could* stick it in a utility class in lib/:

- lib/my_utility.rb
class MyUtility
  def self.my_function(bar)
    # code
  end
end

then use it:

foo = MyUtility::my_function(bar)

That could get kinda messy though.

It really depends on what you're actually trying to do.  You may be able
to write something extends an existing class.  For example,  if you have
a string format that you use throughout your site, you can add a method
into the String class:

- lib/string.rb
class String
  def to_foo
    "foo "+self
  end
end

which results in:

mystring = "bar"
mystring.to_foo   ->   "foo bar"

Hope that provides some thought food :0)

Steve
Cad27a22166c3a46e34da899476ec897?d=identicon&s=25 ryan (Guest)
on 2006-06-08 15:05
What I am trying to do is to write a quota system.  The limits are
stored in the DB and I want to use a function to get the object that
contains the limits (to honor the DRY principle).  Obviously both models
and views will need to access the quota limits.

Eventually it would be nice to have a Quota plugin, but right now I'm
still trying to understand some of the finer points of where to put
objects that need various degrees of coupling to the rest of the system.

But thanks for the help, it seems that putting a utility class in the
lib directory should work for now.

Stephen Bartholomew wrote:
> ryan wrote:
>> Where is the best place to put a function that will be used in both
>> several controller and several models?
> You *could* stick it in a utility class in lib/:
>
> - lib/my_utility.rb
> class MyUtility
>   def self.my_function(bar)
>     # code
>   end
> end
>
> then use it:
>
> foo = MyUtility::my_function(bar)
>
> That could get kinda messy though.
>
> It really depends on what you're actually trying to do.  You may be able
> to write something extends an existing class.  For example,  if you have
> a string format that you use throughout your site, you can add a method
> into the String class:
>
> - lib/string.rb
> class String
>   def to_foo
>     "foo "+self
>   end
> end
>
> which results in:
>
> mystring = "bar"
> mystring.to_foo   ->   "foo bar"
>
> Hope that provides some thought food :0)
>
> Steve
6ef8cb7cd7cd58077f0b57e4fa49a969?d=identicon&s=25 Brian Hogan (Guest)
on 2006-06-08 18:09
(Received via mailing list)
Something like this is perfect for a Module.

Read up on modules and mixins and you will find what you need.

http://www.rubycentral.com/book/tut_modules.html
This topic is locked and can not be replied to.