Skip to content

Instantly share code, notes, and snippets.

@petrokoriakin
Created April 20, 2012 11:45
Show Gist options
  • Save petrokoriakin/2427971 to your computer and use it in GitHub Desktop.
Save petrokoriakin/2427971 to your computer and use it in GitHub Desktop.
code sample
class Identity < ActiveRecord::Base
belongs_to :user
has_one :settings
has_many :text_notifiers
has_many :mail_notifiers
has_many :favorites
has_many :saved_searches
has_many :utterances, :foreign_key => :author_id
has_many :claims do
def active
select{|claim| claim.active?}
end
end
has_many :postings, :through => :claims
has_many :conversations_other, :class_name => 'Conversation', :foreign_key => 'other_party_id' do
def to_json
"[{" + self.collect{|conversation| conversation.to_json}.join("}, {") + "}]"
end
end
has_many :comments do
def mark_as_removed(post_key, comment_id)
if marked_comment = self.detect{|comment| comment.id == comment_id && comment.postKey == post_key}
marked_comment.update_attribute(:is_removed, true)
else
false
end
end
end
has_many :recipient_statuses, :foreign_key => :partner_id, :class_name => 'PartnerStatus'
has_many :partner_statuses
has_many :partners, :through => :partner_statuses, :foreign_key => :partner_id, :class_name => 'Identity'
validates_uniqueness_of :identity_id, :on => :save
validates_uniqueness_of :identity_key, :on => :save
validates_uniqueness_of :name #, :allow_nil => true
validates :name , :presence=>true
def own_postings
claims.active.collect{|claim| claim.posting.post_key}
end
def collect_statuses
partner_statuses = conversations.inject({}) do |hash, conversation|
if conversation.is_answered?
hash[conversation.owner.id] ||= conversation.owner.availability_status(id)
hash[conversation.other_party_id] ||= conversation.partner.availability_status(id)
end
hash
end
partner_statuses = if user_id
partner_statuses.except(*user.identity_ids)
else
partner_statuses.except(id)
end
partner_statuses.collect{|status| status[1]}.sort_by{|status| status[:recentActivity] ? (-1 * status[:recentActivity]) : (-1 * status[:statusUpdated])}
end
def update_activity
if user_id && status_string != user.last_status_string
range = case status_string
when "available" then Posting::OWNER_AVAILABLE
when "sms-available" then Posting::OWNER_SMS_AVAILABLE
when "idle" then Posting::OWNER_IDLE
when "unavailable" then Posting::OWNER_UNAVAILABLE
else 0
end
TapsConnector.update_posting_activity("[#{claims.active.collect{|c| c.posting.data_item_for_update(range)}.join(',')}]")
user.update_last_status(status_string)
end
end
def availability_status(recipient_id = nil)
update_activity
recipient_status = recipient_statuses.where(:identity_id => recipient_id).first.try(:hash_item) || {}
if user_id
recipient_status[:blocksMe] = partner_statuses.where(:partner_id => recipient_id).first.try(:status) == PartnerStatus::BLOCKED
else
recipient_status[:blocksMe] = !is_available?
end
{
:id => id,
:identityID => identity_id,
:name => actual_name,
:avatarURL => avatar_url,
:availability => is_available?(recipient_id),
:statusUpdated => status_updated,
:is_blocked => user_id ? user.is_blocked : false,
:smsAvailable => sms_available?
}.merge recipient_status
end
def sms_available?
text_notifiers.exists?(:notify => TextNotifier::ACTIVE_MESSAGE_SENT, :default => true) || text_notifiers.exists?(:notify => TextNotifier::ACTIVE_MESSAGE_AND_UNAVAILABLE, :default => true)
end
def status_string
return "self-blocked" if blocked?
if !is_available?
return sms_available? ? "sms-available" : "unavailable"
end
if (Time.now.to_i - status_updated) > 900
return sms_available? ? "sms-available" : "idle"
end
return "available"
end
def blocked?(partner_id = nil)
if user_id && user.is_blocked
true
elsif partner_statuses.where(:partner_id => partner_id).first.try(:status) == PartnerStatus::BLOCKED
true
else
false
end
end
def is_available?(partner_id = nil)
user_id ? user.is_available? : try(:last_activity) ? 180 > (Time.now.to_i - last_activity) : true
end
def actual_name
user_id ? name : "Anonymous#{id}"
end
def status_updated
if user_id
user.status_updated ? user.status_updated : 0
else
last_activity
end
end
def update_recent_activity(partner_id)
# if partner_status = partner_statuses.where(:partner_id => partner_id).first
# partner_status.update_attribute(:recent_activity, Time.now.to_i)
if status = recipient_statuses.where(:identity_id => partner_id).first
status.update_attribute(:recent_activity, Time.now.to_i)
else
recipient_statuses.create(:status => 0, :identity_id => partner_id, :recent_activity => Time.now.to_i)
end
end
def close_conversation(conversation_id)
if exist_chat = conversations.detect{|c| c.id.to_i == conversation_id.to_i}
if user_id && user.identity_ids.include?(exist_chat.owner.id)
return exist_chat.update_attribute(:active_owner, false)
end
exist_chat.update_attribute(:active_other_party, false)
else
nil
end
end
def archive_conversation(conversation_id)
if exist_chat = conversations.detect{|c| c.id.to_i == conversation_id.to_i}
if user_id && user.identity_ids.include?(exist_chat.owner.id)
if exist_chat.archive_owner
return exist_chat.update_attribute(:archive_owner, false)
else
return exist_chat.update_attribute(:archive_owner, true)
end
end
if exist_chat.archive_partner
return exist_chat.update_attribute(:archive_partner, false)
else
return exist_chat.update_attribute(:archive_partner, true)
end
else
nil
end
end
def add_utterance(conversation_id, text)
exist_chat = conversations.detect{|c| c.id == conversation_id.to_i}
exist_chat.claim.posting.update_activity(Posting::CONVERSATION_IN_PROGRESS)
utterance = exist_chat.utterances.create(:text => text)
exist_chat.update_attributes(:active_owner => true, :active_other_party => true)
if !user_id
utterance.update_attribute(:author_id, exist_chat.other_party_id)
update_recent_activity(exist_chat.owner.id)
elsif user.identity_ids.include?(exist_chat.owner.id)
utterance.update_attribute(:author_id, exist_chat.owner.id)
update_recent_activity(exist_chat.other_party_id)
else
utterance.update_attribute(:author_id, exist_chat.other_party_id)
update_recent_activity(exist_chat.owner.id)
end
if (recipient_statuses.where(:identity_id => exist_chat.owner.id).first.try(:status) == PartnerStatus::BLOCKED) || (recipient_statuses.where(:identity_id => exist_chat.other_party_id).first.try(:status) == PartnerStatus::BLOCKED)
utterance.update_attribute(:conversation_id, nil)
return false
end
utterance
end
def activate_or_create_conversation(post_key)
posting = Posting.find_by_post_key(post_key)
posting.update_activity(Posting::CONVERSATION_IN_PROGRESS)
update_recent_activity(posting.owner.try :id)
if exist_chat = conversations.detect{|c| c.posting.post_key == post_key}
if exist_chat.claim.identity.user_id == user_id
exist_chat.update_attribute(:active_owner, true)
else
update_recent_activity(posting.owner.try :id)
exist_chat.update_attribute(:active_other_party, true)
end
exist_chat
elsif !user_id
posting.owner.update_recent_activity(id)
posting.conversations.create(:post_key => post_key, :claim_id => posting.claims.active.try(:id), :other_party_id => id, :active_owner => false)
elsif !user.identity_ids.include?(posting.owner.try(:id))
update_recent_activity(posting.owner.try :id)
posting.conversations.create(:post_key => post_key, :claim_id => posting.claims.active.try(:id), :other_party_id => id, :active_owner => false)
else
nil
end
end
def conversations_owner
claims.collect{|claim| claim.conversations}.flatten
end
def conversations
if user_id
user.identities.collect{|identity| identity.conversations_owner + identity.conversations_other}.flatten
else
conversations_owner + conversations_other
end
end
def generate_key
self.identity_key = generate_identity_id_key_name("key", 12)
self.is_active = true unless provider == 'alias'
save
end
def generate_nil_key
update_attributes(:is_active => false)
end
def all_fields
{
:id => id,
:userID => user_id ? user_id : id,
:aliases => user_id ? user.identities.list_names : {},
:provider => provider,
:identityID => identity_id,
:identityKey => identity_key,
:is_blocked => user_id ? user.is_blocked : false,
:phone => user_id ? user.phone : '',
:notifications => notifications
}.merge!(public_fields)
end
def public_fields
response = {
:name => name,
:avatarURL => avatar_url,
:publicProfile => public_profile,
:websiteURL => website_url,
:email => user_id ? user.email : ""
}
response[:twitterID] = provider == 'twitter' ? name : nil
response[:facebookID] = provider == 'facebook' ? name : nil
response[:linkedinID] = provider == 'linked_in' ? name : nil
response
end
def private_fields
{
:commentsSearchable => comments_searchable,
:revealIdentityOnTrade => reveal_identity_on_trade,
:revealPriceOnTrade => reveal_price_on_trade,
:revealReferenceOnTrade => reveal_reference_on_trade
}.merge!(public_fields)
end
private
before_create do |identity|
identity.identity_id = generate_identity_id_key_name("id", 36)
identity.identity_key = generate_identity_id_key_name("key", 12)
#identity.is_active = true
end
def generate_identity_id_key_name(param, sym)
arr = []
arr << Digest::SHA2.new
arr[0] << param + Time.now.to_s + Date.today.to_s
arr[0].hexdigest.first(sym).upcase
end
end
@petrokoriakin
Copy link
Author

У вас в универе были лабы по руби? Действительно - хорошие времена :).
Код я поправил. Теперь можно сдавать.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment