formtastic_jquery_ui 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/formtastic_jquery_ui/autocomplete.rb +21 -15
- metadata +4 -4
| @@ -1,26 +1,29 @@ | |
| 1 1 | 
             
            module FormtasticJQueryUI
         | 
| 2 2 | 
             
              module Autocomplete
         | 
| 3 | 
            -
             | 
| 3 | 
            +
             | 
| 4 4 | 
             
                def autocomplete_input(method, options)
         | 
| 5 5 | 
             
                  html_options = options.delete(:input_html) || {}
         | 
| 6 6 | 
             
                  options = set_include_blank(options)
         | 
| 7 7 | 
             
                  html_options[:multiple] = html_options[:multiple] || options.delete(:multiple)
         | 
| 8 8 | 
             
                  html_options.delete(:multiple) if html_options[:multiple].nil?
         | 
| 9 | 
            -
                  
         | 
| 10 | 
            -
                   | 
| 9 | 
            +
                  autocomplete_options = options.delete(:autocomplete) || {}
         | 
| 10 | 
            +
                  autocomplete_option_min_length = autocomplete_options[:min_length] || 2
         | 
| 11 | 
            +
                  autocomplete_option_min_length_js =  "minLength: #{autocomplete_option_min_length},"
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                  html = ""
         | 
| 11 14 | 
             
                  reflection = self.reflection_for(method)
         | 
| 12 15 | 
             
                  input_name = "autocomplete_for_#{sanitized_object_name}_#{method}"
         | 
| 13 16 | 
             
                  param_name = options[:param_name] || 'term'
         | 
| 14 17 | 
             
                  url = options[:url] || ''
         | 
| 15 | 
            -
             | 
| 18 | 
            +
             | 
| 16 19 | 
             
                  if reflection && [:has_many, :has_and_belongs_to_many].include?(reflection.macro)
         | 
| 17 20 | 
             
                    selections_name = generate_html_id(method, 'selections')
         | 
| 18 21 | 
             
                    selection_name = "#{sanitized_object_name}[#{method.to_s.singularize}_ids][]"
         | 
| 19 | 
            -
                    html << template.content_tag(:ul, object.send(method).map { |i| template.content_tag(:li, "<input type=\"hidden\" name=\"#{selection_name}\" value=\"#{i.id}\" />#{i.to_label} <a href=\"#\" onclick=\"$(this).closest('li').remove();\">Remove</a>") }.join | 
| 22 | 
            +
                    html << template.content_tag(:ul, object.send(method).map { |i| template.content_tag(:li, "<input type=\"hidden\" name=\"#{selection_name}\" value=\"#{i.id}\" />#{i.to_label} <a href=\"#\" onclick=\"$(this).closest('li').remove(); return false;\">Remove</a>".html_safe) }.join.html_safe, :id => selections_name)
         | 
| 20 23 | 
             
                    html << template.tag(:input, :type => 'hidden', :name => selection_name, :value => '')
         | 
| 21 24 | 
             
                    html << template.text_field_tag(input_name)
         | 
| 22 | 
            -
                     | 
| 23 | 
            -
             | 
| 25 | 
            +
                    autocomplete_js = <<-EOT
         | 
| 26 | 
            +
              <script type="text/javascript">
         | 
| 24 27 | 
             
              $('##{input_name}').autocomplete({
         | 
| 25 28 | 
             
                source: function(request, response) {
         | 
| 26 29 | 
             
                  $.ajax({
         | 
| @@ -34,16 +37,18 @@ module FormtasticJQueryUI | |
| 34 37 | 
             
                    }
         | 
| 35 38 | 
             
                  });
         | 
| 36 39 | 
             
                },
         | 
| 40 | 
            +
                #{autocomplete_option_min_length_js}
         | 
| 37 41 | 
             
                select: function(event, selection) {
         | 
| 38 42 | 
             
                  if($('##{selections_name} input[value=' + selection.item.value + ']').length == 0)
         | 
| 39 | 
            -
                    $('##{selections_name}').append('<li><input type="hidden" name="#{selection_name}" value="' + selection.item.value + '" />' + selection.item.label + ' <a href="#" onclick="$(this).closest(\\'li\\').remove();">Remove</a></li>');
         | 
| 43 | 
            +
                    $('##{selections_name}').append('<li><input type="hidden" name="#{selection_name}" value="' + selection.item.value + '" />' + selection.item.label + ' <a href="#" onclick="$(this).closest(\\'li\\').remove(); return false;">Remove</a></li>');
         | 
| 40 44 | 
             
                  $('##{input_name}').val('');
         | 
| 41 45 | 
             
                  return false;
         | 
| 42 46 | 
             
                },
         | 
| 43 47 | 
             
                focus: function(event, ui) { return false; }
         | 
| 44 48 | 
             
              });
         | 
