Skip to content
Extraits de code Groupes Projets
person.rb 2,83 ko
Newer Older
  • Learn to ignore specific revisions
  • maxwell's avatar
    maxwell a validé
      include MongoMapper::Document
    
      xml_accessor :_id
    
    Raphael's avatar
    Raphael a validé
      xml_accessor :serialized_key
    
      xml_accessor :profile, :as => Profile
      
    
      key :email, String, :unique => true
    
    
      key :serialized_key, String 
    
      key :user_refs, Integer, :default => 0
    
    
      belongs_to :owner, :class_name => 'User'
    
      one :profile, :class_name => 'Profile'
    
    maxwell's avatar
    maxwell a validé
      many :posts, :class_name => 'Post', :foreign_key => :person_id
    
    maxwell's avatar
    maxwell a validé
      many :albums, :class_name => 'Album', :foreign_key => :person_id
    
    
      validates_presence_of :email, :url, :serialized_key, :profile
    
      validates_format_of :url, :with =>
         /^(https?):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*(\.[a-z]{2,5})?(:[0-9]{1,5})?(\/.*)?$/ix
    
      after_destroy :remove_all_traces
    
        "#{profile.first_name.to_s} #{profile.last_name.to_s}"
    
        OpenSSL::PKey::RSA.new( serialized_key )
      end
    
      def key= new_key
        raise TypeError unless new_key.class == OpenSSL::PKey::RSA
        serialized_key = new_key.export
    
        key.public_key.export
    
    
      ######## Posting ########
      def post(class_name, options = {})
        options[:person] = self
        model_class = class_name.to_s.camelize.constantize
        post = model_class.instantiate(options)
    
    Raphael's avatar
    Raphael a validé
        if owns?(post)
          post.notify_people
        end
    
      end
    
      ######## Commenting  ########
      def comment(text, options = {})
        raise "must comment on something!" unless options[:on]
        c = Comment.new(:person_id => self.id, :text => text, :post => options[:on])
        if c.save
    
    Raphael's avatar
    Raphael a validé
          send_comment c
          true
        else
          Rails.logger.warn "this failed to save: #{c.inspect}"
        end
        false
      end
      
      def send_comment c
        if self.owner.nil?
    
    Raphael's avatar
    Raphael a validé
            if c.post.person.owner.nil?
              #puts "The commenter is not here, and neither is the poster"
            elsif c.post.person.owner
              #puts "The commenter is not here, and the poster is"
              c.push_downstream
            end
    
    Raphael's avatar
    Raphael a validé
            if owns? c.post
              #puts "The commenter is here, and is the poster"
              c.push_downstream
            else
              #puts "The commenter is here, and is not the poster"
              c.push_upstream
            end
    
      end
      ##profile
      def update_profile(params)
        if self.update_attributes(params)
          self.profile.notify_people!
          true
        else
          false
        end
    
    Raphael's avatar
    Raphael a validé
      def owns?(post)
    
        puts self.class
    
    maxwell's avatar
    maxwell a validé
        self.id == post.person.id
    
      def clean_url
        self.url ||= "http://localhost:3000/" if self.class == User
        if self.url
          self.url = 'http://' + self.url unless self.url.match('http://' || 'https://')
          self.url = self.url + '/' if self.url[-1,1] != '/'
        end
      end