Commit 6bfbec4f098b569f8da5a670e47e23f27f41e9d2

Authored by Jared Pace
1 parent 37694d23
Exists in master and in 1 other branch production

Make labels for nested radio buttons clickable

app/helpers/form_helper.rb
... ... @@ -11,4 +11,8 @@ module FormHelper
11 11 end
12 12 end
13 13  
  14 + def label_for_attr(builder, field)
  15 + (builder.object_name + field).gsub(/[\[\]]/,'_').squeeze('_')
  16 + end
  17 +
14 18 end
15 19 \ No newline at end of file
... ...
app/views/apps/_fields.html.haml
... ... @@ -14,9 +14,9 @@
14 14 %div.watcher.nested
15 15 %div.choose
16 16 = w.radio_button :watcher_type, :user
17   - = label_tag :watcher_type_user, 'User'
  17 + = label_tag :watcher_type_user, 'User', :for => label_for_attr(w, 'watcher_type_user')
18 18 = w.radio_button :watcher_type, :email
19   - = label_tag :watcher_type_email, 'Email Address'
  19 + = label_tag :watcher_type_email, 'Email Address', :for => label_for_attr(w, 'watcher_type_email')
20 20 %div.user{:class => w.object.email.blank? ? 'choosen' : nil}
21 21 = w.select :user_id, User.all.map{|u| [u.name,u.id.to_s]}, :include_blank => '-- Select a User --'
22 22 %div.email{:class => w.object.email.present? ? 'choosen' : nil}
... ...
public/javascripts/form.js
... ... @@ -30,15 +30,20 @@ function makeNestedItemsDestroyable(wrapper) {
30 30 function appendNestedItem() {
31 31 var addLink = $(this);
32 32 var nestedItem = addLink.parent().find('.nested').first().clone().show();
  33 + var timestamp = new Date();
  34 + timestamp = timestamp.valueOf();
  35 +
33 36 nestedItem.find('input, select').each(function(){
34 37 var input = $(this);
35   - var timestamp = new Date();
36   - timestamp = timestamp.valueOf();
37 38 input.attr('id', input.attr('id').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2'));
38 39 input.attr('name', input.attr('name').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2'));
39 40 if(input.attr('type') != 'radio')
40 41 input.val('');
41 42 });
  43 + nestedItem.find('label').each(function(){
  44 + var label = $(this);
  45 + label.attr('for', label.attr('for').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2'));
  46 + });
42 47 addLink.before(nestedItem);
43 48 }
44 49  
... ...