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

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

Instance Method Summary collapse

Instance Method Details

#data_adapterObject



17
18
19
# File 'lib/netzke/active_record/attributes.rb', line 17

def data_adapter
  @data_adapter = Netzke::Basepack::DataAdapters::AbstractAdapter.adapter_class(self).new(self)
end

#netzke_attribute(name, options = {}) ⇒ Object

Define or configure an attribute. Example:

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


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/netzke/active_record/attributes.rb', line 24

def netzke_attribute(name, options = {})
  name = name.to_s
  options[:attr_type] = options.delete(:type) || options.delete(:attr_type) || :string
  declared_attrs = self.netzke_declared_attr.dup
  # 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
  self.netzke_declared_attr = declared_attrs
end

#netzke_attribute_hashObject



67
68
69
# File 'lib/netzke/active_record/attributes.rb', line 67

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

#netzke_attributesObject

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



62
63
64
65
# File 'lib/netzke/active_record/attributes.rb', line 62

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

#netzke_exclude_attributes(*args) ⇒ Object

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


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

def netzke_exclude_attributes(*args)
  self.netzke_excluded_attr = args.map(&:to_s)
end

#netzke_expose_attributes(*args) ⇒ Object

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


57
58
59
# File 'lib/netzke/active_record/attributes.rb', line 57

def netzke_expose_attributes(*args)
  self.netzke_exposed_attr = args.map(&:to_s)
end

#netzke_exposed_attributesObject



71
72
73
74
75
76
77
78
79
# File 'lib/netzke/active_record/attributes.rb', line 71

def netzke_exposed_attributes
  exposed = self.netzke_exposed_attr
  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