Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arbre helper for Rails #38

Closed
timoschilling opened this issue Oct 25, 2014 · 1 comment
Closed

Arbre helper for Rails #38

timoschilling opened this issue Oct 25, 2014 · 1 comment

Comments

@timoschilling
Copy link
Member

Example written from @seanlinsley on activeadmin/activeadmin#2874:

# app/helpers/application_helper.rb
module ApplicationHelper
  def arbre(&block)
    Arbre::Context.new(&block).to_s
  end
end

# app/decorators/foo_decorator.rb
class FooDecorator < Draper::Decorator
  def wheelchair
    arbre do
      status_tag model.wheelchair, class: model.wheelchair
    end
  end
end

The other option is to use a preinitialised instance that be dup for each call:

module ApplicationHelper
  def arbre_context
    @arbre_context ||= Arbre::Context.new
    @arbre_context.dup
  end

  def arbre(&block)
    arbre_context.instance_eval(&block).to_s
  end
end

dup is ~3 times faster that new

context = Arbre::Context.new
Benchmark.ips do |b|
  b.config(:time => 5, :warmup => 2)
  b.report("dup"){ context.dup }
  b.report("new"){ Arbre::Context.new }
  b.compare!
end
Calculating -------------------------------------
                 dup      1110 i/100ms
                 new      1026 i/100ms
-------------------------------------------------
                 dup   819778.0 (±10.9%) i/s -    3751800 in   4.650747s
                 new   284126.6 (±8.3%) i/s -    1367658 in   4.851749s

Comparison:
                 dup:   819778.0 i/s
                 new:   284126.6 i/s - 2.89x slower
@serradura
Copy link

👍 for the dup approach.

@activeadmin activeadmin locked and limited conversation to collaborators Mar 14, 2023
@javierjulio javierjulio converted this issue into discussion #464 Mar 14, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants