Plugin Panic! - no accessing a model's class methods?

Hi plugin-authors.

So this is a little bit of take two on this question, but why can’t I
access a singleton on the model calling my plugin? The crux of the full
plugin below is:

#logger.info self.class.to_s
new_copy = self.class.new( @copyable_attributes )

the logger spits out “Datatype”, but I get an error from rails saying:

undefined method `datatype’ for false:FalseClass

for the self.class.new line. I also feel haunted by the knowledge that
this is far easier than I think it is, though I cannot seem to find the
answer in the acts_as_versioned or any other plugin (since none create
new
instances of their calling model class).

full plugin contents follow.

whimper help?


Matthew B. :: 607 227 0871
Resume & Portfolio @ http://madhatted.com

ActsAsCopyable

module FakeActiveRecord
module Acts #:nodoc:
module Copyable #:nodoc:

  def self.append_features(base)
    super
    base.extend(MacroMethods)
  end

  # declare the class level helper methods which
  # will load the relevant instance methods
  # defined below when invoked
  module MacroMethods
    def acts_as_copyable
      include FakeActiveRecord::Acts::Copyable::InstanceMethods
    end
  end

  # Adds instance methods.
  module InstanceMethods
    def copy( options = {} )
      @copyable_attributes = self.attributes
      @copyable_attributes.delete 'id'
      options.each do |key, value|
        next if key.class == String
        options.delete key
        options[key.to_s] = value
      end
      @copyable_attributes.merge! options
      #logger.info self.class.to_s
      new_copy = self.class.new( @copyable_attributes )
      new_copy.save
    end
  end

end

end
end

Hi again plugin-dieties!

Since noone wants to look at this, I’ll make it easy. This does the
same thing a plugin does, right?


class RealSpore # aka ActiveRecord
end

module PluginSpore # aka FakeActiveRecord in a plugin
def self.append_features(base)
super
base.extend(MacroMethods)
end

module MacroMethods
def acts_as_clonable
include PluginSpore::InstanceMethods
end
end

module InstanceMethods
def clone
self.class.new
end
end
end

Load it, ala plugins

RealSpore.class_eval do
include PluginSpore
end

Ok, now load like a model loads in rails

class SmithSpore < RealSpore
acts_as_clonable
end

call it new like a controller

bob = SmithSpore.new

copy/clone it

bobs_brother = bob.clone

p 'bob has DNA of: ’ + bob.inspect
p 'bobs brother bas DNA of: ’ + bobs_brother.inspect


Well that code works fine. Why doesn’t this plugin do the same? I’ll
file a bug tomorrow if I can’t come up with anything more.


Matthew B. :: 607 227 0871
Resume & Portfolio @ http://madhatted.com

Matthew B. wrote:

Hi plugin-authors.

So this is a little bit of take two on this question, but why can’t I
access a singleton on the model calling my plugin? The crux of the full
plugin below is:

#logger.info self.class.to_s
new_copy = self.class.new( @copyable_attributes )

the logger spits out “Datatype”, but I get an error from rails saying:

undefined method `datatype’ for false:FalseClass

for the self.class.new line. I also feel haunted by the knowledge that
this is far easier than I think it is, though I cannot seem to find the
answer in the acts_as_versioned or any other plugin (since none create
new
instances of their calling model class).

full plugin contents follow.

whimper help?


Matthew B. :: 607 227 0871
Resume & Portfolio @ http://madhatted.com

ActsAsCopyable

module FakeActiveRecord
module Acts #:nodoc:
module Copyable #:nodoc:

  def self.append_features(base)
    super
    base.extend(MacroMethods)
  end

  # declare the class level helper methods which
  # will load the relevant instance methods
  # defined below when invoked
  module MacroMethods
    def acts_as_copyable
      include FakeActiveRecord::Acts::Copyable::InstanceMethods
    end
  end

  # Adds instance methods.
  module InstanceMethods
    def copy( options = {} )
      @copyable_attributes = self.attributes
      @copyable_attributes.delete 'id'
      options.each do |key, value|
        next if key.class == String
        options.delete key
        options[key.to_s] = value
      end
      @copyable_attributes.merge! options
      #logger.info self.class.to_s
      new_copy = self.class.new( @copyable_attributes )
      new_copy.save
    end
  end

end

end
end