katsudo 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -18,7 +18,66 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
First create model by hand: STI and Polymorphic
|
22
|
+
|
23
|
+
$ rails generate model Activity type:string resource_type:string resource_id:integer user_type:string user_id:integer
|
24
|
+
$ rake db:migrate
|
25
|
+
|
26
|
+
Now define any users models to be a subject of activity:
|
27
|
+
|
28
|
+
class User < ActiveRecord::Base
|
29
|
+
activity_user!
|
30
|
+
end
|
31
|
+
|
32
|
+
class Admin < ActiveRecord::Base
|
33
|
+
activity_user!
|
34
|
+
end
|
35
|
+
|
36
|
+
Controller:
|
37
|
+
|
38
|
+
# application_controller.rb:
|
39
|
+
|
40
|
+
def activity_user
|
41
|
+
current_admin||current_user # an accessor to current activity subject. I use Devise and all helpers built in.
|
42
|
+
end
|
43
|
+
|
44
|
+
# common controller action:
|
45
|
+
|
46
|
+
def create
|
47
|
+
respond_to do |format|
|
48
|
+
if @comment.save
|
49
|
+
flash << activity_message(:create_object, @comment)
|
50
|
+
format.html { redirect_to [@article, @comment] }
|
51
|
+
else
|
52
|
+
format.html { render action: "new" }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
Model:
|
58
|
+
|
59
|
+
class CreateObject < Activity
|
60
|
+
def title
|
61
|
+
"Saved!"
|
62
|
+
end
|
63
|
+
|
64
|
+
def message
|
65
|
+
"#{resource.class.model_name.human} was created"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
Partial for activity:
|
70
|
+
|
71
|
+
# views/activities/create_object.html.erb
|
72
|
+
|
73
|
+
<strong><%= create_object.title %></strong>
|
74
|
+
<p><%= create_object.message %></p>
|
75
|
+
|
76
|
+
If you want store each activity, add in controller as any filter:
|
77
|
+
|
78
|
+
trace_activity :only => [:create, :update, :destroy]
|
79
|
+
|
80
|
+
|
22
81
|
|
23
82
|
## Contributing
|
24
83
|
|
data/lib/katsudo/dispatch.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
require "katsudo/dispatch/controller_extension"
|
2
|
-
require "katsudo/dispatch/
|
2
|
+
require "katsudo/dispatch/flash_stack"
|
3
3
|
|
4
4
|
module Katsudo
|
5
5
|
module Dispatch
|
6
|
-
ActionDispatch::Flash::FlashHash.send(:include,
|
7
|
-
ActionDispatch::Flash::FlashNow.send(:include, FlashExtension)
|
6
|
+
ActionDispatch::Flash::FlashHash.send(:include, FlashHashExtension)
|
8
7
|
end
|
9
8
|
end
|
@@ -19,9 +19,6 @@ module Katsudo
|
|
19
19
|
|
20
20
|
included do
|
21
21
|
helper_method :#{name}, :#{activity_user}
|
22
|
-
if Rails.version >= "4.0.0"
|
23
|
-
add_flash_types(:messages)
|
24
|
-
end
|
25
22
|
end
|
26
23
|
|
27
24
|
module ClassMethods
|
@@ -55,7 +52,8 @@ module Katsudo
|
|
55
52
|
end
|
56
53
|
|
57
54
|
def #{name}_message(*args)
|
58
|
-
|
55
|
+
activity = #{name}(*args)
|
56
|
+
{activity.flash_key => render_to_string(activity)}
|
59
57
|
end
|
60
58
|
|
61
59
|
def last_#{name}
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Katsudo
|
2
|
+
module Dispatch
|
3
|
+
|
4
|
+
class FlashStack
|
5
|
+
def initialize(flash)
|
6
|
+
@flash = flash
|
7
|
+
@stack = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def []=(k,v)
|
11
|
+
@stack[k] = Array(v)
|
12
|
+
end
|
13
|
+
|
14
|
+
def [](k)
|
15
|
+
@stack[k] ||= []
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_missing(*args, &block)
|
19
|
+
@stack.send(*args, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def use
|
23
|
+
@stack.dup.tap do
|
24
|
+
@stack.clear
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
module FlashHashExtension
|
31
|
+
extend ActiveSupport::Concern
|
32
|
+
|
33
|
+
included do
|
34
|
+
class_eval do
|
35
|
+
def empty?
|
36
|
+
@flashes.empty? && (@stack.nil? ? true : @stack.empty?)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def <<(*hashes)
|
42
|
+
hashes.each do |hash|
|
43
|
+
hash.each do |k,v|
|
44
|
+
Array(v).each do |value|
|
45
|
+
stack[k] << value
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def stack
|
52
|
+
@stack ||= FlashStack.new(self)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
data/lib/katsudo/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: katsudo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-05 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: User activity for Rails
|
15
15
|
email:
|
@@ -29,7 +29,7 @@ files:
|
|
29
29
|
- lib/katsudo/configuration.rb
|
30
30
|
- lib/katsudo/dispatch.rb
|
31
31
|
- lib/katsudo/dispatch/controller_extension.rb
|
32
|
-
- lib/katsudo/dispatch/
|
32
|
+
- lib/katsudo/dispatch/flash_stack.rb
|
33
33
|
- lib/katsudo/engine.rb
|
34
34
|
- lib/katsudo/models.rb
|
35
35
|
- lib/katsudo/models/activity.rb
|
@@ -1,44 +0,0 @@
|
|
1
|
-
module Katsudo
|
2
|
-
module Dispatch
|
3
|
-
|
4
|
-
class Messages
|
5
|
-
class Item
|
6
|
-
attr_accessor :type, :text
|
7
|
-
def initialize(type, text)
|
8
|
-
@type = type.to_sym
|
9
|
-
@text = text.to_s
|
10
|
-
end
|
11
|
-
|
12
|
-
def inspect
|
13
|
-
@text
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
attr_reader :scope
|
18
|
-
delegate :any?, :many?, :present?, :each, :map, :collect, :group_by, :to => :scope
|
19
|
-
|
20
|
-
def initialize
|
21
|
-
@scope = []
|
22
|
-
end
|
23
|
-
|
24
|
-
def add(type, text)
|
25
|
-
@scope << Item.new(type, text)
|
26
|
-
end
|
27
|
-
|
28
|
-
def grouped
|
29
|
-
group_by(&:type)
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
module FlashExtension
|
35
|
-
extend ActiveSupport::Concern
|
36
|
-
|
37
|
-
def messages
|
38
|
-
self[:messages] ||= Messages.new
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|