I have a bunch of admin pages that will need to update two fields on
all models
there is a UserAccount model and then all other models have an
association from two properties, this example code uses a Product
model
class Product < ActiveRecord::Base
# the foreign keys we always have
belongs_to :created_by, :foreign_key =>
'created_by_id', :class_name => 'UserAccount'
belongs_to :updated_by, :foreign_key =>
'updated_by_id', :class_name => 'UserAccount'
end
I found a way through plugins to mix in those associations, so no code
reuse issues there, the problem comes with active scaffold controllers
class Admin::ProductsController < Admin::AdminBaseController
active_scaffold :product do |config|
...
end
# want to move these to a base class
def before_create_save(record)
record.created_by = current_user
record.updated_by = current_user
end
def before_update_save(record)
record.updated_by = current_user
end
end
then the AdminBaseController where I would like to move the
before_update_save, before_create_save methods
class Admin::AdminBaseController < ApplicationController
layout 'admin'
end
rails is picking up the layout from the base controller so I know that
much works
but active scaffold does not seem to fire off the before save events
when they are in the base controller, I would like to avoid putting
that code into every controller if possible
Thanks in advance for any help or pointers in the right direction
I have found a solution - I guess this might even be the 'standard'
ruby way to do this?
here is the new code - model not shown, see previous post
#
************************************************************************
# the controller example: 'products controller', inherits
AdminBaseController
# note the require 'admin_base_util'
class Admin::ProductsController < Admin::AdminBaseController
require 'admin_base_util'
active_scaffold :product do |config|
...
end
end
#
************************************************************************
# the base controller, common admin functions
class Admin::AdminBaseController < ApplicationController
layout 'admin'
# note the do_ these are not the direct overrides of active scaffold
# but this is where we actually defined the custom functionality
def do_before_create_save(record)
record.created_by = current_user
record.updated_by = current_user
end
def do_before_update_save(record)
record.updated_by = current_user
end
end
#
************************************************************************
# the active scaffold overrides
# this is code that I put in the lib directory and the file is named
'admin_base_util'
# 'admin_base_util' will be the 'required' mix-in in each controller
that wants the overriden behaviour
# i.e. require 'admin_base_util' as in above ProductsController
module ActiveScaffold::Actions
module Create
# overriding active scaffold
def before_create_save(record)
# call beck into the controller
do_before_create_save record
end
end
module Update
# overriding active scaffold
def before_update_save(record)
# call beck into the controller
do_before_update_save record
end
end
end
# ********
one important item, when changing the code in the lib/
admin_base_util.rb, a restart of the web server was required, this
threw me off for a bit
...