Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
7 changed files
with
132 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,4 @@ def logout! | |
visit admin_logout_path | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |