Topic: Writing a Rails plugin

I am trying to figure out how to create a simple Rails plugin
http://www.juixe.com/techknow/index.php … -tutorial/

Let's say I want to apply a before_filter to all actions...
[code ruby]
class ApplicationController < ActionController::Base
   before_filter :do_something

   def do_something
      # ...
   end
end
[/code]
How would I write to have the plugin achieve this?

Thanks for any help

Re: Writing a Rails plugin

The code you posted should work. Ruby allows you to re-open classes, so you can add a before filter to the ApplicationController just like that.

Railscasts - Free Ruby on Rails Screencasts

Re: Writing a Rails plugin

I am trying that but the server fails on start-up...

[code ruby]
    class ApplicationController
        def just_testing
            puts 'test'
        end
    end
[/code]
works but...
[code ruby]
        before_filter :just_testing

    class ApplicationController
        def just_testing
            puts 'test'
        end
    end
[/code]
gives an error 'uninitialized constant ...' when re-starting the server

Re: Writing a Rails plugin

[code ruby]
    class ApplicationController
        before_filter :just_testing
       
        def just_testing
            puts 'test'
        end
    end
[/code]
is it but it still fails

Re: Writing a Rails plugin

can you post the full error?

Railscasts - Free Ruby on Rails Screencasts

Re: Writing a Rails plugin

Let me see if I have figured this out correctly:

init.rb:

ActiveRecord::Base.send(:include, Testing)

then in the plugins 'lib' folder I have 'testing.rb'
[code ruby]
module Testing
    class ApplicationController
        before_filter :just_testing
       
        def just_testing
            puts 'test'
        end
    end
end
[/code]

Re: Writing a Rails plugin

where is the error log file, I can't copy and paste from the DOS window?

Re: Writing a Rails plugin

You don't need to alter ActiveRecord::Base here, that's for models. You just need to add to ApplicationController. Just include that part about the ApplicationController in the init.rb file and you don't need anymore code.

However, it's a best practice to put your custom code in a module, but just get the above working first.

The log file is located in log/development.log, however it may be running into this error before the logger is initialized.

Railscasts - Free Ruby on Rails Screencasts

Re: Writing a Rails plugin

now I put
[code ruby]
class ApplicationController
    before_filter :just_testing
   
    def just_testing
        puts 'test'
    end
end
[/code]
in init.rb but get another error:

undefined method 'before_filter'

also when starting up the server

Re: Writing a Rails plugin

This may be getting loaded before the application.rb file is. In that case you need to mention its superclass.

class ApplicationController < ActionController::Base

Railscasts - Free Ruby on Rails Screencasts

Re: Writing a Rails plugin

now it is starting without errors
[code ruby]
class ApplicationController < ActionController::Base
    before_filter :just_testing
   
    def just_testing
        @stuff = 'ok'
    end
end
[/code]
but it doesn't seem to run because @stuff returns nil from the controllers

Re: Writing a Rails plugin

Hmm, it may not be possible then to add before filters from a plugin. I'm assuming it has something to do with the order things are loaded, but I don't know what exactly.

Railscasts - Free Ruby on Rails Screencasts

Re: Writing a Rails plugin

I think there is another problem, because I get "undefined local variable or method `just_testing'" when I try to call it from the controller...