Unverified Commit 3aa324a7 authored by azul's avatar azul
Browse files

move remaining associations into user namespace

parent 9f32e358
......@@ -59,11 +59,9 @@ class Page::ParticipationsController < Page::SidebarsController
## view access, then we need to destory them to remove access.
def destroy
if may_remove_participation?(@part)
if @part.is_a? UserParticipation
@page.remove(@part.user)
else
@page.remove(@part.group)
end
@part.destroy
entity = @part.entity
entity.clear_access_cache if entity.respond_to? :clear_access_cache
else
raise ErrorMessage.new(:remove_access_error.t)
end
......
......@@ -127,7 +127,7 @@ module Common::Page::ListingHelper
# title = link_to(h(page.title), page_url(page))
# # this is not used for now
# #if participation and participation.instance_of? UserParticipation
# #if participation and participation.user?
# # title += " " + icon_tag("tiny_star") if participation.star?
# #else
# # #title += " " + icon_tag("tiny_pending") unless page.resolved?
......@@ -156,7 +156,7 @@ module Common::Page::ListingHelper
# end
# #
# # Often when you run a page search, you will get an array of UserParticipation
# # Often when you run a page search, you will get an array of User::Participation
# # or Group::Participation objects.
# #
# # This method will convert the array to Pages if they are not.
......
module UserParticipationExtension
module Starring
def self.included(base)
base.class_eval do
after_save :update_stars
end
end
def update_stars
if page_id and self.star_changed?
page.update_star_count
end
end
end
end
......@@ -35,7 +35,9 @@ class Discussion < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
# if we are a private discussion (or 'messages')
has_many :relationships, inverse_of: :discussion do
has_many :relationships,
class_name: 'User::Relationship',
inverse_of: :discussion do
def for_user(user)
self.detect {|relationship| relationship.user_id == user.id}
end
......
......@@ -17,7 +17,6 @@ add_index "group_participations", ["group_id", "page_id"], :name => "index_group
class Group::Participation < ActiveRecord::Base
include Common::ParticipationAccess
self.table_name = :group_participations
belongs_to :page, inverse_of: :group_participations
belongs_to :group, inverse_of: :participations
......
......@@ -345,6 +345,8 @@ class Page < ActiveRecord::Base
# Remove a group or user from this page (by destroying the corresponing
# user_participation or group_participation object). This is the only way
# that groups or users should be removed from pages!
# FIXME: removing the connection should be part of the participation.
# This way we can test participation.destroyed? in page history and views.
def remove(entity)
if entity.is_a? Enumerable
entity.each do |e|
......
......@@ -15,7 +15,10 @@ module PageExtension::Users
belongs_to :created_by, class_name: 'User', foreign_key: 'created_by_id'
belongs_to :updated_by, class_name: 'User', foreign_key: 'updated_by_id'
has_many :user_participations, dependent: :destroy, inverse_of: :page
has_many :user_participations,
class_name: 'User::Participation',
dependent: :destroy,
inverse_of: :page
has_many :users, through: :user_participations do
def with_access
where('access IS NOT NULL')
......@@ -28,19 +31,6 @@ module PageExtension::Users
after_save :reset_users
end
base.class_eval do
def self.flag_all(page_ids, options)
return unless options[:as] and options[:by]
update = case options[:as]
when :read then "viewed = TRUE"
when :unread then "viewed = FALSE"
when :watched then "watch = TRUE"
when :unwatched then "watch = FALSE"
end
UserParticipation.update_all update,
["page_id IN (?) AND user_id = ?", page_ids, options[:by]]
end
end
end
##
......
......@@ -29,6 +29,10 @@ class PageHistory < ActiveRecord::Base
'page_histories/page_history'
end
def self.recipients_for_page(page)
page.user_participations.where(watch: true).map(&:user_id)
end
def notification_sent?
notification_sent_at.present?
end
......@@ -253,8 +257,9 @@ class PageHistory::RevokedGroupAccess < PageHistory::UpdateParticipation
after_save :page_updated_at
def self.tracks(changes, part)
part.group? &&
!Group::Participation.exists?(id: part.id)
# destroyed? does not work here because we destroy the participation via
# page.groups
part.group? && !part.class.exists?(id: part.id)
end
def participation=(part)
......@@ -269,7 +274,7 @@ class PageHistory::GrantUserAccess < PageHistory::UpdateParticipation
include GrantAccess
def self.tracks(changes, part)
part.is_a?(UserParticipation) && changes.keys.include?('access')
part.user? && changes.keys.include?('access')
end
after_save :page_updated_at
......@@ -298,8 +303,9 @@ class PageHistory::GrantUserReadAccess < PageHistory::GrantUserAccess; end
class PageHistory::RevokedUserAccess < PageHistory::UpdateParticipation
def self.tracks(changes, part)
part.is_a?(UserParticipation) &&
!UserParticipation.exists?(id: part.id)
# destroyed? does not work here because we destroy the participation via
# page.users
part.user? && !part.class.exists?(id: part.id)
end
def participation=(part)
......
......@@ -12,7 +12,7 @@ class RequestToFriend < Request
validate :no_friendship_yet, on: :create
def no_friendship_yet
if Friendship.find_by_user_id_and_contact_id(created_by_id, recipient_id)
if created_by.friendships.with(recipient_id).exists?
errors.add(:base, 'Friendship already exists')
end
end
......
......@@ -4,6 +4,6 @@
# Currently, every Friendship record implies a twin going the other way.
#
class Friendship < Relationship
class User::Friendship < User::Relationship
end
......@@ -13,7 +13,8 @@
# attend, :boolean -- the user will attend event
#
class UserParticipation < ActiveRecord::Base
class User::Participation < ActiveRecord::Base
include Common::ParticipationAccess
belongs_to :page, inverse_of: :user_participations
......@@ -26,8 +27,8 @@ class UserParticipation < ActiveRecord::Base
after_destroy :clear_tag_cache
# use this for counting stars :)
include UserParticipationExtension::Starring
include UserParticipationExtension::PageHistory
include Starring
include History
# maybe later use this to replace all the notification stuff
# include ParticipationExtension::Subscribe
......
module UserParticipationExtension::PageHistory
module User::Participation::History
def start_watching?
self.watch_changed? && self.watch == true
end
......
module User::Participation::Starring
extend ActiveSupport::Concern
included do
after_save :update_stars
end
def update_stars
if page_id and self.star_changed?
page.update_star_count
end
end
end
# user to user relationship
class Relationship < ActiveRecord::Base
class User::Relationship < ActiveRecord::Base
self.table_name = :relationships
belongs_to :user
belongs_to :contact, class_name: 'User', foreign_key: :contact_id
belongs_to :discussion, dependent: :destroy, inverse_of: :relationships
def self.with(user)
where(contact_id: user)
end
# mark as read or unread the discussion on this relationship
def mark!(as)
# set a new value for the unread_count field
......@@ -44,6 +50,6 @@ class Relationship < ActiveRecord::Base
end
def inverse
@inverse ||= Relationship.where(user_id: contact, contact_id: user).first
@inverse ||= self.class.where(user_id: contact, contact_id: user).first
end
end
......@@ -16,7 +16,7 @@ module UserExtension::Pages
def self.included(base)
base.instance_eval do
has_many :participations,
class_name: 'UserParticipation',
class_name: 'User::Participation',
dependent: :destroy,
inverse_of: :user
......
......@@ -38,14 +38,18 @@ module UserExtension::Users
## RELATIONSHIPS
##
has_many :relationships, dependent: :destroy do
def with(user) find_by_contact_id(user.id) end
end
has_many :relationships,
class_name: 'User::Relationship',
dependent: :destroy
has_many :friendships,
class_name: 'User::Relationship',
conditions: {type: 'Friendship'}
has_many :discussions, through: :relationships, order: 'discussions.replied_at DESC'
has_many :contacts, through: :relationships
has_many :friends, through: :relationships, conditions: "relationships.type = 'Friendship'", source: :contact do
has_many :friends, through: :friendships, source: :contact do
def most_active(options = {})
options[:limit] ||= 13
max_visit_count = select('MAX(relationships.total_visits) as id').first.id || 1
......@@ -114,15 +118,11 @@ module UserExtension::Users
def add_contact!(other_user, type=nil)
type = 'Friendship' if type == :friend
unless relationship = other_user.relationships.with(self)
relationship = Relationship.new(user: other_user, contact: self)
end
relationship = other_user.relationships.with(self).first_or_initialize
relationship.type = type
relationship.save!
unless relationship = self.relationships.with(other_user)
relationship = Relationship.new(user: self, contact: other_user)
end
relationship = self.relationships.with(other_user).first_or_initialize
relationship.type = type
relationship.save!
......@@ -141,11 +141,11 @@ module UserExtension::Users
# this should be the ONLY way contacts are deleted
def remove_contact!(other_user)
if self.relationships.with(other_user)
if self.relationships.with(other_user).exists?
self.contacts.delete(other_user)
self.update_contacts_cache
end
if other_user.relationships.with(self)
if other_user.relationships.with(self).exists?
other_user.contacts.delete(self)
other_user.update_contacts_cache
end
......@@ -161,7 +161,7 @@ module UserExtension::Users
# notification on the user's wall.
#
def send_message_to!(other_user, body, in_reply_to = nil)
relationship = relationships.with(other_user) || add_contact!(other_user)
relationship = relationships.with(other_user).first || add_contact!(other_user)
relationship.send_message(body, in_reply_to)
end
......
......@@ -23,7 +23,7 @@ module Crabgrass
config.autoload_paths << "#{Rails.root}/lib"
config.autoload_paths << "#{Rails.root}/app/models"
config.autoload_paths += %w(associations discussion chat profile poll task requests mailers notice).
config.autoload_paths += %w(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/helpers/classes"
......
......@@ -92,12 +92,12 @@ FactoryGirl.define do
end
factory :user_participation do
factory 'user_participation', class: User::Participation do
access 1
watch false
end
factory :group_participation do
factory 'group_participation', class: Group::Participation do
access 1
end
......
......@@ -21,10 +21,10 @@ class Page::ParticipationsControllerTest < ActionController::TestCase
@other = FactoryGirl.create :user
login_as @other
@page.update_attribute :public, true
assert_difference 'UserParticipation.count' do
assert_difference 'User::Participation.count' do
xhr :post, :create, page_id: @page, star: true
end
assert UserParticipation.last.star
assert @other.participations.last.star
end
def test_watch
......@@ -51,6 +51,7 @@ class Page::ParticipationsControllerTest < ActionController::TestCase
other_upart.save
assert_difference 'PageHistory.count' do
xhr :post, :update, page_id: @page, id: other_upart, access: :remove
assert_response :success
end
end
......
......@@ -48,6 +48,7 @@ class ActiveSupport::TestCase
set_fixture_class taggings: ActsAsTaggableOn::Tagging
set_fixture_class tags: ActsAsTaggableOn::Tag
set_fixture_class federatings: Group::Federating
set_fixture_class relationships: User::Relationship
set_fixture_class memberships: Group::Membership
end
......
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