hey, i have something like this for the user timestamp (created_by and updated_by), i have seen this on http://delynnberry.com/pages/userstamp/ and http://wiki.rubyonrails.com/rails/pages/ExtendingActiveRecordExample #adding ''created_by'' and ''updated_by'' to all models module ObjectStatistics def self.append_features(base) base.before_create do |model| model.created_by ||= User.backend_user.id if model.respond_to?(:created_by) end base.before_save do |model| model.updated_by = User.backend_user.id if model.respond_to?(:updated_by) end end # belongs_to :created_by, :class_name => "User", :foreign_key => "created_by" # belongs_to :updated_by, :class_name => "User", :foreign_key => "updated_by" end my usermodel class User < ActiveRecord::Base include ObjectStatistics cattr_accessor :backend_user def to_s return self.login end end my productmodel class Product < ActiveRecord::Base include ObjectStatistics end in my application controller i have this: before_filter :set_backend_user def set_backend_user User.backend_user = User.find(session[:backend].id) unless session[:backend].nil? end Now this al works! :) But when i do item.created_by => i get "1" this is the id from the module (User.backend_user.id ), but i want the login. Now my question is, how can i implement this below in the module so i dont need to type it in every model? belongs_to :created_by, :class_name => "User", :foreign_key => "created_by" belongs_to :updated_by, :class_name => "User", :foreign_key => "updated_by" -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
I had the same question about how to implement the belongs_to in a more dry fashion. I din'';t figure it out, os i have that declaration in most all of my models. Not ideal. As for displaying the login, you could override the accessor def self.created_by self.created_by.login end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
module ObjectStatistics def included(other) other.send(:belongs_to, :created_by, :class_name => "User", :foreign_key =>"created_by") other.send(:belongs_to, :updated_by, :class_name => "User", :foreign_key =>"updated_by") end ... end This will call the belongs_to method on the class that the module is included in. You may need to fiddle with the syntax, can''t test this at the moment. Cheers, Max On 8/31/06, Nick Brutyn <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > hey, i have something like this for the user timestamp (created_by and > updated_by), i have seen this on http://delynnberry.com/pages/userstamp/ > and http://wiki.rubyonrails.com/rails/pages/ExtendingActiveRecordExample > > #adding ''created_by'' and ''updated_by'' to all models > module ObjectStatistics > def self.append_features(base) > base.before_create do |model| > model.created_by ||= User.backend_user.id if > model.respond_to?(:created_by) > end > base.before_save do |model| > model.updated_by = User.backend_user.id if > model.respond_to?(:updated_by) > end > end > > # belongs_to :created_by, :class_name => "User", :foreign_key => > "created_by" > # belongs_to :updated_by, :class_name => "User", :foreign_key => > "updated_by" > > end > > my usermodel > class User < ActiveRecord::Base > include ObjectStatistics > cattr_accessor :backend_user > > def to_s > return self.login > end > end > > my productmodel > class Product < ActiveRecord::Base > include ObjectStatistics > end > > in my application controller i have this: > > before_filter :set_backend_user > def set_backend_user > User.backend_user = User.find(session[:backend].id) unless > session[:backend].nil? > end > > Now this al works! :) > > But when i do item.created_by => i get "1" > this is the id from the module (User.backend_user.id ), but i want the > login. > > Now my question is, how can i implement this below in the module so i > dont need to type it in every model? > > belongs_to :created_by, :class_name => "User", :foreign_key => > "created_by" > belongs_to :updated_by, :class_name => "User", :foreign_key => > "updated_by" > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Hi all, I am trying to do a functional test on a controller that mixes in a module with an expensive method, but I would like to mock out that method for my testing purposes. I can''t seem to do it. Here''s what I have tried def setup @controller = MyController.new klass = class << @controller; self; end klass.send(:undef_method, :something_expensive) klass.send(:define_method, :something_expensive, lambda {nil}) end I have also tried to do the less idiomatic but more straightforward def @controller.something_expensive; nil; end but that didn''t help, either. The undef_method fails because it can''t find the method (I guess becaues it''s actually defined in the module). The other way just silently fails to accomplish its objective. I''ve googled some but am somewhat perplexed. Ideas? ### this is what the controller looks like ### class MyController include ModuleWithExpensiveMethod end module ModuleWithExpensiveMethod def something_expensive sleep(100) end end ### thanks -- Yan Pritzker http://skwp.wordpress.com http://planyp.us --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
thx it works ;) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
I''ve got a plugin which handles this very simply. You just add two columns to the table, created_by_id and updated_by_id, then declare: class Person < ActiveRecord::Base acts_as_monitored end And the fields will be updated accordingly. This is a slightly nicer solution as it allows you to include the associations with a find. The plugin assumes the current user can be accessed through User.current(you can change this if you need). http://svn.viney.net.nz/things/rails/plugins/acts_as_monitored -Jonathan. On 8/31/06, Nick Brutyn <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > thx it works ;) > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Both of these techniques work fine when I try your examples - are you sure your example illustrates the problem you are seeing? A third way is like this... def setup @controller = MyController.new class << @controller undef something_expensive # not strictly necessary def something_expensive; end end end But if you are getting into mocking and stubbing, you might like to have a look at Mocha at http://mocha.rubyforge.org which can make your life a bit easier. -- James. http://blog.floehopper.org On 31/08/06, Yan Pritzker <yan.pritzker-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Hi all, > > I am trying to do a functional test on a controller that mixes in a module > with an expensive method, but I would like to mock out that method for my > testing purposes. I can''t seem to do it. Here''s what I have tried > > def setup > @controller = MyController.new > klass = class << @controller; self; end > klass.send(:undef_method, :something_expensive) > klass.send(:define_method, :something_expensive, lambda {nil}) > end > > I have also tried to do the less idiomatic but more straightforward > def @controller.something_expensive; nil; end > > but that didn''t help, either. The undef_method fails because it can''t find > the method (I guess becaues it''s actually defined in the module). The > other way just silently fails to accomplish its objective. I''ve googled > some but am somewhat perplexed. Ideas? > > ### this is what the controller looks like ### > > class MyController > include ModuleWithExpensiveMethod > end > > module ModuleWithExpensiveMethod > def something_expensive > sleep(100) > end > end > > ### > > thanks > -- > Yan Pritzker > http://skwp.wordpress.com > http://planyp.us > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---