best practice in ruby

F

Florian Weber

hi!

im wondering what is a best practice for some design question i have
right now.

i have a template class, presenting a template in a cms application. the
template has a eruby file, which contain helper methods like
editable_content('some_type', 'some_name')

the helper methods like editable_content are defined in my template
class.

now comes the real question: the helper methods can be used in two
ways creating the objects which represent the editable_content (and
saving
it as a active record) and during the rendering, where the
editable_content
method will fill in the real content.

to me it feels kinda bloated to implement both tasks in one class,
thats why
i would like to extract the rendering part into another class.

whats the best practice for this in ruby?

would i just create something like:

MyRenderingTemplateClass.new(my_template_object).render

basically a decorator, where i also redefine the editable_content method
of the my_template_object, so i can use its normal parsing methods?

thanks a lot for any tip!

ciao!
florian
 
S

Sean O'Dell

the helper methods like editable_content are defined in my template
class.

now comes the real question: the helper methods can be used in two
ways creating the objects which represent the editable_content (and
saving
it as a active record) and during the rendering, where the
editable_content
method will fill in the real content.

would i just create something like:

MyRenderingTemplateClass.new(my_template_object).render

I would protect the data in a class designed primarily to save/load from an
"active record" (whatever that is in your program). I would create a public
initializer for the class so it can be created at any time, and I would give
it a save method for saving. I would create a class factory method for the
load function, and a public global method for rendering:

class MyClass
def initialize
end

def save
end

def MyClass::load(record_id)
return new_myclass_instance
end
end

def render_myclass(myclass_instance)
end

The render method might have a better home, but from what I could gather from
your post, a simple public method should do fine. I'm not sure what sort of
data you are dealing with, but if you can present it as a hash, with named
keys and such, you might also provide a [] method and have the render method
simply treat its parameter like a hash; try to make it generic, so you can
test it disconnected from the class itself, or possibly re-use it elsewhere.

Sean O'Dell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top