Hello, I want to add some functionality to a plugin. The original plugin extends ActiveRecord by including ''ActMethods'' .. I''d like to add some methods to this ''ActMethods'' module. I tried just redefining ActMethods, but then original methods didn''t work. - - - So what''s the best way to improve functionality of a plugin? I could write another plugin, but I don''t know if that''s the best idea :/
What about extending the existing plugin without modifying ? -- Posted via http://www.ruby-forum.com/.
Yes, that''s what I want to do. Extend it without modifying. How do I do that? On Jun 16, 12:57 pm, Sijo Kg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> What about extending the existing plugin without modifying ? > -- > Posted viahttp://www.ruby-forum.com/.
You should use class_eval and instance_eval to do it, not redefining the class - http://codeshooter.wordpress.com/2009/06/04/understanding-class_eval-module_eval-and-instance_eval/ - Maurício Linhares http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr On Tue, Jun 16, 2009 at 6:54 AM, Vojto<zero0xxx-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello, > > I want to add some functionality to a plugin. > > The original plugin extends ActiveRecord by including ''ActMethods'' .. > I''d like to add some methods to this ''ActMethods'' module. > > I tried just redefining ActMethods, but then original methods didn''t > work. > > - - - > > So what''s the best way to improve functionality of a plugin? I could > write another plugin, but I don''t know if that''s the best idea :/ > > >
For example if you have the class MyClass in the existing plugin and if you extend and create another plugin Then in the main library file you can do like MyClass.class_eval do def first_def end etc..... This is only an example Like this you can do -- Posted via http://www.ruby-forum.com/.
OK this is cool. Now where should I put this code? On Jun 16, 1:14 pm, Sijo Kg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> For example if you have the class MyClass in the existing plugin and if > you extend and create another plugin Then in the main library file you > can do like > > MyClass.class_eval do > > def first_def > > end > > etc..... This is only an example Like this you can do > -- > Posted viahttp://www.ruby-forum.com/.
Then in any other place in your code you can call like m = MyClass.new m.first_def Again this is just an example.Please refer the above link from Maurício Linhares post for more details Sijo -- Posted via http://www.ruby-forum.com/.
Yes I got it. But assume we have a Rails project. I could group all those methods and save them to lib folder. But where should I add PluginsClass.class_eval line - that one which would trigger this extending. It doesn''t work when I put it into lib. On Jun 16, 1:31 pm, Sijo Kg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Then in any other place in your code you can call like > > m = MyClass.new > m.first_def > > Again this is just an example.Please refer the above link from Maurício > Linhares post for more details > > Sijo > -- > Posted viahttp://www.ruby-forum.com/.
Place at your inittializers folder. - Maurício Linhares http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr On Tue, Jun 16, 2009 at 8:35 AM, Vojto<zero0xxx-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Yes I got it. > > But assume we have a Rails project. > > I could group all those methods and save them to lib folder. > > But where should I add PluginsClass.class_eval line - that one which > would trigger this extending. It doesn''t work when I put it into lib. > > On Jun 16, 1:31 pm, Sijo Kg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> Then in any other place in your code you can call like >> >> m = MyClass.new >> m.first_def >> >> Again this is just an example.Please refer the above link from Maurício >> Linhares post for more details >> >> Sijo >> -- >> Posted viahttp://www.ruby-forum.com/. > > >
OK Here''s the problem: The original module is already doing bunch of magic: def self.included(base) base.extend ActMethods end It adds those ActMethods to base. And I just wanna add my methods to his ActMethods I can do ActMethods.module_eval def ... end But I wanna store it in separate module. Something like ActMethods.module_eval include MyActMethods doesn''t work. On Jun 16, 1:35 pm, Vojto <zero0...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Yes I got it. > > But assume we have a Rails project. > > I could group all those methods and save them to lib folder. > > But where should I add PluginsClass.class_eval line - that one which > would trigger this extending. It doesn''t work when I put it into lib. > > On Jun 16, 1:31 pm, Sijo Kg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > > Then in any other place in your code you can call like > > > m = MyClass.new > > m.first_def > > > Again this is just an example.Please refer the above link from Maurício > > Linhares post for more details > > > Sijo > > -- > > Posted viahttp://www.ruby-forum.com/.
Did you really read the link I posted? It surely doesn''t look like you did. The correct way of doing it is: ActMethods.module_eval do include MyActMethods end - Maurício Linhares http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr On Tue, Jun 16, 2009 at 8:45 AM, Vojto<zero0xxx-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > OK Here''s the problem: > > The original module is already doing bunch of magic: > > def self.included(base) > base.extend ActMethods > end > > It adds those ActMethods to base. And I just wanna add my methods to > his ActMethods > > I can do ActMethods.module_eval def ... end > > But I wanna store it in separate module. > > Something like > > ActMethods.module_eval include MyActMethods doesn''t work. > > On Jun 16, 1:35 pm, Vojto <zero0...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Yes I got it. >> >> But assume we have a Rails project. >> >> I could group all those methods and save them to lib folder. >> >> But where should I add PluginsClass.class_eval line - that one which >> would trigger this extending. It doesn''t work when I put it into lib. >> >> On Jun 16, 1:31 pm, Sijo Kg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> >> >> > Then in any other place in your code you can call like >> >> > m = MyClass.new >> > m.first_def >> >> > Again this is just an example.Please refer the above link from Maurício >> > Linhares post for more details >> >> > Sijo >> > -- >> > Posted viahttp://www.ruby-forum.com/. > > >
That''s what I''m trying to tell - it doesn''t work. When I wrote "ActMethods.module_eval include MyActMethods doesn''t work." I meant ActMethods.module_eval do include MyActMethods end Just was too lazy to write do/end -- Anyway it doesn''t work. While this: ActMethods.module_eval do def my_method ... end end Works. -- Don''t know why :( On Jun 16, 2:04 pm, Maurício Linhares <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Did you really read the link I posted? > > It surely doesn''t look like you did. > > The correct way of doing it is: > > ActMethods.module_eval do > include MyActMethods > end > > - > Maurício Linhareshttp://codeshooter.wordpress.com/|http://twitter.com/mauriciojr > > > > On Tue, Jun 16, 2009 at 8:45 AM, Vojto<zero0...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > OK Here''s the problem: > > > The original module is already doing bunch of magic: > > > def self.included(base) > > base.extend ActMethods > > end > > > It adds those ActMethods to base. And I just wanna add my methods to > > his ActMethods > > > I can do ActMethods.module_eval def ... end > > > But I wanna store it in separate module. > > > Something like > > > ActMethods.module_eval include MyActMethods doesn''t work. > > > On Jun 16, 1:35 pm, Vojto <zero0...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Yes I got it. > > >> But assume we have a Rails project. > > >> I could group all those methods and save them to lib folder. > > >> But where should I add PluginsClass.class_eval line - that one which > >> would trigger this extending. It doesn''t work when I put it into lib. > > >> On Jun 16, 1:31 pm, Sijo Kg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > >> > Then in any other place in your code you can call like > > >> > m = MyClass.new > >> > m.first_def > > >> > Again this is just an example.Please refer the above link from Maurício > >> > Linhares post for more details > > >> > Sijo > >> > -- > >> > Posted viahttp://www.ruby-forum.com/.
And what''s the error? If it''s working the other way, just go for it. - Maurício Linhares http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr On Tue, Jun 16, 2009 at 9:11 AM, Vojto<zero0xxx-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > That''s what I''m trying to tell - it doesn''t work. > > When I wrote "ActMethods.module_eval include MyActMethods doesn''t > work." > > I meant > > ActMethods.module_eval do > include MyActMethods > end > > Just was too lazy to write do/end > > -- > > Anyway it doesn''t work. While this: > > ActMethods.module_eval do > def my_method ... end > end > > Works. > > -- > > Don''t know why :(
Well I''d have to put my whole code into initializer, and that code belongs to lib, so I would break good rules of code separation. And I also wanna know why the hell it doesn''t work :( -- BTW This ActMethods (from original plugin) defines methods in ActiveRecord class scope - like has_many. The plugin use ''included'' method to add those ActMethods to ActiveRecord. I could do the same thing - Just add my ActMethods to ActiveRecord - but I wan''t to extend plugin, because my ActMethods couldn''t work without plugins''. I could use some easy solution - but I want to use the best solution - the most ruby solution. On Jun 16, 2:20 pm, Maurício Linhares <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> And what''s the error? > > If it''s working the other way, just go for it. > > - > Maurício Linhareshttp://codeshooter.wordpress.com/|http://twitter.com/mauriciojr > > > > On Tue, Jun 16, 2009 at 9:11 AM, Vojto<zero0...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > That''s what I''m trying to tell - it doesn''t work. > > > When I wrote "ActMethods.module_eval include MyActMethods doesn''t > > work." > > > I meant > > > ActMethods.module_eval do > > include MyActMethods > > end > > > Just was too lazy to write do/end > > > -- > > > Anyway it doesn''t work. While this: > > > ActMethods.module_eval do > > def my_method ... end > > end > > > Works. > > > -- > > > Don''t know why :(