Skip to content

Commit

Permalink
Reworking FeedEntry a bit to be as simple as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeychich committed Feb 2, 2009
1 parent 60585bb commit 42d50ba
Show file tree
Hide file tree
Showing 45 changed files with 241 additions and 593 deletions.
5 changes: 2 additions & 3 deletions app/models/blog.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# == Schema Information
# Schema version: 2
# Schema version: 20080919221242
#
# Table name: blogs
#
Expand All @@ -17,8 +17,7 @@ class Blog < ActiveRecord::Base
validates_presence_of :title, :body

def after_create
f = FeedEntry::NewBlog.create_from(self)
([profile] + profile.friends + profile.followers).each{ |p| p.feed_items << f }
FeedEntry.create_from(self)
end


Expand Down
9 changes: 4 additions & 5 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# == Schema Information
# Schema version: 2
# Schema version: 20080919221242
#
# Table name: comments
#
Expand All @@ -21,10 +21,9 @@ class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
belongs_to :profile

# def after_create
# feed_item = FeedItem.create(:item => self)
# ([profile] + profile.friends + profile.followers).each{ |p| p.feed_items << feed_item }
# end
def after_create
FeedEntry.create(self)
end


def self.between_profiles profile1, profile2
Expand Down
152 changes: 30 additions & 122 deletions app/models/feed_entry.rb
Original file line number Diff line number Diff line change
@@ -1,146 +1,54 @@
# == Schema Information
# Schema version: 20080919221242
#
# Table name: feed_entries
#
# id :integer(11) not null, primary key
# type :string(255)
# group_on_id :integer(11)
# targets :text
# count :integer(11) default(1), not null
# created_at :datetime
# updated_at :datetime
# group_on_type :string(255)
# metadata :text
#

class FeedEntry < ActiveRecord::Base
# While views in the model is not best practice, after talking to steve, I agree that this
# may be an exception. By using the view_ methods you should be able to set/update the cached view
# in the metadata hash. by using the metadata.view_ methods you should be able to pull up the
# cached view without needing it to be re-rendered. If you choose to store data this way, you can
# use the find_view_for method to reduce the size of your query.
cattr_accessor :template
@@template = ActionView::Base.new("#{RAILS_ROOT}/app/views")

class TargetTypeMismatch < StandardError; end
class FeatureNotImplemented < StandardError; end

belongs_to :group_on, :polymorphic => true
has_many :feed_entry_profiles
has_many :profiles, :through => :feed_entry_profiles, :uniq => true

# serialized fields dont set the dirty bit, so lets disable partial updates for this class
FeedEntry.partial_updates = false
serialize :targets, Array
serialize :metadata, Hash

before_save :update_metadata, :dump, :update_count

class << self
def new_from(record)
if can_be_grouped?
has_group_for(record) ? append_to_group(record) : new_entry_from(record)
else
has_entry_for(record) ? true : new_entry_from(record)
end
end

def create_from(record)
o = new_from(record)
o.save
o
end
def create_from!(record)
o = new_from(record)
o.save!
o
end

def has_group_for(record)
raise TargetTypeMismatch unless acceptable?(record)
find(:first, :conditions => conditions_for(record))
end
alias_method :group_for, :has_group_for
alias_method :has_entry_for, :has_group_for
alias_method :entry_for, :has_group_for

def append_to_group(record)
group_for(record).append(record)
end

def new_entry_from(record)
new({:targets => [record], :group_on => group_on_for(record)})
def create_from(record, type = nil)
klass = "FeedEntry::#{type || record.class.class_name}".constantize
raise TargetTypeMismatch unless klass.acceptable?(record)
o = klass.new(:ref_id => record.id)
o.set_associated if o.respond_to?(:set_associated)
o.can_be_grouped? ? o.group : o.save!
end



# class defaults
[:acceptable?, :conditions_for, :group_on_for, :partial].each do |m|
class_eval <<-EOS
def #{m}(record = nil)
raise 'Overide me in the descendant class'
end
EOS
def can_be_grouped?
false
end

def can_be_grouped?
def acceptable?
false
end
end








# Instance Methods

def append(record)
self.targets.push(record)
self
end

# expand the cached data to an acceptable form (original state)
def load
self.targets = self.targets.map{|t| self.class.acceptable?(t) ? t : Marshal.load(t) }.uniq
end

# cache the original data into a string (storage state)
def dump
self.targets = self.targets.map{|t| self.class.acceptable?(t) ? Marshal.dump(t) : t }.uniq
end

def update_metadata
self.metadata = { :text => {:title => title, :text => text} }
end


# give the cached content if available
def view(format = :html)
self.metadata[format] rescue nil
def acceptable?
self.class.acceptable?
end

def store_render(format = :html, locals = {})
self.metadata ||= {}
self.metadata[format] = render(format, locals)
def group
raise FeatureNotImplemented
end






# Do not touch anything below here
# Matthew Peychich

def can_be_grouped?
self.class.can_be_grouped?
end

def ref_obj
load
targets.first
end

[:title, :text].each do |method|
define_method(method){ raise 'Invalid Feed Entry! Overide me in the descendant class' }
end

def update_count
self.targets.uniq!
self.count = self.targets.size
end

def render(format = :html, locals = {})
self.load
template.template_format = format
template.render :partial => "/feed_entries/#{self.class.partial}", :locals => {:o => self}.merge(locals)
end

end
37 changes: 37 additions & 0 deletions app/models/feed_entry/blog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# == Schema Information
# Schema version: 20080919221242
#
# Table name: feed_entries
#
# id :integer(11) not null, primary key
# type :string(255)
# group_on_id :integer(11)
# targets :text
# count :integer(11) default(1), not null
# created_at :datetime
# updated_at :datetime
# group_on_type :string(255)
# metadata :text
#

class FeedEntry::Blog < FeedEntry
belongs_to :blog, :class_name => "::Blog", :foreign_key => "ref_id"

class << self
def acceptable?(record)
record.is_a?(Blog)
end
end

def display?
!!blog
end

def title
blog.title
end

def text
blog.summary
end
end
37 changes: 37 additions & 0 deletions app/models/feed_entry/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# == Schema Information
# Schema version: 20080919221242
#
# Table name: feed_entries
#
# id :integer(11) not null, primary key
# type :string(255)
# group_on_id :integer(11)
# targets :text
# count :integer(11) default(1), not null
# created_at :datetime
# updated_at :datetime
# group_on_type :string(255)
# metadata :text
#

class FeedEntry::Comment < FeedEntry
belongs_to :comment, :class_name => "Comment", :foreign_key => "ref_id"

class << self
def acceptable?(record)
record.is_a?(Blog)
end
end

def display?
!!comment
end

def title
"New Comment"
end

def text
comment.comment
end
end
30 changes: 0 additions & 30 deletions app/models/feed_entry/new_blog.rb

This file was deleted.

30 changes: 0 additions & 30 deletions app/models/feed_entry/new_comment.rb

This file was deleted.

38 changes: 0 additions & 38 deletions app/models/feed_entry/new_photo.rb

This file was deleted.

Loading

0 comments on commit 42d50ba

Please sign in to comment.