Skip to content

Commit

Permalink
Any object can use a Asset
Browse files Browse the repository at this point in the history
Remove coupling with AssetAssociation for Asset usage
functionality.

When a asset is updated (from Admin::AssetsController) the asset
touches its using objects.
  • Loading branch information
arvida committed Oct 4, 2013
1 parent e7cc73d commit 070d420
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 32 deletions.
1 change: 1 addition & 0 deletions app/controllers/admin/assets_controller.rb
Expand Up @@ -95,6 +95,7 @@ def update_multiple
def update
@asset = Asset.find(params[:id])
if @asset.update_attributes(params[:asset])
@asset.touch_usages
flash[:notice] = "#{@asset.full_name} #{t(:saved, :scope => [:app, :admin_general])}"
end
respond_with @asset, :location => (params[:return_to] || edit_admin_asset_url(@asset))
Expand Down
33 changes: 9 additions & 24 deletions app/models/asset.rb
Expand Up @@ -88,25 +88,19 @@ def of_the_type(filetype)
end

def usages
@usages ||= _usages.map do |u|
u.except('container_id')
end.uniq.collect { |u| u['usage_type'].constantize.find(u['usage_id']) }
@usages ||= AssetUsages.new(self)
end

def remove_usage(association)
usage = usage_from_association(association)
if _usages.include?(usage)
_usages.reject! { |u| u == usage }
save
end
def remove_usage object, context
usages.remove object, context
end

def add_usage(association)
usage = usage_from_association(association)
unless _usages.include?(usage)
_usages << usage
save
end
def add_usage object, context
usages.add object, context
end

def touch_usages
usages.each &:touch
end

class << self
Expand Down Expand Up @@ -172,13 +166,4 @@ def store
def cleanup
Porthos.s3_storage.destroy(full_name)
end

def usage_from_association(association)
{
'container_id' => association.id,
'usage_type' => association._root_document.class.model_name,
'usage_id' => association._root_document.id.to_s
}
end

end
56 changes: 56 additions & 0 deletions app/models/asset_usages.rb
@@ -0,0 +1,56 @@
class AssetUsages
include Enumerable

attr_reader :asset

def initialize asset
@asset = asset
end

def each &block
using_objects.each &block
end

def add object, context = nil
usage = new_usage object, context

unless asset_usages.include?(usage)
asset_usages << usage
save_asset
end
end

def remove object, context = nil
usage = new_usage object, context
asset_usages.reject! { |u| u == usage }
save_asset
end

private

def asset_usages
asset._usages
end

def save_asset
asset.save
end

def using_objects
uniq_usages.collect do |usage|
usage["usage_type"].constantize.find usage["usage_id"]
end
end

def uniq_usages
asset_usages.uniq {|usage| [usage["usage_id"], usage["usage_type"]] }
end

def new_usage object, context
{
"container_id" => context.to_s,
"usage_type" => object.class.model_name,
"usage_id" => object.id.to_s
}
end
end
13 changes: 7 additions & 6 deletions app/models/data/asset_association.rb
Expand Up @@ -87,14 +87,15 @@ def revert_to_asset_attributes

def notify_asset
if changes.include?(:asset_id)
unless asset_id_was.nil?
if old_asset = Asset.find(asset_id_was)
old_asset.remove_usage(self)
if old_asset = Asset.find(asset_id_was)
old_asset.remove_usage _root_document, self.id
end

if asset_id.present?
if new_asset = Asset.find(asset_id)
new_asset.add_usage _root_document, self.id
end
end
Asset.find(asset_id).tap do |new_asset|
new_asset.add_usage(self) if new_asset
end if asset_id.present?
end
end

Expand Down
8 changes: 7 additions & 1 deletion app/views/admin/assets/edit.html.erb
Expand Up @@ -90,7 +90,13 @@
<h2>Används på:</h2>
<ul>
<% @asset.usages.each do |usage| %>
<li><%= link_to usage.title, admin_item_path(usage) %></li>
<li>
<% if usage.is_a?(Item) %>
<%= link_to usage.title, admin_item_path(usage) %>
<% else %>
<%= link_to usage.name, [:admin, usage] %>
<% end %>
</li>
<% end %>
</ul>
</div>
Expand Down
2 changes: 1 addition & 1 deletion test/support/integration_case.rb
Expand Up @@ -39,4 +39,4 @@ def logout!
visit admin_logout_path
end

end
end
51 changes: 51 additions & 0 deletions test/unit/asset_usages_test.rb
@@ -0,0 +1,51 @@
require_relative "../test_helper"
require "minitest/autorun"

describe "AssetUsages" do
let(:item){ FactoryGirl.build :item, id: 123 }
let(:asset){ FactoryGirl.build :asset, created_by: FactoryGirl.build(:user) }
subject{ AssetUsages.new asset }

it "is initialized with a asset" do
subject.asset.must_equal asset
end

it "adds usages to asset" do
usage = {
"usage_type" => "Item",
"usage_id" => "123",
"container_id" => "a context",
}
subject.expects :save_asset

subject.add item, "a context"

asset._usages.must_equal [usage]
end

it "deletes usages from asset" do
asset.stubs _usages: [{
"usage_type" => "Item",
"usage_id" => "123",
"container_id" => "a context"
}]
subject.expects :save_asset

subject.remove item, "a context"

asset._usages.must_equal []
end

it "is enumerable" do
Item.stubs(:find).with("123").returns item
asset.stubs _usages: [{
"usage_type" => "Item",
"usage_id" => "123",
"container_id" => "a context"
}]
usages = []
subject.each {|using_object| usages << using_object }

usages.must_equal [item]
end
end

0 comments on commit 070d420

Please sign in to comment.