arbre icon indicating copy to clipboard operation
arbre copied to clipboard

can i use helper methods that use arbre in *.arb templates ?

Open senid231 opened this issue 9 years ago • 5 comments

example that does not work:

app/helpers/application_helper.rb

module ApplicationHelper
  def arbre(&block)
    Arbre::Context.new({}, self, &block)
  end

  def static_pie_chart(data)
    arbre do
      div class: 'd3-pie-chart', 'data-series': data
    end
  end
end

app/views/dashboard/index/html.arb

h1 'Dashboard#index'
static_pie_chart [{label: 'first', value: 23}, {label: 'second', value: 46}]

and it's same as:

h1 'Dashboard#index'
Arbre::Context.new({}, self) do
  div class: 'd3-pie-chart', 'data-series': [{label: 'first', value: 23}, {label: 'second', value: 46}]
end

and this is doesn't work. div with class 'd3-pie-chart' doesn't appear in rendered html because context that executes *.arb file just ignore new Arbre::Context

example that works but not like i want №1:

app/helpers/application_helper.rb

module ApplicationHelper
  def arbre(&block)
    Arbre::Context.new({}, self, &block)
  end

  def static_pie_chart(data)
    arbre do
      div class: 'd3-pie-chart', 'data-series': data
    end
  end
end

app/views/dashboard/index/html.arb

h1 'Dashboard#index'
div do
  text_node static_pie_chart([{label: 'first', value: 23}, {label: 'second', value: 46}])
end

it works because text_node convert first parameter into string but i don't want to wrap each method into text_node

example that works but not like i want №2:

app/helpers/application_helper.rb

module ApplicationHelper
  def arbre(&block)
    Arbre::Context.new({}, self, &block).to_s
  end

  def static_pie_chart(data)
    arbre do
      div class: 'd3-pie-chart', 'data-series': data
    end
  end
end

app/views/dashboard/index/html.arb

h1 'Dashboard#index'
div do
  static_pie_chart [{label: 'first', value: 23}, {label: 'second', value: 46}]
end

it works because static_pie_chart returns html_safe string but i don't want to wrap every method into div or span

question

how can i write helper methods if i want them to return child elements of current context?

senid231 avatar Feb 06 '16 13:02 senid231

arbre know about helpers but helpers doesn't know anything about context only solution i have right now is:

module ApplicationHelper
  def arbre(&block)
    Arbre::Context.new({}, self, &block).to_s
  end

  def static_pie_chart(data)
    arbre do
      div class: 'd3-pie-chart', 'data-series': data
    end
  end
end
h1 'Dashboard#index'
pie_chart = static_pie_chart [{label: 'first', value: 23}, {label: 'second', value: 46}]
text_node pie_chart

senid231 avatar Feb 06 '16 13:02 senid231

have you tried this?

module ApplicationHelper
  def arbre(&block)
    text_node Arbre::Context.new({}, self, &block).to_s
  end

  def static_pie_chart(data)
    arbre do
      div class: 'd3-pie-chart', 'data-series': data
    end
  end
end

# or

module ApplicationHelper
  def arbre(&block)
    Arbre::Context.new({}, self, &block).to_s
  end

  def static_pie_chart(data)
    arbre do
      text_node div(class: 'd3-pie-chart', 'data-series': data)
    end
  end
end
h1 'Dashboard#index'
static_pie_chart [{label: 'first', value: 23}, {label: 'second', value: 46}]

timoschilling avatar Feb 06 '16 15:02 timoschilling

@timoschilling How can we use text_node in a helper?

knubie avatar Nov 03 '17 14:11 knubie

@senid231 Don't do that. :scream: Try the Arbre way:

class StaticPieChart < Arbre::Component
  builder_method :static_pie_chart
  def default_class_name
    'd3-pie-chart'
  end
  def buid(data, attributes = {})
    super attributes.merge('data-series': data)
  end
end

@knubie Don't do that. If you are trying to use text_node in a helper you are lost and confused. Post more details on StackOverflow tagged arbre and I'll respond.

varyonic avatar Nov 03 '17 18:11 varyonic

thanks @varyonic I think this will help

senid231 avatar Nov 07 '17 12:11 senid231

Components are the way.

javierjulio avatar Feb 10 '23 15:02 javierjulio