Refactor Relayable Creation

This commit is contained in:
Dennis Collinson 2012-02-09 00:05:47 -08:00 committed by Maxwell Salzberg
parent 8ae6303a91
commit b0e81af1fa
9 changed files with 119 additions and 84 deletions

View file

@ -84,4 +84,19 @@ class Comment < ActiveRecord::Base
def parent= parent def parent= parent
self.post = parent self.post = parent
end end
class Generator < Federated::Generator
def self.federated_class
Comment
end
def initialize(person, target, text)
@text = text
super(person, target)
end
def relayable_options
{:post => @target, :text => @text}
end
end
end end

View file

@ -1,42 +0,0 @@
class FederatedRelayable < ActiveRecord::Base
self.abstract_class = true
#crazy ordering issues - DEATH TO ROXML
include ROXML
include Diaspora::Webhooks
include Diaspora::Guid
#seriously, don't try to move this shit around until you have killed ROXML
xml_attr :target_type
include Diaspora::Relayable
xml_attr :diaspora_handle
belongs_to :target, :polymorphic => true
belongs_to :author, :class_name => 'Person'
#end crazy ordering issues
validates_uniqueness_of :target_id, :scope => [:target_type, :author_id]
validates :parent, :presence => true #should be in relayable (pending on fixing Message)
def diaspora_handle
self.author.diaspora_handle
end
def diaspora_handle=(nh)
self.author = Webfinger.new(nh).fetch
end
def parent_class
self.target_type.constantize
end
def parent
self.target
end
def parent= parent
self.target = parent
end
end

View file

@ -2,7 +2,17 @@
# licensed under the Affero General Public License version 3 or later. See # licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file. # the COPYRIGHT file.
class Like < FederatedRelayable class Like < Federated::Relayable
class Generator < Federated::Generator
def self.federated_class
Like
end
def relayable_options
{:target => @target, :positive => true}
end
end
after_create do after_create do
self.parent.update_likes_counter self.parent.update_likes_counter
end end

View file

@ -1,3 +1,11 @@
class Participation < FederatedRelayable class Participation < Federated::Relayable
class Generator < Federated::Generator
def self.federated_class
Participation
end
def relayable_options
{:target => @target}
end
end
end end

View file

@ -285,52 +285,19 @@ class User < ActiveRecord::Base
end end
def comment!(post, text, opts={}) def comment!(post, text, opts={})
comment = build_comment(opts.merge!(:post => post, :text => text)) Comment::Generator.new(self.person, post, text).create!(opts)
if comment.save
dispatch_post(comment)
comment
else
false
end
end end
def participate!(target, opts={}) def participate!(target, opts={})
participation = build_participation(opts.merge!(:target => target)) Participation::Generator.new(self.person, target).create!(opts)
if participation.save
dispatch_post(participation)
participation
else
false
end
end end
def like!(target, opts={}) def like!(target, opts={})
like = build_like(opts.merge!(:target => target, :positive => true)) Like::Generator.new(self.person, target).create!(opts)
if like.save
dispatch_post(like)
like
else
false
end
end end
def build_relayable(model, options = {}) def build_comment(options={})
r = model.new(options.merge(:author_id => self.person.id)) Comment::Generator.new(self.person, options.delete(:post), options.delete(:text)).build(options)
r.set_guid
r.initialize_signatures
r
end
def build_comment(options = {})
build_relayable(Comment, options)
end
def build_participation(options = {})
build_relayable(Participation, options)
end
def build_like(options = {})
build_relayable(Like, options)
end end
# Check whether the user has liked a post. # Check whether the user has liked a post.

View file

@ -0,0 +1,32 @@
module Federated
class Generator
def initialize(person, target)
@person = person
@target = target
end
def build(options={})
options.merge!(relayable_options)
relayable = self.class.federated_class.new(options.merge(:author_id => @person.id))
relayable.set_guid
relayable.initialize_signatures
relayable
end
def create!(options={})
relayable = build(options)
if relayable.save
Postzord::Dispatcher.defer_build_and_post(@person, relayable)
relayable
else
false
end
end
protected
def relayable_options
{}
end
end
end

View file

@ -0,0 +1,44 @@
module Federated
class Relayable < ActiveRecord::Base
self.abstract_class = true
#crazy ordering issues - DEATH TO ROXML
include ROXML
include Diaspora::Webhooks
include Diaspora::Guid
#seriously, don't try to move this shit around until you have killed ROXML
xml_attr :target_type
include Diaspora::Relayable
xml_attr :diaspora_handle
belongs_to :target, :polymorphic => true
belongs_to :author, :class_name => 'Person'
#end crazy ordering issues
validates_uniqueness_of :target_id, :scope => [:target_type, :author_id]
validates :parent, :presence => true #should be in relayable (pending on fixing Message)
def diaspora_handle
self.author.diaspora_handle
end
def diaspora_handle=(nh)
self.author = Webfinger.new(nh).fetch
end
def parent_class
self.target_type.constantize
end
def parent
self.target
end
def parent= parent
self.target = parent
end
end
end

View file

@ -90,7 +90,7 @@ describe Like do
@object_on_remote_parent = @local_luke.like!(@remote_parent) @object_on_remote_parent = @local_luke.like!(@remote_parent)
end end
let(:build_object) { alice.build_like(:target => @status, :positive => true) } let(:build_object) { Like::Generator.new(alice.person, @status).build }
it_should_behave_like 'it is relayable' it_should_behave_like 'it is relayable'
end end
end end

View file

@ -16,7 +16,8 @@ describe Participation do
@object_on_remote_parent = @local_luke.participate!(@remote_parent) @object_on_remote_parent = @local_luke.participate!(@remote_parent)
end end
let(:build_object) { alice.build_participation(:target => @status) } let(:build_object) { Participation::Generator.new(alice.person, @status).build }
it_should_behave_like 'it is relayable' it_should_behave_like 'it is relayable'
end end
end end