Module: Netzke::Sequel::Attributes::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#column_namesObject



31
32
33
# File 'lib/netzke/sequel/attributes.rb', line 31

def column_names
  columns.map &:to_s
end

#columns_hashObject

mostly AR compatible for our purposes ;-)



27
28
29
# File 'lib/netzke/sequel/attributes.rb', line 27

def columns_hash
  db_schema.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}
end

#data_adapterObject



22
23
24
# File 'lib/netzke/sequel/attributes.rb', line 22

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

#human_attribute_name(attr) ⇒ Object

human attribute name



36
37
38
# File 'lib/netzke/sequel/attributes.rb', line 36

def human_attribute_name attr
  I18n.translate(attr, :scope => [:activerecord, :attributes, model_name.downcase.to_sym], :default => attr.to_s.humanize)
end

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

Example:

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


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/netzke/sequel/attributes.rb', line 42

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.to_s
      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



85
86
87
# File 'lib/netzke/sequel/attributes.rb', line 85

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.



80
81
82
83
# File 'lib/netzke/sequel/attributes.rb', line 80

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


66
67
68
# File 'lib/netzke/sequel/attributes.rb', line 66

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


75
76
77
# File 'lib/netzke/sequel/attributes.rb', line 75

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

#netzke_exposed_attributesObject



89
90
91
92
93
94
95
96
97
# File 'lib/netzke/sequel/attributes.rb', line 89

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