Commit b859be43cd01bc74bf12576e0c6d56059527a6be
1 parent
608ddc9d
Exists in
master
and in
29 other branches
metadata: customize og_type via param and use controllers class
Showing
12 changed files
with
139 additions
and
92 deletions
Show diff stats
plugins/metadata/config.yml.dist
1 | open_graph: | 1 | open_graph: |
2 | domain: domainregisteredonfacebook.com | 2 | domain: domainregisteredonfacebook.com |
3 | environment_logo: 'http://example.com/designs/themes/environmenttheme/images/logo.png' | 3 | environment_logo: 'http://example.com/designs/themes/environmenttheme/images/logo.png' |
4 | + namespace: app_scope | ||
4 | types: | 5 | types: |
5 | article: article | 6 | article: article |
6 | product: facebook_app:sse_product | 7 | product: facebook_app:sse_product |
plugins/metadata/lib/ext/article.rb
@@ -3,8 +3,13 @@ require_dependency 'article' | @@ -3,8 +3,13 @@ require_dependency 'article' | ||
3 | class Article | 3 | class Article |
4 | 4 | ||
5 | metadata_spec namespace: :og, key_attr: :property, tags: { | 5 | metadata_spec namespace: :og, key_attr: :property, tags: { |
6 | - type: MetadataPlugin.og_types[:article] || :article, | ||
7 | - url: proc{ |a, plugin| plugin.og_url_for a.url }, | 6 | + type: proc do |a, plugin| |
7 | + plugin.context.params[:og_type] || MetadataPlugin.og_types[:article] || :article | ||
8 | + end, | ||
9 | + url: proc do |a, plugin| | ||
10 | + url = a.url.merge! profile: a.profile.identifier, og_type: plugin.context.params[:og_type] | ||
11 | + plugin.og_url_for url | ||
12 | + end, | ||
8 | title: proc{ |a, plugin| "#{a.title} - #{a.profile.name}" }, | 13 | title: proc{ |a, plugin| "#{a.title} - #{a.profile.name}" }, |
9 | image: proc do |a, plugin| | 14 | image: proc do |a, plugin| |
10 | result = a.body_images_paths | 15 | result = a.body_images_paths |
plugins/metadata/lib/ext/community.rb
@@ -4,7 +4,7 @@ require_dependency "#{File.dirname __FILE__}/profile" | @@ -4,7 +4,7 @@ require_dependency "#{File.dirname __FILE__}/profile" | ||
4 | class Community | 4 | class Community |
5 | 5 | ||
6 | metadata_spec namespace: :og, tags: { | 6 | metadata_spec namespace: :og, tags: { |
7 | - type: MetadataPlugin.og_types[:community] || :community, | 7 | + type: proc{ |c, plugin| plugin.context.params[:og_type] || MetadataPlugin.og_types[:community] || :community }, |
8 | } | 8 | } |
9 | 9 | ||
10 | end | 10 | end |
plugins/metadata/lib/ext/enterprise.rb
@@ -4,7 +4,7 @@ require_dependency "#{File.dirname __FILE__}/profile" | @@ -4,7 +4,7 @@ require_dependency "#{File.dirname __FILE__}/profile" | ||
4 | class Enterprise | 4 | class Enterprise |
5 | 5 | ||
6 | metadata_spec namespace: :og, tags: { | 6 | metadata_spec namespace: :og, tags: { |
7 | - type: MetadataPlugin.og_types[:enterprise] || :enterprise, | 7 | + type: proc{ |e, plugin| plugin.context.params[:og_type] || MetadataPlugin.og_types[:enterprise] || :enterprise }, |
8 | } | 8 | } |
9 | 9 | ||
10 | metadata_spec namespace: 'business:contact_data', tags: { | 10 | metadata_spec namespace: 'business:contact_data', tags: { |
plugins/metadata/lib/ext/environment.rb
@@ -7,7 +7,7 @@ class Environment | @@ -7,7 +7,7 @@ class Environment | ||
7 | } | 7 | } |
8 | 8 | ||
9 | metadata_spec namespace: :og, tags: { | 9 | metadata_spec namespace: :og, tags: { |
10 | - type: 'website', | 10 | + type: proc{ |e, plugin| plugin.context.params[:og_type] || 'website' }, |
11 | title: proc{ |e, plugin| e.name }, | 11 | title: proc{ |e, plugin| e.name }, |
12 | site_name: proc{ |e, plugin| e.name }, | 12 | site_name: proc{ |e, plugin| e.name }, |
13 | description: proc{ |e, plugin| e.name }, | 13 | description: proc{ |e, plugin| e.name }, |
plugins/metadata/lib/ext/person.rb
@@ -4,7 +4,7 @@ require_dependency "#{File.dirname __FILE__}/profile" | @@ -4,7 +4,7 @@ require_dependency "#{File.dirname __FILE__}/profile" | ||
4 | class Person | 4 | class Person |
5 | 5 | ||
6 | metadata_spec namespace: :og, tags: { | 6 | metadata_spec namespace: :og, tags: { |
7 | - type: MetadataPlugin.og_types[:person] || :person, | 7 | + type: proc{ |p, plugin| plugin.context.params[:og_type] || MetadataPlugin.og_types[:person] || :person }, |
8 | } | 8 | } |
9 | 9 | ||
10 | end | 10 | end |
plugins/metadata/lib/ext/product.rb
@@ -3,8 +3,12 @@ require_dependency 'product' | @@ -3,8 +3,12 @@ require_dependency 'product' | ||
3 | class Product | 3 | class Product |
4 | 4 | ||
5 | metadata_spec namespace: :og, tags: { | 5 | metadata_spec namespace: :og, tags: { |
6 | - type: MetadataPlugin.og_types[:product] || :product, | 6 | + type: proc{ |p, plugin| plugin.context.params[:og_type] || MetadataPlugin.og_types[:product] || :product }, |
7 | url: proc{ |p, plugin| plugin.og_url_for p.url }, | 7 | url: proc{ |p, plugin| plugin.og_url_for p.url }, |
8 | + url: proc do |p, plugin| | ||
9 | + url = p.url.merge! profile: p.profile.identifier, og_type: plugin.context.params[:og_type] | ||
10 | + plugin.og_url_for url | ||
11 | + end, | ||
8 | gr_hascurrencyvalue: proc{ |p, plugin| p.price.to_f }, | 12 | gr_hascurrencyvalue: proc{ |p, plugin| p.price.to_f }, |
9 | gr_hascurrency: proc{ |p, plugin| p.environment.currency_unit }, | 13 | gr_hascurrency: proc{ |p, plugin| p.environment.currency_unit }, |
10 | title: proc{ |p, plugin| "#{p.name} - #{p.profile.name}" if p }, | 14 | title: proc{ |p, plugin| "#{p.name} - #{p.profile.name}" if p }, |
plugins/metadata/lib/ext/profile.rb
@@ -3,7 +3,7 @@ require_dependency 'profile' | @@ -3,7 +3,7 @@ require_dependency 'profile' | ||
3 | class Profile | 3 | class Profile |
4 | 4 | ||
5 | metadata_spec namespace: :og, tags: { | 5 | metadata_spec namespace: :og, tags: { |
6 | - type: MetadataPlugin.og_types[:profile] || :profile, | 6 | + type: proc{ |p, plugin| plugin.context.params[:og_type] || MetadataPlugin.og_types[:profile] || :profile }, |
7 | image: proc{ |p, plugin| "#{p.environment.top_url}#{p.image.public_filename}" if p.image }, | 7 | image: proc{ |p, plugin| "#{p.environment.top_url}#{p.image.public_filename}" if p.image }, |
8 | title: proc{ |p, plugin| if p.nickname.present? then p.nickname else p.name end }, | 8 | title: proc{ |p, plugin| if p.nickname.present? then p.nickname else p.name end }, |
9 | url: proc do |p, plugin| | 9 | url: proc do |p, plugin| |
plugins/metadata/lib/ext/uploaded_file.rb
@@ -6,9 +6,12 @@ class UploadedFile | @@ -6,9 +6,12 @@ class UploadedFile | ||
6 | metadata_spec namespace: :og, tags: { | 6 | metadata_spec namespace: :og, tags: { |
7 | type: proc do |u, plugin| | 7 | type: proc do |u, plugin| |
8 | type = if u.image? then :image else :uploaded_file end | 8 | type = if u.image? then :image else :uploaded_file end |
9 | - MetadataPlugin.og_types[type] || type | 9 | + plugin.context.params[:og_type] || MetadataPlugin.og_types[type] || type |
10 | + end, | ||
11 | + url: proc do |u, plugin| | ||
12 | + url = u.url.merge! profile: u.profile.identifier, view: true, og_type: plugin.context.params[:og_type] | ||
13 | + plugin.og_url_for url | ||
10 | end, | 14 | end, |
11 | - url: proc{ |u, plugin| plugin.og_url_for u.url.merge(view: true) }, | ||
12 | title: proc{ |u, plugin| u.title }, | 15 | title: proc{ |u, plugin| u.title }, |
13 | image: proc{ |u, plugin| "#{u.environment.top_url}#{u.public_filename}" if u.image? }, | 16 | image: proc{ |u, plugin| "#{u.environment.top_url}#{u.public_filename}" if u.image? }, |
14 | description: proc{ |u, plugin| u.abstract || u.title }, | 17 | description: proc{ |u, plugin| u.abstract || u.title }, |
plugins/metadata/lib/metadata_plugin.rb
1 | 1 | ||
2 | -class MetadataPlugin < Noosfero::Plugin | 2 | +module MetadataPlugin |
3 | + | ||
4 | + extend Noosfero::Plugin::ParentMethods | ||
3 | 5 | ||
4 | def self.plugin_name | 6 | def self.plugin_name |
5 | _('Export metadata') | 7 | _('Export metadata') |
@@ -13,89 +15,14 @@ class MetadataPlugin < Noosfero::Plugin | @@ -13,89 +15,14 @@ class MetadataPlugin < Noosfero::Plugin | ||
13 | @config ||= HashWithIndifferentAccess.new(YAML.load File.read("#{File.dirname __FILE__}/../config.yml")) rescue {} | 15 | @config ||= HashWithIndifferentAccess.new(YAML.load File.read("#{File.dirname __FILE__}/../config.yml")) rescue {} |
14 | end | 16 | end |
15 | 17 | ||
16 | - def self.og_types | ||
17 | - @og_types ||= self.config[:open_graph][:types] rescue {} | ||
18 | - end | ||
19 | - | ||
20 | - CONTROLLERS = { | ||
21 | - manage_products: { | ||
22 | - variable: :@product, | ||
23 | - }, | ||
24 | - content_viewer: { | ||
25 | - variable: proc do | ||
26 | - if profile and @page and profile.home_page_id == @page.id | ||
27 | - @profile | ||
28 | - elsif @page.respond_to? :encapsulated_file | ||
29 | - @page.encapsulated_file | ||
30 | - else | ||
31 | - @page | ||
32 | - end | ||
33 | - end, | ||
34 | - }, | ||
35 | - profile: { | ||
36 | - variable: :@profile, | ||
37 | - }, | ||
38 | - # fallback | ||
39 | - environment: { | ||
40 | - variable: :@environment, | ||
41 | - }, | ||
42 | - } | ||
43 | - | ||
44 | - def head_ending | ||
45 | - plugin = self | ||
46 | - lambda do | ||
47 | - options = MetadataPlugin::CONTROLLERS[controller.controller_path.to_sym] | ||
48 | - options ||= MetadataPlugin::CONTROLLERS[:profile] if controller.is_a? ProfileController | ||
49 | - options ||= MetadataPlugin::CONTROLLERS[:environment] | ||
50 | - return unless options | ||
51 | - | ||
52 | - return unless object = case variable = options[:variable] | ||
53 | - when Proc then instance_exec(&variable) | ||
54 | - else instance_variable_get variable | ||
55 | - end | ||
56 | - return if object.respond_to? :public? and not object.public? | ||
57 | - return unless specs = (object.class.metadata_specs rescue nil) | ||
58 | - | ||
59 | - r = [] | ||
60 | - specs.each do |namespace, spec| | ||
61 | - namespace = "#{namespace}:" if namespace.present? | ||
62 | - key_attr = spec[:key_attr] || :property | ||
63 | - value_attr = spec[:value_attr] || :content | ||
64 | - tags = spec[:tags] | ||
65 | - | ||
66 | - tags.each do |key, values| | ||
67 | - key = "#{namespace}#{key}" | ||
68 | - values = values.call(object, plugin) if values.is_a? Proc | ||
69 | - next if values.blank? | ||
70 | - | ||
71 | - Array(values).each do |value| | ||
72 | - value = value.call(object, plugin) if value.is_a? Proc | ||
73 | - next if value.blank? | ||
74 | - r << tag(:meta, key_attr => key, value_attr => value) | ||
75 | - end | ||
76 | - end | ||
77 | - end | ||
78 | - r.join | ||
79 | - end | ||
80 | - end | ||
81 | - | ||
82 | - # context HELPERS | ||
83 | - def og_url_for options | ||
84 | - options.delete :port | ||
85 | - options[:host] = self.class.config[:open_graph][:domain] rescue context.send(:environment).default_hostname | ||
86 | - Noosfero::Application.routes.url_helpers.url_for options | 18 | + def self.og_config |
19 | + @og_config ||= self.config[:open_graph] rescue {} | ||
87 | end | 20 | end |
88 | - | ||
89 | - def helpers | ||
90 | - self.context.class.helpers | 21 | + def self.og_types |
22 | + @og_types ||= self.og_config[:types] rescue {} | ||
91 | end | 23 | end |
92 | 24 | ||
93 | - protected | ||
94 | - | ||
95 | -end | 25 | + mattr_accessor :controllers |
26 | + self.controllers = MetadataPlugin::Controllers.new | ||
96 | 27 | ||
97 | -ActiveSupport.run_load_hooks :metadata_plugin, MetadataPlugin | ||
98 | -ActiveSupport.on_load :active_record do | ||
99 | - ActiveRecord::Base.extend MetadataPlugin::Specs::ClassMethods | ||
100 | end | 28 | end |
101 | - |
@@ -0,0 +1,80 @@ | @@ -0,0 +1,80 @@ | ||
1 | + | ||
2 | +class MetadataPlugin::Base < Noosfero::Plugin | ||
3 | + | ||
4 | + def self.plugin_name | ||
5 | + _('Export metadata') | ||
6 | + end | ||
7 | + | ||
8 | + def self.plugin_description | ||
9 | + _('Export metadata for models on meta tags') | ||
10 | + end | ||
11 | + | ||
12 | + def self.config | ||
13 | + @config ||= HashWithIndifferentAccess.new(YAML.load File.read("#{File.dirname __FILE__}/../config.yml")) rescue {} | ||
14 | + end | ||
15 | + | ||
16 | + def self.og_types | ||
17 | + @og_types ||= self.config[:open_graph][:types] rescue {} | ||
18 | + end | ||
19 | + | ||
20 | + class_attribute :controllers | ||
21 | + self.controllers = MetadataPlugin::Controllers.new | ||
22 | + | ||
23 | + def head_ending | ||
24 | + plugin = self | ||
25 | + lambda do | ||
26 | + variable = plugin.class.controllers.send controller.controller_path rescue nil | ||
27 | + variable ||= plugin.class.controllers.send :profile if controller.is_a? ProfileController | ||
28 | + variable ||= plugin.class.controllers.send :home | ||
29 | + return unless variable | ||
30 | + | ||
31 | + return unless object = case variable | ||
32 | + when Proc then instance_exec(&variable) | ||
33 | + else instance_variable_get variable | ||
34 | + end | ||
35 | + return if object.respond_to? :public? and not object.public? | ||
36 | + return unless specs = (object.class.metadata_specs rescue nil) | ||
37 | + | ||
38 | + r = [] | ||
39 | + specs.each do |namespace, spec| | ||
40 | + namespace = "#{namespace}:" if namespace.present? | ||
41 | + key_attr = spec[:key_attr] || :property | ||
42 | + value_attr = spec[:value_attr] || :content | ||
43 | + tags = spec[:tags] | ||
44 | + | ||
45 | + tags.each do |key, values| | ||
46 | + key = "#{namespace}#{key}" | ||
47 | + values = values.call(object, plugin) if values.is_a? Proc rescue nil | ||
48 | + next if values.blank? | ||
49 | + | ||
50 | + Array(values).each do |value| | ||
51 | + value = value.call(object, plugin) if value.is_a? Proc rescue nil | ||
52 | + next if value.blank? | ||
53 | + r << tag(:meta, key_attr => key, value_attr => CGI.escape_html(value.to_s)) | ||
54 | + end | ||
55 | + end | ||
56 | + end | ||
57 | + r.join | ||
58 | + end | ||
59 | + end | ||
60 | + | ||
61 | + # context HELPERS | ||
62 | + def og_url_for options | ||
63 | + options.delete :port | ||
64 | + options[:host] = self.class.config[:open_graph][:domain] rescue context.send(:environment).default_hostname | ||
65 | + Noosfero::Application.routes.url_helpers.url_for options | ||
66 | + end | ||
67 | + | ||
68 | + def helpers | ||
69 | + self.context.class.helpers | ||
70 | + end | ||
71 | + | ||
72 | + protected | ||
73 | + | ||
74 | +end | ||
75 | + | ||
76 | +ActiveSupport.run_load_hooks :metadata_plugin, MetadataPlugin | ||
77 | +ActiveSupport.on_load :active_record do | ||
78 | + ActiveRecord::Base.extend MetadataPlugin::Specs::ClassMethods | ||
79 | +end | ||
80 | + |
@@ -0,0 +1,27 @@ | @@ -0,0 +1,27 @@ | ||
1 | +class MetadataPlugin::Controllers | ||
2 | + | ||
3 | + def manage_products | ||
4 | + :@product | ||
5 | + end | ||
6 | + | ||
7 | + def content_viewer | ||
8 | + lambda do | ||
9 | + if profile and @page and profile.home_page_id == @page.id | ||
10 | + @profile | ||
11 | + elsif @page.respond_to? :encapsulated_file | ||
12 | + @page.encapsulated_file | ||
13 | + else | ||
14 | + @page | ||
15 | + end | ||
16 | + end | ||
17 | + end | ||
18 | + | ||
19 | + def profile | ||
20 | + :@profile | ||
21 | + end | ||
22 | + | ||
23 | + def home | ||
24 | + :@environment | ||
25 | + end | ||
26 | + | ||
27 | +end |