Class: FormKeeper::Respondent

Inherits:
Object
  • Object
show all
Defined in:
lib/formkeeper.rb

Instance Method Summary collapse

Constructor Details

#initializeRespondent

Returns a new instance of Respondent.



882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
# File 'lib/formkeeper.rb', line 882

def initialize
  replace_method = Proc.new { |elem, param| replase_value(elem, param) }
  check_method   = Proc.new { |elem, param| check_if_selected(elem, param) }
  @input_elem_fill_methods = {} 
  @input_elem_fill_methods[:text] = replace_method
  @input_elem_fill_methods[:password] = replace_method
  @input_elem_fill_methods[:hidden] = replace_method
  @input_elem_fill_methods[:search] = replace_method
  @input_elem_fill_methods[:number] = replace_method
  @input_elem_fill_methods[:range] = replace_method
  @input_elem_fill_methods[:tel] = replace_method
  @input_elem_fill_methods[:url] = replace_method
  @input_elem_fill_methods[:email] = replace_method
  @input_elem_fill_methods[:time] = replace_method
  @input_elem_fill_methods[:date] = replace_method
  @input_elem_fill_methods[:week] = replace_method
  @input_elem_fill_methods[:color] = replace_method
  @input_elem_fill_methods[:datetime] = replace_method
  @input_elem_fill_methods[:"datetime-local"] = replace_method
  @input_elem_fill_methods[:radio] = check_method
  @input_elem_fill_methods[:checkbox] = check_method
end

Instance Method Details

#fill_up(str, params) ⇒ Object



905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
# File 'lib/formkeeper.rb', line 905

def fill_up(str, params)
  doc = Hpricot(str)
  (doc/"input").each do |elem|
    name = elem[:name]
    next if name.nil?
    name = name.sub(/\[\]$/, '');
    next unless params.has_key?(name.to_s)
    type = elem[:type]
    next if type.nil?
    next unless @input_elem_fill_methods.has_key?(type.to_sym)
    @input_elem_fill_methods[type.to_sym].call(elem, params[name.to_s])
  end
  (doc/"select").each do |select_elem|
    name = select_elem[:name]
    next if name.nil?
    name = name.sub(/\[\]$/, '');
    next unless params.has_key?(name.to_s)
    param = params[name.to_s]
    multiple = select_elem.has_attribute?(:multiple)
    can_select_more = true
    (select_elem/"option").each do |option_elem|
      value = option_elem[:value]
      next if value.nil? or value.empty?
      case param
      when Array
        if can_select_more and param.include?(value)
          option_elem[:selected] = "selected"
          can_select_more = false unless multiple
        else
          option_elem.remove_attribute(:selected)
        end
      when String
        if can_select_more and param == value
          option_elem[:selected] = "selected"
          can_select_more = false unless multiple
        else
          option_elem.remove_attribute(:selected)
        end
      else 
        if can_select_more and param.to_s == value
          option_elem[:selected] = "selected"
          can_select_more = false unless multiple
        else
          option_elem.remove_attribute(:selected)
        end
      end
    end
  end
  (doc/"textarea").each do |elem|
    name = elem[:name]
    next if name.nil?
    next unless params.has_key?(name.to_s)
    param = params[name.to_s]
    if param.kind_of?(Array)
      elem.innerHTML = Rack::Utils::escape_html(param[0])
    else
      elem.innerHTML = Rack::Utils::escape_html(param)
    end
  end
  doc.to_html
end