Skip to content

Commit

Permalink
Make userstamp columns configurable
Browse files Browse the repository at this point in the history
  # Globally in environment.rb
  ActiveRecord::Base.created_userstamp_column = :creator_id

  # In a model definition
  class Subscription
    self.created_userstamp_column = :creator_id
    self.updated_userstamp_column = :updater_id
  end
  • Loading branch information
infused committed Dec 17, 2008
1 parent a22a22f commit bf05c3e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
== 1.1.0, released 2008-12-17

* Make userstamp columns configurable

== 1.0.0, released 2008-10-28

* Initial release
13 changes: 12 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
== Blame

Automatically userstamps create and update operations if the table has fields named *created_by* and/or *updated_by*.
Automatically userstamps create and update operations if the table has columns named *created_by* and/or *updated_by*.
The Blame plugin attempts to mirror the simplicity of ActiveRecord's timestamp module.

Blame adds a *userstamps* migration helper which will create both created_by and updated_by columns in your table:
Expand All @@ -18,6 +18,17 @@ can override this behavior by writing your own *userstamp_object* method in Acti
User.find(session[:user_id])
end

You can change the names of the userstamp columns:

# Globally in environment.rb
ActiveRecord::Base.created_userstamp_column = :creator_id

# In a model definition
class Subscription
self.created_userstamp_column = :creator_id
self.updated_userstamp_column = :updater_id
end

Automatic userstamping can be turned off globally by setting:

ActiveRecord::Base.record_userstamps = false
Expand Down
2 changes: 1 addition & 1 deletion about.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ summary: adds userstamps to ActiveRecord models
homepage: http://www.infused.org
plugin: git://github.com/infused/blame.git
license: MIT
version: 1.0.0
version: 1.1.0
12 changes: 9 additions & 3 deletions lib/blame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ def self.included(base)

base.class_inheritable_accessor :record_userstamps, :instance_writer => false
base.record_userstamps = true

base.class_inheritable_accessor :created_userstamp_column, :instance_writer => false
base.created_userstamp_column = :created_by

base.class_inheritable_accessor :updated_userstamp_column, :instance_writer => false
base.updated_userstamp_column = :updated_by
end

private
Expand All @@ -16,15 +22,15 @@ def userstamp_object

def create_with_userstamps
if record_userstamps && userstamp_object
write_attribute('created_by', userstamp_object.id) if respond_to?(:created_by)
write_attribute('updated_by', userstamp_object.id) if respond_to?(:updated_by)
write_attribute(created_userstamp_column, userstamp_object.id) if respond_to?(created_userstamp_column)
write_attribute(updated_userstamp_column, userstamp_object.id) if respond_to?(updated_userstamp_column)
end
create_without_userstamps
end

def update_with_userstamps(*args)
if record_userstamps && userstamp_object && (!partial_updates? || changed?)
write_attribute('updated_by', userstamp_object.id) if respond_to?(:updated_by)
write_attribute(updated_userstamp_column, userstamp_object.id) if respond_to?(updated_userstamp_column)
end
update_without_userstamps
end
Expand Down

0 comments on commit bf05c3e

Please sign in to comment.