Skip to content

Commit

Permalink
Merge pull request #68 from it3s/activity-stream
Browse files Browse the repository at this point in the history
Activity stream
  • Loading branch information
andersoncardoso committed Aug 24, 2014
2 parents e1fc241 + 806a55c commit ab096c0
Show file tree
Hide file tree
Showing 41 changed files with 562 additions and 61 deletions.
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ gem 'paper_trail' # model versioning
gem 'differ' # build diffs
gem 'safe_yaml', require: false # safe yaml loading
gem "active_model_serializers", "~> 0.8.0" # model serializer
gem 'public_activity' # record users activity

gem 'better_errors' # better error page, and shell session when crash
gem 'binding_of_caller' # used by better_errors
gem 'better_errors' # better error page, and shell session when crash
gem 'binding_of_caller' # used by better_errors

group :doc do
gem 'sdoc', require: false
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ GEM
slop (~> 3.4)
pry-rails (0.3.2)
pry (>= 0.9.10)
public_activity (1.4.2)
actionpack (>= 3.0.0)
activerecord (>= 3.0)
i18n (>= 0.5.0)
railties (>= 3.0.0)
puma (2.8.2)
rack (>= 1.1, < 2.0)
rack (1.5.2)
Expand Down Expand Up @@ -405,6 +410,7 @@ DEPENDENCIES
poltergeist
pry
pry-rails
public_activity
puma
rails (~> 4.1.1)
rdiscount
Expand Down
4 changes: 2 additions & 2 deletions app/assets/javascripts/templates/relationItem.jst.ejs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<div class="relation-item" data-rel-index="<%= index %>">
<input type="hidden" name="relation_id" class="relation_id" />

<select name="relations_type_<%= index%>" class="relation_type" >
<select class="relation_type" >
<option value=""> </option>
<% _.each(options, function(opt) { %>
<option value="<%= opt[1] %>"><%= opt[0] %></option>
<% }); %>
</select>

<input type="hidden" name="relations_target_<%= index%>" class="relation_target"
<input type="hidden" class="relation_target"
id="relation_target_<%= index %>-autocomplete"/>
<input type="text" class="relation_target_autocomplete" placeholder="<%= autocomplete_placeholder %>"
data-components="autocomplete"
Expand Down
6 changes: 3 additions & 3 deletions app/assets/javascripts/templates/relationMetadata.jst.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
</textarea>
<div class="wrapper" data-components="dateRange">
<div class="time-field inline-field">
<label for="metadata_start_date_<%=index%>"><%= start_date_label %></label>
<label><%= start_date_label %></label>
<input type="text" class="metadata_start_date" data-date-range="from" />
</div>
<div class="time-field inline-field">
<label for="metadata_end_date_<%=index%>"><%= end_date_label %></label>
<label><%= end_date_label %></label>
<input type="text" class="metadata_end_date" data-date-range="to" />
</div>
<div class="currency-field inline-field">
Expand All @@ -20,7 +20,7 @@
</select>
</div>
<div class="amount-field inline-field">
<label for="metadata_amount_<%=index%>"><%= amount_label %></label>
<label><%= amount_label %></label>
<input type="text" class="metadata_amount" data-components="money" />
</div>
</div>
Expand Down
42 changes: 42 additions & 0 deletions app/assets/stylesheets/activities.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.activities-list {
.list-item {

&.small .item { width: 562px; }

.trackable { font-weight: bold; }

&.user {
.item-avatar { background-color: $meppit-dark-green; }
.trackable { color: $meppit-dark-green; }
}

&.geo_data {
.trackable { color: $meppit-data-color; }
}

&.map {
.trackable { color: $meppit-maps-color; }
}
}

.event-type {
font-weight: bold;
}

.activity-event {
display: inline-block;
}

.activity-time {
display: inline-block;
font-size: 0.8em;
color: $grey;
margin-left: 10px;
}

.activity-changes {
margin-top: 5px;
font-size: 0.9em;
color: $grey;
}
}
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@
@import "maps";
@import "versions";
@import "relations";
@import "activities";
14 changes: 14 additions & 0 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ActivitiesController < ApplicationController
before_action :find_user, only: [:user_activity]

def user_activity
@activities = paginate @user.activities_performed
render layout: nil if request.xhr?
end

private

def find_user
@user ||= User.find(id: params[:user_id])
end
end
8 changes: 5 additions & 3 deletions app/controllers/concerns/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ def to_bool(string)
raise ArgumentError.new("invalid value for Boolean: \"#{string}\"")
end

def update_object(obj, params_hash)
def save_object(obj, params_hash)
obj.assign_attributes(params_hash)
changes = obj.changes
if obj.valid? && obj.save
obj_name = obj.class.name.underscore
EventBus.publish "#{obj_name}_updated", obj_name.to_sym => obj, current_user: current_user
event_type = params[:action] == 'create' ? 'created' : 'updated'
EventBus.publish "#{obj_name}_#{event_type}", obj_name.to_sym => obj, current_user: current_user, changes: changes
flash[:notice] = t('flash.saved')
render json: {redirect: polymorphic_path([obj])}
else
Expand Down Expand Up @@ -84,7 +86,7 @@ def cleaned_relations_attributes(_params)

