From 12122b7ecebaaaca01b315854ea29447b91a2366 Mon Sep 17 00:00:00 2001 From: Jared Pace Date: Thu, 19 Aug 2010 22:35:34 -0400 Subject: [PATCH] Make nested watcher form more user friendly --- app/models/app.rb | 3 ++- app/models/watcher.rb | 17 +++++++++++++++++ app/views/apps/_fields.html.haml | 14 ++++++++------ public/javascripts/form.js | 15 ++++++++++++++- public/stylesheets/application.css | 12 ++++++++++++ 5 files changed, 53 insertions(+), 8 deletions(-) diff --git a/app/models/app.rb b/app/models/app.rb index 9309ac8..42c317a 100644 --- a/app/models/app.rb +++ b/app/models/app.rb @@ -16,9 +16,10 @@ class App validates_presence_of :name, :api_key validates_uniqueness_of :name, :allow_blank => true validates_uniqueness_of :api_key, :allow_blank => true + validates_associated :watchers accepts_nested_attributes_for :watchers, :allow_destroy => true, - :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } + :reject_if => proc { |attrs| attrs[:user_id].blank? && attrs[:email].blank? } # Mongoid Bug: find(id) on association proxies returns an Enumerator def self.find_by_id!(app_id) diff --git a/app/models/watcher.rb b/app/models/watcher.rb index 84e25ef..dfadd04 100644 --- a/app/models/watcher.rb +++ b/app/models/watcher.rb @@ -9,6 +9,14 @@ class Watcher validate :ensure_user_or_email + before_validation :clear_unused_watcher_type + + attr_accessor :watcher_type + + def watcher_type + @watcher_type ||= email.present? ? 'email' : 'user' + end + def label user ? user.name : email end @@ -22,5 +30,14 @@ class Watcher def ensure_user_or_email errors.add(:base, "You must specify either a user or an email address") unless user.present? || email.present? end + + def clear_unused_watcher_type + case watcher_type + when 'user' + self.email = nil + when 'email' + self.user = self.user_id = nil + end + end end diff --git a/app/views/apps/_fields.html.haml b/app/views/apps/_fields.html.haml index bef2ab6..68752e4 100644 --- a/app/views/apps/_fields.html.haml +++ b/app/views/apps/_fields.html.haml @@ -11,11 +11,13 @@ %fieldset.nested-wrapper %legend Watchers - f.fields_for :watchers do |w| - %div.nested - %div - = w.label :user + %div.watcher.nested + %div.choose + = w.radio_button :watcher_type, :user + = label_tag :watcher_type_user, 'User' + = w.radio_button :watcher_type, :email + = label_tag :watcher_type_email, 'Email Address' + %div.user{:class => w.object.email.blank? ? 'choosen' : nil} = w.select :user_id, User.all.map{|u| [u.name,u.id.to_s]}, :include_blank => '-- Select a User --' - %strong.option - OR - - %div - = w.label :email + %div.email{:class => w.object.email.present? ? 'choosen' : nil} = w.text_field :email \ No newline at end of file diff --git a/public/javascripts/form.js b/public/javascripts/form.js index c63d4b2..1641849 100644 --- a/public/javascripts/form.js +++ b/public/javascripts/form.js @@ -1,5 +1,8 @@ $(function(){ activateNestedForms(); + + if($('div.watcher.nested').length) + activateWatcherTypeSelector(); }); function activateNestedForms() { @@ -33,7 +36,8 @@ function appendNestedItem() { timestamp = timestamp.valueOf(); input.attr('id', input.attr('id').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2')); input.attr('name', input.attr('name').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2')); - input.val(''); + if(input.attr('type') != 'radio') + input.val(''); }); addLink.before(nestedItem); } @@ -49,4 +53,13 @@ function removeNestedItem() { $("input[name='"+idFieldName+"']").after(destroyFlag); } nestedItem.hide(); +} + +function activateWatcherTypeSelector() { + $('div.watcher input[name*=watcher_type]').live('click', function(){ + var choosen = $(this).val(); + var wrapper = $(this).closest('.nested'); + wrapper.find('div.choosen').removeClass('choosen'); + wrapper.find('div.'+choosen).addClass('choosen'); + }); } \ No newline at end of file diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index 87c4453..877dcc0 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -486,6 +486,18 @@ a.button.active { margin-right: 14px; } +/* Watchers Form */ +div.nested.watcher .user, div.nested.watcher .email { + display: none; +} +div.nested.watcher .choosen { + display: block; +} +div.nested.watcher .choose { + margin-bottom: 0.5em; +} + + /* Apps Table */ table.apps tbody tr:hover td ,table.errs tbody tr:hover td { background-color: #F2F2F2;} -- libgit2 0.21.2