Unverified Commit 4955ee0a authored by azul's avatar azul
Browse files

namespace Activity and remove from autoload_path

We used to have app/models/activity in the autoload path and had classes
like FriendActivity < Activity

This conflicts with how rails4 handles class lookup. Plus it clutters the
autoload path and makes it harder to find classes.

Using proper namespaces instead now and I'm about to apply this to all the
different namespace avoiding things like ...Extension. We can use classes
as namespaces as long as we:
* make sure to define things inside the namespace in one line or in a
   class instead of a module block:
   ```ruby
     class Activity::Friend < Activity
     # or
     class Activity
       class Friend < ::Activity
   ```
 * have names inside the namespaces that do not conflict with global names

The second one is important because if PrivatePost is already loaded and
Activity::PrivatePost is referenced somewhere it will be found as an
constant inside the Object namespace of Activity (since Activity is a class).
So instead of loading Activity::PrivatePost we'll see a warning that global
constant PrivatePost was referenced by Activity::PrivatePost.

So how do we prevent this?
* do not clutter the global namespace
* use more specific names in other namespaces
  -> in this case i am renaming PrivatePostActivity to Activity::MessageSent
  and not Activity::PrivatePost
parent 1dfaa1b3
......@@ -28,7 +28,6 @@
# related_id and extra are used for generic storage and association, whatever
# the subclass wants to use it for.
#
class Activity < ActiveRecord::Base
# activity access (relative to self.subject):
......@@ -138,10 +137,10 @@ class Activity < ActiveRecord::Base
[ "(subject_type = 'User' AND subject_id = ?) OR
(subject_type = 'User' AND subject_id IN (?) AND access != ?) OR
(subject_type = 'Group' AND subject_id IN (?)) ",
user.id,
other_users_ids_list,
Activity::PRIVATE,
user.all_group_id_cache]
user.id,
other_users_ids_list,
Activity::PRIVATE,
user.all_group_id_cache]
end
# for user's landing page
......@@ -157,8 +156,8 @@ class Activity < ActiveRecord::Base
def self.for_user(user, current_user)
if (current_user and current_user.friend_of?(user) or current_user == user)
restricted = Activity::PRIVATE
# elsif current_user and current_user.peer_of?(user)
# restricted = Activity::DEFAULT
# elsif current_user and current_user.peer_of?(user)
# restricted = Activity::DEFAULT
else
restricted = Activity::DEFAULT
end
......@@ -225,4 +224,3 @@ class Activity < ActiveRecord::Base
end
end
class FriendActivity < Activity
class Activity::Friend < Activity
validates_format_of :subject_type, with: /User/
validates_format_of :item_type, with: /User/
......@@ -35,16 +35,16 @@ class FriendActivity < Activity
other_user: user_span(:other_user))
end
# Warning: Do not use self.class or even FriendActivity here...
# Warning: Do not use self.class or even Activity::Friend here...
# Why? It seems the scope of self is kept in that case.
# So activity.twin.twin would always return nil because it tries to
# fullfill both conditions (those for the twin and for the twin of that twin)
# at the same time.
# UPGRADE: Check if this is fixed
def twin
Activity.where(type: self.type).
where(subject_id: item_id, subject_type: 'User').
where(item_id: subject_id, item_type: 'User')
Activity.where type: 'Friend',
subject_id: item_id, subject_type: 'User',
item_id: subject_id, item_type: 'User'
end
def icon
......
class GroupCreatedActivity < Activity
class Activity::GroupCreated < Activity
validates_format_of :subject_type, with: /Group/
validates_presence_of :subject_id
......
class GroupDestroyedActivity < Activity
class Activity::GroupDestroyed < Activity
validates_format_of :subject_type, with: /User/
validates_presence_of :subject_id
......
class GroupGainedUserActivity < Activity
class Activity::GroupGainedUser < Activity
validates_format_of :subject_type, with: /Group/
validates_format_of :item_type, with: /User/
......
class GroupLostUserActivity < GroupGainedUserActivity
class Activity::GroupLostUser < Activity::GroupGainedUser
def description(view=nil)
I18n.t(:activity_user_left_group,
user: user_span(:user),
......
class PrivatePostActivity < Activity
class Activity::MessageSent < Activity
validates_format_of :subject_type, with: /User/
validates_presence_of :subject_id
......
class MessageWallActivity < Activity
class Activity::MessageWall < Activity
include ActionView::Helpers::TagHelper
validates_format_of :subject_type, with: /User/
......
class TwinkledActivity < Activity
class Activity::Twinkled < Activity
include ActionView::Helpers::TagHelper
validates_format_of :subject_type, with: /User/
......
class UnreadActivity < Activity
class Activity::Unread < Activity
validates_format_of :subject_type, with: /User/
validates_presence_of :subject_id
......@@ -22,12 +22,12 @@ class UnreadActivity < Activity
self.unread_count = user.relationships.sum('unread_count') || 0
end
# We want to delete the other UnreadActivities even if we don't pass
# We want to delete the other Activity::Unread even if we don't pass
# validations, because if there are no unread messages, we want no
# UnreadActivities.
# Activity.
before_validation :destroy_twins, on: :create
def destroy_twins
UnreadActivity.destroy_all 'subject_id = %s' % user.id
self.class.destroy_all 'subject_id = %s' % user.id
end
public
......
class UserCreatedGroupActivity < Activity
class Activity::UserCreatedGroup < Activity
validates_format_of :subject_type, with: /User/
validates_format_of :item_type, with: /Group/
......
class UserDestroyedActivity < Activity
class Activity::UserDestroyed < Activity
validates_format_of :subject_type, with: /User/
validates_presence_of :subject_id
......
class UserJoinedGroupActivity < Activity
class Activity::UserJoinedGroup < Activity
validates_format_of :subject_type, with: /User/
validates_format_of :item_type, with: /Group/
......
class UserJoinedSiteActivity < UserJoinedGroupActivity
class Activity::UserJoinedSite < Activity::UserJoinedGroup
def description(view=nil)
I18n.t(:activity_user_joined_site,
......
class UserLeftGroupActivity < UserJoinedGroupActivity
class Activity::UserLeftGroup < Activity::UserJoinedGroup
def description(view=nil)
I18n.t(:activity_user_left_group,
user: user_span(:user),
......
class UserLeftSiteActivity < UserLeftGroupActivity
class Activity::UserLeftSite < Activity::UserLeftGroup
def description(view=nil)
I18n.t(:activity_user_left_site,
user: user_span(:user),
......
class UserProposedToDestroyGroupActivity < Activity
class Activity::UserProposedToDestroyGroup < Activity
validates_format_of :subject_type, with: /User/
validates_format_of :item_type, with: /Group/
validates_presence_of :subject_id
......
class PrivatePost < Post
has_one :private_post_activity,
has_one :activity,
foreign_key: :related_id,
dependent: :delete
dependent: :delete,
class_name: 'Activity::MessageSent'
def private?
true
......
module Tracking::Action
EVENT_CREATES_ACTIVITIES = {
create_group: ['GroupCreatedActivity', 'UserCreatedGroupActivity'],
create_membership: ['GroupGainedUserActivity', 'UserJoinedGroupActivity'],
destroy_membership: ['GroupLostUserActivity', 'UserLeftGroupActivity'],
request_to_destroy_group: ['UserProposedToDestroyGroupActivity'],
create_friendship: ['FriendActivity'],
create_group: ['Activity::GroupCreated', 'Activity::UserCreatedGroup'],
create_membership: ['Activity::GroupGainedUser', 'Activity::UserJoinedGroup'],
destroy_membership: ['Activity::GroupLostUser', 'Activity::UserLeftGroup'],
request_to_destroy_group: ['Activity::UserProposedToDestroyGroup'],
create_friendship: ['Activity::Friend'],
create_post: ['PageHistory::AddComment'],
update_post: ['PageHistory::UpdateComment'],
destroy_post: ['PageHistory::DestroyComment'],
......
......@@ -23,7 +23,7 @@ module Crabgrass
config.autoload_paths << "#{Rails.root}/lib"
config.autoload_paths << "#{Rails.root}/app/models"
config.autoload_paths += %w(activity assets associations discussion chat profile poll task requests mailers notice).
config.autoload_paths += %w(assets associations discussion chat profile poll task requests mailers notice).
collect { |dir| "#{Rails.root}/app/models/#{dir}" }
config.autoload_paths << "#{Rails.root}/app/permissions"
config.autoload_paths << "#{Rails.root}/app/sweepers"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment