रेल 3.1 वाक्य रचना ActiveSupport द्वारा एक छोटे से सरल है के रूप में है: : चिंता:
अब आप कर सकते हैं
require 'active_support/concern'
module M
extend ActiveSupport::Concern
included do
scope :disabled, where(:disabled => true)
end
module ClassMethods
...
end
end
ActiveSupport :: चिंता भी शामिल मॉड्यूल के निर्भरता में स्वीप, here is the documentation
[अद्यतन, को संबोधित aceofbassgreg की टिप्पणी]
रेल 3.1 और बाद ActiveSupport :: चिंता सीधे शामिल होने के लिए एक मॉड्यूल के उदाहरण तरीकों में शामिल की अनुमति देता है , ताकि शामिल मॉड्यूल के अंदर एक InstanceMethods मॉड्यूल बनाना आवश्यक नहीं है। इसके अलावा रेल 3.1 में और बाद में एम :: इंस्टेंस मोड्स को शामिल करने और एम :: क्लासमेड्स का विस्तार करने के लिए अब आवश्यक नहीं है।
require 'active_support/concern'
module M
extend ActiveSupport::Concern
# foo will be an instance method when M is "include"'d in another class
def foo
"bar"
end
module ClassMethods
# the baz method will be included as a class method on any class that "include"s M
def baz
"qux"
end
end
end
class Test
# this is all that is required! It's a beautiful thing!
include M
end
Test.new.foo # ->"bar"
Test.baz # -> "qux"
स्रोत
2013-01-07 23:13:50
क्या सत्यापन और संबंध इस तरह से परिभाषित किए जा सकते हैं? – CantGetANick
100% नहीं कह सकता है, लेकिन मुझे नहीं लगता कि क्यों नहीं। –
याप, और यह काम करता है। उदाहरण के लिए धन्यवाद। – CantGetANick