Created
November 27, 2011 17:09
-
-
Save rahulcn/1397836 to your computer and use it in GitHub Desktop.
method error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def owner_picture | |
default = "#{root_url}images/user/default.jpg" | |
image = "#{root_url}app/assets/images/user/#{User.first.profile.last_name.gsub(/ /,'').downcase}.jpg" | |
unless File.exist?(image) | |
image_tag image, :id => "user_picture" | |
else File.exist?(image) | |
image_tag default, :id => "user_picture" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
include ROXML | |
xml_accessor :email | |
xml_accessor :url | |
xml_accessor :profile, :as => Profile | |
field :email | |
field :url | |
has_one :profile, :class_name => 'Profile', :foreign_key => :person_id | |
has_many :posts, :class_name => 'Post', :foreign_key => :person_id | |
validates_presence_of :url | |
validates_format_of :url, :with => | |
/^(https?):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*(\.[a-z]{2,5})?(:[0-9]{1,5})?(\/.*)?$/ix | |
validates_uniqueness_of :url, :logic => lambda { self.url_unique?} | |
validates_presence_of :email | |
before_validation :clean_url | |
def real_name | |
profile.first_name.to_s + " " + profile.last_name.to_s | |
end | |
protected | |
def url_unique? | |
same_url = Person.first(:url => self.url) | |
return same_url.nil? || same_url.id == self.id | |
end | |
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 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post | |
require './lib/common' | |
include ApplicationHelper | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
include ROXML | |
xml_accessor :_id | |
xml_accessor :person, :as => Person | |
field :person_id | |
has_many :comments, :class_name => 'Comment', :foreign_key => :post_id | |
belongs_to :person, :class_name => 'Person' | |
cattr_reader :per_page | |
@@per_page = 10 | |
after_save :send_to_view | |
after_save :notify_friends | |
def self.stream | |
Post.sort(:created_at.desc).all | |
end | |
def self.newest(person = nil) | |
return self.last if person.nil? | |
self.first(:person_id => person.id, :order => '_id desc') | |
end | |
def self.my_newest | |
self.newest(User.first) | |
end | |
def self.newest_by_email(email) | |
self.newest(Person.first(:email => email)) | |
end | |
protected | |
def send_to_view | |
WebSocket.update_clients(self) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Profile | |
include Mongoid::Document | |
include ROXML | |
xml_accessor :first_name | |
xml_accessor :last_name | |
field :first_name | |
field :last_name | |
field :person_id | |
belongs_to :person | |
belongs_to :user | |
validates_presence_of :first_name, :last_name, :person_id | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable | |
has_one :profile, :class_name => 'Profile', :foreign_key => :person_id | |
has_many :posts, :class_name => 'Post', :foreign_key => :person_id | |
def comment(text, options = {}) | |
raise "Comment on what, motherfucker?" unless options[:on] | |
c = Comment.new(:person_id => self.id, :text => text, :post => options[:on]) | |
if c.save | |
if mine?(c.post) | |
c.push_to(c.post.friends_with_permissions) # should return plucky query | |
else | |
c.push_to([c.post.person]) | |
end | |
true | |
end | |
false | |
end | |
validates_presence_of :profile | |
before_validation :do_bad_things | |
def do_bad_things | |
self.password_confirmation = self.password | |
end | |
def mine?(post) | |
self == post.person | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment