Module: Zena::Use::Forms::ZafuMethods

Defined in:
lib/zena/use/forms.rb

Overview

ViewMethods

Instance Method Summary collapse

Instance Method Details

#form_hidden_fields(opts) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/zena/use/forms.rb', line 267

def form_hidden_fields(opts)
  dom_name = node.dom_prefix || dom_name
  
  hidden_fields = super
  add_params = @context[:add] ? @context[:add].params : {}
  set_fields = []
  @markup.params[:class] ||= 'form'

  (descendants('input') + descendants('select')).each do |tag|
    set_fields << "#{node.form_name}[#{tag.params[:name]}]"
  end

  if (descendants('input') || []).detect {|elem| elem.params[:type] == 'submit'}
    # has submit
  else
    # Hidden submit for Firefox compatibility
    hidden_fields['submit'] = ["<input type='submit'/>"]
  end

  if template_url = @context[:template_url]
    # Ajax
    hidden_fields['link_id'] = "<%= #{node}.link_id %>" if @context[:has_link_id] && node.will_be?(Node)

    f = @method == 'form_tag' ? ancestor('form') : self
    if upd = f && f.params[:update]
      if target = find_target(upd)
        hidden_fields['u_url']   = target.template_url
        hidden_fields['udom_id'] = upd # target.node.dom_prefix ? (but target.node is not set yet...)
        # hidden_fields['u_id']    = "<%= #{@context[:parent_node]}.zip %>" if @context[:in_add]
        hidden_fields['s']       = "<%= start_node_zip %>"
      end
    # elsif (block = ancestor('block')) && node.will_be?(DataEntry)
    #   # updates template url
    #   hidden_fields['u_url']   = block.template_url
    #   hidden_fields['udom_id'] = block.erb_dom_id
    end

    hidden_fields['t_url'] = template_url
                                                                         # This is a hack to fix wrong dom_prefix in drop+add.
    #erb_dom_id = @context[:saved_template] ? "<%= ndom_id(#{node}, false) %>" : (@context[:dom_prefix] || node.dom_prefix)

    if @context[:saved_template]
      hidden_fields['dom_id'] = erb_dom_id = "<%= ndom_id(#{node}, false) %>"
    else
      hidden_fields['dom_id'] = erb_dom_id = node.dom_prefix
    end

    if node.will_be?(Comment)
      # FIXME: the "... || '@node'" is a hack and I don't understand why it's needed...
      hidden_fields['node_id'] = "<%= #{node.get(Node) || '@node'}.zip %>"
    elsif node.will_be?(DataEntry)
      return parser_error("Missing :data_root in context (internal error)") unless data_root = @context[:data_root]
      hidden_fields["data_entry[#{data_root}_id]"] = "<%= #{@context[:in_add] ? node(Node) : "#{node}.#{data_root}"}.zip %>"
    end

    if add_block = @context[:add]
      params = add_block.params
      hidden_fields['zadd'] = 'true'
      [:after, :before, :top, :bottom].each do |sym|
        if value = params[sym]
          hidden_fields['position'] = sym.to_s
          if value == 'self'
            if sym == :before
              hidden_fields['reference'] = "#{erb_dom_id}_add"
            else
              hidden_fields['reference'] = "#{erb_dom_id}_0"
            end
          else
            hidden_fields['reference'] = value
          end
          break
        end
      end
      
      if params[:done]
        if params[:done] == 'focus'
          hidden_fields['done'] = "'$(\"#{erb_dom_id}_form_t\").focusFirstElement();'"
        else
          done = RubyLess.translate_string(self, params[:done])
        end
      elsif params[:focus]
        hidden_fields['done'] = "'$(\"#{erb_dom_id}_#{params[:focus]}\").focus();'"
      end
    else
      # ajax form, not in 'add'
      done = RubyLess.translate_string(self, @params[:done])
    end
    if done
      if done.literal
        done = done.literal
      else
        done = "<%= fquote #{done} %>"
      end
      hidden_fields['done'] = done
    end
  elsif upd = @params.delete(:preview)
    hidden_fields['t_url']  = template_url(upd)
    hidden_fields['dom_id'] = upd
    hidden_fields['s']      = "<%= start_node_zip %>"
    loading = @params[:loading]
    loading = 'Zena.loading' if loading == 'true'
    out "<% filter_form(#{node}, \"#{dom_name}_form_t\", #{loading.inspect}, '#{upd}') %>"
  else
    # no ajax
    cancel = "" # link to normal node ?
  end

  if node.will_be?(Node) && (@params[:klass] || @context[:klass])
    hidden_fields['node[klass]'] = @params[:klass] || @context[:klass].name
  end

  if node.will_be?(Node) && @params[:mode]
    hidden_fields['mode'] = @params[:mode]
  end

  hidden_fields['node[v_status]'] = Zena::Status::Pub.to_s if add_params[:publish] || auto_publish_param || @context[:publish_after_save]

  # All default values set in the <r:new> field should at least appear as hidden fields

  if new_keys = node.opts[:new_keys]
    input_keys = (
      (descendants('input') || []).map {|e| e.params[:name]} +
      hidden_fields.keys.map do |e|
        if e =~ /.*\[(.*)\]/
          $1.to_sym
        else
          nil
        end
      end
    ).compact.uniq

    new_keys.each do |key, value|
      # Security: make sure value does not come from user input !
      # TINT !
      next if input_keys.include?(key) || value.nil?
      hidden_fields["node[#{key}]"] = "<%= #{value.sub(/^this\./, "#{node}.")} %>"
    end
  end

  # Read @params
  add_params.merge(@params).each do |key, value|
    # r_add params
    next if [:after, :before, :top, :bottom, :focus, :publish,
    # r_form params
             :klass, :done, :on, :update,
    # r_each params (make_form)
             :join, :alt_class].include?(key)
    code = ::RubyLess.translate(self, value)
    if code.literal.kind_of?(String) || code.literal.kind_of?(Number)
      hidden_fields[key.to_s] = "#{code.literal}"
    else
      hidden_fields[key.to_s] = "<%= #{code} %>"
    end
  end

  hidden_fields.reject! do |k,v|
    # There is an explicit <input> field for this key, remove hidden value
    set_fields.include?(k)
  end

  hidden_fields
