Skip to content

This Rails plugin extends ActiveRecord::Base to add automatic updating of created_by and updated_by attributes of your models in much the same way that the ActiveRecord::Timestamp module updates created_(at/on) and updated_(at/on) attributes.

License

Notifications You must be signed in to change notification settings

akm/magic_userstamp

 
 

Repository files navigation

MagicUserstamp

About MagicUserstamp

This plugin is a fork of delynn/userstamp ( github.com/delynn/userstamp )

But almost of classes have been changed. And you can use creator_id, updater_id or deleter_id (with acts_as_paranod) easiliy. It is enough to write config/initializers/magic_userstamp.rb only instead of definition in each model.

Install

as a plugin

ruby script/plugin install git://github.com/akm/magic_userstamp.git

as a gem

insert following line to config/environment.rb

config.gem 'magic_userstamp', :version => '0.1.0'

and

$ sudo rake gems:install

Or install gem manually

$ sudo gem install gemcutter
$ sudo gem tumble
$ sudo gem install magic_userstamp

Simple Configuration

make config/initializers/magic_userstamp.rb

The most simple setting

require 'magic_userstamp'
# Comment out if you don't use t.userstamp at all in migrations
ActiveRecord::ConnectionAdapters::TableDefinition.send(:include, MagicUserstamp::MigrationHelper)
ActiveRecord::Base.module_eval do
  include MagicUserstamp
end

MagicUserstamp::Config.setup.defaults

# If you have some classes which has loaded before Userstamp is loaded,
# set record_userstamp true
# Example: vestal_versions gem
# Version.record_userstamp = true if defined?(Version)

Controller setting

add followings to ApplicationController or some controller

 before_filter :set_user
 after_filter :reset_user

def set_user
  user_id = session[:user] # ここではIDが帰ってくるはず。
  User.model_stamper       # UserがIDを覚えられるようにする。
  User.stamper = user_id   # IDを覚えておく。stamper=にはオブジェクトを渡してもOK。
end

def reset_user
  User.model_stamper  # Userが覚えているIDを忘れることができるようにする。
  User.reset_stamper  # Userが覚えているIDを実際に忘れる。
end

Various Configuration

If you want to customize setting, chage config/initializers/magic_userstamp.rb

The most simple setting

MagicUserstamp::Config.setup.defaults

Specify User class name

MagicUserstamp::Config.setup.defaults(:stamper_class_name => 'AdminUser')

Actually

MagicUserstamp::Config.setup.defaults

means

MagicUserstamp.config.setup do |config|
  config.with_options(:stamper_class_name => 'User') do |c|
    c.on(:create , :creator_id)
    c.on(:update , :updater_id)
    c.on(:destroy, :deleter_id)
  end
end

Specify User class name and creator/updater columns

MagicUserstamp.config.setup do |config|
  config.with_options(:stamper_class_name => 'Person') do |c|
    c.on(:create , :creator_person_id)
    c.on(:update , :updater_person_id)
    c.on(:destroy, :deleter_person_id)
  end
end

Specify target stampable class names

MagicUserstamp.config.setup do |config|
  config.defaults(:stampable_class_names => %w(Book Schedule))

Specify target stampable class names and columns

MagicUserstamp.config.setup do |config|
  config.with_options(:stampable_class_names => %w(Book Schedule)) do |c|
    c.on(:create , :creator_user_id)
    c.on(:update , :updater_user_id)
    c.on(:destroy, :deleter_user_id)
  end
end

Use in compatibility mode

MagicUserstamp.config.compatibles

Same as

MagicUserstamp.config.setup do |c|
  c.on(:create , :created_by)
  c.on(:update , :updated_by)
  c.on(:destroy, :deleted_by)
end

Complex pattern

MagicUserstamp.config.setup do |config|
  config.with_options(:stampable_class_names => %w(Book Schedule)) do |c|
    c.on(:create , :creator_user_id)
    c.on(:update , :updater_user_id)
    c.on(:destroy, :deleter_user_id)
  end
  config.compatibles(:stamper_class_name => 'Person', :stampable_class_names => %w(Person Email))
  config.defaults
end

Copyright © 2009 ‘Takeshi AKIMA, released under the MIT license

About

This Rails plugin extends ActiveRecord::Base to add automatic updating of created_by and updated_by attributes of your models in much the same way that the ActiveRecord::Timestamp module updates created_(at/on) and updated_(at/on) attributes.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%