acts_as_loggable 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in acts_as_loggable.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,39 @@
1
+ Acts as Loggable
2
+ ================
3
+ Simplifies the process of logging activities.
4
+
5
+ Prepare database
6
+ ================
7
+ ruby script/generate log_activity_files # This command will copy 3 files: helper file, RecentActivity model, and a migration file.
8
+ rake db:migrate
9
+
10
+ Example
11
+ =======
12
+ Lets log the signup process and edit profile process
13
+ Add these lines to user.rb:
14
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
15
+
16
+
17
+ acts_as_loggable
18
+
19
+ after_create :create_signup_activity
20
+ after_save :create_profile_updated_activity
21
+
22
+ def create_signup_activity
23
+ # Here '1' denotes the type of the activity i.e. user creation.
24
+ # Also :user denotes the object who creates the activity.
25
+ create_recent_activity(:user => self, :activity_type => 1)
26
+ end
27
+
28
+ def create_profile_updated_activity
29
+ if name_changed? || city_changed? || country_changed? # Create the recent activity only the any of these fields have changed.
30
+ # Here '2' denotes the type of the activity i.e. edit profile.
31
+ # If the user further updates his profile, no new record will be created. But it will simply update the time_stamp field of this record.
32
+ create_recent_activity(:user => self, :activity_type => 2, :disable_further_logging_for => 15.minutes)
33
+ end
34
+ end
35
+
36
+
37
+
38
+
39
+ Copyright (c) 2009 [Arun Kumar], released under the MIT license
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "acts_as_loggable/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "acts_as_loggable"
7
+ s.version = ActsAsLoggable::VERSION
8
+ s.authors = ["Arun Kumar Arjunan"]
9
+ s.email = ["arun.immanuel1608@gmail"]
10
+ s.homepage = ""
11
+ s.summary = %q{Create and display activity feeds easily}
12
+ s.description = %q{Create and display activity feeds easily}
13
+
14
+ s.rubyforge_project = "acts_as_loggable"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,10 @@
1
+ class LogActivityFilesGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.file "recent_activities_helper.rb", "app/helpers/recent_activities_helper.rb"
5
+ m.file "recent_activity.rb", "app/models/recent_activity.rb"
6
+ m.migration_template 'migration.rb', 'db/migrate', :migration_file_name => 'create_recent_activities'
7
+ end
8
+ end
9
+
10
+ end
@@ -0,0 +1,15 @@
1
+ class CreateRecentActivities < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :recent_activities do |t|
4
+ t.integer :user_id, :null => false
5
+ t.integer :activity_type, :null => false
6
+ t.references :onobject, :polymorphic => true
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+
12
+ def self.down
13
+ drop_table :recent_activities
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ module RecentActivitiesHelper
2
+
3
+ # Displays the recent activity string for the 'recent_activity'
4
+ def recent_activity_string_for(recent_activity, viewer = current_user, options = {})
5
+ # Uncomment the following lines
6
+ # viewer_name = viewer.full_name
7
+ # time = recent_activity.created_at(:yMd)
8
+ # case recent_activity.for_object
9
+ # when
10
+ # "#{viewer_name} updated his profile on #{time}"
11
+ # end
12
+ end
13
+ end
@@ -0,0 +1,4 @@
1
+ class RecentActivity < ActiveRecord::Base
2
+ belongs_to :onobject, :polymorphic => true
3
+ belongs_to :user
4
+ end
@@ -0,0 +1,50 @@
1
+ require "acts_as_loggable/version"
2
+
3
+ module ActiveRecord #:nodoc:
4
+ module ActsAsLoggable #:nodoc:
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ def acts_as_loggable
11
+ has_many :recent_activities, :as => :onobject, :dependent => :destroy, :order => 'recent_activities.id DESC'
12
+
13
+ include ActiveRecord::ActsAsLoggable::InstanceMethods
14
+ end
15
+
16
+ end
17
+
18
+
19
+ module InstanceMethods
20
+ # Creates the recent activity record for the object.
21
+ #
22
+ # Compulsory Params:
23
+ # =============
24
+ # => user - Who creates this activity
25
+ # => activity_type - Type of the activity[Integer]
26
+ #
27
+ # Optional Params:
28
+ # =============
29
+ # => disable_further_logging_for - Ex: 15.minutes, No more record creation for further 15 minutes. But will update the updated at, and created_at field to the current Time.
30
+ #
31
+ def create_recent_activity(options = {})
32
+ user = options[:user]
33
+ disable_period = options.delete(:disable_further_logging_for)
34
+ if disable_period
35
+ activity = self.recent_activities.last(:conditions => ["user_id = ? AND activity_type = ? AND created_at > ?", user.id, options[:activity_type], Time.now.utc - disable_period])
36
+ if activity
37
+ activity.destroy
38
+ end
39
+ end
40
+
41
+ activity = RecentActivity.create(options)
42
+ self.recent_activities << activity
43
+ return activity
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+
50
+ ActiveRecord::Base.send(:include, ActiveRecord::ActsAsLoggable)
@@ -0,0 +1,3 @@
1
+ module ActsAsLoggable
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_loggable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Arun Kumar Arjunan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-06-30 00:00:00.000000000 +05:30
13
+ default_executable:
14
+ dependencies: []
15
+ description: Create and display activity feeds easily
16
+ email:
17
+ - arun.immanuel1608@gmail
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - README
25
+ - Rakefile
26
+ - acts_as_loggable.gemspec
27
+ - generators/log_activity_files/log_activity_files_generator.rb
28
+ - generators/log_activity_files/templates/migration.rb
29
+ - generators/log_activity_files/templates/recent_activities_helper.rb
30
+ - generators/log_activity_files/templates/recent_activity.rb
31
+ - lib/acts_as_loggable.rb
32
+ - lib/acts_as_loggable/version.rb
33
+ has_rdoc: true
34
+ homepage: ''
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project: acts_as_loggable
54
+ rubygems_version: 1.6.2
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Create and display activity feeds easily
58
+ test_files: []