Skip to content
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

undefined method `status_tag' for NodeDecorator #2874

Closed
christoph-buente opened this issue Jan 16, 2014 · 11 comments
Closed

undefined method `status_tag' for NodeDecorator #2874

christoph-buente opened this issue Jan 16, 2014 · 11 comments

Comments

@christoph-buente
Copy link

I added draper support for my app (rails 4, activeadmin 1.0.0.pre, draper 1.3) and created a NodeDecorator with the following method:

  def wheelchair
    status_tag(model.wheelchair, :class => model.wheelchair)
  end

But it wouldn't work. Do i have to include AA View helpers manually? Maybe you can update the decorators documentation.

Cheers

@zorab47
Copy link
Contributor

zorab47 commented Jan 16, 2014

Currently the StatusTag component is only available within an Arbre context. I've added an application helper in the past to facilitate that type of usage:

# application_helper.rb
def status_tag(status)                                                            
  content_tag(:span, status, class: "status_tag #{status.titleize.gsub(/\s/,'').underscore}")
end

But then again, it would also be useful to provide a compatible method to access status_tag outside of Arbre contexts.

@christoph-buente
Copy link
Author

A context can be passed to the decorator. Can I access that abre context somehow an pass it on? Maybe that would be sufficient.

@seanlinsley
Copy link
Contributor

You can set up a helper that will let you use Arbre:

# 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

@christoph-buente
Copy link
Author

Thx a lot, that looks pretty neat. But what about performance? On a list of 100 Nodes and lets say 3 different columns, that would be 300 times a new Arbre::Context. Is it expensive to create a new context? I'll give it a try.

@timoschilling
Copy link
Member

Creating 100 Arbre::Context instances costs ~0.5ms. I think is can be closed.

@Fivell
Copy link
Member

Fivell commented Oct 27, 2014

Hello guys, what is proper way to use this peace of code #2874 (comment)?
there is no access to helpers from decorator using master, same situation when using helper.
I'm getting undefined method `arbre' for #ApiLogDecorator:0x007fb158b9ab48

UPS. Resolved we just need to pass decorator object as helper (2nd argument in Arbre::Context constructor)

@timoschilling
Copy link
Member

@Fivell can you post your code?

@Fivell
Copy link
Member

Fivell commented Nov 15, 2014

@cpfarher
Copy link

cpfarher commented Sep 2, 2015

I think, there is another problem with this approach. Some helpers are not defined in arbre context. For example in https://gist.github.com/cpfarher/d96cdb0c4de617237c7e#file-recipe_recorator-rb-L16 .... is there any solution for this kind of problem?

@danielricecodes
Copy link
Contributor

danielricecodes commented Jan 1, 2018

I can confirm @seanlinsley's example works with Draper Decorators also. You don't have to add the arbre method to ApplicationHelper either.

Gem Versions

  • ActiveAdmin 1.0
  • Draper 3.0

Code Example

class FooDecorator < Draper::Decorator
  delegate_all

  def aasm_state
    foo_instance = object
    #use foo_instance so the block doesn't get confused since `object` is already a method inside of an Arbre Context.
    arbre { status_tag(foo_instance.aasm_state, :ok) }
  end

  def some_boolean_field
    object.some_boolean_field? ? arbre { status_tag( "yes", :ok ) } : arbre { status_tag( "no" ) }
  end

  private
  
  def arbre(&block)
    Arbre::Context.new(&block).to_s
  end
end

@danielricecodes
Copy link
Contributor

danielricecodes commented Aug 8, 2020

At some point between my last comment in 2018 and now, there's been a change in Rails and/or ActiveAdmin which is causing the previously suggested solutions to be HTML Escaped.

That said, the following worked for me in present day on Rails 6.0.3.2 and ActiveAdmin 2.7.0.

Creating instances of ActiveAdmin::Views::StatusTag and calling #status_tag works, too.

class FooDecorator < ApplicationDecorator
  delegate_all
  def active
    object.active? ? status_tag( 'yes', class: 'ok' ) : status_tag( 'no' )
  end
end

class ApplicationDecorator < Draper::Decorator
  def status_tag(status, options = {})
    ActiveAdmin::Views::StatusTag.new.status_tag(status, options)
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants