problem with dynamic fieldnames in haml with datamapper

42 views
Skip to first unread message

Torsten Schrör

unread,
Feb 2, 2016, 7:27:51 AM2/2/16
to Haml
Hello,

i have the following problem.
i try to build dynamical tables on haml with an set of fields in an array.

this meens i have the array
h['car']=Array[Array['title',fieldname'], Array['title2',fieldname2']]


i the haml i am able to do an

cars.each do |c|
  #{c.fieldname}
  #{c.fieldname2}


but i am not able to address this fields by reading the arry for the possible fields


can everyone help?


thanks
torsten

engineerDave

unread,
Feb 2, 2016, 10:37:16 AM2/2/16
to Haml
you have essentially the Array version of a ruby hash. 

so you could transform it to a hash and iterate over the keys and values

h['car'] = [['title', 'something1'], ['title2','something2']]
=> [["title", "something1"], ["title2", "something2"]]

a = h['car'].to_h
 => {"title"=>"something1", "title2"=>"something2"}

2.3.0 :022 > a.each {|key, value| puts key; puts value}
title
something1
title2
something2
 => {"title"=>"something1", "title2"=>"something2"}

Torsten Schrör

unread,
Feb 29, 2016, 12:44:18 AM2/29/16
to Haml
Hello engineerDave,

thank for you quick response.
But the convert from array to hash is not my problem.


I try do write an global list.haml for all tables to reduce the code

this is the list

%table
  %tr
    %td
      #{@title}
    %td{:colspan=>"7"}
      %a{:href=>"/#{@table}/new"}
        %img{:src=>"/pic/add.png"}
  -if @body.any?
    %tr
      %td{:colspan=>@header.count+1}
        =will_paginate @body
  %tr
    %td
    -...@header.each do |h|
      %td
        #{h[0]}
        %a{:href=>"/order/#{@table}/#{h[1]}/asc"}
          %img{:src=>"/pic/up.png"}
        %a{:href=>"/order/#{@table}/#{h[1]}/desc"}
          %img{:src=>"/pic/down.png"}
  %tbody
    -if @body.any?
      - @body.each do |b|
        %tr
          %td
            %a{:href=>"/#{@table}/edit/#{b.id}"}
              %img{:src=>"/pic/edit.png"}
            %a{:href=>"/#{@table}/del/#{b.id}"}
              %img.del{:src=>"/pic/del.png"}
          -...@header.each do |h|
            %td
              -if @datefields.include? h[1]
                #{format_date_display(b[h[1]])}
              -else
                %h=b[h[1]]

an input a have an array of fieldnames an fields as an helper

  def header(table)
    h={'default'=>Array[Array['ID','id']]}
    h['customer']=Array[Array['Name','name'],Array['Kundennr','reference']]
    h['domain']=Array[Array['Name','name'],Array['Kunde',"customer.name"] ]
    @header=h[table]
    @header=h['default'] if !@header || @header.count==0
  end

this works fine for normal fields(this meen fields in the table direct)
but if i try do access to an referencetable this not works
for instance domain.customer.name

direcly in  haml this works

-...@domainlist.each do |d|
  #{d.id}
  #{d.customer.name}




my problem is, that i find no syntax to handle this with a variable.

%h=b[h[1]]
-> this works only for fields in the table, references are empty

do you have an idea?

Thanks Torsten

Gunnar Wolf

unread,
Feb 29, 2016, 9:09:13 AM2/29/16
to ha...@googlegroups.com

Can you use hashes instead of arrays? That would be a much more natural use:

  - cars.each do |car| 
     - car.each  do |attr,val|
        %p
           %b= attr
           = val

Excuse me for uneven spacing, am not at my desktop.

--
You received this message because you are subscribed to the Google Groups "Haml" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haml+uns...@googlegroups.com.
To post to this group, send email to ha...@googlegroups.com.
Visit this group at https://groups.google.com/group/haml.
For more options, visit https://groups.google.com/d/optout.

Gunnar Wolf

unread,
Mar 1, 2016, 11:45:26 AM3/1/16
to ha...@googlegroups.com
Torsten Schrör dijo [Sun, Feb 28, 2016 at 09:44:18PM -0800]:
> Hello engineerDave,
>
> thank for you quick response.
> But the convert from array to hash is not my problem.
>
> I try do write an global list.haml for all tables to reduce the code
>
> this is the list
> (...)
>
> an input a have an array of fieldnames an fields as an helper
>
> def header(table)
> h={'default'=>Array[Array['ID','id']]}
> h['customer']=Array[Array['Name','name'],Array['Kundennr','reference']]
> h['domain']=Array[Array['Name','name'],Array['Kunde',"customer.name"] ]
> @header=h[table]
> @header=h['default'] if !@header || @header.count==0
> end
>
> this works fine for normal fields(this meen fields in the table direct)
> but if i try do access to an referencetable this not works
> for instance domain.customer.name
>
> direcly in haml this works
>
> -...@domainlist.each do |d|
> #{d.id}
> #{d.customer.name}
>
> my problem is, that i find no syntax to handle this with a variable.
>
> %h=b[h[1]]
> -> this works only for fields in the table, references are empty
>
> do you have an idea?

Ahh, right, welcome to the wonderful world of metaprogramming ;-)

In Ruby, you can "send" a string to arbitrary object, that is, you can
call the method of the corresponding name. Take this as an example:

>> string = 'This is a string'
=> "This is a string"
>> queries = ['length', 'reverse', 'empty?' ]
=> ["length", "reverse", "nil?"]
>> queries.map {|q| string.send(q) }
=> [16, "gnirts a si sihT", false]

But be *very* aware that sending arbitrary strings to be called on an
object, specially if any user-defined data is involved, can become a
huge security liability.
Reply all
Reply to author
Forward
0 new messages