activity_engine 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dd4228050d5a42c2041d1887fc5190c8f674e835
4
- data.tar.gz: 0d2eca2469b07a9c17395d7e2336c97b44c11316
3
+ metadata.gz: fe36940c0dacb58105501173923de40fd93f05c3
4
+ data.tar.gz: d4895b0c5e7ed3e764e0061e9d076286d869d89d
5
5
  SHA512:
6
- metadata.gz: d429da6d161dc74357cfd57d14646b5f1191572f37f7a4373046ccef7c82b8ebebcd7af8cf01c13b3a43fef49fd64c178739eac97215b8f0dd11516ae395d4b2
7
- data.tar.gz: f4246549a5c52a4b8ae6af8dd6fb2f1f861f5160bcfe6270df577ee64c24929155d3dab913131fd66cc0b97af7e2a05d0262cd5076e81a84ec5e498e39666060
6
+ metadata.gz: 596cd8e2e546555d3cb3ad035459344d32f12e07149b344487b5c1800fc28b1ccb57d34179766f85d09595cc830cab62d76963c020dcbf0f8089deb253fbc8f2
7
+ data.tar.gz: aede8f94b3cd189e83a6fd7110d3d7e7e42c097acd840386f2f26424d05ef3289a07b9c1f09597b5b035b718b074c4eb7e732911e51a12ee47b4a31b4e88e96e
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.0.0"
5
+
6
+ script: "bundle exec rake"
7
+
8
+ notifications:
9
+ irc: "irc.freenode.org#ndlib"
10
+
11
+ before_install:
12
+ - gem install bundler
data/Gemfile CHANGED
@@ -16,10 +16,18 @@ gem "jquery-rails"
16
16
  # To use debugger
17
17
  # gem 'debugger'
18
18
 
19
- group :test do
20
- gem 'simplecov', require: false
21
- gem "guard"
19
+
20
+ group :test, :development do
21
+ gem 'guard'
22
22
  gem "guard-rspec"
23
23
  gem 'guard-bundler'
24
+ gem 'guard-livereload'
25
+ gem 'highline'
26
+ gem 'rb-inotify', require: false
27
+ gem 'rb-fsevent', require: false if RUBY_PLATFORM =~ /darwin/i
28
+ gem 'rb-fchange', require: false
29
+ gem 'terminal-notifier-guard' if RUBY_PLATFORM =~ /darwin/i
30
+ gem 'ruby_gntp'
31
+ gem 'simplecov', require: false
24
32
  gem 'database_cleaner'
25
33
  end
data/Guardfile CHANGED
@@ -14,3 +14,5 @@ guard 'bundler' do
14
14
  # Uncomment next line if Gemfile contain `gemspec' command
15
15
  watch(/^.+\.gemspec/)
16
16
  end
17
+
18
+ notification :gntp
data/Rakefile CHANGED
@@ -9,3 +9,19 @@ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
9
9
  load 'rails/tasks/engine.rake'
10
10
 
11
11
  Bundler::GemHelper.install_tasks
12
+
13
+ task :spec do
14
+ require 'rspec/core/rake_task'
15
+ ENV['RAILS_ENV'] = 'test'
16
+ Rails.env = 'test'
17
+ Rake::Task["db:drop"].invoke rescue true
18
+ Rake::Task["db:create"].invoke
19
+ Rake::Task['environment'].invoke
20
+ Rake::Task['db:schema:load'].invoke
21
+ RSpec::Core::RakeTask.new(:__spec) do |t|
22
+ t.pattern = "./spec/**/*_spec.rb"
23
+ end
24
+ Rake::Task['__spec'].invoke
25
+ end
26
+
27
+ task default: :spec
@@ -21,6 +21,19 @@ module ActivityEngine
21
21
  scope :ascending_date, lambda { order("created_at ASC") }
22
22
  scope :descending_date, lambda { order("created_at DESC") }
23
23
 
