A small gem making your own gem Rails 2 and 3 compliant and providing the String class a few goodies
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_initializehook of the Rails application
- it provides the
ActiveSupport::Dependencies.autoload_pathsto be called in both Rails 2 and 3
- it defines
String.html_safeif 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.
Add Rich-Support as gem dependency in your
Require Rich-Support in your gem (e.g. in
libs/rich_cms.rb for the gem Rich-CMS)
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
append_gem_path registers the following in Rails 2:
controllers– Which is assumed to be located in
views– Which is assumed to be located in
routes– Which is assumed to be located in
Some String class goodies
Rich-Support adds a small amount of methods to the
String.upcase_first– Upcases the first character of a String and leaves the remainder alone unlike
String.capitalizewhich downcases it
String.upcase_first!– Only returns the upcased String when it has changed otherwise it returns a
String.copy_case– Copies the letter casing of a passed String (e.g.
String.copy_case!– Only returns the case copied String when it has changed otherwise it returns a
String.pluralize!– Which (as you expected) only returns a String when it has changed otherwise a
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 firstname.lastname@example.org:<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)
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.
For support, remarks and requests please mail me at email@example.com.
Copyright © 2011 Paul Engel, released under the MIT license
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.