Generate Plugin with command: ruby script/generate plugin hello_world
It will create file system as bellow:
- lib
- hello_world.rb
- tasks
- hello_world_tasks.rake
- test
- hello_world_test.rb
- init.rb
- install.rb
- uninstall.rb
- README
- Rakefile
- MIT-LICENSE
init.rb will be executed every time when your application runs. Generally hook code is to be included here like you want to make all methods of your plugin available in your app’s models, controllers, views and helpers
Example:
#All methods in module HelloWorld will be available in all #model’s object
ActiveRecord::Base.class_eval do
include HelloWorld
end
#All methods in module HelloWorld will be available in all controllers
ActionController::Base.class_eval do
include HelloWorld
end
#All methods in module HelloWorld will be available in all views and all helpers
ActionView::Base.class_eval do
include HelloWorld
end
lib/ hello_world.rb :
Example:
# All methods in this library will be available in all models, controllers, views and helpers if you write code as init.rb above
module HelloWorld
def say_hello
return "Hello World"
end
def hello_text
return “this is text”
end
end
Now you need to do unit test your plugin’s methods. To do so follow:
test/hello_world_test.rb:
Example:
require 'test/unit'
require File.join(File.dirname(__FILE__),'../lib/hello_world.rb')
class HelloWorldTest < Test::Unit::TestCase
include HelloWorld # includes your library methods to test
def test_this_plugin
hello_world = say_hello
assert_equal hello_world, "Hello World"
assert_not_nil hello_world
end
def test_another
assert_equal true,true
end
end
To run these test cases, go to your plugin’s directory with command prompt and run like: ruby test/hello_world_test.rb
You are done actually with plugin!
Install.rb: It will be executed only once when the plugin is being installed.
Uninstall.rb: This will be executed when you do uninstall your plugin
README: In this file, you should write easy documentation of your plugin with examples. Also you may highlight yourself here.
Hi dude,
ReplyDeletei read your blog,This is a wonderful blog.I was able to get the
information that I had been looking for. Thanks once again.
Rajat
Ruby on Rails
Thank you dude :D
ReplyDelete