choo_choo 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.
Files changed (73) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +108 -0
  4. data/Rakefile +21 -0
  5. data/app/assets/javascripts/choo_choo/application.js +15 -0
  6. data/app/assets/stylesheets/choo_choo/activity_default.scss.css +17 -0
  7. data/app/assets/stylesheets/choo_choo/application.css +13 -0
  8. data/app/controllers/choo_choo/application_controller.rb +4 -0
  9. data/app/helpers/choo_choo/application_helper.rb +4 -0
  10. data/app/models/choo_choo/activity.rb +29 -0
  11. data/app/models/choo_choo/child_node.rb +38 -0
  12. data/app/models/choo_choo/parent_node.rb +38 -0
  13. data/app/views/choo_choo/activities/_all.html.erb +13 -0
  14. data/config/initializers/assets_helpers.rb +4 -0
  15. data/config/routes.rb +11 -0
  16. data/db/migrate/20140221100053_create_choo_choo_locomotives.rb +8 -0
  17. data/db/migrate/20140221100201_create_choo_choo_carriages.rb +11 -0
  18. data/db/migrate/20140223212120_add_recipienttype_to_carriage.rb +5 -0
  19. data/db/migrate/20140225222624_add_columns_to_carriage.rb +8 -0
  20. data/db/migrate/20140310144724_create_choo_choo_activities.rb +13 -0
  21. data/db/migrate/20140310170148_remove_locomotives_and_carriages.rb +6 -0
  22. data/db/migrate/20140311120314_rename_events_to_nodes.rb +9 -0
  23. data/lib/choo_choo.rb +4 -0
  24. data/lib/choo_choo/engine.rb +12 -0
  25. data/lib/choo_choo/version.rb +3 -0
  26. data/lib/tasks/choo_choo_tasks.rake +4 -0
  27. data/spec/controllers/choo_choo/activities_controller_spec.rb +7 -0
  28. data/spec/dummy/README.rdoc +261 -0
  29. data/spec/dummy/Rakefile +7 -0
  30. data/spec/dummy/app/assets/javascripts/activities.js +2 -0
  31. data/spec/dummy/app/assets/javascripts/application.js +16 -0
  32. data/spec/dummy/app/assets/stylesheets/activities.css +4 -0
  33. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  34. data/spec/dummy/app/controllers/activities_controller.rb +5 -0
  35. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  36. data/spec/dummy/app/helpers/activities_helper.rb +2 -0
  37. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  38. data/spec/dummy/app/models/comment.rb +13 -0
  39. data/spec/dummy/app/models/post.rb +13 -0
  40. data/spec/dummy/app/models/user.rb +3 -0
  41. data/spec/dummy/app/views/activities/index.html.erb +3 -0
  42. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  43. data/spec/dummy/config.ru +4 -0
  44. data/spec/dummy/config/application.rb +66 -0
  45. data/spec/dummy/config/boot.rb +10 -0
  46. data/spec/dummy/config/database.yml +25 -0
  47. data/spec/dummy/config/environment.rb +5 -0
  48. data/spec/dummy/config/environments/development.rb +37 -0
  49. data/spec/dummy/config/environments/production.rb +67 -0
  50. data/spec/dummy/config/environments/test.rb +37 -0
  51. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  52. data/spec/dummy/config/initializers/inflections.rb +15 -0
  53. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  54. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  55. data/spec/dummy/config/initializers/session_store.rb +8 -0
  56. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  57. data/spec/dummy/config/locales/en.yml +5 -0
  58. data/spec/dummy/config/routes.rb +6 -0
  59. data/spec/dummy/db/migrate/20140303122720_create_posts.rb +10 -0
  60. data/spec/dummy/db/migrate/20140303165156_create_users.rb +9 -0
  61. data/spec/dummy/db/migrate/20140303170331_add_user_id_to_posts.rb +5 -0
  62. data/spec/dummy/db/migrate/20140310145716_create_comments.rb +11 -0
  63. data/spec/dummy/db/schema.rb +48 -0
  64. data/spec/dummy/db/seeds.rb +19 -0
  65. data/spec/dummy/public/404.html +26 -0
  66. data/spec/dummy/public/422.html +26 -0
  67. data/spec/dummy/public/500.html +25 -0
  68. data/spec/dummy/public/favicon.ico +0 -0
  69. data/spec/dummy/script/rails +6 -0
  70. data/spec/factories.rb +21 -0
  71. data/spec/models/choo_choo/activity_spec.rb +62 -0
  72. data/spec/spec_helper.rb +29 -0
  73. metadata +220 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a77cc4e6f34e32ab2957530979831a258de91728
