Create some custom modules for your functionality then include them
in your model.
module MyStringUtilities
def make_url_friendly(string)
end
end
class Tag < AR::Base
include MyStringUtilities
end
Write direct extensions to built-in classes
class String
def make_url_friendly
end
end
The above would let you use the syntax you use in your post
(Tag.name.make_url_friendly). Both have their place. Choose whichever
you feel more comfortable with.
Perfect! I put class String… end at the end of my
application_helper.rb. I am not sure if tha tis the best place to put
it but right now it makes the most sense. Any better ideas?
The above would let you use the syntax you use in your post
(Tag.name.make_url_friendly). Both have their place. Choose whichever
you feel more comfortable with.
I think this is merely a mix up of terminology. The word “helper” in
the Rails sense derives from the ViewHelper pattern. I believe the OP
was simply referring to common utility methods used throughout his code.
From my understanding, helper methods are meant to help with views
only. For something like this, you should put it in the Tag model class
(app/models/tag.rb)
The above would let you use the syntax you use in your post
(Tag.name.make_url_friendly). Both have their place. Choose whichever
you feel more comfortable with.
I would say you should prefer the first method of using modules unless
there is a very strong case for extending builtin objects. I think
going for a module first, and then seeing just how useful certain
methods are before going to a builtin. Extending String is a very
powerful and useful thing, but it can also be dangerous as it clutters
the namespace and could result in conflicts in the future (ex: you
write String#to_url, and two months from now Rails includes a
conflicting version).
Seth, I fail to see how putting utility, yet project-specific, library
code in lib (where it belongs) is “hacky”. Plugins would be completely
overkill for this.
Seth, I fail to see how putting utility, yet project-specific, library
code in lib (where it belongs) is “hacky”. Plugins would be completely
overkill for this.
There is one implementation option that is surprisingly void from this
conversation: plugins.
One of the reasons plugins exist is a better form of “throw some shit
in /lib and require it”
This smells like a good case for an extension of AR::Base, perhaps.
Look at plugins. They are easy and grand and way better than the hacky
lib approach.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.