24
+ scope :for_stats, lambda {
25
+ group("#{quoted_table_name}.activity_type").
26
+ select("#{quoted_table_name}.activity_type,
27
+ MAX(#{quoted_table_name}.created_at) AS last_activity_date,
28
+ COUNT(#{quoted_table_name}.id) AS count"
29
+ )
30
+ }
31
+
32
+ def self.statistics_for_subject(subject)
33
+ for_subject(subject).for_stats.each_with_object([]) {|stat, collector|
34
+ collector << ActivityEngine::Statistic.new(subject,stat.attributes)
35
+ }
36
+ end
24
37
 
25
38
  belongs_to :user
26
39
 
@@ -0,0 +1,12 @@
1
+ module ActivityEngine
2
+ class Statistic
3
+ attr_accessor :activity_type, :count, :last_activity_date, :subject
4
+
5
+ def initialize(subject, opts)
6
+ @subject = subject
7
+ opts.each_pair do |key, value|
8
+ send("#{key}=", value)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,10 +1,8 @@
1
1
  module ActivityEngine
2
- if !defined?(ActivityDataStructure)
3
- ActivityDataStructure = Struct.new(
4
- :subject,
5
- :current_user,
6
- :message,
7
- :activity_type
8
- )
9
- end
10
- end
2
+ ActivityDataStructure = Struct.new(
3
+ :subject,
4
+ :current_user,
5
+ :message,
6
+ :activity_type
7
+ )
8
+ end
@@ -0,0 +1,39 @@
1
+ module ActivityEngine
2
+ class ActivitySweeper < ActionController::Caching::Sweeper
3
+ class << self
4
+ def observe(*models)
5
+ models.flatten!
6
+ models.collect! { |model| model.respond_to?(:to_sym) ? model.to_s.camelize.constantize : model }
7
+ models.each {|model|
8
+ model.send(:include, ActiveModel::Observing) unless model.included_modules.include?(ActiveModel::Observing)
9
+ }
10
+ models_to_observe = (observed_classes + models).uniq
11
+ singleton_class.redefine_method(:observed_classes) { models_to_observe }
12
+ end
13
+ end
14
+
15
+ attr_accessor :records_processed
16
+
17
+ def after(controller)
18
+ self.controller = controller
19
+ self.records_processed = []
20
+ callback(:after)
21
+ self.records_processed = []
22
+ self.controller = nil
23
+ true
24
+ end
25
+
26
+ def after_save(record)
27
+ self.records_processed ||= []
28
+ if ! self.records_processed.include?(record)
29
+ self.records_processed << record
30
+ ActivityEngine::Activity.new.tap {|activity|
31
+ activity.subject = record
32
+ activity.user = current_user
33
+ activity.activity_type = "#{controller_name}##{action_name}"
34
+ }.save!
35
+ end
36
+ true
37
+ end
38
+ end
39
+ end
@@ -5,13 +5,12 @@ module ActivityEngine
5
5
  config.generators do |g|
6
6
  g.test_framework :rspec, :fixture => false
7
7
  end
8
-
9
- extend ActiveSupport::Autoload
10
- eager_autoload do
11
- autoload :ContextBuilder
12
- autoload :ActivityBuilder
13
- autoload :ActivityDataStructure
14
- autoload :Activity
15
- end
16
8
  end
9
+
10
+ extend ActiveSupport::Autoload
11
+ autoload :ContextBuilder
12
+ autoload :ActivityBuilder
13
+ autoload :ActivityDataStructure
14
+ autoload :ActivitySweeper
15
+ autoload :Activity
17
16
  end
@@ -1,3 +1,3 @@
1
1
  module ActivityEngine
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -1,11 +1,17 @@
1
- require "activity_engine/engine"
2
- require 'activity_engine/context_builder'
3
- require 'activity_engine/activity_builder'
4
-
1
+ require 'activity_engine/engine'
5
2
  module ActivityEngine
6
3
  module_function
4
+ def register_models(*models)
5
+ ActivitySweeper.observe(*models)
6
+ end
7
+
8
+ def register_controller(controller_name, actions)
9
+ controller_name.constantize.module_exec(actions) do
10
+ cache_sweeper ActivitySweeper, only: actions
11
+ end
12
+ end
7
13
 
8
- def register(class_name, method_name, activity_receiver = ActivityEngine::Activity, &config_block)
14
+ def register(class_name, method_name, activity_receiver = Activity, &config_block)
9
15
  context_builder = ContextBuilder.new(class_name, method_name)
10
16
  activity_builder = ActivityBuilder.new(activity_receiver, config_block)
11
17
  context_builder.wrap!(activity_builder)
File without changes
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActivityEngine::ActivitySweeper do
4
+ class TestSweeper < ActivityEngine::ActivitySweeper
5
+ end
6
+
7
+ class Book
8
+ end
9
+
10
+ class Animal
11
+ end
12
+
13
+ describe '#observe' do
14
+ it 'should allow multiple registrations' do
15
+ expect {
16
+ TestSweeper.observe(:book)
17
+ }.to change{TestSweeper.observed_classes}.from([]).to([Book])
18
+
19
+ expect {
20
+ TestSweeper.observe(:animal)
21
+ }.to change{TestSweeper.observed_classes}.from([Book]).to([Book, Animal])
22
+ end
23
+ end
24
+ end
@@ -23,6 +23,32 @@ module ActivityEngine
23
23
  end
24
24
  end
25
25
 
26
+ describe '.statistics_for_subject' do
27
+ let(:stats_builder) {
28
+ {"hello" => 3, "good_bye" => 2}
29
+ }
30
+ before(:each) do
31
+ stats_builder.each do |activity_type, times|
32
+ (1..times).each {
33
+ ActivityEngine::Activity.create { |a|
34
+ a.subject = object; a.activity_type = activity_type
35
+ }
36
+ }
37
+ end
38
+ end
39
+
40
+ it 'returns an array of statistics' do
41
+ stats = ActivityEngine::Activity.statistics_for_subject(object)
42
+ expect(stats.size).to eq(stats_builder.size)
43
+
44
+ stats.each do |stat|
45
+ expected_count = stats_builder.fetch(stat.activity_type)
46
+ expect(stat.count).to eq(expected_count)
47
+ expect(stat).to respond_to(:last_activity_date)
48
+ end
49
+ end
50
+ end
51
+
26
52
  describe '#subject=' do
27
53
  describe 'with persisted object' do
28
54
  it 'should marshal the subject' do
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+ describe ActivityEngine::Statistic do
3
+ subject{
4
+ ActivityEngine::Statistic.new(object, opts)
5
+ }
6
+ let(:opts) { {activity_type: activity_type} }
7
+ let(:object){ Object.new }
8
+
9
+ let(:activity_type){ 'activity_type' }
10
+ it 'should have activity_type and subject when created' do
11
+ expect( subject.subject ).to eq object
12
+ end
13
+
14
+ it 'should assign activity type' do
15
+ expect( subject.activity_type ).to eq activity_type
16
+ end
17
+
18
+ end
data/spec/spec_helper.rb CHANGED
@@ -8,12 +8,12 @@ if ENV['COVERAGE']
8
8
  end
9
9
  end
10
10
 
11
-
12
11
  require File.expand_path("../dummy/config/environment.rb", __FILE__)
13
12
  require File.expand_path('../spec_patch', __FILE__)
14
13
  require "rails/test_help"
15
14
  require 'rspec/rails'
16
15
  require 'database_cleaner'
16
+ require 'activity_engine'
17
17
 
18
18
 
19
19
  Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each {|f| require f}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activity_engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Friesen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-23 00:00:00.000000000 Z
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -61,6 +61,7 @@ extra_rdoc_files: []
61
61
  files:
62
62
  - .gitignore
63
63
  - .rspec
64
+ - .travis.yml
64
65
  - Gemfile
65
66
  - Guardfile
66
67
  - LICENSE.txt
@@ -74,12 +75,14 @@ files:
74
75
  - app/controllers/activity_engine/application_controller.rb
75
76
  - app/helpers/activity_engine/application_helper.rb
76
77
  - app/models/activity_engine/activity.rb
78
+ - app/models/activity_engine/statistic.rb
77
79
  - app/views/layouts/activity_engine/application.html.erb
78
80
  - config/routes.rb
79
81
  - db/migrate/20130722162331_create_activity_engine_activities.rb
80
82
  - lib/activity_engine.rb
81
83
  - lib/activity_engine/activity_builder.rb
82
84
  - lib/activity_engine/activity_data_structure.rb
85
+ - lib/activity_engine/activity_sweeper.rb
83
86
  - lib/activity_engine/context_builder.rb
84
87
  - lib/activity_engine/engine.rb
85
88
  - lib/activity_engine/exceptions.rb
@@ -114,6 +117,7 @@ files:
114
117
  - spec/dummy/config/initializers/wrap_parameters.rb
115
118
  - spec/dummy/config/locales/en.yml
116
119
  - spec/dummy/config/routes.rb
120
+ - spec/dummy/db/.gitkeep
117
121
  - spec/dummy/db/schema.rb
118
122
  - spec/dummy/lib/assets/.gitkeep
119
123
  - spec/dummy/log/.gitkeep
@@ -124,9 +128,11 @@ files:
124
128
  - spec/dummy/script/rails
125
129
  - spec/lib/activity_engine/activity_builder_spec.rb
126
130
  - spec/lib/activity_engine/activity_data_structure_spec.rb
131
+ - spec/lib/activity_engine/activity_sweeper_spec.rb
127
132
  - spec/lib/activity_engine/context_builder_spec.rb
128
133
  - spec/lib/activity_engine_spec.rb
129
134
  - spec/models/activity_engine/activity_spec.rb
135
+ - spec/models/activity_engine/statistic_spec.rb
130
136
  - spec/spec_helper.rb
131
137
  - spec/spec_patch.rb
132
138
  - spec/support/persistence_layer.rb
@@ -180,6 +186,7 @@ test_files:
180
186
  - spec/dummy/config/initializers/wrap_parameters.rb
181
187
  - spec/dummy/config/locales/en.yml
182
188
  - spec/dummy/config/routes.rb
189
+ - spec/dummy/db/.gitkeep
183
190
  - spec/dummy/db/schema.rb
184
191
  - spec/dummy/lib/assets/.gitkeep
185
192
  - spec/dummy/log/.gitkeep
@@ -190,9 +197,11 @@ test_files:
190
197
  - spec/dummy/script/rails
191
198
  - spec/lib/activity_engine/activity_builder_spec.rb
192
199
  - spec/lib/activity_engine/activity_data_structure_spec.rb
200
+ - spec/lib/activity_engine/activity_sweeper_spec.rb
193
201
  - spec/lib/activity_engine/context_builder_spec.rb
194
202
  - spec/lib/activity_engine_spec.rb
195
203
  - spec/models/activity_engine/activity_spec.rb
204
+ - spec/models/activity_engine/statistic_spec.rb
196
205
  - spec/spec_helper.rb
197
206
  - spec/spec_patch.rb
198
207
  - spec/support/persistence_layer.rb