Class: Column

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
Zena::Acts::Secure
Includes:
Property::StoredColumn, RubyLess
Defined in:
app/models/column.rb

Constant Summary collapse

TYPES_FOR_FORM =
%w{string datetime integer float hash}
INDICES_FOR_FORM =
%w{string ml_string datetime integer float}
FIELD_INDICES =
[]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Zena::Acts::Secure

secure_scope, secure_write_scope, visitor=

Methods included from Zena::Acts::Secure::SecureResult

#construct_id_map, #secure_result

Class Method Details

.add_field_index(*args) ⇒ Object

Declare a new index table or field



47
48
49
50
51
# File 'app/models/column.rb', line 47

def add_field_index(*args)
  args.flatten.each do |idx|
    FIELD_INDICES << idx
  end
end

.indices_for_formObject



37
38
39
40
41
42
43
44
# File 'app/models/column.rb', line 37

def indices_for_form
  [
    ['field',
      FIELD_INDICES.map {|i| [i, ".#{i}"]}],
    ['key/value',
      INDICES_FOR_FORM.map {|i| [i, i]}]
  ]
end

.roles_for_formObject



27
28
29
30
31
32
33
34
35
# File 'app/models/column.rb', line 27

def roles_for_form
  if roles = secure(Role) { Role.all(:order => 'name ASC') }
    roles.map do |role|
      [role.name, role.id]
    end
  else
    []
  end
end

Instance Method Details

#exportObject



63
64
65
66
67
68
69
70
# File 'app/models/column.rb', line 63

def export
  root = current_site.root_node
  {
    'ptype' => ptype,
    'index' => index,
    'comment' => root.unparse_assets(comment, root, 'comment')
  }
end

#index_nameObject

Used to display index name in forms



59
60
61
# File 'app/models/column.rb', line 59

def index_name
  self.index.to_s.gsub(/\A\./,'')
end

#klassObject



103
104
105
106
107
108
109
# File 'app/models/column.rb', line 103

def klass
  if ptype == 'hash'
    StringHash
  else
    nil
  end
end

#kpathObject



54
55
56
# File 'app/models/column.rb', line 54

def kpath
  @kpath ||= role.kpath
end

#merge_hash(orig, value) ⇒ Object



95
96
97
98
99
100
101
# File 'app/models/column.rb', line 95

def merge_hash(orig, value)
  unless orig.kind_of?(StringHash)
    orig = StringHash.from_hash(orig)
  end
  orig.merge!(value)
  return orig
end

#type_cast(value) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/column.rb', line 72

def type_cast(value)
  if value.blank?
    return nil
  end
  if ptype == 'datetime'
    if value.kind_of?(Time)
      value
    elsif value.kind_of?(String)
      value.to_utc(_(Zena::Use::Dates::DATETIME), visitor.tz)
    else
      nil
    end
  elsif ptype == 'hash'
    if value.kind_of?(Hash)
      StringHash.from_hash(value)
    elsif value.kind_of?(String)
      StringHash.from_string(value)
    end
  else
    nil
  end
end