Liquid Markup Adapter Pattern For Rails Models
Liquid Markup is a way to build safe web page templates which protect the security of the web server.
Ruby On Rails has a typical model like this:
class Item < ActiveRecord::Basae
...
end
Liquid Markup adds a mixin named
liquid_methods that defines which methods are accessible via liquid:
class Item < ActiveRecord::Basae
...
liquid_methods :name, :description
...
end
Instead of putting the Liquid Markup into the ActiveRecord class, we can use an adapter pattern:
class Item < ActiveRecord::Basae
include ItemToLiquidMarkupAdapter
end
module ItemToLiquidMarkupAdapter
liquid_methods :name, :description
end
Advantages:
- Our designers are faster at reading our Liquid Markup Adapter modules because the code is so simple.
- One developer can work on the class, and another developer can work on the Liquid Markup adapter.
- Liquid Markup adapters can get long if there are many methods, and we like keeping the model clean.
- We use this pattern to add Liquid Markup methods for third party classes, such as Ruby gems.
- We can easily turn Liquid Markup on and off, simply by moving the module files.
What's Next?
blog comments powered by