Module: Netzke::ActiveRecord::Attributes::ClassMethods

Defined in:
lib/netzke/active_record/attributes.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) netzke_attribute(name, options = {})

Define or configure an attribute. Example:

netzke_attribute :recent, :type => :boolean, :read_only => true


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/netzke/active_record/attributes.rb', line 10

def netzke_attribute(name, options = {})
  name = name.to_s
  options[:attr_type] = options.delete(:type) || options.delete(:attr_type) || :string
  declared_attrs = read_inheritable_attribute(:netzke_declared_attributes) || []
  # if the attr was declared already, simply merge it with the new options
  existing = declared_attrs.detect{ |va| va[:name] == name }
  if existing
    existing.merge!(options)
  else
    attr_config = {:name => name}.merge(options)
    # if primary_key, insert in front, otherwise append
    if name == self.primary_key
      declared_attrs.insert(0, attr_config)
    else
      declared_attrs << {:name => name}.merge(options)
    end
  end
  write_inheritable_attribute(:netzke_declared_attributes, declared_attrs)
end

- (Object) netzke_attribute_hash



53
54
55
# File 'lib/netzke/active_record/attributes.rb', line 53

def netzke_attribute_hash
  netzke_attributes.inject({}){ |r,a| r.merge(a[:name].to_sym => a) }
end

- (Object) netzke_attributes

Returns the attributes that will be picked up by grids and forms.



48
49
50
51
# File 'lib/netzke/active_record/attributes.rb', line 48

def netzke_attributes
  exposed = netzke_exposed_attributes
  exposed ? netzke_attrs_in_forced_order(exposed) : netzke_attrs_in_natural_order
end

- (Object) netzke_exclude_attributes(*args)

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


34
35
36
# File 'lib/netzke/active_record/attributes.rb', line 34

def netzke_exclude_attributes(*args)
  write_inheritable_attribute(:netzke_excluded_attributes, args.map(&:to_s))
end

- (Object) netzke_expose_attributes(*args)

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


43
44
45
# File 'lib/netzke/active_record/attributes.rb', line 43

def netzke_expose_attributes(*args)
  write_inheritable_attribute(:netzke_exposed_attributes, args.map(&:to_s))
end

- (Object) netzke_exposed_attributes



57
58
59
60
61
62
63
64
65
# File 'lib/netzke/active_record/attributes.rb', line 57

def netzke_exposed_attributes
  exposed = read_inheritable_attribute(:netzke_exposed_attributes)
  if exposed && !exposed.include?(self.primary_key)
    # automatically declare primary key as a netzke attribute
    netzke_attribute(self.primary_key)
    exposed.insert(0, self.primary_key)
  end
  exposed
end