Class: Role

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Property, Property::StoredRole, RubyLess
Defined in:
app/models/role.rb

Direct Known Subclasses

VirtualClass

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#klassObject

We define ‘klass’ in the Role used by the real classes so that property methods can be defined.



6
7
8
# File 'app/models/role.rb', line 6

def klass
  @klass
end

Class Method Details

.exportObject



38
39
40
# File 'app/models/role.rb', line 38

def export
  {'Node' => VirtualClass['Node'].export}
end

.import(definitions, delete = false) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/role.rb', line 42

def import(definitions, delete = false)
  res = []
  post_import = []
  # Create everything in a transaction
  transaction do
    definitions.each do |name, definition|
      klass = VirtualClass[name]
      if !klass || !klass.real_class?
        # Error, missing superclass
        raise Exception.new("Importation needs to start with a real class: '#{name}' is not a real class.")
      else
        # start importing
        res += import_vclass(nil, name, definition, post_import)
      end
    end

    post_import.each do |l|
      l.call
    end
  end
  res
end

Instance Method Details

#defined_safe_column(name) ⇒ Object

By default, all defined columns are safe (see )



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/models/role.rb', line 197

def defined_safe_column(name)
  if real_class?
    # Get columns from the 'native' schema of the real class (this schema is a Property::Role,
    # not a VirtualClass or ::Role).
    #
    # Only columns explicitly declared safe are safe here
    if real_class.safe_method_type(name)
      real_class.schema.defined_columns[name]
    end
  else
    defined_columns[name]
  end
end

#defined_safe_columnsObject

By default, all defined columns are safe (see )



192
193
194
# File 'app/models/role.rb', line 192

def defined_safe_columns
  @safe_column ||= defined_columns.values.sort {|a,b| a.name <=> b.name}
end

#exportObject



211
212
213
214
215
216
217
218
# File 'app/models/role.rb', line 211

def export
  res = Zafu::OrderedHash.new
  res['type'] = real_class? ? 'Class' : type
  if !defined_columns.empty?
    res['columns'] = export_columns
  end
  res
end

#icon=(txt) ⇒ Object



169
170
171
172
173
# File 'app/models/role.rb', line 169

def icon=(txt)
  # FIXME: remove gsub when we stop using ImageBuilder on
  # icon images. SECURITY
  super(txt.gsub('..', '.'))
end

#import_columns(columns) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'app/models/role.rb', line 220

def import_columns(columns)
  transaction do
    columns.each do |name, definition|
      column = secure(::Column) { ::Column.find_by_name(name) }
      if !column
        # create
        column = ::Column.new(:name => name)
      elsif column.role_id != self.id
        # error (do not move a column)
        raise Exception.new("Cannot set property '#{name}' in '#{self.name}': already defined in '#{column.role.name}'.")
      end
      column.role_id = self.id
      column.ptype   = definition['ptype']
      column.index   = definition['index']
      column.save!
    end
  end
end

#import_relations(relations) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'app/models/role.rb', line 239

def import_relations(relations)
  relations.each do |name, definition|
    relation = secure(::Relation) { ::Relation.first(
      :conditions => ['target_role = ? AND source_kpath = ? AND site_id = ?',
        name, self.kpath, self.site_id
      ]
    )}
    if !relation
      # create
      relation = ::Relation.new(:target_role => name, :source_kpath => self.kpath)
    end
    Relation::EXPORT_FIELDS.each do |key|
      value = definition[key]
      if key == 'target_name' && !value.blank?
        if target = VirtualClass[value]
          relation['target_kpath'] = target.kpath
        else
          raise Exception.new("Could not find a target named '#{value}' to create relation '#{name}'.")
        end
      elsif !value.blank?
        relation[key] = value
      end
    end
    relation.save!
  end
end

#real_class?Boolean

class << self

Returns:

  • (Boolean)


165
166
167
# File 'app/models/role.rb', line 165

def real_class?
  false
end

#superclassObject



175
176
177
178
179
180
181
# File 'app/models/role.rb', line 175

def superclass
  if new_record?
    Node
  else
    VirtualClass.find_by_kpath(kpath)
  end
end

#superclass=(klass) ⇒ Object



183
184
185
186
187
188
189
# File 'app/models/role.rb', line 183

def superclass=(klass)
  if klass.kind_of?(VirtualClass) || klass = VirtualClass[klass]
    @superclass = klass
  else
    errors.add('superclass', 'invalid')
  end
end