Skip to content

mikdiet/polymorphic_render

Repository files navigation

PolymorphicRender

Installing

Add in your gemfile

gem 'polymorphic_render'

Using

Just write in your views instead:

= render @model
-# or
= render @models

this:

= polymorphic_render @model
-# or
= polymorphic_render @models

And.. nothing changes! :) It renders views/models/_model partial and passes model local variable into.

But if you write:

= polymorphic_render @model, :admin
-# or
= polymorphic_render @models, :admin

It renders views/models/_model_admin partial and passes model local variable into.

Array can be passed as second parameter:

= polymorphic_render @model, [:super, :admin]
-# or
= polymorphic_render @models, [:super, :admin]

It renders views/models/_model_super_admin partial and passes model local variable into.

When it's useful?

  • It's useful when your need another partial for resource rendering, so instead:

    - @models.each do |model|
      = render partial: 'model/model_admin', object: model, as: 'model'

    you can simply write:

    = polymorphic_render @models, :admin
  • It's useful when some model's atribute defines which kind of partial to render:

    = polymorphic_render @model, @model.kind

License

This project rocks and uses MIT-LICENSE.