wfl_simple_activity 0.1.25 → 0.1.26
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.
- checksums.yaml +4 -4
- data/lib/public_activity.rb +70 -0
- data/lib/public_activity/.DS_Store +0 -0
- data/lib/public_activity/actions/creation.rb +19 -0
- data/lib/public_activity/actions/destruction.rb +19 -0
- data/lib/public_activity/actions/update.rb +20 -0
- data/lib/public_activity/activity.rb +8 -0
- data/lib/public_activity/common.rb +363 -0
- data/lib/public_activity/config.rb +102 -0
- data/lib/public_activity/models/activist.rb +11 -0
- data/lib/public_activity/models/activity.rb +6 -0
- data/lib/public_activity/models/adapter.rb +7 -0
- data/lib/public_activity/models/trackable.rb +11 -0
- data/lib/public_activity/orm/.DS_Store +0 -0
- data/lib/public_activity/orm/active_record.rb +7 -0
- data/lib/public_activity/orm/active_record/activist.rb +35 -0
- data/lib/public_activity/orm/active_record/activity.rb +66 -0
- data/lib/public_activity/orm/active_record/adapter.rb +23 -0
- data/lib/public_activity/orm/active_record/trackable.rb +17 -0
- data/lib/public_activity/orm/mongo_mapper.rb +6 -0
- data/lib/public_activity/orm/mongo_mapper/activist.rb +36 -0
- data/lib/public_activity/orm/mongo_mapper/activity.rb +35 -0
- data/lib/public_activity/orm/mongo_mapper/adapter.rb +19 -0
- data/lib/public_activity/orm/mongo_mapper/trackable.rb +13 -0
- data/lib/public_activity/orm/mongoid.rb +6 -0
- data/lib/public_activity/orm/mongoid/activist.rb +36 -0
- data/lib/public_activity/orm/mongoid/activity.rb +34 -0
- data/lib/public_activity/orm/mongoid/adapter.rb +19 -0
- data/lib/public_activity/orm/mongoid/trackable.rb +13 -0
- data/lib/public_activity/renderable.rb +166 -0
- data/lib/public_activity/roles/deactivatable.rb +44 -0
- data/lib/public_activity/roles/tracked.rb +196 -0
- data/lib/public_activity/testing.rb +37 -0
- data/lib/public_activity/utility/store_controller.rb +32 -0
- data/lib/public_activity/utility/view_helpers.rb +30 -0
- data/lib/public_activity/version.rb +6 -0
- data/lib/wfl_simple_activity.rb +0 -1
- data/lib/wfl_simple_activity/version.rb +1 -1
- metadata +36 -2
- data/lib/wfl_simple_activity/common_activity.rb +0 -23
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'singleton'
|
|
4
|
+
|
|
5
|
+
module PublicActivity
|
|
6
|
+
# Class used to initialize configuration object.
|
|
7
|
+
class Config
|
|
8
|
+
include ::Singleton
|
|
9
|
+
|
|
10
|
+
# Evaluates given block to provide DSL configuration.
|
|
11
|
+
# @example Initializer for Rails
|
|
12
|
+
# PublicActivity::Config.set do
|
|
13
|
+
# orm :mongo_mapper
|
|
14
|
+
# enabled false
|
|
15
|
+
# table_name "activities"
|
|
16
|
+
# end
|
|
17
|
+
def self.set &block
|
|
18
|
+
b = Block.new
|
|
19
|
+
b.instance_eval(&block)
|
|
20
|
+
instance
|
|
21
|
+
orm(b.orm) unless b.orm.nil?
|
|
22
|
+
enabled(b.enabled) unless b.enabled.nil?
|
|
23
|
+
table_name(b.table_name) unless b.table_name.nil?
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# alias for {#orm}
|
|
27
|
+
# @see #orm
|
|
28
|
+
def self.orm=(orm = nil)
|
|
29
|
+
orm(orm)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# alias for {#enabled}
|
|
33
|
+
# @see #enabled
|
|
34
|
+
def self.enabled=(en = nil)
|
|
35
|
+
enabled(en)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# instance version of {Config#orm}
|
|
39
|
+
# @see Config#orm
|
|
40
|
+
def orm(orm=nil)
|
|
41
|
+
self.class.orm(orm)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# instance version of {Config#table_name}
|
|
45
|
+
# @see Config#orm
|
|
46
|
+
def table_name(name = nil)
|
|
47
|
+
self.class.table_name(name)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# instance version of {Config#enabled}
|
|
51
|
+
# @see Config#orm
|
|
52
|
+
def enabled(en = nil)
|
|
53
|
+
self.class.enabled(en)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Set the ORM for use by PublicActivity.
|
|
57
|
+
def self.orm(orm = nil)
|
|
58
|
+
if orm.nil?
|
|
59
|
+
Thread.current[:public_activity_orm] || :active_record
|
|
60
|
+
else
|
|
61
|
+
Thread.current[:public_activity_orm] = orm.to_sym
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def self.table_name(name = nil)
|
|
66
|
+
if name.nil?
|
|
67
|
+
Thread.current[:public_activity_table_name] || "activities"
|
|
68
|
+
else
|
|
69
|
+
Thread.current[:public_activity_table_name] = name
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def self.enabled(en = nil)
|
|
74
|
+
if en.nil?
|
|
75
|
+
value = Thread.current[:public_activity_enabled]
|
|
76
|
+
value.nil? ? true : value
|
|
77
|
+
else
|
|
78
|
+
Thread.current[:public_activity_enabled] = en
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Provides simple DSL for the config block.
|
|
83
|
+
class Block
|
|
84
|
+
# @see Config#orm
|
|
85
|
+
def orm(orm = nil)
|
|
86
|
+
@orm = (orm ? orm.to_sym : false) || @orm
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Decides whether to enable PublicActivity.
|
|
90
|
+
# @param en [Boolean] Enabled?
|
|
91
|
+
def enabled(en = nil)
|
|
92
|
+
@enabled = (en.nil? ? @enabled : en)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Sets the table_name
|
|
96
|
+
# for the model
|
|
97
|
+
def table_name(name = nil)
|
|
98
|
+
@table_name = (name.nil? ? @table_name : name)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PublicActivity
|
|
4
|
+
# Provides helper methods for selecting activities from a user.
|
|
5
|
+
module Activist
|
|
6
|
+
# Delegates to configured ORM.
|
|
7
|
+
def self.included(base)
|
|
8
|
+
base.extend PublicActivity::inherit_orm("Activist")
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PublicActivity
|
|
4
|
+
# Provides association for activities bound to this object by *trackable*.
|
|
5
|
+
module Trackable
|
|
6
|
+
# Delegates to ORM.
|
|
7
|
+
def self.included(base)
|
|
8
|
+
base.extend PublicActivity::inherit_orm("Trackable")
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
Binary file
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PublicActivity
|
|
4
|
+
module ORM
|
|
5
|
+
module ActiveRecord
|
|
6
|
+
# Module extending classes that serve as owners
|
|
7
|
+
module Activist
|
|
8
|
+
# Adds ActiveRecord associations to model to simplify fetching
|
|
9
|
+
# so you can list activities performed by the owner.
|
|
10
|
+
# It is completely optional. Any model can be an owner to an activity
|
|
11
|
+
# even without being an explicit activist.
|
|
12
|
+
#
|
|
13
|
+
# == Usage:
|
|
14
|
+
# In model:
|
|
15
|
+
#
|
|
16
|
+
# class User < ActiveRecord::Base
|
|
17
|
+
# include PublicActivity::Model
|
|
18
|
+
# activist
|
|
19
|
+
# end
|
|
20
|
+
#
|
|
21
|
+
# In controller:
|
|
22
|
+
# User.first.activities
|
|
23
|
+
#
|
|
24
|
+
def activist
|
|
25
|
+
has_many :activities_as_owner,
|
|
26
|
+
:class_name => "::PublicActivity::Activity",
|
|
27
|
+
:as => :owner
|
|
28
|
+
has_many :activities_as_recipient,
|
|
29
|
+
:class_name => "::PublicActivity::Activity",
|
|
30
|
+
:as => :recipient
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PublicActivity
|
|
4
|
+
|
|
5
|
+
if not defined? ::PG::ConnectionBad
|
|
6
|
+
module ::PG
|
|
7
|
+
class ConnectionBad < Exception; end
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
if not defined? Mysql2::Error::ConnectionError
|
|
11
|
+
module Mysql2
|
|
12
|
+
module Error
|
|
13
|
+
class ConnectionError < Exception; end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
module ORM
|
|
19
|
+
module ActiveRecord
|
|
20
|
+
# The ActiveRecord model containing
|
|
21
|
+
# details about recorded activity.
|
|
22
|
+
class Activity < ::ActiveRecord::Base
|
|
23
|
+
include Renderable
|
|
24
|
+
self.table_name = PublicActivity.config.table_name
|
|
25
|
+
self.abstract_class = true
|
|
26
|
+
|
|
27
|
+
# Define polymorphic association to the parent
|
|
28
|
+
belongs_to :trackable, :polymorphic => true, optional: true
|
|
29
|
+
|
|
30
|
+
case ::ActiveRecord::VERSION::MAJOR
|
|
31
|
+
when 3..4
|
|
32
|
+
# Define ownership to a resource responsible for this activity
|
|
33
|
+
belongs_to :owner, :polymorphic => true, optional: true
|
|
34
|
+
# Define ownership to a resource targeted by this activity
|
|
35
|
+
belongs_to :recipient, :polymorphic => true, optional: true
|
|
36
|
+
when 5..6
|
|
37
|
+
with_options(:required => false) do
|
|
38
|
+
# Define ownership to a resource responsible for this activity
|
|
39
|
+
belongs_to :owner, :polymorphic => true, optional: true
|
|
40
|
+
# Define ownership to a resource targeted by this activity
|
|
41
|
+
belongs_to :recipient, :polymorphic => true, optional: true
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Serialize parameters Hash
|
|
46
|
+
begin
|
|
47
|
+
if table_exists?
|
|
48
|
+
serialize :parameters, Hash unless [:json, :jsonb, :hstore].include?(columns_hash['parameters'].type)
|
|
49
|
+
else
|
|
50
|
+
warn("[WARN] table #{name} doesn't exist. Skipping PublicActivity::Activity#parameters's serialization")
|
|
51
|
+
end
|
|
52
|
+
rescue ::ActiveRecord::NoDatabaseError => e
|
|
53
|
+
warn("[WARN] database doesn't exist. Skipping PublicActivity::Activity#parameters's serialization")
|
|
54
|
+
rescue ::PG::ConnectionBad => e
|
|
55
|
+
warn("[WARN] couldn't connect to database. Skipping PublicActivity::Activity#parameters's serialization")
|
|
56
|
+
rescue Mysql2::Error::ConnectionError
|
|
57
|
+
warn("[WARN] couldn't connect to database. Skipping PublicActivity::Activity#parameters's serialization")
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
if ::ActiveRecord::VERSION::MAJOR < 4 || defined?(ProtectedAttributes)
|
|
61
|
+
attr_accessible :key, :owner, :parameters, :recipient, :trackable
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PublicActivity
|
|
4
|
+
module ORM
|
|
5
|
+
# Support for ActiveRecord for PublicActivity. Used by default and supported
|
|
6
|
+
# officialy.
|
|
7
|
+
module ActiveRecord
|
|
8
|
+
# Provides ActiveRecord specific, database-related routines for use by
|
|
9
|
+
# PublicActivity.
|
|
10
|
+
class Adapter
|
|
11
|
+
# Creates the activity on `trackable` with `options`
|
|
12
|
+
def self.create_activity(trackable, options)
|
|
13
|
+
trackable.activities.create options
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Creates activity on `trackable` with `options`; throws error on validation failure
|
|
17
|
+
def self.create_activity!(trackable, options)
|
|
18
|
+
trackable.activities.create! options
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PublicActivity
|
|
4
|
+
module ORM
|
|
5
|
+
module ActiveRecord
|
|
6
|
+
# Implements {PublicActivity::Trackable} for ActiveRecord
|
|
7
|
+
# @see PublicActivity::Trackable
|
|
8
|
+
module Trackable
|
|
9
|
+
# Creates an association for activities where self is the *trackable*
|
|
10
|
+
# object.
|
|
11
|
+
def self.extended(base)
|
|
12
|
+
base.has_many :activities, :class_name => "::PublicActivity::Activity", :as => :trackable
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PublicActivity
|
|
4
|
+
module ORM
|
|
5
|
+
module MongoMapper
|
|
6
|
+
# Module extending classes that serve as owners
|
|
7
|
+
module Activist
|
|
8
|
+
# Adds MongoMapper associations to model to simplify fetching
|
|
9
|
+
# so you can list activities performed by the owner.
|
|
10
|
+
# It is completely optional. Any model can be an owner to an activity
|
|
11
|
+
# even without being an explicit activist.
|
|
12
|
+
#
|
|
13
|
+
# == Usage:
|
|
14
|
+
# In model:
|
|
15
|
+
#
|
|
16
|
+
# class User
|
|
17
|
+
# include MongoMapper::Document
|
|
18
|
+
# include PublicActivity::Model
|
|
19
|
+
# activist
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# In controller:
|
|
23
|
+
# User.first.activities
|
|
24
|
+
#
|
|
25
|
+
def activist
|
|
26
|
+
many :activities_as_owner,
|
|
27
|
+
:class_name => "::PublicActivity::Activity",
|
|
28
|
+
:as => :owner
|
|
29
|
+
many :activities_as_recipient,
|
|
30
|
+
:class_name => "::PublicActivity::Activity",
|
|
31
|
+
:as => :recipient
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'mongo_mapper'
|
|
4
|
+
require 'active_support/core_ext'
|
|
5
|
+
|
|
6
|
+
module PublicActivity
|
|
7
|
+
module ORM
|
|
8
|
+
module MongoMapper
|
|
9
|
+
# The MongoMapper document containing
|
|
10
|
+
# details about recorded activity.
|
|
11
|
+
class Activity
|
|
12
|
+
include ::MongoMapper::Document
|
|
13
|
+
include Renderable
|
|
14
|
+
|
|
15
|
+
class SymbolHash < Hash
|
|
16
|
+
def self.from_mongo(value)
|
|
17
|
+
value.symbolize_keys unless value.nil?
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Define polymorphic association to the parent
|
|
22
|
+
belongs_to :trackable, polymorphic: true
|
|
23
|
+
# Define ownership to a resource responsible for this activity
|
|
24
|
+
belongs_to :owner, polymorphic: true
|
|
25
|
+
# Define ownership to a resource targeted by this activity
|
|
26
|
+
belongs_to :recipient, polymorphic: true
|
|
27
|
+
|
|
28
|
+
key :key, String
|
|
29
|
+
key :parameters, SymbolHash
|
|
30
|
+
|
|
31
|
+
timestamps!
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PublicActivity
|
|
4
|
+
module ORM
|
|
5
|
+
module MongoMapper
|
|
6
|
+
class Adapter
|
|
7
|
+
# Creates the activity on `trackable` with `options`
|
|
8
|
+
def self.create_activity(trackable, options)
|
|
9
|
+
trackable.activities.create options
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Creates activity on `trackable` with `options`; throws error on validation failure
|
|
13
|
+
def self.create_activity!(trackable, options)
|
|
14
|
+
trackable.activities.create! options
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PublicActivity
|
|
4
|
+
module ORM
|
|
5
|
+
module MongoMapper
|
|
6
|
+
module Trackable
|
|
7
|
+
def self.extended(base)
|
|
8
|
+
base.many :activities, :class_name => "::PublicActivity::Activity", order: :created_at.asc, :as => :trackable
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PublicActivity
|
|
4
|
+
module ORM
|
|
5
|
+
module Mongoid
|
|
6
|
+
# Module extending classes that serve as owners
|
|
7
|
+
module Activist
|
|
8
|
+
# Adds ActiveRecord associations to model to simplify fetching
|
|
9
|
+
# so you can list activities performed by the owner.
|
|
10
|
+
# It is completely optional. Any model can be an owner to an activity
|
|
11
|
+
# even without being an explicit activist.
|
|
12
|
+
#
|
|
13
|
+
# == Usage:
|
|
14
|
+
# In model:
|
|
15
|
+
#
|
|
16
|
+
# class User < ActiveRecord::Base
|
|
17
|
+
# include PublicActivity::Model
|
|
18
|
+
# activist
|
|
19
|
+
# end
|
|
20
|
+
#
|
|
21
|
+
# In controller:
|
|
22
|
+
# User.first.activities
|
|
23
|
+
#
|
|
24
|
+
def activist
|
|
25
|
+
has_many :activities_as_owner,
|
|
26
|
+
:class_name => "::PublicActivity::Activity",
|
|
27
|
+
:inverse_of => :owner
|
|
28
|
+
|
|
29
|
+
has_many :activities_as_recipient,
|
|
30
|
+
:class_name => "::PublicActivity::Activity",
|
|
31
|
+
:inverse_of => :recipient
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|