Extend an existing attribute getter in rails model

i have a problem using acts_as_textiled and has_foreign_language plugins together.

TextElement my model in my app

class TextElement < ActiveRecord::Base
    has_foreign_language :value
    acts_as_textiled :value

HasForeignLanguage

def has_foreign_language(*args)
    args.each do |field|
        # Define the Getter
        define_method(field.to_s) do
        .
        .
        .

ActsAsTextiled

def acts_as_textiled(*attributes)
.
.
.
  attributes.each do |attribute|
    define_method(attribute) do |*type|
.
.
.

Both plugins use define_method and which ever way round i call the mixins in TextElement the latter overrides the getter previously defined.

Is there a way to save the existing getter and call that in the newly defined getter? similar to using super if they were inherited.

I have forked these plugins so all is fair game there.

All help appreciated.

Answers


Alternatively you can rewrite these two using alias_method_chain.

def some_class_method_that_overrides(*columns)
  columns.each do | c |
    overriden_name = if instance_methods.include?(c)
      alias_method_chain(c.to_sym, "extra")
      "#{c}_with_extra"
    else
      c
    end
    define_method(overriden_name) do ...
    end
  end
end

Need Your Help

Scrolling content with negative margins

c# wpf scroll

I have generated XAML file from Windows Forms, and with the process calculated right and bottom margins because they do not exist in Forms. I have this output:

date = date + part vs. date.concat(part)

java concatenation javamail

I've got a small Java program that I'm developing for a project, which pulls a user's inbox from a specified URI using JavaMail, and then begins processing the messages.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.