diff options
author | Xavier Noria <fxn@hashref.com> | 2010-02-02 00:37:15 +0100 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-02-02 00:37:33 +0100 |
commit | 27fa38cc3e162b2f8c4bb2d50130fbc17bad6039 (patch) | |
tree | 54e2d611515bd33b1010076494eb1013bddf79cd /railties/guides/source/active_support_core_extensions.textile | |
parent | 7ec7328f2af7eb82d4377cfe9fc3f3296feb184d (diff) | |
download | rails-27fa38cc3e162b2f8c4bb2d50130fbc17bad6039.tar.gz rails-27fa38cc3e162b2f8c4bb2d50130fbc17bad6039.tar.bz2 rails-27fa38cc3e162b2f8c4bb2d50130fbc17bad6039.zip |
AS guide: documents Class#class_attribute
Diffstat (limited to 'railties/guides/source/active_support_core_extensions.textile')
-rw-r--r-- | railties/guides/source/active_support_core_extensions.textile | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index 562bc76135..fb4c42f118 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -496,7 +496,42 @@ The class method +delegate+ h3. Extensions to +Class+ -h4. Class Attribute Accessors +h4. Class Attributes + +The method +Class#class_attribute+ declares one or more inheritable class attributes that can be overriden at any level down the hierarchy: + +<ruby> +class A + class_attribute :x +end + +class B < A; end + +class C < B; end + +A.x = :a +B.x # => :a +C.x # => :a + +B.x = :b +A.x # => :a +C.x # => :b + +C.x = :c +A.x # => :a +B.x # => :b +</ruby> + +For example that's the way the +allow_forgery_protection+ flag is implemented for controllers: + +<ruby> +class_attribute :allow_forgery_protection +self.allow_forgery_protection = true +</ruby> + +For convenience +class_attribute+ defines also a predicate, so that declaration also generates +allow_forgery_protection?+. Such predicate returns the double boolean negation of the value. + +NOTE: Defined in +active_support/core_ext/class/attribute.rb+ The macros +cattr_reader+, +cattr_writer+, and +cattr_accessor+ are analogous to their +attr_*+ counterparts but for classes. They initialize a class variable to +nil+ unless it already exists, and generate the corresponding class methods to access it: |