app/models/account.rb in has_accounts-3.0.0.beta1 vs app/models/account.rb in has_accounts-3.0.0.beta2
- old
+ new
@@ -49,18 +49,36 @@
end
# Tagging
# =======
if defined?(ActsAsTaggableOn) && ActsAsTaggableOn::Tag.table_exists?
+ class Account::AmbiguousTag < StandardError
+ def initialize(tag, count)
+ @tag = tag
+ @count = count
+ end
+
+ def to_s
+ "Given tag '#{@tag}' is ambiguous, found #{@count} records"
+ end
+ end
+
acts_as_taggable
attr_accessible :tag_list
def self.default_tags
['invoice:debit', 'invoice:earnings', 'invoice:credit', 'invoice:costs', 'vat:credit', 'vat:debit']
end
def self.tag_collection
(default_tags + Account.tag_counts.pluck(:name)).uniq
+ end
+
+ def self.find_by_tag(tag)
+ accounts = tagged_with(tag)
+ count = accounts.count
+ raise Account::AmbiguousTag.new(tag, count) if count > 1
+ accounts.first
end
end
# Holder
# ======