Attributarchy
An attribute-driven hierarchy builder for Rails views.
The Gist
- Configure a hierarchy (or hierarchies) of attributes
- Create partials for each attribute that you want to render
- Style and/or bind JavaScript as desired
- Feed the attributarchy some data
The result: Dynamic, hierarchical views that allow you to easily dish out an array of layouts, styles, and behavior. All as simple or complex as you'd like.
Usage
Gemfile
gem 'attributarchy'
Controller (example)
include Attributarchy
has_attributarchy \
#----------#
# Required #
#----------#
# Name it, as you can define multiple
:name,
# Specify an array of attributes that constitute the (?:attribut|hier)archy
as: [:attribute, ...],
#----------#
# Optional #
#----------#
# Specify an additional lookup path (as a string) or paths (as an array)
in: %w[this_path that_path],
# Specify an attribute (as a symbol) or attributes (as an array) that will
# only be used for grouping -- not rendering
without_rendering: [:a_no_show]
Partials (example)
In your controller's view directory (or another location configured via :in
), define a partial for all rendering attributes. The locals provided to these are:
-
group_data
— The data set of the grouped-by attribute -
group_value
— The name of the grouped-by attribute -
group_level
— An integer representing the position of the grouping within the hierarchy
View (example)
build_attributarchy(:name, data_set)
Assets (example)
The entire attributarchy will be wrapped in a div
with the class "attributarchy" and each attributarchy will be wrapped in a div
with the class of "attribute-attributarchy".
Working With Engines
My knowledge is limited here, but to get this working in an engine you must specify a full lookup path, e.g.:
in: "#{YourEngine::Engine.root}/app/views/your_engine/..."
I believe this is the only way to do this short of converting the gem to a rail tie, but I'm not positive. Besides, I don't want Attributarchy included in every controller anyway — it seems quite specialized.
Examples
To see a simple example, start the dummy rails app and hit its root.