| 49 | 
            +
              </script>
         | 
| 45 50 | 
             
            EOT
         | 
| 46 | 
            -
                     | 
| 51 | 
            +
                    html << autocomplete_js.html_safe
         | 
| 47 52 | 
             
                  elsif (reflection && [:belongs_to].include?(reflection.macro)) || reflection.nil?
         | 
| 48 53 | 
             
                    hidden_field_name = reflection.try(:primary_key_name) || "#{method}_id"
         | 
| 49 54 | 
             
                    html << self.hidden_field(hidden_field_name)
         | 
| @@ -63,23 +68,24 @@ EOT | |
| 63 68 | 
             
                    }
         | 
| 64 69 | 
             
                  });
         | 
| 65 70 | 
             
                },
         | 
| 71 | 
            +
                #{autocomplete_option_min_length_js}
         | 
| 66 72 | 
             
                select: function(event, selection) {
         | 
| 67 73 | 
             
                  $('##{sanitized_object_name}_#{hidden_field_name}').val(selection.item.value);
         | 
| 68 74 | 
             
                  $('##{input_name}').val(selection.item.label);
         | 
| 69 75 | 
             
                  return false;
         | 
| 70 76 | 
             
                },
         | 
| 71 77 | 
             
                focus: function(event, ui) { return false; }
         | 
| 72 | 
            -
              }); | 
| 78 | 
            +
              });
         | 
| 73 79 | 
             
              $('##{input_name}').blur(function(event){
         | 
| 74 80 | 
             
                if($(this).val() == '')
         | 
| 75 81 | 
             
                  $('##{sanitized_object_name}_#{hidden_field_name}').val('');
         | 
| 76 | 
            -
              }) | 
| 82 | 
            +
              })
         | 
| 77 83 | 
             
            EOT
         | 
| 78 84 | 
             
                    end
         | 
| 79 85 | 
             
                  end
         | 
| 80 | 
            -
             | 
| 81 | 
            -
                  template.content_tag(:label, method.to_s.humanize, :for => input_name) << html
         | 
| 86 | 
            +
                  template.content_tag(:label, localized_string(method, options[:label].is_a?(::String) ? options[:label] : nil, :label) || humanized_attribute_name(method), :for => input_name) << Formtastic::Util.html_safe(html)
         | 
| 82 87 | 
             
                end
         | 
| 83 | 
            -
             | 
| 88 | 
            +
             | 
| 84 89 | 
             
              end
         | 
| 85 | 
            -
            end
         | 
| 90 | 
            +
            end
         | 
| 91 | 
            +
             | 
    
        metadata
    CHANGED
    
    | @@ -1,13 +1,13 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: formtastic_jquery_ui
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              hash:  | 
| 4 | 
            +
              hash: 23
         | 
| 5 5 | 
             
              prerelease: false
         | 
| 6 6 | 
             
              segments: 
         | 
| 7 7 | 
             
              - 0
         | 
| 8 8 | 
             
              - 0
         | 
| 9 | 
            -
              -  | 
| 10 | 
            -
              version: 0.0. | 
| 9 | 
            +
              - 4
         | 
| 10 | 
            +
              version: 0.0.4
         | 
| 11 11 | 
             
            platform: ruby
         | 
| 12 12 | 
             
            authors: 
         | 
| 13 13 | 
             
            - Paul Smith
         | 
| @@ -15,7 +15,7 @@ autorequire: | |
| 15 15 | 
             
            bindir: bin
         | 
| 16 16 | 
             
            cert_chain: []
         | 
| 17 17 |  | 
| 18 | 
            -
            date:  | 
| 18 | 
            +
            date: 2011-02-14 00:00:00 +00:00
         | 
| 19 19 | 
             
            default_executable: 
         | 
| 20 20 | 
             
            dependencies: 
         | 
| 21 21 | 
             
            - !ruby/object:Gem::Dependency 
         |