jensljungblad / elemental_components

Simple view components for Rails 5.1+
MIT License
76 stars 5 forks source link

Eliminating the need for crazy rendering #19

Closed jensljungblad closed 5 years ago

jensljungblad commented 6 years ago

I think the only reason for the delegation craziness when rendering is because of helpers. Attributes and elements could have their values passed to the template as locals, no need for them to be functions. Only helpers need that. But if helpers are getters only, and calculated at render time, there is no need for them to be passed as functions either.

Without thinking further, either there could be a DSL, or perhaps even simpler:

class FooComponent < Component
  attribute :id
  attribute :data

  has_one :header
  has_one :footer

  def css_classes
    ...
  end

  protected

  def locals
    super.merge(
      css_classes: css_classes
    )
  end
end
jensljungblad commented 6 years ago

The issue here is perhaps that other elements, apart from the root component, would have access to actual functions (helpers) defined on the element, making the API not consistent. Perhaps the component and the element should be seen more as component/element builders somehow? Should element also have a render function? That way you could implement the element in ruby as well.. So element to_s would run render which would return @value by default?

I guess the discrepancy here is that components render by default a partial, while elements currently lack a render altogether.

Also, should elements be instantiated already at component creation?

jensljungblad commented 6 years ago

Another solution would be to just compute a hash when rendering. Never to expose the actual objects in the template.