Tuesday, 15 May 2012

ruby on rails - How to DRY up arbre code into a reusable component? -



ruby on rails - How to DRY up arbre code into a reusable component? -

i have mutual pattern or repeated code i'd dry in activeadmin views. i'm using arbre components render much of views can , i'd maintain way if possible (i.e. don't want convert straight html in normal fashion -- i'm trying understand arbre way here). here's code i'd dry up:

clients.in_groups_of(3).each |clients_group| columns clients_group.compact.each |client| column panel client.name # ... end end end end end

after reading through documentation in arbre gem, started seek create own, custom arbre component. forced realize have no thought how satisfy arbre. couldn't figure out how pass local variables block. example:

# config/initializers/active_admin.rb module activeadmin module views class clientsbreakdown < activeadmin::component builder_method :clients_breakdown def build(clients, attributes = {}) group_size = attributes.delete(:in_groups_of) { 3 } clients.in_groups_of(group_size).each |clients_group| columns clients_group.compact.each |client| column panel client.name super(attributes) # doesn't seem matter `super` phone call # is, want able pass `client` # `clients_breakdown` block here # yield(client) # -- i've tried adding this. end end end end end end end end end

then, calling in activeadmin user view might like:

clients_breakdown(client.all, in_groups_of: 2) |client| ul li client.name end end

running above code results in error:

update 2 exception has changed after moving custom component code activeadmin::views module.

my key issue seems can't phone call yield(client) have super(attributes). that's arbre thing don't know there pass client calling block. right track or there way dry up?

update 1

i've realized phone call super can happen anywhere in build method , has nil output. if move super(attributes) phone call up... still can't figure out set within of panel block can render rest of arbre components in there phone call clients_breakdown.

here 1 potential solution.

a few things note super(attributes) should not called unless clientbreakdown arbre component outputting own html. arbre components used build html scratch, not compose components.

module activeadmin module views class clientsbreakdown < activeadmin::component builder_method :clients_breakdown def build(clients, attributes = {}) group_size = attributes.delete(:in_groups_of) { 3 } clients.in_groups_of(group_size).each |clients_group| columns clients_group.compact.each |client| column panel client.name yield client end end end end end end end end end

another approach define helper methods provide same functionality in module included in activeadmin::views::pages::base. activeadmin defines helper methods build various views, attributes_table.

module clientsbreakdown def clients_breakdown(clients, attributes = {}) group_size = attributes.delete(:in_groups_of) { 3 } clients.in_groups_of(group_size).each |clients_group| columns clients_group.compact.each |client| column panel client.name yield client end end end end end end end # config/initializers/active_admin.rb activeadmin::views::pages::base.include clientsbreakdown

ruby-on-rails ruby ruby-on-rails-4 activeadmin arbre

No comments:

Post a Comment