Coder Social home page Coder Social logo

shanlalit / userstamp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from current-rms/userstamp

1.0 0.0 0.0 153 KB

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: MIT License

userstamp's Introduction

Userstamp

Fork Details

This is a fork of the grosser fork [github.com/grosser/userstamp] of the original userstamp plugin created by delynn [github.com/delynn/userstamp].

This fork has been created to combine the grosser changes that enable use of the plugin within applications that perform soft deletes but are not using the acts_as_paranoid plugin/gem (for example this fork can now be used in conjunction with the rails3_acts_as_paranoid plugin/gem [github.com/goncalossilva/rails3_acts_as_paranoid]).

This fork also includes changes to perform the model stamping before validation is performed so that the model can enforce the presence of stamp attribute values eg.

validates :created_by, :presence => true
validates :updated_by, :presence => true

Using the Fork

To use this fork add the following to your application gemfile:

gem 'userstamp', :git => "git://github.com/insphire/userstamp.git"

Overview

Userstamp extends ActiveRecord::Base to add automatic updating of ‘creator’, ‘updater’, and ‘deleter’ attributes. It is based loosely on the ActiveRecord::Timestamp module.

Two class methods (model_stamper and stampable) are implemented in this plugin. The model_stamper method is used in models that are responsible for creating, updating, or deleting other objects. The stampable method is used in models that are subject to being created, updated, or deleted by ‘stampers’.

Usage

The assumption is that you have two different categories of objects; those that manipulate, and those that are manipulated. For those objects that are being manipulated there’s the Stampable module and for the manipulators there’s the Stamper module. There’s also the actual Userstamp module for your controllers that assists in setting up your environment on a per request basis.

Example

Assume a weblog application has User and Post objects. # 1: Create the migrations for these objects

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users, :force => true do |t|
      ...
      t.userstamps # use t.userstamps(true) if you also want 'deleter_id'
    end
  end
  def self.down
    drop_table :users
  end
end
class CreatePosts < ActiveRecord::Migration
  def self.up
    create_table :posts, :force => true do |t|
      ...
      t.userstamps # use t.userstamps(true) if you also want 'deleter_id'
    end
  end
  def self.down
    drop_table :posts
  end
end

# 2: Users are going to manipulate Post’s, use the model_stamper:

class User < ActiveRecord::Base
  model_stamper
end

# 3: Set the current user in the ApplicationController:

class ApplicationController < ActionController::Base
  include Userstamp
end

If all you are interested in is making sure all tables that have the proper columns are stamped by the currently logged in user you can stop right here. More than likely you want all your associations setup on your stamped objects, and that’s where the stampable class method comes in. So in our example we’ll want to use this method in both our User and Post classes:

class User < ActiveRecord::Base
  model_stamper
  stampable
end
class Post < ActiveRecord::Base
  stampable
end

Okay, so what all have we done? The model_stamper class method injects two methods into the User class. They are #stamper= and #stamper and look like this:

def stamper=(object)
  object_stamper = if object.is_a?(ActiveRecord::Base)
    object.send("#{object.class.primary_key}".to_sym)
  else
    object
  end
  Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = object_stamper
end
def stamper
  Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"]
end

The stampable method allows you to customize what columns will get stamped, and also creates the creator, updater, and deleter associations.

The Userstamp module that we included into our ApplicationController uses the setter method to set which user is currently making the request. By default the ‘set_stampers’ method works perfectly with the RestfulAuthentication plug-in:

def set_stampers
  User.stamper = self.current_user
end

If you aren’t using ActsAsAuthenticated, then you need to create your own version of the set_stampers method in the controller where you’ve included the Userstamp module.

Now, let’s get back to the Stampable module (since it really is the interesting one). The Stampable module sets up before_* filters that are responsible for setting those attributes at the appropriate times. It also creates the belongs_to relationships for you.

If you need to customize the columns that are stamped, the stampable method can be completely customized. Here’s an quick example:

class Post < ActiveRecord::Base
  stampable :stamper_class_name => :person,
            :creator_attribute  => :create_user,
            :updater_attribute  => :update_user,
            :deleter_attribute  => :delete_user,

:deleter => true

end

Upgrade from 1.x

# config/environment.rb
Ddb::Userstamp.compatibility_mode = true

Example userstamp application

Running Unit Tests

All: rake
One: ruby test/compatibility_stamping_test.rb

Author

DeLynn Berry

The original idea for this plugin came from the Rails Wiki article entitled Extending ActiveRecord.

Contributors / maintenance / enhancement

userstamp's People

Contributors

chrisbranson avatar delynn avatar grosser avatar uge-developer avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.