inline templates
I'm kicking the tires of cells and am using a cell to handle the display of an address. I've found that the view object is useful for handling display logic, but the use of an extra template file seems like a lot for the small case I have.
Here's an example cell. It has some simple methods in it so that if I only have 1 value out of a typical pair, it'll only display the one value and won't have hanging commas with something like , New Netherland or Franklin,
class AddressCell < Cell::ViewModel
def show
render
end
private
property :address1
property :address2
property :address3
property :state
property :city
property :province
property :country
property :postal
def city_state
[city, state_name].reject{|att| att.blank? }.join(', ')
end
def state_name
(state.presence && state.name) || province
end
def country_and_postal
[country_name, postal].reject{|att| att.blank? }.join(', ')
end
def country_name
country.presence && country.name
end
end
The template is as small as this:
<%= address1 %><br />
<%= address2 %><br />
<%= address3 %><br />
<%= city_state %><br />
<%= country_and_postal %>
As I was hooking this up, I wondered about just using the model part of it and not the template file:
<%= cell :address, location do %>
<%= address1 %><br />
<%= address2 %><br />
<%= address3 %><br />
<%= city_state %><br />
<%= country_and_postal %>
<%- end -%>
I'm not sure that my app would stay this way, but it would allow me to play with handling the display logic and adjusting the layout before deciding to place it into a template file.
I started digging into the code but before trying to figure it out, I wanted both a sanity check and to see if this is something you'd be interested in having in the project.
+1 ... i'm finding the similar approach in react.js for simple 'templates' is really nice ... having it along with the normal cell template files would be a nice option to add ....
Wow, I love this "using the model part" idea - I never thought of this! It shouldn't be hard to implement. We could simply catch the block and evaluate it in cells context.
Many people use content_tag blocks in cell methods to achieve just that.
I had a slightly different idea a few years ago and I was evaluating either Erector or Ambre where you have the templates defined in Ruby, in the cell itself instead of a rather clumsy template snippet. This does not only speed up things but also allows way better overriding semantics, where you can programmatically overwrite parts of your "view" in subclasses.
class SongCell < Cell::ViewModel
view do
div do
address1
address2
end
end
I remember I loved the Erector gem by @alexch but we never integrated the two projects. Maybe it's time now? :fireworks:
for reference erector and arbre
Thats a fuckin' amazing idea... specially because we can "overwrite" and customize views (manually or programmatically) using ruby OOP
trying to go one step further on @saturnflyer idea, what is we could create "wrappers" for the view?
so we could do:
<%= cell :address, location do %>
<%= address1 %><br />
<%= address2 %><br />
<%= address3 %><br />
<%= city_state %><br />
<%= country_and_postal %>
<%- end -%>
or
cell :address, location, wrapper: :bootstrap
this would be a great addition to "form-builder-sprite-thing" no @apotonick ?
Another Erector spinoff is https://github.com/ageweke/fortitude which is a bit cleaner codebase and more up to date with the latest Rails. I've been using it on a client project and (apart for some headaches with nested blocks passed in to Rails helpers) it's working fine.
On Wed, Jul 22, 2015 at 8:40 AM, Celso Fernandes [email protected] wrote:
for reference erector https://github.com/erector/erector and arbre https://github.com/activeadmin/arbre
Thats a fuckin' amazing idea... specially because we can "overwrite" and customize views (manually or programmatically) using ruby OOP
trying to go one step further on @saturnflyer https://github.com/saturnflyer idea, what is we could create "wrappers" for the view?
so we could do:
<%= cell :address, location do %> <%= address1 %>
<%= address2 %>
<%= address3 %>
<%= city_state %>
<%= country_and_postal %><%- end -%>or
cell :address, location, wrapper: :bootstrap
this would be a great addition to "form-builder-sprite-thing" no @apotonick https://github.com/apotonick ?
— Reply to this email directly or view it on GitHub https://github.com/apotonick/cells/issues/309#issuecomment-123707888.
Alex Chaffee - [email protected] http://alexchaffee.com http://twitter.com/alexch
I attempted to implement this but don't understand enough about cells structure to do it quickly. So I looked to see if I could just implement it in my cell with this:
def show(&block)
if block
instance_eval(&block)
else
render
end
end
But it seems like the block is ignored.
Related to the template builders, I'd rather bake in easy support but not the actual implementation. There are already 3 options (Erector, Arbre, and Fortitude) mentioned. Why not take the approach that cells does with rails helpers and just force users to add what they need?
Both Erector and Fortitude use constructors to initialize view objects (because, you know, OO), so it would be easy to pass in a cell as a parameter that then turns into an instance variable. Or to pass in the cell's model.
Or alternately to give a cell a "view" method that returns a widget pre-initialized to point to the cell itself. Or to automagically make each of the model's fields a view instance var. Or...
I've tried a few times but never quite wrapped my head around the Cells architecture. Can you point me to a tutorial? Or better, a fully functional app project?