ruby - Rendering a template based on object type in Rails -


i'm writing small forum app users can create different types of forums. example, announcements, discussions, or questions forum.

the forum model has many posts, , forum_type column. render posts#show using different template, based on @post.forum_type column.

so each forum, based on type, have distinct appearance posts.

how can without littering code if @post.forum_type == 'something' ... ?

delegation. write post renderer, , implement 1 concrete renderer subclass per forum type:

class post   attr_accessor :forum_type        end  class baserenderer   def renderer_for(post)      # create correct renderer post here   end    def render_post(post)     renderer = renderer_for(post)     renderer.to_html  # return results end  class forumapostrenderer   def initialize(post)     @post = post   end    def render     # render post forum here\   end end  class forumbpostrenderer   def initialize(post)     @post = post   end    def render     #render post forum b here   end end 

this means can skin different forums implementing renderer , adding little bit of logic renderer_for method.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -