1 Store activity
1.1 Normal use
If you want to record information about a controller action and differentiate between successful action and failed action, you can do something like this:
if @book.save
store_activity :successful, @book
...
else
store_activity :error, @book
...
end
1.2 Personalized use
You can store activity with additional information:
if @book.save
store_activity :successful, @book, { :message => "Saved new book with title #{@book.title}" }
...
else
store_activity :error, @book, { :message => "Error saving book: #{@book.errors}" }
...
end
2 Register Activity. Doing it more DRY (after filter)
If you only have typical actions, you can use ‘register_activity’ statement that includes internally store_activity calls.
register_activity :create, :update, :destroy
This is equivalent to:
def create
...
if @book.save
store_activity :successful, @book
...
else
store_activity :error, @book
...
end
...
end
def update
...
if @book.update_attributes(params[:book]
store_activity :successful, @book
...
else
store_activity :error, @book
...
end
...
end
def destroy
...
if @book.destroy
store_activity :successful, @book
...
else
store_activity :error, @book
...
end
...
end
This statement must be called on the top of the controller