aboutsummaryrefslogtreecommitdiffstats
path: root/lib/model_attribute.rb
diff options
context:
space:
mode:
authorTeddy Wing2017-09-22 14:10:45 +0200
committerTeddy Wing2017-09-22 14:10:45 +0200
commit2d7ce83439f29d261f0c5ec05140359218cfe661 (patch)
tree8b958dc8a247721b6ac3f914419bad0257cfec21 /lib/model_attribute.rb
parent9086e4c7d253927e7fad07bcd4eb194eeff393ca (diff)
downloadchouette-core-2d7ce83439f29d261f0c5ec05140359218cfe661.tar.bz2
ModelAttribute: Add `#from_code` method
Enables finding a `ModelAttribute` by a string code combining klass and name. Refs #4401
Diffstat (limited to 'lib/model_attribute.rb')
-rw-r--r--lib/model_attribute.rb8
1 files changed, 8 insertions, 0 deletions
diff --git a/lib/model_attribute.rb b/lib/model_attribute.rb
index 78c9168eb..60580e306 100644
--- a/lib/model_attribute.rb
+++ b/lib/model_attribute.rb
@@ -21,6 +21,14 @@ class ModelAttribute
all.group_by(&:klass)
end
+ def self.from_code(code)
+ klass, name = code.split('#').map(&:to_sym)
+
+ methods_by_class(klass).select do |model_attr|
+ model_attr.name == name
+ end.first
+ end
+
def self.methods_by_class(klass)
all.select do |model_attr|
model_attr.klass == klass