Rich-Support

A small gem making your own gem Rails 2 and 3 compliant and providing the String class a few goodies

Introduction

Rich-Support is a small module of E9s (http://github.com/archan937/e9s) making your own gem Rails 2 and 3 compliant. It also adds a small amount of handy methods to the String class.

Attention: This gem does NOT add Rails 3 functionality to a Rails 2 application, but provides an unified interface for certain differences in implementation between Rails 2 and 3. A few examples:

  • it provides a unified interface for the after_initialize hook of the Rails application
  • it provides the ActiveSupport::Dependencies.autoload_paths to be called in both Rails 2 and 3
  • it defines String.html_safe if it hasn’t been defined already (it only returns the String and does NOT HTML escapes the String)

A real world example of a Rich-Support implementation is the Rich-CMS gem of which Rich-Support is extracted from.

Installation

Add Rich-Support as gem dependency in your gemspec file:


  s.add_dependency "rich_support"

Require Rich-Support in your gem (e.g. in libs/rich_cms.rb for the gem Rich-CMS)


  require "rich_support"

Your gem is now Rails 2 and 3 compliant.

Using the Rails configuration after_initialize hook regardless of the major Rails version

As calling the after_initialize differs in the major Rails versions, but you can just call Rich::Support.after_initialize instead as it will handle things for you:


  Rich::Support.after_initialize do
    register_assets
    copy_images
    require_models
  end

Registering your gem controllers, views and routes in Rails 2

As only Rails 3 recognizes your controllers, views and routes automatically, you will have to append them in Rails 2 yourself. Fortunately with Rich::Support, you will only have to append your gem path:


  Rich::Support.append_gem_path File.expand_path("../..", __FILE__)

Note: The source file in this example is located in the lib directory of the gem itself

Calling append_gem_path registers the following in Rails 2:

  • controllers – Which is assumed to be located in app/controllers
  • views – Which is assumed to be located in app/views
  • routes – Which is assumed to be located in config/routes.rb

Some String class goodies

Rich-Support adds a small amount of methods to the String class:

  • String.upcase_first – Upcases the first character of a String and leaves the remainder alone unlike String.capitalize which downcases it
  • String.upcase_first! – Only returns the upcased String when it has changed otherwise it returns a nil value
  • String.copy_case – Copies the letter casing of a passed String (e.g. "paul".copy_case("Engel") returns "Paul" and "CoDe".copy_case("HERO") returns "CODE")
  • String.copy_case! – Only returns the case copied String when it has changed otherwise it returns a nil value
  • String.singularize! and String.pluralize! – Which (as you expected) only returns a String when it has changed otherwise a nil value
  • String.singular? and String.plural? – Assuming that the String is a verb, it returns whether it is singular / plural or not

Developing Rich-Support yourself

1. Create your own Rich-Support fork https://github.com/archan937/rich_support/fork 2. Clone your Rich-Support fork repository


  git clone [email protected]:<your_username>/rich_support.git && cd rich_support

3. Setup your environment in order to use the GemSuit tests provided within Rich-Support


  gem install gem_suit && suit fit -v

4. Run unit tests in both Rails 2 and 3


  suit test unit -v

5. Start either of the dummy Rails applications for development purposes (Rails 3 at default)


  suit s

To run the Rails 2 application:


  suit s -r2

6. Get on programming and send your pull request! ;)

Note: For more information about GemSuit, please visit its Github page.

Contact me

For support, remarks and requests please mail me at [email protected].

Enrichments

The all-in-one gem at – http://codehero.es/rails_gems_plugins/e9shttp://github.com/archan937/e9s

E9s modules

License

Copyright © 2011 Paul Engel, released under the MIT license

http://holder.nlhttp://codehero.eshttp://gettopup.comhttp://twitter.com/archan937[email protected]

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.