lib/netzke/basepack/attributes.rb in netzke-basepack-1.0.0.1 vs lib/netzke/basepack/attributes.rb in netzke-basepack-1.0.1.0
- old
+ new
@@ -41,12 +41,32 @@
# end
#
# component :roles
# end
#
+ # Another way to override attributes is by overriding the +augment_attribute_config+ method:
+ #
+ # class Users < Netzke::Grid::Base
+ # def configure(c)
+ # super
+ # c.model = User
+ # end
+ #
+ # def augment_attribute_config(c)
+ # super
+ # c.read_only = true if [:address, :salary].include?(c.name)
+ # end
+ # end
+ #
# The following attribute config options are available:
#
+ # [label]
+ #
+ # Field label and/or column title used for this attribute. Defaults to
+ # `ActiveRecord::Base.human_attribute_name(attribute)`, which means that this value will be localized according to
+ # Rails conventions.
+ #
# [read_only]
#
# A boolean that defines whether the attribute should be editable via grid/form.
#
# [getter]
@@ -154,23 +174,38 @@
# we *must* use a writer here
self.declared_attribute_names = declared_attribute_names + [name]
end
end
+ # Returns the list of (non-normalized) attributes to be used.
+ # Can be overridden.
+ def attributes
+ config.attributes || model_adapter.model_attributes
+ end
+
def attribute_overrides
return @attribute_overrides if @attribute_overrides
- declared = self.class.declared_attribute_names.reduce({}) do |res, name|
- c = AttributeConfig.new(name)
+ declared = (attributes | self.class.declared_attribute_names).reduce({}) do |res, name|
+ c = Netzke::Basepack::AttrConfig.new(name, model_adapter)
augment_attribute_config(c)
res.merge!(name => c)
end
@attribute_overrides = (config.attribute_overrides || {}).deep_merge(declared)
end
+ # Extends passed column config with DSL declaration for this column
+ def apply_attribute_dsl(c)
+ method_name = ATTRIBUTE_METHOD_NAME % c.name
+ send(method_name, c) if respond_to?(method_name)
+ end
+
+ # Receives a +Netzke::Basepack::AttrConfig+ with minimum attribute configuration and extends it according to the
+ # attribute's type. May be overridden.
def augment_attribute_config(c)
- send(ATTRIBUTE_METHOD_NAME % c.name, c)
+ apply_attribute_dsl(c)
+ c.set_defaults
end
def association_attr?(attr)
!!attr[:name].to_s.index("__")
end