<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Rails Plugins</title>
	<atom:link href="http://railsplugins.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://railsplugins.wordpress.com</link>
	<description>Hansraj</description>
	<lastBuildDate>Thu, 26 Nov 2009 15:34:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='railsplugins.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/0ae8cda939085515729a80548d745770?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Rails Plugins</title>
		<link>http://railsplugins.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://railsplugins.wordpress.com/osd.xml" title="Rails Plugins" />
	<atom:link rel='hub' href='http://railsplugins.wordpress.com/?pushpress=hub'/>
		<item>
		<title>How to write a plugin ?</title>
		<link>http://railsplugins.wordpress.com/2009/09/24/how-to-write-a-plugin/</link>
		<comments>http://railsplugins.wordpress.com/2009/09/24/how-to-write-a-plugin/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 07:19:09 +0000</pubDate>
		<dc:creator>hansraj</dc:creator>
				<category><![CDATA[Plugins]]></category>
		<category><![CDATA[controllers]]></category>
		<category><![CDATA[helpers]]></category>
		<category><![CDATA[init.rb]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://railsplugins.wordpress.com/2009/09/24/how-to-write-a-plugin/</guid>
		<description><![CDATA[Hi, Here are some of the problems i faced and the solutions to those .. Firstly, How  to write a plugin ? Its quite easy. There are many tutorials over the web tha will give you a nice guidelines on it. I shall just put it up for you. Creating the plugin First, generate the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=railsplugins.wordpress.com&amp;blog=7368873&amp;post=3&amp;subd=railsplugins&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hi,</p>
<p>Here are some of the problems i faced and the solutions to those .. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Firstly,</p>
<h3><strong>How  to write a plugin ?</strong></h3>
<p>Its quite easy. There are many tutorials over the web tha will give you a nice guidelines on it.</p>
<p>I shall just put it up for you.</p>
<h4>Creating the plugin</h4>
<p>First, generate the basic code for a plugin:</p>
<p><span style="color:#ff9900;">./script/generate plugin &lt;YOUR_PLUGIN_NAME&gt;</span></p>
<p style="text-align:left;">This will create a new directory in vendor/plugins containing all the basic files you need.</p>
<p>The vendor/plugins/acts_as_exportable/lib/&lt;YOUR_PLUGIN_NAME&gt;.rb contains a module that’s named after our plugin:</p>
<div>
<div>
<pre style="font-family:monospace;"><span style="color:#9966cc;font-weight:bold;">module</span> YOUR_PLUGIN_NAME
<span style="color:#9966cc;font-weight:bold;">end
</span>
Next, we add a module named ‘ClassMethods’.
These class methods will be added to ActionController::Base when the plugin is loaded.
For more info
<a href="http://ariejan.net/2007/07/06/how-to-write-a-rails-plugin-for-controllers/" target="_blank">http://ariejan.net/2007/07/06/how-to-write-a-rails-plugin-for-controllers/</a></pre>
</div>
</div>
<h4>Enabling the plugin by default</h4>
<p>We have a very nice plugin now, but it is not loaded by default! If you take a look at your plugin directory,<br />
in this create a directory, rails and a initiliser file init.rb<br />
Similiar to the rails application. The loader searches for the &#8220;rails/init.rb&#8221; in your plugin.</p>
<pre>--rails
 |--- init.rb</pre>
<pre>--plugin
 |--lib
 |--rails
    |--init.rb</pre>
<p>And thats it.<br />
Now, Secondly<br />
<strong>How to add Controllers, Helpers, and Models to your Plugin?</strong><br />
Its very easy <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
in the file: &#8220;rails/init.rb&#8221; in vendor/plugins/YOUR_PLUGIN<br />
Copy your controllers, models, helpers, views in a directory under vendor/plugins/YOUR_PLUGIN/lib/app</p>
<p>similar to the rails application.</p>
<p style="padding-left:30px;"><em>app/controllers</em></p>
<p style="padding-left:30px;"><em>app/helpers</em></p>
<p style="padding-left:30px;"><em> app/views</em></p>
<p style="padding-left:30px;"><em>app/models</em></p>
<p style="text-align:left;">And then just add the following lines in the file: &#8220;rails/init.rb&#8221; in vendor/plugins/YOUR_PLUGIN</p>
<p style="padding-left:30px;"><span style="color:#ff9900;"><em>%w{ models controllers helpers }.each do |dir|</em></span></p>
<p style="text-align:left;padding-left:30px;"><span style="color:#ff9900;"><em>path = File.join(directory, &#8216;lib/app&#8217;, dir)</em></span></p>
<p style="padding-left:30px;"><span style="color:#ff9900;"><em>$LOAD_PATH &lt;&lt; path</em></span></p>
<p style="padding-left:30px;"><span style="color:#ff9900;"><em>#The Dependencies class has now changed to ActiveSupport::Dependencies</em></span></p>
<p style="padding-left:30px;"><span style="color:#ff9900;"><em>ActiveSupport::Dependencies.load_paths &lt;&lt; path</em></span></p>
<p style="padding-left:30px;"><span style="color:#ff9900;"><em>ActiveSupport::Dependencies.load_once_paths.delete(path)</em></span></p>
<p style="padding-left:30px;"><span style="color:#ff9900;"><em>end</em></span></p>
<p>Thats it. All the controllers, helpers, Models will be loaded in the main app when the server starts.</p>
<p>Thirdly, one more issue you fill face is :<br />
<strong>How to add the views to your Plugin?</strong><br />
Thats to simple from now.. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Just copy the below line in the rails/init.rb</p>
<p style="padding-left:30px;"><span style="color:#ff9900;">ActionController::Base.append_view_path(File.join(directory, &#8220;lib/app&#8221;, &#8220;views&#8221;))</span></p>
<p>and you are done. All your views  for the plugin shall be included in the main application.</p>
<p>Hope this helps you in your plugins ..</p>
<p>Good Luck</p>
<p>Hansraj</p>
<p>&nbsp;</p>
<h2><span style="color:#000000;">Some more issues you might come through, are</span></h2>
<h4>1.   Can’t dup NilClass error</h4>
<p>&nbsp;</p>
<p>The <strong>can’t dup NilClass</strong> error was really tricking  me.</p>
<p>It seemed to be saying something about duping nil class but not really.  The problem is that your app tries to re-load</p>
<p>the plugin models / controllers in the &#8216;development &#8216; environment.</p>
<p>But as it already has it loaded, it gives this error. Hence it must be taken care of.</p>
<p>On many of the post and blogs around, you will find that using &#8220;unloadable&#8221; in the app solves the problem.</p>
<p>But, as i know, it has been depricated.</p>
<p>Thats no the part to worry.. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>To get over this error, we have 2 options :</p>
<p>&nbsp;</p>
<p>Firstly, while exporting your plugins path to the app, you need to add the following to the init.rb of your plugin.</p>
<p style="padding-left:30px;"><span style="color:#ff9900;">load_paths.each do |path|<br />
ActiveSupport::Dependencies.load_once_paths.delete(path)<br />
end if config.environment == ‘development’</span></p>
<p>and thats it..</p>
<p>The second approach has two steps.</p>
<p style="padding-left:30px;"><span style="color:#ff9900;"><span style="color:#000000;">First, you could put something like this in your plugin’s init.rb file:</span><br />
# This plugin should be reloaded in development mode.<br />
if RAILS_ENV == ‘development’<br />
ActiveSupport::Dependencies.load_once_paths.reject!{|x| x =~ /^#{Regexp.escape(File.dirname(__FILE__))}/}<br />
end</span></p>
<p style="padding-left:30px;">Second, you could put something like this in your application’s environment.rb file:<br />
<span style="color:#ff9900;">config.reload_plugins = true if RAILS_ENV == ‘development’</span></p>
<p style="padding-left:30px;">
<p style="padding-left:30px;">And you are done.</p>
<p style="padding-left:30px;">
<p style="padding-left:30px;">
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/railsplugins.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/railsplugins.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/railsplugins.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/railsplugins.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/railsplugins.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/railsplugins.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/railsplugins.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/railsplugins.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/railsplugins.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/railsplugins.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/railsplugins.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/railsplugins.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/railsplugins.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/railsplugins.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=railsplugins.wordpress.com&amp;blog=7368873&amp;post=3&amp;subd=railsplugins&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://railsplugins.wordpress.com/2009/09/24/how-to-write-a-plugin/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b780b201e78d954b5286eff4bfab7421?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hansraj</media:title>
		</media:content>
	</item>
	</channel>
</rss>