end

#form_optionsObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/zena/use/forms.rb', line 174

def form_options
  opts = super

  dom_name = node.dom_prefix
  opts[:form_helper] = 'f'

  if upd = @params[:update]
    if target = find_target(upd)
      @context[:template_url] = target.template_url
    end
  end

  if template_url = @context[:template_url]
    # Ajax

    if edit_or_cancel = descendant('cancel') || descendant('edit')
      if cancel_text = edit_or_cancel.params[:cancel] ||
        (edit_or_cancel.method == 'cancel' && edit_or_cancel.params[:text])
      elsif cancel_text = edit_or_cancel.params[:tcancel] ||
        (edit_or_cancel.method == 'cancel' && edit_or_cancel.params[:t])
        cancel_text = RubyLess.translate(self, "t(%Q{#{cancel_text}})")
        if cancel_text.literal
          cancel_text = cancel_text.literal
        else
          cancel_text_ruby = cancel_text
          cancel_text = "<%= #{cancel_text} %>"
        end
      end
      cancel_pre  = ''
      cancel_post = ''
    else
      cancel_pre  = "<p class='btn_x'>"
      cancel_post = "</p>"
    end

    cancel_text ||= _('btn_x')
    cancel_text_ruby ||= cancel_text.inspect

    if @context[:in_add]
      # Inline form used to create new elements: set values to '' and 'parent_id' from context
      opts[:id]          = "#{node.dom_prefix}_0"
      opts[:form_tag]    = "<% remote_form_for(:#{node.form_name}, #{node}, :url => #{node.form_name.pluralize}_path, :html => {:id => \"#{dom_name}_form_t\"}) do |f| %>"
      opts[:form_cancel] = "#{cancel_pre}<a href='javascript:void(0)' onclick='[\"#{dom_name}_add\", \"#{dom_name}_0\"].each(Element.toggle);return false;'>#{cancel_text}</a>#{cancel_post}\n"
    else

      # Saved form
      if @markup.tag == 'table'
        # the normal id goes to the form wrapping the table
        opts[:id]   = "#{node.dom_prefix}_tbl"
        str_form_id = "\#{ndom_id(#{node})}"
      else
        opts[:id]   = "<%= ndom_id(#{node}) %>"
        str_form_id = "\#{ndom_id(#{node})}_form_t"
      end

      form_id ||= "#{node.dom_prefix}_form_t"

      opts[:form_tag]    = %Q{
  <% remote_form_for(:#{node.form_name}, #{node}, :url => #{node}.new_record? ? #{node.form_name.pluralize}_path : #{node.form_name}_path(#{node}.zip), :html => {:method => #{node}.new_record? ? :post : :put, :id => \"#{str_form_id}\"}) do |f| %>
  }

      opts[:form_cancel] = %Q{
  <% if #{node}.new_record? %>
  #{cancel_pre}<a href='javascript:void(0)' onclick='[\"<%= params[:dom_id] %>_add\", \"<%= params[:dom_id] %>_0\"].each(Element.toggle);return false;'>#{cancel_text}</a>#{cancel_post}
  <% else %>
  #{cancel_pre}<%= link_to_remote(#{cancel_text_ruby}, :url => #{node.form_name}_path(#{node}.zip) + \"/zafu?t_url=#{CGI.escape(template_url)}&dom_id=\#{params[:dom_id]}#{@context[:has_link_id] ? "&link_id=\#{#{node}.link_id}" : ''}\", :method => :get) %>#{cancel_post}
  <% end %>
  }
    end
  else
    # no ajax
    if descendants('errors')
      error_messages = ''
    else
      error_messages = r_errors + "\n"
    end

    html_id = "#{node.dom_prefix}_form_t".inspect
    if descendant('upload_field')
      uuid = UUIDTools::UUID.random_create.to_s.gsub('-','')
      set_context_var('upload', 'uuid', uuid)
      upload_html = ", :multipart => true, :onsubmit => %Q{submitUploadForm(#{html_id}, '#{uuid}');}"
      upload_url  = "'#{Zena::Use::Upload::UPLOAD_KEY}' => '#{uuid}'"
    end
    
    opts[:form_tag] = %Q{
<% form_for(:#{node.form_name}, #{node}, :url => #{node}.new_record? ? #{node.form_name.pluralize}_path(#{upload_url}) : #{node.form_name}_path(#{node}.zip#{upload_url ? ", #{upload_url}" : ""}), :html => {:method => #{node}.new_record? ? :post : :put, :id => #{html_id}#{upload_html}}) do |f| %>
#{error_messages}}
  end

  opts
end

#get_input_params(params = @params) ⇒ Object

Parse params to extract everything that is relevant to building input fields. TODO: refactor and pass the @markup so that attributes are added directly TODO: get attribute type in get_input_params (safe_method_type)



704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
# File 'lib/zena/use/forms.rb', line 704

def get_input_params(params = @params)
  res = Zafu::OrderedHash.new
  if name = (params[:param] || params[:name] || params[:date])
    res[:name] = name
    if params[:param]
      if name =~ /^[a-z_]+$/
        sub_attr_ruby = "params[:#{name}]"
      else
        sub_attr_ruby = "param_value(#{name.inspect})"
      end
    else
      # build name
      if res[:name] =~ /\A([\w_]+)\[(.*?)\]/
        # Sub attributes are used with tags or might be used for other features. It
        # enables things like 'tagged[foo]'
        attribute, sub_attr = $1, $2
      else
        attribute = res[:name]
      end

      if sub_attr
        res[:name] = "#{node.form_name}[#{attribute}][#{sub_attr}]"
      else
        res[:name] = "#{node.form_name}[#{attribute}]"
      end
    end
    
    if value = params[:value]
      # On refactor, use append_markup_attr(markup, key, value)
      value = RubyLess.translate_string(self, value)

      if value.literal
        res[:value] = form_quote(value.literal.to_s)
      else
        res[:value] = "<%= fquote(#{value}) %>"
      end
    elsif params[:param]
      res[:value] = "<%= fquote(#{sub_attr_ruby}) %>"
    end
    
    if sub_attr
      type = node.klass.safe_method_type([attribute], node)
      if sub_attr_ruby = RubyLess.translate(self, %Q{this.#{attribute}[#{sub_attr.inspect}]})
        res[:value] ||= "<%= fquote(#{sub_attr_ruby}) %>"
      end
    elsif attribute && type = node.klass.safe_method_type([attribute], node)
      res[:value] ||= "<%= fquote(#{node}.#{type[:method]}) %>"
    end

    if sub_attr && params[:type] == 'checkbox' && !params[:value]
      # Special case when we have a sub_attribute: default value for "tagged[foobar]" is "foobar"
      res[:value] = sub_attr
    end

  elsif node.will_be?(Column)
    res[:erb_attr] = "<%= #{node}.name %>"
    res[:name]  = "node[<%= #{node}.name %>]"
    res[:value] = "<%= fquote #{node(Node)}.prop[#{node}.name] %>"
  end
  
  if params[:id]
    res[:id] = params[:id]
  elsif base = @context[:form_prefix]
    res[:id] = "#{base}_#{attribute}"
  end
  
  if params[:type] == 'checkbox' && sub_attr_ruby
    if value = params[:value]
      res[:checked] = "<%= #{sub_attr_ruby} == #{value.inspect} ? \" checked='checked'\" : '' %>"
    else
      res[:checked] = "<%= #{sub_attr_ruby}.blank? ? '' : \" checked='checked'\" %>"
    end
  end

  params.each do |k, v|
    if [:size, :style, :class].include?(k) || k.to_s =~ /^data-/
      res[k] = params[k]
    end
  end
  
  if sub_attr
    return [res, "#{attribute}_#{sub_attr}"]
  else
    return [res, attribute]
  end
end

#make_formObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/zena/use/forms.rb', line 145

def make_form
  if !@context[:make_form] || node.list_context? || @context[:form_helper].blank?
    return super
  else
    form_helper = @context[:form_helper]
  end

  if %W{link show}.include?(method) || method == 'zazen' || (name = method[/zazen\(\s*(\w+)\s*\)/,1])
    name ||= @params[:attr] || @params[:date] || 'title'
    textarea = method =~ /zazen/
  elsif type = node.klass.safe_method_type([method])
    name = method
  end

  if name
    type ||= node.klass.safe_method_type([name])
    # do we have a property ?
    if type && (node.real_class.column_names.include?(name) || node.klass.column_names.include?(name))
      # create an input field
      out make_input(form_helper, name, type[:class], textarea)
    else
      # ignore
      out ''
    end
  else
    super
  end
end

#make_input(form_helper, name, type, textarea = false) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/zena/use/forms.rb', line 134

def make_input(form_helper, name, type, textarea = false)
  if type == Time
    code = RubyLess.translate(self, "this.#{name}")
    "<%= date_box(#{node}, 'node[#{name}]', :value => #{code}) %>"
  elsif textarea
    "<%= #{form_helper}.text_area :#{name}, :id => '#{node.dom_prefix}_#{name}' %>"
  else
    "<%= #{form_helper}.text_field :#{name}, :id => '#{node.dom_prefix}_#{name}' %>"
  end
end

#r_checkboxObject

<r:checkbox role=‘collaborator_for’ values=‘projects’ in=‘site’/>“



639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# File 'lib/zena/use/forms.rb', line 639

def r_checkbox
  nodes  = @params[:nodes]
  values = @params[:values]
  return parser_error("missing 'nodes' or 'values'") unless nodes || values

  if values
    return parser_error("missing attribute 'name'") unless name = @params[:name]
    # parse literal values
    opts = [":name => #{name.inspect}", ":list => #{values.split(',').map(&:strip).inspect}"]
    if show_values = @params[:show]
      opts << ":show => #{show_values.split(',').map(&:strip).inspect}"
    elsif show_values = @params[:tshow]
      opts << ":show => #{translate_list(show_values).inspect}"
    else
      opts << ":show => #{translate_list(values, @params[:tprefix] || name).inspect}"
    end
    meth = RubyLess.translate(self, "this.#{name}")
    opts << ":selected => #{meth}"
    if default = @params[:default]
      opts << ":default => #{default.inspect}"
    end
    
    if type = @params[:type]
      opts << ":type => #{type.inspect}"
    end
    attribute = name
    res = "<%= make_checkbox(#{node}, #{opts.join(', ')}) %>"
  else
    if name = @params[:name]
      if name =~ /(.*)_ids?\Z/
        role = $1
      else
        role = name
      end
    else
      role = @params[:role]
    end
    return parser_error("missing 'role'") unless role
    # nodes
    meth = role.singularize

    if nodes =~ /^\d+\s*($|,)/
      values = nodes.split(',').map{|v| v.to_i}
      finder = "secure(Node) { Node.all(:conditions => 'zip IN (#{values.join(',')})') }"
    else
      return unless finder = build_finder(:all, nodes, @params)
      return parser_error("invalid class (#{finder[:class]})") unless finder[:class].first <= Node
      finder = finder[:method]
    end

    attribute = @params[:attr] || 'title'
    res = "<%= make_checkbox(#{node}, :list => #{finder}, :role => #{meth.inspect}, :attr => #{attribute.inspect}) %>"
  end

  extract_label(res, attribute)
end

#r_cropObject

form << “<%= error_messages_for(#node) %>”

@blocks = blocks_bak if blocks_bak

@html_tag_done = false
@html_tag_params.merge!(id_hash)
out render_html_tag(res)


806
807
808
809
810
811
812
813
814
# File 'lib/zena/use/forms.rb', line 806

def r_crop
  return parser_error("Invalid node type #{node.klass} (should be an Image).") unless node.will_be?(Image)
  @markup.tag ||= 'div'
  node.dom_prefix = dom_name
  @markup.set_id(node.dom_id(:list => false))
  dom = node.dom_id(:erb => false, :list => false)
  out %Q{<%= render :partial => 'documents/crop', :locals => {:node => #{node(Node)}, :img_id => "img#{dom}"} %>}
  out %Q{<% js_data << %Q{new Zena.Div_editor("img#{dom}", 'posx', 'posy', 'width', 'height', \#{#{node}.width / #{node}.width(Iformat['edit']).to_f}, Element.viewportOffset('#{dom}').left, Element.viewportOffset('#{dom}').top);} %>}
end

#r_errorsObject



129
130
131
132
# File 'lib/zena/use/forms.rb', line 129

def r_errors
  # Very basic for the moment
  "<%= error_messages_for(#{node.form_name}, :object => #{node}) %>"
end

#r_input(skip_col = false) ⇒ Object



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/zena/use/forms.rb', line 544

def r_input(skip_col = false)
  html_attributes, attribute = get_input_params()
  
  erb_attr = html_attributes.delete(:erb_attr)
  # TODO: get attribute type from get_input_params

  if !skip_col && node.will_be?(Column) && !@params[:type]
    # hack to change @params
    out "<% if #{node}.ptype == :string -%>"
    out r_textarea
    out "<% elsif #{node}.ptype == :datetime -%>"
    res = "<%= date_box(#{node(Node)}, %Q{node[\#{#{node}.name}]}, :value => #{node(Node)}.prop[#{node}.name]) %>"
    out extract_label(res, erb_attr)
    out "<% else -%>"
    out r_input(true)
    out "<% end -%>"
    return
  end

  res = case @params[:type]
  when 'select' # FIXME: why is this only for classes ?
    out parser_error("please use [select] here")
    r_select
  when 'date_box', 'date'
    return parser_error("date_box without name") unless attribute
    if code = @params[:value]
      code = ::RubyLess.translate(self, code)
    elsif code = html_attributes[:value]
      if code =~ /\A<%= .*?\((.+)\)\s*%>/
        # remove <%= %>
        code = $1
      else
        code = code.inspect
      end
    else
      code = ::RubyLess.translate(self, "this.#{attribute}")
    end
    value = code # @context[:in_add] ? "''" : code
    html_params = [':size => 15']
    [:style, :class, :onclick, :size, :time, :id, :onUpdate].each do |key|
      html_params << ":#{key} => #{@params[key].inspect}" if @params[key]
    end
    if !@params[:id] and node.dom_prefix
      html_params << ":id=>\"#{node.dom_id(:erb => false)}_#{attribute}\""
    end
    "<%= date_box(#{node}, #{html_attributes[:name].inspect}, :value => #{value}, #{html_params.join(', ')}) %>"
  when 'id'
    return parser_error("select id without name") unless attribute
    name = "#{attribute}_id" unless attribute[-3..-1] == '_id'
    input_id = @context[:erb_dom_id] ? ", :input_id =>\"#{erb_dom_id}_#{attribute}\"" : ''
    # FIXME: pass object
    "<%= select_id('#{node.form_name}', #{attribute.inspect}#{input_id}) %>"
  when 'time_zone'
    out parser_error("please use [select] here")
    r_select
  when 'submit'
    @markup.tag = 'input'
    @markup.set_param(:type, @params[:type])
    @markup.set_param(:text, @params[:text]) if @params[:text]
    @markup.set_params(html_attributes)
    @markup.done = false
    wrap('')
  else
    # 'text', 'hidden', 'checkbox', ...
    return parser_error('Missing name.') unless attribute || html_attributes[:name]
    
    if @markup.tag != 'input'
      # Do not rewrite tag, use another one.
      markup = Zafu::Markup.new('input')
    else
      markup = @markup
    end
    
    markup.tag = 'input'
    markup.set_param(:type, @params[:type] || 'text')
    
    checked = html_attributes.delete(:checked)
    markup.set_dyn_params(html_attributes)
    markup.append_attribute checked if checked
    markup.done = false
    
    if markup == @markup
      wrap('')
    else
      markup.wrap('')
    end
  end

  if @params[:type] == 'checkbox'
    out "<input type='hidden' name='#{html_attributes[:name]}' value='#{@params[:blank]}'/>"
  end
  extract_label(res, attribute || erb_attr)
end

#r_newObject

Enter the context of a newly created object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/zena/use/forms.rb', line 93

def r_new
  return parser_error("missing 'klass' parameter") unless class_name = @params[:klass]
  return parser_error("invalid 'klass' parameter") unless klass = get_class(class_name)
  return parser_error("invalid 'klass' parameter (not a Node)") unless klass <= Node

  res  = []
  keys = {:klass => 'this.klass', :parent_id => "#{node(Node)}.zip"}
  @params.each do |key, value|
    next if key == :klass
    # TODO: maybe it would be safer to check with [:"key="] and change safe_property to
    # authorize both ?
    next unless type = klass.safe_method_type([key.to_s])
    # Store how to access current value to show hidden field in form.
    keys[key] = "this.#{type[:method]}"
    code = RubyLess.translate(self, value)
    if code.klass == type[:class]
      res << ":#{key} => #{code}"
    else
      out parser_error("invalid type for '#{key}' (found #{code.klass}, expected #{type[:class]})")
    end
  end

  if res == []
    method = "new_instance(#{class_name.inspect})"
  else
    method = "new_instance(#{class_name.inspect}, #{res.join(', ')})"
  end

  expand_with_finder(
    :method     => method,
    :class      => klass,
    :nil        => true,
    :new_keys   => keys
  )
end

#r_radioObject



696
697
698
699
# File 'lib/zena/use/forms.rb', line 696

def r_radio
  @params[:type] = 'radio'
  r_checkbox
end

#r_selectObject

<r:select name=‘klass’ root_class=‘…’/> <r:select name=‘parent_id’ nodes=‘projects in site’/> <r:select name=‘parent_id’ values=‘a,b,c’/> TODO: optimization (avoid loading full AR to only use [id, name])



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/zena/use/forms.rb', line 461

def r_select
  html_attributes, attribute = get_input_params()
  erb_attr = html_attributes.delete(:erb_attr)
  # TEMPORARY HACK UNTIL WE FIX get_input_params to return a single hash with
  # {:html => { prepared html attributes }, :raw => {:value => '..', :name => '..', :param => '..'}}
  if param = @params[:param]
    selected  = "param_value(#{param.inspect}).to_s"
    attribute = param
  else
    return parser_error("missing name") unless attribute
    # {:value=>"<%= fquote((@node.prop['settings'] ? @node.prop['settings'][\"foo\"] : nil)) %>", :name=>"node[settings][foo]"}
    # "settings_foo"
    
    
    if selected = @params[:value] || @params[:selected]
      selected = ::RubyLess.translate(self, selected)
      unless selected.klass <= String
        selected = "#{selected}.to_s"
      end
    elsif @context[:in_filter]
      selected = "param_value(#{attribute.inspect}).to_s"
    elsif %w{parent_id}.include?(attribute)
      selected = "#{node}.parent_zip.to_s"
    elsif attribute == 'copy_id'
      selected = 'nil'
    elsif attribute =~ /^(.*)_id$/
      # relation
      selected = "#{node}.rel[#{$1.inspect}].other_zip.to_s"
    elsif selected = html_attributes[:value]
      if selected =~ /\A<%= .*?\((.+)\)\s*%>/
        # remove <%= %>
        selected = "#{$1}.to_s"
      else
        selected = selected.inspect
      end
    else
      # FIXME: This would not work for a [asdf][asdf] hash property type...
      selected = ::RubyLess.translate(self, "this.#{attribute}")
    end
  end
  
  if @context[:in_filter] || @params[:param]
    html_attributes[:name] = attribute
  end
  html_attributes.delete(:value)
  select_tag = Zafu::Markup.new('select', html_attributes)

  res = if klass = @params[:root_class]
    class_opts = ''
    class_opts << ", :without => #{@params[:without].inspect}" if @params[:without]
    tprefix = @params[:tprefix] || @params[:name] || @params[:param]
    # do not use 'selected' if the node is not new
    options_list = Node.classes_for_form(:class => klass, :without => @params[:without], :class_attr => @params[:attr] || 'name')

    if !tprefix.blank? && tprefix != 'false'
      options_list.map! do |e|
        if e[0] =~ /^([^a-zA-Z]*)(.*)$/
          name = "#{tprefix}_#{$2}"
          t = trans(name)
          t = $2 if t == name
          [$1 + t, e[1]]
        else
          e
        end
      end
    end
    select_tag.wrap "<%= options_for_select(#{options_list.inspect}, #{selected}) %>"
  elsif @params[:type] == 'time_zone'
    # <r:select name='d_tz' type='time_zone'/>
    select_tag.wrap "<%= options_for_select(TZInfo::Timezone.all_identifiers, #{selected}) %>"
  elsif @params[:type] == 'profile'
    # <r:select name='auth[model]' type='model'/>
    select_tag.wrap "<%= options_for_select(profile_users.map{|u| u.login}.sort, #{selected}) %>"
  elsif options_list = get_options_for_select
    select_tag.wrap "<%= options_for_select(#{options_list}, #{selected}) %>"
  else
    parser_error("missing 'nodes', 'root_class' or 'values'")
  end

  extract_label(res, attribute || erb_attr)
end

#r_textareaObject



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/zena/use/forms.rb', line 434

def r_textarea
  html_attributes, attribute = get_input_params()
  erb_attr = html_attributes.delete(:erb_attr)
  value = html_attributes.delete(:value)

  return parser_error('Missing name.') unless attribute || html_attributes[:name]

  @markup.tag = 'textarea'
  @markup.set_dyn_params(html_attributes)

  if @blocks == [] || @blocks == ['']
    if @context[:in_add]
      value = ''
    end
  else
    value = expand_with
  end

  res = @markup.wrap(value)

  extract_label(res, attribute || erb_attr)
end

#r_upload_fieldObject



430
431
432
# File 'lib/zena/use/forms.rb', line 430

def r_upload_field
  "<%= upload_field(:uuid => #{get_context_var('upload', 'uuid').inspect}, :dom => #{node.dom_prefix.inspect}) %>"
end