Audit site using acts_as_audited plugin

Posted by Bhushan Ahire | Posted in acts_as_audited, Rails | Posted on 17-05-2008

0

acts_as_audited is an Active Record plugin that logs all modifications to your models in an audits table. It uses a polymorphic association to store an audit record for any of the model objects that you wish to have audited. The audit log stores the model that the change was on, the “action” (create, update, destroy), a serialzied hash of the changes, and optionally the user that performed the action.

Auditing in Rails

NOTE: read the caveats section if the following isn’t working.

If you’re using acts_as_audited within Rails, you can simply declare which models should be audited. acts_as_audited can also automatically record the user that made the change if your controller has a current_user method.

class ApplicationController < ActionController::Base

audit User, List, Item
protected
def current_user
@user ||= User.find(session[:user])

end
end

Caveats

Auditing with user support depends on Rails’ caching mechanisms, therefore auditing isn’t enabled during development mode. To test that auditing is working, start up your app in production mode, or change the following options in config/environments/development.rb:

config.cache_classes = true

config.action_controller.perform_caching = true

Customizing

To get auditing outside of Rails, or to customize which fields are audited within Rails, you can explicitly declare acts_as_audited on your models. The :except option allows you to specify one or more attributes that you don’t want to be saved in the audit log.

class User < ActiveRecord::Base
acts_as_audited :except => [:password, :credit_card_number]

end

Installation

You can grab the plugin by running:

script/plugin install http://source.collectiveidea.com/public/rails/plugins/acts_as_audited

Run the migration generator and migrate to add the audits table.

script/generate audited_migration add_audits_table
rake db:migrate

Upgrading

Those upgrading from version 0.2 need to add 2 fields the audits table:

add_column :audits, :user_type, :string
add_column :audits, :username, :string

May this information fulfills your requirement… ;-)

Sort array of ActiveRecord object without querying to database.

Posted by Bhushan Ahire | Posted in Rails, ruby | Posted on 13-05-2008

0

Sometimes it may happen you want to sort the array of objects, fetched from ActiveRecord.

e.g. I have a users table having name column after I done any manipulation on the fetched object.

You can just call the following code for doing the above stuff.

@users = User.find :all
@users = @users – xyz_obj.users #remove existing users.
@users = @users.sort_by { |user| user[:name] }

May this code helps anyone….

Thanks ;-)

New features in Rails 2

Posted by Bhushan Ahire | Posted in Rails | Posted on 13-05-2008

0

Today i was reading about the new features of Rails 2, there are a lot of changes, for an overview you can checkout the official rails blog announcement. Here is a little list of major changes and new features:

How to raise and rescue Exceptions in Ruby on Rails

Posted by Bhushan Ahire | Posted in Rails, ruby | Posted on 09-05-2008

0

Creating the Exception class

To create our “PersonalException” we just need to extend “Exception” class.


class PersonalException < Exception
end

Raise our Exception


raise PersonalException.new, "message"

Rescue our exception

We can do this by overwriting rescue_action_in_public and local_request? functions to our application.rb file:

Send mail after capistrano deployment

Posted by Bhushan Ahire | Posted in capistrano, Rails | Posted on 07-05-2008

0

I got a very good plugin at http://code.google.com/p/capistrano-mailer/.
Just copied the details below to have use in future.

It is a Capistrano Plugin AND a Rails Plugin

Ever wanted to be emailed whenever someone on the team does a cap deploy of trunk or some tag to some server. Wouldn’t it be nice to know about it every time a release was deployed? For large rails projects this type of coordination is essential, and this plugin makes sure everyone on the need to know list is notified when something new is deployed.

This plugin is an extension to Capistrano.

That means it registers itself with Capistrano as a plugin and is therefore available to call in your recipes.

If you are looking to roll your own email integration into capistrano then try this pastie: http://pastie.org/146264 (thanks to Mislav Marohni?). But if you want to take the easy road to riches then keep reading ;)

– figurative “riches” of course, I promise nothing in return for your using this plugin

Requirements

  • Rails 2.0.2
  • Capistrano 2.1.0 – 2.2.0

Installation

./script/plugin install http://capistrano-mailer.googlecode.com/svn/trunk/capistrano_mailer

Usage

1. Install the plugin.

2. Add this line to the top of your deploy.rb:

require 'vendor/plugins/capistrano_mailer/lib/capistrano_mailer'

3. Add a cap_mailer_settings.rb file to your config/ directory:

require 'vendor/plugins/capistrano_mailer/lib/cap_mailer'

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address        => "mail.default.com",
:port           => 25,
:domain         => 'default.com',
:perform_deliveries => true,
:user_name      => "releases@default.com",
:password       => "mypassword",
:authentication => :login }
ActionMailer::Base.default_charset = "latin1"

CapMailer.template_root = "vendor/plugins/capistrano_mailer/views/"
CapMailer.recipient_addresses = ["dev1@default.com"]
CapMailer.sender_address = %("Capistrano Deployment" <releases@default.com>)
CapMailer.email_prefix = "[MYSITE-CAP-DEPLOY]"
CapMailer.site_name = "MySite.com"
CapMailer.email_content_type = "text/html"

4. Add these two tasks to your deploy.rb:

namespace :show do
desc "Show some internal Cap-Fu: What's mah NAYM?!?"
task :me do
set :command, task_call_frames.first.task.fully_qualified_name
puts "Running #{command} task"
end
end

namespace :deploy do
...

desc "Send email notification of deployment"
task :notify, :roles => :app do
show.me
mailer.send(rails_env, repository, command, deploy_to, host)
end

...
end

Make sure you’ve defined rails_env, repository, deploy_to and host. command is defined by the show:me task above.

The only required parameters to mailer.send are rails_env, repository, command and deploy_to. The complete set of possible parameters is:

mailer.send(rails_env, repository, command, deploy_to, host = nil, ip_address = nil, output = nil)

If anyone has a cool way of recording the output into a capistrano accessible variable, so that it can be shoved into the release email that would be an excellent contribution!

5. Then add the hook somewhere in your deploy.rb:

after "deploy", "deploy:notify"

6. Enjoy and Happy Capping!

Move Email and Attachments to Storage Card

Posted by Bhushan Ahire | Posted in Pocket PC, WM6 | Posted on 06-05-2008

0

You can free up even more memory on your phone by moving all of your downloaded mail and attachments to your Storage Card.

Updated for Windows Mobile 6!

01 Using the file manager in activesync, create the following folders on your Storage Card.

MAPI
MAPIATTACHMENTS

You can actually name them whatever you want, “Inbox” for example.

02 Now browse to WindowsMessaging (SmartPhone) or WindowsMAPI (PocketPC)

If you want to save all of your existing mail, move everything in this directory to the new ones on your Storage Card.

03 Now fire up your registry editor and navigate to the following.

For WM5 Phones: [ HKEY_CURRENT_USERSoftwareMicrosoftMAPI ]For WM6 Phones: [ HKEY_LOCAL_MACHINESystemInboxSettings ]

Then create the following STRING VALUES.

Name: AttachPath
String: Storage CardMAPIAttachments
Name: PropertyPath
String: Storage CardMAPI

04 Reboot your phone and then goto your inbox. If you can see all of your mail then mission accomplished. Double check your attachments as well.