Class: DcJsonLd

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
app/models/dc_json_ld.rb

Overview

Schema information

Collection name: dc_json_ld : JSON_LD data for site optimization

_id                  BSON::ObjectId       _id
type                 String               Type of structure
data                 String               Structure data in YAML
dc_json_lds          Object               Can embed substructure
created_at           Time                 created_at
updated_at           Time                 Last updated at
created_by           BSON::ObjectId       created_by
updated_by           BSON::ObjectId       Last updated by

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_schema_menu(parent) ⇒ Object

Create menu to add schema element. Called from DRGCMS Form action.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/dc_json_ld.rb', line 104

def self.add_schema_menu(parent)
  yaml = YAML.load_file( CmsHelper.form_file_find('json_ld_schema') )
  if (level = parent.params['ids'].split(';').size) == 1
    # select only top level elements
    yaml.delete_if { |schema_name, schema_data| schema_data['level'].nil? }
  else
    # select only elemets which are subelements of type
    parent_type = self.find_document_by_ids(parent.params['table'],parent.params['ids']).type
    _yaml = []
    yaml[parent_type].each do |name, data|
      next unless data.class == Hash
      _yaml << [data['type'], yaml[data['type']] ] if data['type'] and yaml[data['type']]
    end
    yaml = _yaml
  end
  # create menu code
  html = '<ul>'
  yaml.each do |schema_name, schema_data|
    next if level == 1 and schema_data['level'].nil?
    url = "/dc_common/add_json_ld_schema?table=#{parent.params['table']}&ids=#{parent.params['ids']}&schema=#{schema_name}&url=#{parent.request.url}"
    html << %Q[<li class="dc-link-ajax in-menu" data-url="#{url}">#{schema_name}</li>]
  end
  html << '</ul>' 
end

.choices4_typeObject

Returns possible options for type select field on form.



95
96
97
98
99
# File 'app/models/dc_json_ld.rb', line 95

def self.choices4_type()
  yaml = YAML.load_file( CmsHelper.form_file_find('json_ld_schema') )
  
  yaml.inject([]) {|result, schema_name| result << schema_name.first }
end

.find_document_by_ids(tables, ids) ⇒ Document

Find document by ids when document are embedded into main d even if embedded

Parameters:

  • Tables (tables)

    parameter as send in url. Tables are separated by ;

  • ids (ids)

    as send in url. ids are separated by ;

Returns:

  • (Document)


82
83
84
85
86
87
88
89
90
# File 'app/models/dc_json_ld.rb', line 82

def self.find_document_by_ids(tables, ids)
  collection = tables.split(';').first.classify.constantize
  ar_ids = ids.split(';')
  # Find top document
  document = collection.find(ar_ids.shift)
  # Search for embedded document
  ar_ids.each {|id| document = document.dc_json_lds.find(id) }
  document
end

Instance Method Details

#get_json_ld(parent_data) ⇒ Object

Returns JSON LD data as YAML



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/dc_json_ld.rb', line 59

def get_json_ld(parent_data)
  yaml = (YAML.load(self.data) rescue nil) || {}
  yaml['@type'] = self.type if yaml.size > 0
  if dc_json_lds.size > 0
    dc_json_lds.where(active: true).each do |element|
      yml = element.get_json_ld(parent_data)
      if yml.size > 0
        yaml[element.name] ||= []
        yaml[element.name] << yml 
      end
    end
  end
  yaml
end