4
+ data.tar.gz: d4576efa4bd3c868adc71e55a7d5842727f694bb
5
+ SHA512:
6
+ metadata.gz: fc879ed83236c253a234d5e566d68819d7289f29d2ce872eff231d3c2f7fe6930bea63e13dbead63133879f9bad600873962dda61eced3065fe43441acf7434b
7
+ data.tar.gz: d0118dfac26cd761cad87d26b8ec54b950d917a2ec086da6652ba5f7677f6457b8d7237ffb269a48573a071fe90117018b6f1cea8d75dd09077b57860bf31902
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # ChooChoo [![Build Status](https://travis-ci.org/lnucrowding/choo_choo.png?branch=master)](https://travis-ci.org/lnucrowding/choo_choo)
2
+
3
+ ## About
4
+
5
+ ChooChoo is an application for dynamic activity tracking in Ruby On Rails. By storing information triggered by (tracked) model changes, ChooChoo will then let you reference these changes in a dynamic and highly customizable way.
6
+
7
+ ## Installation
8
+
9
+ Add this to your `Gemfile`:
10
+
11
+ # since it's not a proper gem yet, pull from this repo
12
+ gem 'choo_choo', github: 'lnucrowding/choo_choo'
13
+
14
+ Install the required choo_choo migrations:
15
+
16
+ rake choo_choo:install:migrations
17
+
18
+ And then run:
19
+
20
+ rake db:migrate
21
+
22
+ **Optional:**
23
+
24
+ You can also specify a branch, tag or even a ref:
25
+
26
+ gem 'choo_choo', github: 'lnucrowding/choo_choo', branch: 'my_feature'
27
+
28
+ gem 'choo_choo', github: 'lnucrowding/choo_choo', tag: 'v0.1.0'
29
+
30
+ gem 'choo_choo', github: 'lnucrowding/choo_choo', ref: '6c46204'
31
+
32
+ ## Usage
33
+
34
+ When listening for model activities through ChooCchoo you will need to set up a parent-children behaviour.
35
+ In the example below, we will be using the dummy app found in spec/dummy to illustrate how ChooChoo works.
36
+
37
+
38
+ The first thing to do is to decide for a model that will act as a parent. Using the dummy app, we will go for
39
+ app/models/post.rb as it is a connecting point for comments and users.
40
+
41
+ ```ruby
42
+ # Simply include ChooChoo::ParentNode into post.rb and you're all set
43
+ include ChooChoo::ParentNode
44
+ ```
45
+
46
+ Now that we have a parent, let's give ChooChoo some children. In this example we will only focus on
47
+ Comment model. But you can of course add as many children as you wish.
48
+
49
+ ```ruby
50
+ # First, include the ChooChoo::ChildNode into comment.rb
51
+ include ChooChoo::ChildNode
52
+ ```
53
+
54
+ ```ruby
55
+ # And moving on, we need to tell this child where his parent is
56
+ # by simply adding this line into comment.rb
57
+ set_parent :post
58
+ ```
59
+
60
+ So that's all you really need for setting upp ChooChoo - a simple and lightweight activity listener for your models.
61
+ From here you can now get access to your activities. For instance, in your views or controller actions by the following examples.
62
+
63
+ ```ruby
64
+ # Get the whole related ChooChoo instance for a post
65
+ @post.activity
66
+ ```
67
+ ```ruby
68
+ # Find out what the last action for the activity was
69
+ # This returns a string of 'created', 'updated' or 'deleted'
70
+ # Please do note that if a parent node is deleted you will no longer have access to the activity
71
+ @post.activity.last_action
72
+ ```
73
+ ```ruby
74
+ # Get the latest changed child for the parent
75
+ # This returns the instance of the object (child) tracked by ChooChoo
76
+ @post.activity.last_updated_node
77
+ ```
78
+
79
+ ```ruby
80
+ # You can also just access the reference id if you so wish by
81
+ @post.activity.last_updated_node_id
82
+ ```
83
+
84
+ Of course, you also have access to the timestamp columns created_at and updated_at that are updated accordingly on
85
+ new activities. From here, you are free to customize the ChooChoo so it fits your needs or simply use the given solution
86
+ and make own customizations through views and helpers in your app that utilizes ChooChoo.
87
+
88
+ ### Events
89
+
90
+ The `activity.last_action` contains a string of the last registered event like
91
+ follows:
92
+
93
+ - `created` when a parent or child is created
94
+ - `updated` when a parent or child is updated
95
+ - `deleted` when a **child** is deleted
96
+ - `destroyed` when a parent is deleted (this is different because this means
97
+ you lose the references to the models)
98
+
99
+ ## Testing
100
+
101
+ To run the tests, you first have to make sure it's installed properly, and then
102
+ clone the testing database:
103
+
104
+ rake app:db:test:clone
105
+
106
+ You can then run the tests with:
107
+
108
+ bundle exec rspec
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
9
+ load 'rails/tasks/engine.rake'
10
+
11
+ Bundler::GemHelper.install_tasks
12
+
13
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
14
+
15
+ require 'rspec/core'
16
+ require 'rspec/core/rake_task'
17
+
18
+ desc "Run all specs in spec directory (excluding plugin specs)"
19
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
20
+
21
+ task :default => :spec
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,17 @@
1
+ body {
2
+ font-family: "Helvetica";
3
+ }
4
+
5
+ li {
6
+ padding: 12px 8px 6px 8px;
7
+ background-color: #eee;
8
+ margin-left: -40px;
9
+ list-style: none;
10
+ border-bottom: 1px solid #ddd;
11
+ margin-bottom: 20px;
12
+ }
13
+
14
+ pre {
15
+ padding: 3px;
16
+ background-color: #ede8ea;
17
+ }
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,4 @@
1
+ module ChooChoo
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ChooChoo
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,29 @@
1
+ module ChooChoo
2
+ # A new activity is created when a ParentNode is created
3
+ # the event_happened method is called whenever there is a state change for the
4
+ # tracked models using Activity as a ParentNode or a ChildNode
5
+ class Activity < ActiveRecord::Base
6
+
7
+ attr_accessible :last_action, :parent_node, :last_updated_node
8
+
9
+ belongs_to :parent_node, polymorphic: true
10
+ belongs_to :last_updated_node, polymorphic: true
11
+
12
+ after_create :on_create
13
+
14
+ # Initial creation of a new activity with a ParentNode set to the model
15
+ # in the hosting app using choo_choo that will act as a parent
16
+ def on_create
17
+ self.event_happened('created', self.parent_node)
18
+ end
19
+
20
+ # Tracks model changes (created, updated, deleted)
21
+ # Wether it's a parent or child node
22
+ def event_happened(event_name, model)
23
+ self.last_action = event_name
24
+ self.last_updated_node = model
25
+ self.updated_at = Time.zone.now
26
+ self.save!
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,38 @@
1
+ module ChooChoo
2
+ module ChildNode
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ cattr_reader :my_parent
7
+
8
+ # Callbakcs for when a model tacked by choo_choo
9
+ # is updated, creeated or destroyed
10
+ after_create :on_create
11
+ after_update :on_update
12
+ before_destroy :on_destroy
13
+
14
+ # Defines the parent (the model where ParentNode is included)
15
+ # for the model to be tracked by choo_choo as a child node
16
+ def self.set_parent(parent)
17
+ @@my_parent = parent
18
+ end
19
+
20
+ end
21
+
22
+ # The callback actions below are called when there is a new
23
+ # activity for a child node tracked by choo_choo
24
+ private
25
+
26
+ def on_create
27
+ self.send(@@my_parent).activity.event_happened('created', self)
28
+ end
29
+
30
+ def on_update
31
+ self.send(@@my_parent).activity.event_happened('updated', self)
32
+ end
33
+
34
+ def on_destroy
35
+ self.send(@@my_parent).activity.event_happened('deleted', self)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ module ChooChoo
2
+
3
+ module ParentNode
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+
8
+ # TODO: break out into base_event?
9
+ after_create :on_create
10
+ after_update :on_update
11
+ before_destroy :on_destroy
12
+
13
+ has_one :activity, as: :parent_node, class_name: 'ChooChoo::Activity', dependent: :destroy
14
+ end
15
+
16
+
17
+ # called from subactivites when something has happened
18
+ def child_event_happened(activity_name, model)
19
+ # facade
20
+ self.activity.event_happened(activity_name, model)
21
+ end
22
+
23
+ private
24
+
25
+ def on_create
26
+ ChooChoo::Activity.create(parent_node: self)
27
+ end
28
+
29
+ def on_update
30
+ self.activity.event_happened('updated', self)
31
+ end
32
+
33
+ def on_destroy
34
+ self.activity.event_happened('destroyed', nil)
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,13 @@
1
+ <div>
2
+ <% @activities.each do |activity| %>
3
+ <div style="background-color: #eee">
4
+ <h2><%= activity.parent_node.title %></h2>
5
+ <strong><%= activity.parent_node.body %></strong>
6
+
7
+ <h4>Comments:</h4>
8
+ <ul>
9
+ <li><%= activity.last_updated_node.body %></li>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+ </div>
@@ -0,0 +1,4 @@
1
+ Rails.application.assets.context_class.class_eval do
2
+ # Allows assets to use the engines url helpers
3
+ include ChooChoo::Engine.routes.url_helpers
4
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,11 @@
1
+ ChooChoo::Engine.routes.draw do
2
+
3
+ # a intresting thing here would be to have altanative routes.
4
+ # one example is the facebook user profile where you can se another persons activities.
5
+ # Then we cannot rely on current_user to know what should be shown.
6
+ # Instead we need to send a id param to get another users activities.
7
+ # For example: get "activities/users/:id/", to: "activities#index"
8
+
9
+ get '/activities/', to: 'activities#index', as: 'activities'
10
+ root :to => 'activities#index'
11
+ end
@@ -0,0 +1,8 @@
1
+ class CreateChooChooLocomotives < ActiveRecord::Migration
2
+ def change
3
+ create_table :choo_choo_locomotives do |t|
4
+
5
+ t.timestamps
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ class CreateChooChooCarriages < ActiveRecord::Migration
2
+ def change
3
+ create_table :choo_choo_carriages do |t|
4
+ t.string :excerpt
5
+ t.string :activity_type
6
+ t.integer :recipient_id
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ class AddRecipienttypeToCarriage < ActiveRecord::Migration
2
+ def change
3
+ add_column :choo_choo_carriages, :recipient_type, :string
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ class AddColumnsToCarriage < ActiveRecord::Migration
2
+ def change
3
+ rename_column :choo_choo_carriages, :recipient_id, :tracked_resource_id
4
+ rename_column :choo_choo_carriages, :recipient_type, :tracked_resource_type
5
+ add_column :choo_choo_carriages, :header, :string
6
+ add_column :choo_choo_carriages, :creator_id, :integer
7
+ end
8
+ end
@@ -0,0 +1,13 @@
1
+ class CreateChooChooActivities < ActiveRecord::Migration
2
+ def change
3
+ create_table :choo_choo_activities do |t|
4
+
5
+ t.string :last_action
6
+
7
+ t.belongs_to :master_event, polymorphic: true
8
+ t.belongs_to :last_event, polymorphic: true
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ class RemoveLocomotivesAndCarriages < ActiveRecord::Migration
2
+ def change
3
+ drop_table :choo_choo_locomotives
4
+ drop_table :choo_choo_carriages
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ class RenameEventsToNodes < ActiveRecord::Migration
2
+ def change
3
+ rename_column :choo_choo_activities, :last_event_id, :last_updated_node_id
4
+ rename_column :choo_choo_activities, :last_event_type, :last_updated_node_type
5
+
6
+ rename_column :choo_choo_activities, :master_event_id, :parent_node_id
7
+ rename_column :choo_choo_activities, :master_event_type, :parent_node_type
8
+ end
9
+ end