Class: DcJsonLd
- Inherits:
-
Object
- Object
- DcJsonLd
- 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
-
.add_schema_menu(parent) ⇒ Object
Create menu to add schema element.
-
.choices4_type ⇒ Object
Returns possible options for type select field on form.
-
.dc_find_form_file(form_file) ⇒ String
Searches forms path for file_name and returns full file name or nil if not found.
-
.find_document_by_ids(tables, ids) ⇒ Document
Find document by ids when document are embedded into main d even if embedded.
Instance Method Summary collapse
-
#get_json_ld(parent_data) ⇒ Object
Returns JSON LD data as YAML.
Class Method Details
.add_schema_menu(parent) ⇒ Object
Create menu to add schema element. Called from DRGCMS Form action.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'app/models/dc_json_ld.rb', line 127 def self.(parent) yaml = YAML.load_file( dc_find_form_file('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 dc-animate" data-url="#{url}">#{schema_name}</li>] end html << '</ul>' end |
.choices4_type ⇒ Object
Returns possible options for type select field on form.
118 119 120 121 122 |
# File 'app/models/dc_json_ld.rb', line 118 def self.choices4_type() yaml = YAML.load_file( dc_find_form_file('json_ld_schema') ) yaml.inject([]) {|result, schema_name| result << schema_name.first } end |
.dc_find_form_file(form_file) ⇒ String
Searches forms path for file_name and returns full file name or nil if not found.
be useful when you are extending form but want to retain same name as original form For example. You are extending dc_user form from drg_cms gem and want to retain same dc_user name. This can be done by setting drg_cms.dc_user to extend option.
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/models/dc_json_ld.rb', line 84 def self.dc_find_form_file(form_file) form_path=nil if form_file.match(/\.|\//) form_path,form_file=form_file.split(/\.|\//) end DrgCms.paths(:forms).reverse.each do |path| f = "#{path}/#{form_file}.yml" return f if File.exist?(f) and (form_path.nil? or path.to_s.match(/\/#{form_path}\//i)) end p "Form file #{form_file} not found!" nil end |
.find_document_by_ids(tables, ids) ⇒ Document
Find document by ids when document are embedded into main d even if embedded
105 106 107 108 109 110 111 112 113 |
# File 'app/models/dc_json_ld.rb', line 105 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 |