def cleaned_relation_metadata(m)
OpenStruct.new(
description: m['description'].strip().blank? ? nil : m['description'],
description: m['description'].blank? ? nil : m['description'],
start_date: m['start_date'].blank? ? nil : Date.parse(m['start_date']),
end_date: m['end_date'].blank? ? nil : Date.parse(m['end_date']),
currency: m['amount'].blank? ? nil : m['currency'],
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/followings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class FollowingsController < ApplicationController

before_action :require_login, except: [:followers, :following]
before_action :find_followable
after_action :publish_followed_event, only: [:create]

def create
follow_button_json_response @followable.add_follower(current_user)
Expand Down Expand Up @@ -34,4 +35,8 @@ def follow_button_json_response(action_result)
render json: {ok: false}, status: :unprocessable_entity
end
end

def publish_followed_event
EventBus.publish "followed", object: @followable, current_user: current_user if response.ok?
end
end
2 changes: 1 addition & 1 deletion app/controllers/maps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class MapsController < ObjectsController

def create
_params = cleaned_params.merge(administrator: current_user)
update_object @map, _params
save_object @map, _params
end

def geo_data
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/objects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def new
end

def create
update_object current_object, cleaned_params
save_object current_object, cleaned_params
end

def show
Expand All @@ -25,7 +25,7 @@ def edit
end

def update
update_object current_object, cleaned_params
save_object current_object, cleaned_params
end

def search_by_name
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class UsersController < ApplicationController
before_action :is_current_user, only: [:edit, :update]
before_action :contributions_list, only: [:show]
before_action :following_list, only: [:show]
before_action :activities_list, only: [:show]

def new
@user = User.new
Expand Down Expand Up @@ -42,7 +43,7 @@ def edit
end

def update
update_object @user, user_params
save_object @user, user_params
end

private
Expand Down Expand Up @@ -70,4 +71,8 @@ def contributions_list
def following_list
@following ||= paginate @user.try(:following), params[:following_page]
end

def activities_list
@activities ||= paginate @user.try(:activities_performed), params[:activities_page]
end
end
46 changes: 46 additions & 0 deletions app/listeners/activity_listener.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class ActivityListener

IGNORE_KEYS = ["updated_at", "created_at", "administrator_id"]
REMOVE_VALS = ["avatar"]

def geo_data_created(payload)
save_activity payload, :geo_data, :create
end

def geo_data_updated(payload)
save_activity payload, :geo_data, :update
end

def map_created(payload)
save_activity payload, :map, :create
end

def map_updated(payload)
save_activity payload, :map, :update
end

def user_updated(payload)
save_activity payload, :user, :update
end

def followed(payload)
save_activity payload, :object, :follow
end

private

def save_activity(params, key, action)
trackable = params[key]
changes = cleaned_changes(params)

trackable.create_activity action, owner: params[:current_user], parameters: {changes: changes}
end

def cleaned_changes(params)
return {} unless params[:changes]
changes = params[:changes].except(*IGNORE_KEYS)
REMOVE_VALS.each { |key| changes[key] = ["", ""] if changes[key] }
changes
end
end

8 changes: 8 additions & 0 deletions app/listeners/contributings_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ def geo_data_updated(payload)
save_contribution payload[:geo_data], payload[:current_user]
end

def geo_data_created(payload)
save_contribution payload[:geo_data], payload[:current_user]
end

def map_updated(payload)
save_contribution payload[:map], payload[:current_user]
end

def map_created(payload)
save_contribution payload[:map], payload[:current_user]
end

private

def save_contribution(contributable, contributor)
Expand Down
12 changes: 10 additions & 2 deletions app/listeners/relationships_listener.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
class RelationshipsListener

def geo_data_updated(payload)
geo_data = payload[:geo_data]
geo_data.save_relations_from_attributes if geo_data.relations_attributes
save_relation payload[:geo_data]
end

def geo_data_created(payload)
save_relation payload[:geo_data]
end

private

def save_relation(geo_data)
geo_data.save_relations_from_attributes if geo_data.relations_attributes
end
end

1 change: 1 addition & 0 deletions app/models/geo_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class GeoData < ActiveRecord::Base
include Searchable
include Relationships
include Exportable
include PublicActivity::Common

geojson_field :location
searchable_tags :tags
Expand Down
1 change: 1 addition & 0 deletions app/models/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Map < ActiveRecord::Base
include Mappable
include Searchable
include Exportable
include PublicActivity::Common

belongs_to :administrator, class_name: 'User'

Expand Down
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class User < ActiveRecord::Base
include Followable
include Follower
include Searchable
include PublicActivity::Common

mount_uploader :avatar, AvatarUploader
process_in_background :avatar
Expand Down Expand Up @@ -38,4 +39,8 @@ def send_reset_password_email!
def geojson_properties
{name: name, id: id}
end

def activities_performed
PublicActivity::Activity.where(owner: self).includes(:trackable, :owner).order('created_at desc')
end
end
Loading

0 comments on commit ab096c0

Please sign in to comment.