From 071e0ac1a01ef8d70334153f21d7228d79414454 Mon Sep 17 00:00:00 2001 From: Yomi Colledge Date: Mon, 21 Jun 2010 13:39:41 +0100 Subject: [PATCH] Added inflection and refactored the hover_dialog file to use inflection instead of our hack. --- app/views/layouts/no_sidebar.html.erb | 1 + public/javascripts/hover_dialog.js | 13 +- public/javascripts/inflection.js | 511 ++++++++++++++++++++++++++ 3 files changed, 515 insertions(+), 10 deletions(-) create mode 100644 public/javascripts/inflection.js diff --git a/app/views/layouts/no_sidebar.html.erb b/app/views/layouts/no_sidebar.html.erb index 5bd500ec..d1f9af92 100644 --- a/app/views/layouts/no_sidebar.html.erb +++ b/app/views/layouts/no_sidebar.html.erb @@ -20,6 +20,7 @@ <%= javascript_include_tag "jquery.formtastic.tooltips" %> <%= javascript_include_tag "application" %> <%= javascript_include_tag "jquery-validate" %> + <%= javascript_include_tag "inflection" %> <%= javascript_include_tag "form" %> <%= yield :js_head %> <%= javascript_include_tag "http://cdn.jquerytools.org/1.2.0/all/jquery.tools.min.js" %> diff --git a/public/javascripts/hover_dialog.js b/public/javascripts/hover_dialog.js index 6f908c83..f47a964a 100644 --- a/public/javascripts/hover_dialog.js +++ b/public/javascripts/hover_dialog.js @@ -8,14 +8,8 @@ $(document).ready(function() { // We need singular & plural resource names to take advantage of hover dialog var formIdArray = $formID.split('_'); var $resourceSingular = formIdArray[1]; - var $resourcePlural = ''; + var $resourcePlural = formIdArray[1].pluralize(); - // Should really use some kind of pluralisation technique - if ($resourceSingular == 'story') { - $resourcePlural = 'stories' - } else { - $resourcePlural = $resourceSingular + 's'; - }; // Selector for our tag input var $tagInput = $('input#'+ $resourceSingular + '_tag_list'); var $tagInputWrapper = $('li#'+ $resourceSingular + '_tag_list_input'); @@ -62,9 +56,8 @@ $(document).ready(function() { $tagInput.attr('value',$tagInput.attr('value') + $(this).html() + ', '); $(this).fadeOut(); return false; - })) - .append(' ') - .fadeIn(); + }) + ).append(' ').fadeIn(); }; }; }); \ No newline at end of file diff --git a/public/javascripts/inflection.js b/public/javascripts/inflection.js new file mode 100644 index 00000000..8cd326bb --- /dev/null +++ b/public/javascripts/inflection.js @@ -0,0 +1,511 @@ +/* +Copyright (c) 2007 Ryan Schuft (ryan.schuft@gmail.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +/* + This code is based in part on the work done in Ruby to support + infection as part of Ruby on Rails in the ActiveSupport's Inflector + and Inflections classes. It was initally ported to Javascript by + Ryan Schuft (ryan.schuft@gmail.com). + + The code is available at http://code.google.com/p/inflection-js/ + + The basic usage is: + 1. Include this script on your web page. + 2. Call functions on any String object in Javascript + + Currently implemented functions: + + String.pluralize(plural) == String + renders a singular English language noun into its plural form + normal results can be overridden by passing in an alternative + + String.singularize(singular) == String + renders a plural English language noun into its singular form + normal results can be overridden by passing in an alterative + + String.camelize(lowFirstLetter) == String + renders a lower case underscored word into camel case + the first letter of the result will be upper case unless you pass true + also translates "/" into "::" (underscore does the opposite) + + String.underscore() == String + renders a camel cased word into words seperated by underscores + also translates "::" back into "/" (camelize does the opposite) + + String.humanize(lowFirstLetter) == String + renders a lower case and underscored word into human readable form + defaults to making the first letter capitalized unless you pass true + + String.capitalize() == String + renders all characters to lower case and then makes the first upper + + String.dasherize() == String + renders all underbars and spaces as dashes + + String.titleize() == String + renders words into title casing (as for book titles) + + String.demodulize() == String + renders class names that are prepended by modules into just the class + + String.tableize() == String + renders camel cased singular words into their underscored plural form + + String.classify() == String + renders an underscored plural word into its camel cased singular form + + String.foreign_key(dropIdUbar) == String + renders a class name (camel cased singular noun) into a foreign key + defaults to seperating the class from the id with an underbar unless + you pass true + + String.ordinalize() == String + renders all numbers found in the string into their sequence like "22nd" +*/ + +/* + This function adds plurilization support to every String object + Signature: + String.pluralize(plural) == String + Arguments: + plural - String (optional) - overrides normal output with said String + Returns: + String - singular English language nouns are returned in plural form + Examples: + "person".pluralize() == "people" + "octopus".pluralize() == "octopi" + "Hat".pluralize() == "Hats" + "person".pluralize("guys") == "guys" +*/ +if(!String.prototype.pluralize)String.prototype.pluralize=function(plural) +{ + var str=this; + if(plural)str=plural; + else + { + var uncountable=false; + for(var x=0;!uncountable&&x