Skip to content

Commit

Permalink
add embed model
Browse files Browse the repository at this point in the history
  • Loading branch information
reddavis committed Mar 16, 2009
1 parent b00b85d commit 75a3dd4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/models/comment.rb
Expand Up @@ -21,10 +21,13 @@ class Comment < ActiveRecord::Base

belongs_to :commentable, :polymorphic => true
belongs_to :profile


has_many :embeds, :as => :embed_owner

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


Expand All @@ -36,4 +39,14 @@ def self.between_profiles profile1, profile2
profile1.id, profile2.id, profile2.id, profile1.id]
})
end

private

def create_embeds
comment.scan(EmbeditRuby::REGEX).each do |video|
url = video.match(EmbeditRuby::EXTRACT_URL)[1].strip!
embed = embeds.create(:url => url)
end
end

end
19 changes: 19 additions & 0 deletions app/models/embed.rb
@@ -0,0 +1,19 @@
class Embed < ActiveRecord::Base

validates_presence_of :html, :title, :url

belongs_to :embed_owner, :polymorphic => true

before_validation :add_information

private

def add_information
embed = EmbeditRuby::Url.new(url)
if embed.valid? == 'true'
self.title = embed.title
self.html = embed.html
end
end

end
16 changes: 16 additions & 0 deletions db/migrate/20090314141851_create_embeds.rb
@@ -0,0 +1,16 @@
class CreateEmbeds < ActiveRecord::Migration
def self.up
create_table :embeds do |t|
t.string :embed_owner_type
t.integer :embed_owner_id
t.string :url
t.string :title
t.text :html
t.timestamps
end
end

def self.down
drop_table :embeds
end
end
7 changes: 7 additions & 0 deletions test/fixtures/embeds.yml
@@ -0,0 +1,7 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

# one:
# column: value
#
# two:
# column: value
9 changes: 9 additions & 0 deletions test/unit/embed_test.rb
@@ -0,0 +1,9 @@
require 'test_helper'

class EmbedTest < ActiveSupport::TestCase

context 'An embed instance' do
# should_belong_to :embed_owner
end

end

0 comments on commit 75a3dd4

Please sign in to comment.