Define or configure an attribute. Example:
netzke_attribute :recent, :type => :boolean, :read_only => true
# File lib/netzke/active_record/attributes.rb, line 8
8: def netzke_attribute(name, options = {})
9: name = name.to_s
10: options[:attr_type] = options.delete(:type) || options.delete(:attr_type) || :string
11: declared_attrs = read_inheritable_attribute(:netzke_declared_attributes) || []
12: # if the attr was declared already, simply merge it with the new options
13: existing = declared_attrs.detect{ |va| va[:name] == name }
14: if existing
15: existing.merge!(options)
16: else
17: attr_config = {:name => name}.merge(options)
18: # if primary_key, insert in front, otherwise append
19: if name == self.primary_key
20: declared_attrs.insert(0, attr_config)
21: else
22: declared_attrs << {:name => name}.merge(options)
23: end
24: end
25: write_inheritable_attribute(:netzke_declared_attributes, declared_attrs)
26: end
# File lib/netzke/active_record/attributes.rb, line 51
51: def netzke_attribute_hash
52: netzke_attributes.inject({}){ |r,a| r.merge(a[:name].to_sym => a) }
53: end
Returns the attributes that will be picked up by grids and forms.
# File lib/netzke/active_record/attributes.rb, line 46
46: def netzke_attributes
47: exposed = netzke_exposed_attributes
48: exposed ? netzke_attrs_in_forced_order(exposed) : netzke_attrs_in_natural_order
49: end
Exclude attributes from being picked up by grids and forms. Accepts an array of attribute names (as symbols). Example:
netzke_expose_attributes :created_at, :updated_at, :crypted_password
# File lib/netzke/active_record/attributes.rb, line 32
32: def netzke_exclude_attributes(*args)
33: write_inheritable_attribute(:netzke_excluded_attributes, args.map(&:to_s))
34: end
Explicitly expose attributes that should be picked up by grids and forms. Accepts an array of attribute names (as symbols). Takes precedence over netzke_exclude_attributes. Example:
netzke_expose_attributes :name, :role__name
# File lib/netzke/active_record/attributes.rb, line 41
41: def netzke_expose_attributes(*args)
42: write_inheritable_attribute(:netzke_exposed_attributes, args.map(&:to_s))
43: end
# File lib/netzke/active_record/attributes.rb, line 55
55: def netzke_exposed_attributes
56: exposed = read_inheritable_attribute(:netzke_exposed_attributes)
57: if exposed && !exposed.include?(self.primary_key)
58: # automatically declare primary key as a netzke attribute
59: netzke_attribute(self.primary_key)
60: exposed.insert(0, self.primary_key)
61: end
62: exposed
63: end
# File lib/netzke/active_record/attributes.rb, line 128
128: def association_attr?(attr_name)
129: !!attr_name.index("__") # probably we can't do much better than this, as we don't know at this moment if the associated model has a specific attribute, and we don't really want to find it out
130: end
# File lib/netzke/active_record/attributes.rb, line 74
74: def netzke_attrs_in_forced_order(attrs)
75: attrs.collect do |attr_name|
76: declared = netzke_declared_attributes.detect { |va| va[:name] == attr_name } || {}
77: in_columns_hash = columns_hash[attr_name] && {:name => attr_name, :attr_type => columns_hash[attr_name].type, :default_value => columns_hash[attr_name].default} || {} # {:virtual => true} # if nothing found in columns, mark it as "virtual" or not?
78: if in_columns_hash.empty?
79: # If not among the model columns, it's either virtual, or an association
80: merged = association_attr?(attr_name) ? declared.merge!(:name => attr_name) : declared.merge(:virtual => true)
81: else
82: # .. otherwise merge with what's declared
83: merged = in_columns_hash.merge(declared)
84: end
85:
86: # We didn't find it among declared, nor among the model columns, nor does it seem association attribute
87: merged[:name].nil? && raise(ArgumentError, "Unknown attribute '#{attr_name}' for model #{self.name}", caller)
88:
89: merged
90: end
91: end
Returns netzke attributes in the order of columns in the table, followed by extra declared attributes Detects one-to-many association columns and replaces the name of the column with association column name (Netzke style), e.g.:
role_id => role__name
# File lib/netzke/active_record/attributes.rb, line 97
97: def netzke_attrs_in_natural_order
98: (
99: declared_attrs = netzke_declared_attributes
100:
101: column_names.map do |name|
102: c = {:name => name, :attr_type => columns_hash[name].type}
103:
104: # If it's named as foreign key of some association, then it's an association column
105: assoc = reflect_on_all_associations.detect{|a| a.primary_key_name == c[:name]}
106:
107: if assoc && !assoc.options[:polymorphic]
108: candidates = %{name title label} << assoc.primary_key_name
109: assoc_method = candidates.detect{|m| (assoc.klass.instance_methods.map(&:to_s) + assoc.klass.column_names).include?(m) }
110: c[:name] = "#{assoc.name}__#{assoc_method}"
111: c[:attr_type] = assoc.klass.columns_hash[assoc_method].try(:type) || :string # when it's an instance method rather than a column, fall back to :string
112: end
113:
114: # auto set up the default value from the column settings
115: c.merge!(:default_value => columns_hash[name].default) if columns_hash[name].default
116:
117: # if there's a declared attr with the same name, simply merge it with what's taken from the model's columns
118: if declared = declared_attrs.detect{ |va| va[:name] == c[:name] }
119: c.merge!(declared)
120: declared_attrs.delete(declared)
121: end
122: c
123: end +
124: declared_attrs
125: ).reject { |attr| netzke_excluded_attributes.include?(attr[:name]) }
126: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.