Hi, I''m interested in learning to write acts_as_* plugins. Is there any more info out there than what is currently on the rubyonrails wiki or Jamis Buck''s site? Thanks, Peter
Peter Michaux wrote:> Hi, > > I''m interested in learning to write acts_as_* plugins. Is there any > more info out there than what is currently on the rubyonrails wiki or > Jamis Buck''s site? > > Thanks, > Peter > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/railsHere''s what I did (assuming a fox plugin - no chunky bacon this time) (A 5 step program to acting foxy...) 1 - under your_app/vender/plugins create a directory "acts_as_fox" 2 - in this directory create a lib directory so now you should have your_app/vender/plugins/acts_as_fox/lib 3 - in the acts_as_fox directory create the following file your_app/vender/plugins/acts_as_fox/init.rb #------ require ''acts_as_fox'' 4 - in the lib subdirectory add your code to a file called acts_as_fox.rb your_app/vender/plugins/acts_as_fox/lib/acts_as_fox.rb #------ require ''active_record'' module ActiveRecord module Acts #:nodoc: module Fox#:nodoc: def self.append_features(base) super base.extend(ClassMethods) end # Adds a find_all_visible method which finds all records which are not hidden module ClassMethods def acts_as_hidable class_eval do extend ActiveRecord::Acts::Hidable::SingletonMethods end end end #add in your methods here... module SingletonMethods def catch_chickens find(:all, :conditions => ''chickens = true'') end #etc... end end end end ActiveRecord::Base.class_eval do include ActiveRecord::Acts::Fox end 5 - Finally use your method in your classes class Thing < ActiveRecord::Base acts_as_fox end You''ve got to be running 0.14.x (not sure which final point release) but it doesn''t work with 0.13.1 Hope this helps Kev
Kev Jackson wrote:> Peter Michaux wrote: > >> Hi, >> >> I''m interested in learning to write acts_as_* plugins. Is there any >> more info out there than what is currently on the rubyonrails wiki or >> Jamis Buck''s site? >> >> Thanks, >> Peter >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > > Here''s what I did (assuming a fox plugin - no chunky bacon this time) > (A 5 step program to acting foxy...) > > 1 - > under your_app/vender/plugins > create a directory "acts_as_fox" > > 2 - > in this directory create a lib directory > > so now you should have > > your_app/vender/plugins/acts_as_fox/lib > > 3 - > in the acts_as_fox directory create the following file > > your_app/vender/plugins/acts_as_fox/init.rb > #------ > require ''acts_as_fox'' > > 4 - > in the lib subdirectory add your code to a file called acts_as_fox.rb > your_app/vender/plugins/acts_as_fox/lib/acts_as_fox.rb > #------ > require ''active_record'' > > module ActiveRecord > module Acts #:nodoc: > module Fox#:nodoc: > def self.append_features(base) > super > base.extend(ClassMethods) > end > # Adds a find_all_visible method which finds all records which > are not hidden > module ClassMethodsSorry this should of course be> def acts_as_hidabledef acts_as_fox> class_eval doand similarly this should be> extend ActiveRecord::Acts::Hidable::SingletonMethodsextend ActiveRecord::Acts::Fox::SingletonMethods> end > end > end > #add in your methods here... > module SingletonMethods > def catch_chickens > find(:all, :conditions => ''chickens = true'') > end > #etc... > end > end > end > end > > ActiveRecord::Base.class_eval do > include ActiveRecord::Acts::Fox > end > > 5 - > Finally use your method in your classes > > class Thing < ActiveRecord::Base > acts_as_fox > end > > You''ve got to be running 0.14.x (not sure which final point release) > but it doesn''t work with 0.13.1 > > Hope this helps > Kev > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Nov 3, 2005, at 12:39 AM, Kev Jackson wrote:> 4 - > in the lib subdirectory add your code to a file called acts_as_fox.rb > your_app/vender/plugins/acts_as_fox/lib/acts_as_fox.rb > #------ > require ''active_record'' > > module ActiveRecord > module Acts #:nodoc: > module Fox#:nodoc: > def self.append_features(base) > super > base.extend(ClassMethods) > end > # Adds a find_all_visible method which finds all records > which are not hidden > module ClassMethods > def acts_as_hidable > class_eval do > extend ActiveRecord::Acts::Hidable::SingletonMethods > end > end > end > #add in your methods here... > module SingletonMethods > def catch_chickens > find(:all, :conditions => ''chickens = true'') > end > #etc... > end > end > end > end > > ActiveRecord::Base.class_eval do > include ActiveRecord::Acts::Fox > endI''ve noticed many plugin developers taking this particular route, and I''d like to discourage it, strenuously. There really is no need to add your own acts to the ActiveRecord::Acts namespace. Instead, I''d encourage the creation of your own Acts namespace: module Foo module Acts class Fox end end end Foo::Acts::Fox This will allow (potentially) multiple developers to develop identically-named features without conflict. (Otherwise, great summary, Kev! Please don''t think I''m picking on you specifically--I''ve seen lots of developers taking this route lately.) - Jamis
> > I''ve noticed many plugin developers taking this particular route, and > I''d like to discourage it, strenuously. There really is no need to > add your own acts to the ActiveRecord::Acts namespace. Instead, I''d > encourage the creation of your own Acts namespace: > > module Foo > module Acts > class Fox > end > end > end > > Foo::Acts::Fox > > This will allow (potentially) multiple developers to develop > identically-named features without conflict. > > (Otherwise, great summary, Kev! Please don''t think I''m picking on you > specifically--I''ve seen lots of developers taking this route lately.)No problems, I used another plugin to copy the format of my first plugin - I suppose that most people go through this - find an example and copy the structure route whilst they are learning. Kev
Thank Kev and Jamis for the info. I''ve got foxes that can catch chickens! Since I couldn''t find a wiki page on this and I thought it was worth preserving your info I made a page. http://wiki.rubyonrails.com/rails/pages/HowToWriteAnActsAsPlugIn I''d like to know a little more about the anatomy of a plugin. Why are three top level moduals nested? I''ve seen inside these three, modual ClassMethods, modual InstanceMethods and modual SingletonMethods. What are the differences? And also a trick that prevents the plugin from being called more than once which maybe is essential? def acts_as_fox(options = {}) ## this prevents acts_as_fox from being called multiple times return if self.included_modules.include?(Foo::Acts::Fox::InstanceMethods) Maybe a plugin template for best practices or even better ruby script/generate plugin acts_as_fox With some testing files or something. That would be cool! Thanks, Peter