Using a plugin to modify Rails core?


#1

Hi,

I’ve been trying to learn about plugins by reading plugin source files
where
I can find them. I feel like the fog is clearing a little. Is it true
that a
plugin can be used to modify any part of the Rails core? If so, how can
I do
it? For example, say I want to create a plugin in
vendor/plugin/new_error
parts of ActiveRecord’s Errors class.

Trimmed down from ActiveRecord it has this:

module ActiveRecord
class Errors
@@default_error_messages = {
:inclusion => “is not included in the list”,
:exclusion => “is reserved”,
:invalid => “is invalid”
}

def add_to_base(msg)
add(:base, msg)
end
end
end

and say I want to change it to this (I know, silly example):

module ActiveRecord
class Errors
@@default_error_messages = {
:inclusion => “is not included in the list, idiot”,
:exclusion => “is reserved, ignoramus”
}

def add_to_base(msg)
foo = msg
add(:base, foo)
end
end
end

Would I need to have a vendor/plugin/new_error/init.rb file? What would
it
say?

Would vendor/plugin/new_error/lib/new_error.rb simply be what I want
above?

Thanks,
Peter


#2

hey everyone,
i read this article:
http://www.bitslapped.nl/articles/2005/06/05/rails-on-windows-2003
but am still unable to get RoR working on win2k3/iis6. i can confirm
that my app is working with WEBrick, but whenever i try to access it
with IIS, i get a 404 error. i am pretty sure i have everything
configured correctly.
anyone have any tips to get this running?
thanks a lot!
steve


#3

In your init.rb file, you should require your new_error.rb file, and
that should be all you need. I.e.

vendor/plugins/new_error/init.rb:

require ‘new_error’ # note no ‘lib/’ here

vendor/plugins/new_error/lib/new_error.rb

module ActiveRecord
class Errors
@@default_error_messages = {
:inclusion => “is not included in the list, idiot”,
:exclusion => “is reserved, ignoramus”
}

   def add_to_base(msg)
      foo = msg
      add(:base, foo)
  end
end

end

… and that should do the trick. Plugin code is (almost) the last
thing to load, so you can quite easily use it to hack about with Rails
internals (as I do to a small degree with the Engines plugin).

Hope this helps

  • James

#4

Quick tip, delete all the log files in your app and restart IIS (not
just the site your app is using, the whole thing). Do you get log
files generated again? If so you’re headed in the right direction. You
should have the environmnet log, and a couple fast-cgi logs generated.

Let us know if you get these or not and we can go from there. :slight_smile:

-Nick


#5

ok, did that. now i’m getting “Server Error, unable to connect to
fastcgi server.”
and no logs have been created in the log dir.
thanks for the tip…i guess i messed something up along the way while
configuring fastCGI?


#6

I’m having troubles getting this to work. I have the following files and
reboot WEBrick but I do not see my redefined error messages. Any ideas?

Thanks,
Peter

= vendor/plugins/new_error/init.rb: =

require ‘new_error’ # note no ‘lib/’ or ‘.rb’ here

= vendor/plugins/new_error/lib/new_error.rb =

module ActiveRecord
class Errors
@@default_error_messages = {
:inclusion => “pm is not included in the list”,
:exclusion => “pm is reserved”,
:invalid => “pm is invalid”,
:confirmation => “pm doesn’t match confirmation”,
:accepted => “pm must be accepted”,
:empty => “pm can’t be empty”,
:blank => “pm can’t be blank”,
:too_long => “pm is too long (max is %d characters)”,
:too_short => “pm is too short (min is %d characters)”,
:wrong_length => “pm is the wrong length (should be %d
characters)”,
:taken => “pm has already been taken”,
:not_a_number => “pm is not a number”
}
end
end


#7

It works just fine. Even though I am using Rails 14.3 the project was
generated with a Rails version less than 0.14 so the plugins didn’t
work. I
didn’t know the Rails version generating the app mattered. I genrated a
new
app and tried again and everything worked as expected.

-Peter


#8

Do I have to make a reference to the plugin somewhere in my app to make
sure
that the plugin loads? I thought plugins were supposed to be auto
loading.

(If I put the vendor/plugins/new_error/lib/new_error.rb code in
application_controller.rb then I do see the new error messages. So the
plugin is not loading.)

Thanks,
Peter


#9

You can also updgrade existing apps. From your current apps directory
just
run rails . and it will go through and update stuff. Becareful with
this
though, as it will override most of your config files.