Skip to content

Commit

Permalink
Messages and Friendships have relationship through Activity
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanPorta committed Dec 6, 2014
1 parent e128cff commit c827cfa
Show file tree
Hide file tree
Showing 25 changed files with 213 additions and 474 deletions.
68 changes: 0 additions & 68 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
@@ -1,75 +1,7 @@
class ActivitiesController < ApplicationController
before_action :set_activity, only: [:show, :edit, :update, :destroy]

# GET /activities
# GET /activities.json
def index
@activities = current_user.activities.order created_at: :desc
end

# GET /activities/1
# GET /activities/1.json
def show
end

# GET /activities/new
def new
@activity = Activity.new
end

# GET /activities/1/edit
def edit
end

# POST /activities
# POST /activities.json
def create
@activity = Activity.new(activity_params)

respond_to do |format|
if @activity.save
format.html { redirect_to @activity, notice: 'Activity was successfully created.' }
format.json { render :show, status: :created, location: @activity }
else
format.html { render :new }
format.json { render json: @activity.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /activities/1
# PATCH/PUT /activities/1.json
def update
respond_to do |format|
if @activity.update(activity_params)
format.html { redirect_to @activity, notice: 'Activity was successfully updated.' }
format.json { render :show, status: :ok, location: @activity }
else
format.html { render :edit }
format.json { render json: @activity.errors, status: :unprocessable_entity }
end
end
end

# DELETE /activities/1
# DELETE /activities/1.json
def destroy
@activity.destroy
respond_to do |format|
format.html { redirect_to activities_url, notice: 'Activity was successfully destroyed.' }
format.json { head :no_content }
end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_activity
@activity = Activity.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def activity_params
params.require(:activity).permit(:user_id, :message_id, :type)
end
end
55 changes: 16 additions & 39 deletions app/decorators/activity_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,30 @@
class ActivityDecorator < Draper::Decorator
delegate :id, :user, :message, :type
delegate :id, :user, :actionable, :type, :actionable_type

decorates_association :user
decorates_association :message
decorates_association :actionable

def reciprocate_message
actionable.unacknowledged_message_for_sender_reciprocation
end

def acknowledge_message
actionable.acknowledged_message_for_recipient
end

def activity_message
# hashtag uhg.
if object.type == 'sent'
if message.acknowledged_at
acknowledged_message_for_sender
if actionable.acknowledged_at
actionable.acknowledged_message_for_sender
else
unacknowledged_message_for_sender
actionable.unacknowledged_message_for_sender
end
else
if message.acknowledged_at
acknowledged_message_for_recipient
if actionable.acknowledged_at
actionable.acknowledged_message_for_recipient
else
unacknowledged_message_for_recipient
actionable.unacknowledged_message_for_recipient
end
end
end

def reciprocate_message
unacknowledged_message_for_sender_reciprocation
end

def acknowledge_message
acknowledged_message_for_recipient
end

private

def acknowledged_message_for_sender
"You #{ message.conjugate_verb(:past, :perfective) } #{ message.recipient.first_name }." # past tense
end

def acknowledged_message_for_recipient
"#{ message.sender.first_name } #{ message.conjugate_verb(:past, :perfective) } you." # past tense
end

def unacknowledged_message_for_sender
"You tried to #{ message.verb } #{ message.recipient.first_name }." # present
end

def unacknowledged_message_for_recipient
"#{ message.sender.first_name } wants to #{ message.verb } you." # present
end

def unacknowledged_message_for_sender_reciprocation
"You tried to #{ message.verb } #{ message.sender.first_name }." # present
end
end
54 changes: 46 additions & 8 deletions app/decorators/friendship_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
class FriendshipDecorator < Draper::Decorator
delegate_all

# Define presentation-specific methods here. Helpers are accessed through
# `helpers` (aka `h`). You can override attributes, for example:
#
# def created_at
# helpers.content_tag :span, class: 'time' do
# object.created_at.strftime("%a %m/%d/%y")
# end
# end
def created_at
object.created_at.to_time.to_i
end

def created_at_in_words
helpers.time_ago_in_words object.created_at
end

def acknowledged_at
return nil unless object.approved
object.approved.to_time.to_i
end

def acknowledged_at_in_words
return nil unless object.approved
helpers.time_ago_in_words object.approved
end

def reciprocate_message
unacknowledged_message_for_sender_reciprocation
end

def acknowledge_message
acknowledged_message_for_recipient
end

def acknowledged_message_for_sender
"#{ friend.first_name } accepted your friendship."
end

def acknowledged_message_for_recipient
"You accepted #{ user.first_name } friendship." # past tense
end

def unacknowledged_message_for_sender
"You offered friendship to #{ friend.first_name }." # present
end

def unacknowledged_message_for_recipient
"#{ user.first_name } offers friendship." # present
end

def unacknowledged_message_for_sender_reciprocation
# TODO: How to handle this case?
'' # present
end
end
30 changes: 29 additions & 1 deletion app/decorators/message_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ def acknowledged_at_in_words
end

def conjugate_verb(tense, aspect)
Verbs::Conjugator.conjugate "#{ object.verb }", tense: tense, aspect: aspect
Verbs::Conjugator.conjugate "#{ verb }", tense: tense, aspect: aspect
end

def reciprocate_message
unacknowledged_message_for_sender_reciprocation
end

def acknowledge_message
acknowledged_message_for_recipient
end

def acknowledged_message_for_sender
"You #{ conjugate_verb(:past, :perfective) } #{ recipient.first_name }." # past tense
end

def acknowledged_message_for_recipient
"#{ sender.first_name } #{ conjugate_verb(:past, :perfective) } you." # past tense
end

def unacknowledged_message_for_sender
"You tried to #{ verb } #{ recipient.first_name }." # present
end

def unacknowledged_message_for_recipient
"#{ sender.first_name } wants to #{ verb } you." # present
end

def unacknowledged_message_for_sender_reciprocation
"You tried to #{ verb } #{ sender.first_name }." # present
end
end
16 changes: 12 additions & 4 deletions app/models/activity.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
class Activity < ActiveRecord::Base
self.inheritance_column = :_type_disabled

belongs_to :message
belongs_to :actionable, polymorphic: true
belongs_to :user

validates :user, :message, :type, presence: true
validates :user, :actionable, :type, presence: true
validates :id, absence: true, on: :create
validates :type, inclusion: { in: %w(sent received) }

after_save :notify

def self.activities_for_message(message)
# Sender activity
Activity.create user: message.sender, message: message, type: 'sent'
Activity.create user: message.sender, actionable: message, type: 'sent'

# Recipient activity
Activity.create user: message.recipient, message: message, type: 'received'
Activity.create user: message.recipient, actionable: message, type: 'received'
end

def self.activities_for_friendship(friendship)
# Sender activity
Activity.create user: friendship.user, actionable: friendship, type: 'sent'

# Recipient activity
Activity.create user: friendship.friend, actionable: friendship, type: 'received'
end

private
Expand Down
9 changes: 9 additions & 0 deletions app/models/friendship.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: 'User'
has_many :activities, as: :actionable, dependent: :destroy

after_create :create_activity_entries

def accept
update approved: Time.now
end

private

def create_activity_entries
Activity.activities_for_friendship self
end
end
18 changes: 9 additions & 9 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ class Message < ActiveRecord::Base
belongs_to :sender, class_name: 'User'
belongs_to :recipient, class_name: 'User'

has_many :activities, dependent: :destroy
has_one :sender_activity, ->(message) { where type: 'sent', message_id: message.id }, class_name: 'Activity'
has_one :recipient_activity, ->(message) { where type: 'received', message_id: message.id }, class_name: 'Activity'
has_many :activities, as: :actionable, dependent: :destroy
has_one :sender_activity, ->(message) { where type: 'sent', actionable_id: message.id, actionable_type: 'Message' }, class_name: 'Activity'
has_one :recipient_activity, ->(message) { where type: 'received', actionable_id: message.id, actionable_type: 'Message' }, class_name: 'Activity'

after_create :create_activity_entries
after_save do
Expand Down Expand Up @@ -43,11 +43,11 @@ def create_activity_entries

# Notify sender that message was ack'd
def notify_acknowledgement
message = sender_activity.decorate.activity_message
logger.info "Preparing to send acknowledgment push notification of #{ message } to sender: #{ sender.id }"

sender.devices.each do |device|
device.notify message
end
# message = sender_activity.decorate.activity_message
# logger.info "Preparing to send acknowledgment push notification of #{ message } to sender: #{ sender.id }"
#
# sender.devices.each do |device|
# device.notify message
# end
end
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class User < ActiveRecord::Base
has_many :devices
has_many :activities
has_many :messages, through: :activities
has_many :messages

has_many :friendships
has_many :friends, -> { where.not(friendships: { approved: nil }) }, through: :friendships
Expand Down
29 changes: 0 additions & 29 deletions app/views/activities/_form.html.erb

This file was deleted.

6 changes: 0 additions & 6 deletions app/views/activities/edit.html.erb

This file was deleted.

0 comments on commit c827cfa

Please sign in to comment.