Module: ASModel::CRUD::ClassMethods

Defined in:
backend/app/model/ASModel_crud.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) corresponds_to(jsonmodel)



440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'backend/app/model/ASModel_crud.rb', line 440

def corresponds_to(jsonmodel)
  @jsonmodel = jsonmodel

  include(DynamicEnums)

  enums = []
  @jsonmodel.schema['properties'].each do |prop, defn|
    if defn["dynamic_enum"]
      enums << {:property => prop, :uses_enum => [defn['dynamic_enum']]}
    end
  end

  uses_enums(*enums)
end

- (Object) create_from_json(json, extra_values = {})

Create a new record instance from the JSONModel ‘json’. Also creates any nested record instances that it contains.



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'backend/app/model/ASModel_crud.rb', line 335

def create_from_json(json, extra_values = {})
  self.strict_param_setting = false
  values = ASUtils.keys_as_strings(extra_values)

  if model_scope == :repository && !values.has_key?("repo_id")
    values["repo_id"] = active_repository
  end

  values['created_by'] = RequestContext.get(:current_username)

  obj = self.create(prepare_for_db(json.class,
                                   json.to_hash.merge(values)))

  obj.apply_nested_records(json, true)

  fire_update(json, obj)

  obj.refresh
  obj
end

- (Object) def_nested_record(opts)

Match a JSONModel object to an existing database association.

This linkage manages records that contain subrecords:

  • When storing a JSON blob in the database, the linkage indicates which parts of the JSON should be plucked out and stored as separate database records (with the appropriate associations)

  • When requesting a record in JSON format, the linkage indicates which associated database records should be pulled back and included in the JSON returned.

For example, this definition from subject.rb:

def_nested_record(:the_property => :terms, :contains_records_of_type => :term, :corresponding_to_association => :term)

Causes an incoming JSONModel(:subject) to have each of the objects in its “terms” array to be coerced into a Sequel model (based on the :terms association) and stored in the database. The provided list of terms are associated with the subject as it is stored, and these replace any previous terms.

The definition also causes Subject.to_jsonmodel(obj) to automatically pull back the list of terms associated with the object and include them in the response.



408
409
410
411
412
413
414
415
416
# File 'backend/app/model/ASModel_crud.rb', line 408

def def_nested_record(opts)
  opts[:association] = self.association_reflection(opts[:corresponding_to_association])
  opts[:jsonmodel] = opts[:contains_records_of_type]
  opts[:json_property] = opts[:the_property]

  opts[:is_array] = true if !opts.has_key?(:is_array)

  nested_records << opts
end

- (Object) fire_update(json, sequel_obj)

(Potentially) notify the real-time indexer that an update is available.



363
364
365
366
367
368
369
370
371
372
# File 'backend/app/model/ASModel_crud.rb', line 363

def fire_update(json, sequel_obj)
  if high_priority?
    model = self
    uri = sequel_obj.uri
    hash = model.to_jsonmodel(sequel_obj.id).to_hash(:trusted)
    DB.after_commit do
      RealtimeIndexing.record_update(hash, uri)
    end
  end
end

- (Object) get_nested_graph



419
420
421
422
423
424
425
426
# File 'backend/app/model/ASModel_crud.rb', line 419

def get_nested_graph
  Hash[nested_records.map {|nested_record|
         model = Kernel.const_get(nested_record[:association][:class_name])
         association = nested_record[:corresponding_to_association]

         [association, model.get_nested_graph]
       }]
end

- (Object) get_or_die(id)



429
430
431
432
433
434
435
436
437
# File 'backend/app/model/ASModel_crud.rb', line 429

def get_or_die(id)
  obj = if self.model_scope == :repository
          self.this_repo[:id => id]
        else
          self[id]
        end

  obj or raise NotFoundException.new("#{self} not found")
end

- (Object) handle_delete(ids_to_delete)



492
493
494
# File 'backend/app/model/ASModel_crud.rb', line 492

def handle_delete(ids_to_delete)
  self.filter(:id => ids_to_delete).delete
end

- (Boolean) has_jsonmodel?

Does this model have a corresponding JSONModel?

Returns:

  • (Boolean)


457
458
459
# File 'backend/app/model/ASModel_crud.rb', line 457

def has_jsonmodel?
  !@jsonmodel.nil?
end

- (Boolean) high_priority?

Returns:

  • (Boolean)


357
358
359
# File 'backend/app/model/ASModel_crud.rb', line 357

def high_priority?
  RequestContext.get(:is_high_priority)
end

- (Boolean) is_relationship?

Returns:

  • (Boolean)


516
517
518
# File 'backend/app/model/ASModel_crud.rb', line 516

def is_relationship?
  false
end

- (Object) my_jsonmodel(ok_if_missing = false)

Return the JSONModel class that maps to this backend model



463
464
465
# File 'backend/app/model/ASModel_crud.rb', line 463

def my_jsonmodel(ok_if_missing = false)
  @jsonmodel or (ok_if_missing ? nil : raise("No corresponding JSONModel set for model #{self.inspect}"))
end

- (Object) nested_records



375
376
377
# File 'backend/app/model/ASModel_crud.rb', line 375

def nested_records
  @nested_records ||= []
end

- (Object) repo_unique_constraint(property, constraints)



510
511
512
513
# File 'backend/app/model/ASModel_crud.rb', line 510

def repo_unique_constraint(property, constraints)
  @repo_unique_constraints ||= []
  @repo_unique_constraints << constraints.merge(:property => property)
end

- (Object) repo_unique_constraints



505
506
507
# File 'backend/app/model/ASModel_crud.rb', line 505

def repo_unique_constraints
  Array(@repo_unique_constraints)
end

- (Object) sequel_to_jsonmodel(objs, opts = {})



468
469
470
# File 'backend/app/model/ASModel_crud.rb', line 468

def sequel_to_jsonmodel(objs, opts = {})
  NestedRecordResolver.new(nested_records, objs).resolve
end

- (Object) to_jsonmodel(obj, opts = {})



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'backend/app/model/ASModel_crud.rb', line 473

def to_jsonmodel(obj, opts = {})
  if obj.is_a? Integer
    # An ID.  Get the Sequel row for it.
    ds = if self.model_scope == :repository
           self.this_repo
         else
           self
         end

    obj = ds.eager(get_nested_graph).filter(:id => obj).all[0]
    raise NotFoundException.new("#{self} not found") unless obj

    obj.eagerly_load!
  end

  sequel_to_jsonmodel([obj], opts)[0]
end

- (Object) update_mtime_for_ids(ids)



497
498
499
500
501
502
# File 'backend/app/model/ASModel_crud.rb', line 497

def update_mtime_for_ids(ids)
  now = Time.now
  ids.each_slice(50) do |subset|
    self.dataset.filter(:id => subset).update(:system_mtime => now)
  end
end