ActiveRecord::Extensions 0.5.2 Released!
on March 15, 2007 @ 01:32 AM

ActiveRecord::Extensions 0.5.2 is released! A rubygem has been born!

Installation is so easy now for ActiveRecord::Extensions:


gem install -r 'ar-extensions'

Enjoy!

0.5.1 was also released, but I quickly released 0.5.2 after it to fix a possible bug with method aliasing. This would only occur if you had another plugin or rubygem which used the same alias name for ActiveRecord::Base#quote


  1. xTRiM 03.28.07 / 16PM
    Hi! Nice lib! But I have some problems with CVS ... ;) Im on rails 1.1.6 currently. 1. How to get "to_csv" functionality on AR objects? I tryed to install ActiveRecord::Extensions as gem and as plugin, but i do not get "to_csv" on AR objects (but finders work). Looking in code, there seems to be no call to ActiveRecord::Extensions::FindToCSV anywhere. After I put "ActiveRecord::Base.send :include, ActiveRecord::Extensions::FindToCSV" to init.rb of plugin after loading of file, it worked fine. 2. Maybe this is somehow involved with point 1, but method find(*args) isnt overwriten by one in module ClassMethods. So no ArrayInstanceMethods and "to_csv" for arrays returned by "find". Fixed by renaming "find" in ClassMethods to "find_for_csv" and changing "def self.included" to def self.included( cl ) # :nodoc: virtual_class = class << cl ; self ; end if not virtual_class.ancestors.include?( self::ClassMethods ) # cl.instance_eval "alias #{ALIAS_FOR_FIND} :find" cl.extend( ClassMethods ) cl.send( :include, InstanceMethods ) end cl.class_eval do class << self alias_method ALIAS_FOR_FIND, :find unless method_defined?(ALIAS_FOR_FIND) alias_method :find, :find_for_csv end end end Strange... 3. This is not a problem, but an additional feature :) Some values in objects returned by "find" need to be... initialized (?), i.e. pass through actual method of model. To be formatted for example. So, changed: def to_csv_data( options={} ) fields = self.class.to_csv_fields( options ).fields data, model_data = [], fields.inject( [] ) { |arr,field| arr << attributes[field].to_s } if options[:include] ....... to: def to_csv_data( options={} ) fields = self.class.to_csv_fields( options ).fields data, model_data = [], fields.inject( [] ) do |arr,field| if options[:typecast] and options[:typecast].include?(field.intern) arr << self.send(field.intern).to_s else arr << attributes[field].to_s end end if options[:include] ........ Example - using StoreMultiplied for prices of books. Old: Book.find(10).to_csv(:only=>[:id, :price]) => "id,value\n1,12300\n" New: Book.find(10).to_csv(:only=>[:id, :price], :typecast =>[:price]) => "id,value\n1,123.0\n"
  2. xTRiM 03.28.07 / 16PM
    Oh, I mean CSV of course, not CVS. No formating in posts?..
  3. Zach Dennis 03.31.07 / 03AM
    xtrim, thanks for posting. In regards to formatting I am using whatever Mephisto allows in 0.7.2, I have never tried to format, but that would definitely be a nice improvement to this comments stuff. (i think textile is allowed though) To get CSV functionality on individual models put the following in your model: * include ActiveRecord::Extensions::FindToCSV
  4. Zach Dennis 03.31.07 / 03AM

    I just toggled textile on as the default comments setting. It doesn’t appear to take over in the old comments. So here is a new comment to try it out.

    To get CSV functionality on individual models put the following in your model:

    • include ActiveRecord::Extensions::FindToCSV