Skip to content

Commit

Permalink
make attachments polymorphic
Browse files Browse the repository at this point in the history
  • Loading branch information
gerrit committed Mar 21, 2011
1 parent 21c8fd6 commit 4a3fd5b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
7 changes: 6 additions & 1 deletion app/models/asset.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Asset < ActiveRecord::Base
has_many :attachments, :include => :page, :dependent => :destroy
has_many :attachments, :as => :attachable, :dependent => :destroy

# HACK: incomplete
AUDIO_FORMATS = [:wav, :mp3, :m4a, :ogg]
VIDEO_FORMATS = [:mp4, :avi]
Expand All @@ -8,6 +9,10 @@ class Asset < ActiveRecord::Base
validates_presence_of :upload
delegate :url, :width, :height, :landscape, :portrait, :to => :upload

def page_attachments
attachments.select(&:attached_to_page?)
end

def uploads
[]
end
Expand Down
18 changes: 13 additions & 5 deletions app/models/attachment.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'acts_as_list'

class Attachment < ActiveRecord::Base
belongs_to :asset, :autosave => true
belongs_to :page
belongs_to :attachable, :polymorphic => true, :autosave => true
belongs_to :parent, :polymorphic => true

acts_as_list :scope => :page_id
acts_as_list :scope => :parent_id

def self.reorder(new_order)
new_order.each_with_index do |id, index|
Expand All @@ -13,11 +13,19 @@ def self.reorder(new_order)
new_order
end

def attached_to_page?
parent_type == 'Page'
end

def asset
attachable if attachable_type == 'Asset'
end

def uploads=(new_uploads)
(asset || build_asset).uploads=new_uploads
(self.attachable ||= Asset.new).uploads=new_uploads
end

def uploads
(asset || build_asset).uploads
(self.attachable ||= Asset.new).uploads
end
end
8 changes: 4 additions & 4 deletions app/views/admin/assets/remove.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

<table class="index" id="assets">
<tr><td class="name"><%= asset_listing(@asset) %></td></tr>
<% if @asset.attachments.any? %>
<% if @asset.page_attachments.any? %>
<tr>
<th>
It's being used on the following
<%= pluralize(@asset.attachments.size, 'page') %> and will be removed from
<%= pluralize(@asset.page_attachments.size, 'page') %> and will be removed from
there as well:
</th>
</tr>
<% @asset.attachments.each do |attachment| %>
<tr><td><%= attachment.page.title %></td></tr>
<% @asset.page_attachments.each do |attachment| %>
<tr><td><%= attachment.parent.title %></td></tr>
<% end %>
<% end %>
</table>
Expand Down
2 changes: 1 addition & 1 deletion assets_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def activate
ApplicationController.helper(:assets)
Page.class_eval do
include AssetTags
has_many :attachments, :include => :asset, :order => :position
has_many :attachments, :as => :parent, :include => :attachable, :order => :position, :dependent => :destroy
end
end
end
17 changes: 17 additions & 0 deletions db/migrate/20110321005357_make_attachments_polymorphic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class MakeAttachmentsPolymorphic < ActiveRecord::Migration
def self.up
rename_column :attachments, :page_id, :parent_id
add_column :attachments, :parent_type, :string
Attachment.update_all 'parent_type = "Page"'

rename_column :attachments, :asset_id, :attachable_id
add_column :attachments, :attachable_type, :string
Attachment.update_all 'attachable_type = "Asset"'
end

def self.down
warn 'This will delete all Attachments that aren’t attached to pages'
remove_column :attachments, :parent_type
rename_column :attachments, :parent_id, :page_id
end
end

0 comments on commit 4a3fd5b

Please sign in to comment.