<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Rubynaut: How to create a plugin</title>
    <link>http://www.rubynaut.net/articles/2007/04/13/how-to-create-a-plugin</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>How to create a plugin</title>
      <description>&lt;p&gt;Create a plugin on rails is the best way to share a features or a code between applications and with others. I found a lot information about how to extend or mixin ActiveRecord, but no information about how to provide a piece of application. Maybe I do wrong but that&amp;#8217;s how I did:&lt;/p&gt;


	&lt;p&gt;The more completed information is on &lt;a href="http://weblog.techno-weenie.net/"&gt;Rick Olson blog&lt;/a&gt;, not in the &lt;a href="http://weblog.techno-weenie.net/2007/1/26/understanding-the-rails-plugin-initialization-process"&gt;plugin initialization&lt;/a&gt; :), but in the &lt;a href="http://weblog.techno-weenie.net/2007/1/24/understanding-the-rails-initialization-process"&gt;initialization process part I&lt;/a&gt;. The installation process has changed with Rails 1.2.&lt;/p&gt;


	&lt;p&gt;The work with model can be simple, you can put a &lt;code&gt;require&lt;/code&gt; in your &lt;code&gt;init.rb&lt;/code&gt; and your model will be available for the application.&lt;/p&gt;


	&lt;p&gt;In my case I need to make available a whole piece of application with models, views and controller. the way to do (from &lt;a href="http://weblog.techno-weenie.net/"&gt;techno weenie&lt;/a&gt;):&lt;/p&gt;


	&lt;p&gt;for your model :&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;    &lt;span class="ident"&gt;models_path&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;File&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;join&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;directory&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;app&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;models&lt;/span&gt;&lt;span class="punct"&gt;')&lt;/span&gt;
    &lt;span class="global"&gt;$LOAD_PATH&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="ident"&gt;models_path&lt;/span&gt;
    &lt;span class="constant"&gt;Dependencies&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;load_paths&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="ident"&gt;models_path&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;for the controllers it&amp;#8217;s quite the same:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;    &lt;span class="ident"&gt;controller_path&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;File&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;join&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;directory&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;app&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;controllers&lt;/span&gt;&lt;span class="punct"&gt;')&lt;/span&gt;
    &lt;span class="global"&gt;$LOAD_PATH&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="ident"&gt;controller_path&lt;/span&gt;
    &lt;span class="constant"&gt;Dependencies&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;load_paths&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="ident"&gt;controller_path&lt;/span&gt;
    &lt;span class="ident"&gt;config&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;controller_paths&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="ident"&gt;controller_path&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;It&amp;#8217;s very important to register controllers this way, else they will not be recognized for routing.&lt;/p&gt;


	&lt;p&gt;And finally to include views, you should integrate in your controller:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;    &lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;template_root&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;File&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;join&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;File&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;dirname&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;__FILE__&lt;/span&gt;&lt;span class="punct"&gt;),&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;..&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;views&lt;/span&gt;&lt;span class="punct"&gt;')&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;to make views available for your controller.&lt;/p&gt;


	&lt;p&gt;Once you have that, your application plugin is ready to be used in another application.&lt;/p&gt;


	&lt;p&gt;Now, I have many questions:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Is it the good way to share a piece of application?&lt;/li&gt;
		&lt;li&gt;How standardize the way of communicate between application and plugin ?&lt;/li&gt;
		&lt;li&gt;In the same topic, I have 2 parts in the plugin, one for admin and one for front user, How should I do to easily provide a plugin knowing every application have a different way to authorize or authenticate users?&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Please feel free to share any knowledge on these subjects :)&lt;/p&gt;


	&lt;p&gt;I will update this post later because have some other steps to include like migration and dispatching.&lt;/p&gt;</description>
      <pubDate>Fri, 13 Apr 2007 10:44:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:4e8c7539-e5ea-4b0b-8445-6dcc4b302ba1</guid>
      <author>stephane</author>
      <link>http://www.rubynaut.net/articles/2007/04/13/how-to-create-a-plugin</link>
    </item>
  </channel>
</rss>
