metadata_plugin.rb
1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class MetadataPlugin < Noosfero::Plugin
def self.plugin_name
_('Export metadata')
end
def self.plugin_description
_('Export metadata for models on meta tags')
end
def self.config
@config ||= HashWithIndifferentAccess.new(YAML.load File.read("#{File.dirname __FILE__}/../config.yml")) rescue {}
end
def self.og_types
@og_types ||= self.config[:open_graph][:types] rescue {}
end
def head_ending
plugin = self
lambda do
options = MetadataPlugin::Spec::Controllers[controller.controller_path.to_sym]
options ||= MetadataPlugin::Spec::Controllers[:profile] if controller.is_a? ProfileController
options ||= MetadataPlugin::Spec::Controllers[:environment]
return unless options
return unless object = case variable = options[:variable]
when Proc then instance_exec(&variable) rescue nil
else instance_variable_get variable
end
return unless metadata = (object.class.const_get(:Metadata) rescue nil)
metadata.map do |property, contents|
contents = contents.call(object, plugin) rescue nil if contents.is_a? Proc
next if contents.blank?
Array(contents).map do |content|
content = content.call(object, plugin) rescue nil if content.is_a? Proc
next if content.blank?
tag 'meta', property: property, content: content
end.join
end.join
end
end
# context HELPERS
def og_url_for options
options.delete :port
options[:host] = self.class.config[:open_graph][:domain] rescue context.send(:environment).default_hostname
Noosfero::Application.routes.url_helpers.url_for options
end
protected
end
ActiveSupport.run_load_hooks :metadata_plugin, MetadataPlugin