Skip to content

Instantly share code, notes, and snippets.

@clifton
Created May 5, 2011 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clifton/957733 to your computer and use it in GitHub Desktop.
Save clifton/957733 to your computer and use it in GitHub Desktop.
# join model
class Membership < ActiveRecord::Base
include PortalEntity
include Notifiable
cattr_accessor :skip_create_feed
@@skip_create_feed = false
# virtual attribute for verification purposes
attr_accessor :password
#validates_presence_of :portal_id, :account_id#, :account_group_id
#validates_associated :account, :portal
belongs_to :account
belongs_to :school
belongs_to :account_group
#before_create :log_join_org#, :notify_admin # => should we notify admins when people join?
#before_destroy :log_unjoin_org#, :notify_admin # => should we notify admins when people leave?
# attribute whitelist
#attr_accessible :attribute
validates_uniqueness_of :account_id, :scope => :portal_id
has_many :feed_items, :as => :feedable, :conditions => "feed_items.deleted_at IS NULL"
has_many :notifications, :as => :notifiable, :dependent => :destroy
# after_create :create_feed_item
# before_destroy :remove_feed_item_association
def create_feed_item
if (membership_feed_item = self.portal.feed_items.not_deleted.all(:limit => 5, :order => 'created_at DESC').find{|f| f.feedable_type == 'Membership'})
membership_feed_item.add_object(membership_feed_item.feedable || {:id => membership_feed_item.account_id, :account_id => membership_feed_item.account_id}) if membership_feed_item.object_count.zero?
else
membership_feed_item = self.feed_items.build(:portal_id => self.portal_id,
:action => FeedItem::JOINED,
:cache => {},
:is_public => true)
end
membership_feed_item.add_object(self)
membership_feed_item.feedable_id = self.id
membership_feed_item.account_id = self.account_id
membership_feed_item.created_at = Time.now
membership_feed_item.cache[:portal_name] = self.portal.long_name
membership_feed_item.save
end
def remove_feed_item_association
feed_item = self.portal.feed_items.not_deleted.of_type(self.class_s).first(:conditions => "feed_items.account_id = #{self.account_id} OR feed_items.cache REGEXP '- - #{self.account_id}'")
return unless feed_item
if feed_item.object_count > 1
feed_item.remove_object self
feed_item.account = feed_item.last_account
feed_item.feedable_id = feed_item.cache[:accounts].last.last
feed_item.save
else
feed_item.destroy
end
end
def account_access_level
self.account.account_access_levels.first(:joins => :account_group,
:conditions => "account_groups.portal_id = #{self.portal_id}")
end
def update_or_destroy
if self.account_access_level.nil?
self.destroy
elsif self.account_group_id != self.account_access_level.account_group_id
self.update_attribute(:account_group_id, self.account_access_level.account_group_id)
end
end
def self.hanging_memberships
self.all(:conditions => "not exists (select id, account_group_id, account_id from account_access_levels where account_access_levels.account_id = memberships.account_id and account_access_levels.account_group_id = memberships.account_group_id)")
end
# Accounts cannot join an organization more than once -- handled here (again) just in case
def validate
errors.add_to_base("You already are a part of this organization.") if Membership.find_by_account_id_and_portal_id(self.account_id, self.portal_id)
#errors.add_to_base("Sorry, this organization is not active. Please try again with an active organization") unless self.portal && self.portal.active?
end
# logs when someone joins an organization
def log_join_org
logger.info "Membership: ID: #{self.id} created by account ID: #{self.account_id} for org #{self.portal_id} at #{Time.now}."
end
# logs when someone leaves an organization
def log_unjoin_org
logger.info "Membership: ID: #{self.id} destroyed by account ID: #{self.account_id} for org #{self.portal_id} at #{Time.now}."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment