Suppose a user fills his profile "About" section and has a very long "aaaaaaaaaaaaaaa......" word in it which breaks the site markup. Now, this word needs to be broken.
Generally, there are two ways to do it. I can either write a Rails helper which will do the job, and it will result in something like (HAML here)
.about= break_long_words(@user.about)
in my views, or I can use css and say:
.about { word-break: break-all; }
However the problem is that both options require me to go through soooo many places in my views (where I output something users entered into a field once) and I need to insert either an appropriate class name or a call to the break_long_words
helper! I can't remember all of those places.
Unfortunately, I also can't use body { word-break: break-all }
because it behaves really odd, breaking rather short words in odd places.
Ideally, I'd like to just list all fields in a model that require word breaking when called from views. How would one go about this task?
You may consider using a decorator with the Draper gem.
As a very rough example:
# Note, name as #{ClassName}Decorator
class UserInputDecorator < Draper::Decorator
delegate_all
LONG_ATTRS = %i(
attr1
attr2
).freeze
LONG_ATTRS.each do |long_attr|
define_method long_attr do
# your code to break word here
end
end
end
You can then call decorate
on the instance and it should override the values rendered in the view.