diff --git a/plugins/metadata/config.yml.dist b/plugins/metadata/config.yml.dist index 7d0b1e1..602edd9 100644 --- a/plugins/metadata/config.yml.dist +++ b/plugins/metadata/config.yml.dist @@ -1,6 +1,7 @@ open_graph: domain: domainregisteredonfacebook.com environment_logo: 'http://example.com/designs/themes/environmenttheme/images/logo.png' + namespace: app_scope types: article: article product: facebook_app:sse_product diff --git a/plugins/metadata/lib/ext/article.rb b/plugins/metadata/lib/ext/article.rb index 435d6b9..419c069 100644 --- a/plugins/metadata/lib/ext/article.rb +++ b/plugins/metadata/lib/ext/article.rb @@ -3,8 +3,13 @@ require_dependency 'article' class Article metadata_spec namespace: :og, key_attr: :property, tags: { - type: MetadataPlugin.og_types[:article] || :article, - url: proc{ |a, plugin| plugin.og_url_for a.url }, + type: proc do |a, plugin| + plugin.context.params[:og_type] || MetadataPlugin.og_types[:article] || :article + end, + url: proc do |a, plugin| + url = a.url.merge! profile: a.profile.identifier, og_type: plugin.context.params[:og_type] + plugin.og_url_for url + end, title: proc{ |a, plugin| "#{a.title} - #{a.profile.name}" }, image: proc do |a, plugin| result = a.body_images_paths diff --git a/plugins/metadata/lib/ext/community.rb b/plugins/metadata/lib/ext/community.rb index e5dbb0d..a015735 100644 --- a/plugins/metadata/lib/ext/community.rb +++ b/plugins/metadata/lib/ext/community.rb @@ -4,7 +4,7 @@ require_dependency "#{File.dirname __FILE__}/profile" class Community metadata_spec namespace: :og, tags: { - type: MetadataPlugin.og_types[:community] || :community, + type: proc{ |c, plugin| plugin.context.params[:og_type] || MetadataPlugin.og_types[:community] || :community }, } end diff --git a/plugins/metadata/lib/ext/enterprise.rb b/plugins/metadata/lib/ext/enterprise.rb index ae43783..eaa48a7 100644 --- a/plugins/metadata/lib/ext/enterprise.rb +++ b/plugins/metadata/lib/ext/enterprise.rb @@ -4,7 +4,7 @@ require_dependency "#{File.dirname __FILE__}/profile" class Enterprise metadata_spec namespace: :og, tags: { - type: MetadataPlugin.og_types[:enterprise] || :enterprise, + type: proc{ |e, plugin| plugin.context.params[:og_type] || MetadataPlugin.og_types[:enterprise] || :enterprise }, } metadata_spec namespace: 'business:contact_data', tags: { diff --git a/plugins/metadata/lib/ext/environment.rb b/plugins/metadata/lib/ext/environment.rb index fda9177..e002c43 100644 --- a/plugins/metadata/lib/ext/environment.rb +++ b/plugins/metadata/lib/ext/environment.rb @@ -7,7 +7,7 @@ class Environment } metadata_spec namespace: :og, tags: { - type: 'website', + type: proc{ |e, plugin| plugin.context.params[:og_type] || 'website' }, title: proc{ |e, plugin| e.name }, site_name: proc{ |e, plugin| e.name }, description: proc{ |e, plugin| e.name }, diff --git a/plugins/metadata/lib/ext/person.rb b/plugins/metadata/lib/ext/person.rb index f3f8600..060e6b5 100644 --- a/plugins/metadata/lib/ext/person.rb +++ b/plugins/metadata/lib/ext/person.rb @@ -4,7 +4,7 @@ require_dependency "#{File.dirname __FILE__}/profile" class Person metadata_spec namespace: :og, tags: { - type: MetadataPlugin.og_types[:person] || :person, + type: proc{ |p, plugin| plugin.context.params[:og_type] || MetadataPlugin.og_types[:person] || :person }, } end diff --git a/plugins/metadata/lib/ext/product.rb b/plugins/metadata/lib/ext/product.rb index 11a7e69..b2de60c 100644 --- a/plugins/metadata/lib/ext/product.rb +++ b/plugins/metadata/lib/ext/product.rb @@ -3,8 +3,12 @@ require_dependency 'product' class Product metadata_spec namespace: :og, tags: { - type: MetadataPlugin.og_types[:product] || :product, + type: proc{ |p, plugin| plugin.context.params[:og_type] || MetadataPlugin.og_types[:product] || :product }, url: proc{ |p, plugin| plugin.og_url_for p.url }, + url: proc do |p, plugin| + url = p.url.merge! profile: p.profile.identifier, og_type: plugin.context.params[:og_type] + plugin.og_url_for url + end, gr_hascurrencyvalue: proc{ |p, plugin| p.price.to_f }, gr_hascurrency: proc{ |p, plugin| p.environment.currency_unit }, title: proc{ |p, plugin| "#{p.name} - #{p.profile.name}" if p }, diff --git a/plugins/metadata/lib/ext/profile.rb b/plugins/metadata/lib/ext/profile.rb index 630905e..ce8c3f8 100644 --- a/plugins/metadata/lib/ext/profile.rb +++ b/plugins/metadata/lib/ext/profile.rb @@ -3,7 +3,7 @@ require_dependency 'profile' class Profile metadata_spec namespace: :og, tags: { - type: MetadataPlugin.og_types[:profile] || :profile, + type: proc{ |p, plugin| plugin.context.params[:og_type] || MetadataPlugin.og_types[:profile] || :profile }, image: proc{ |p, plugin| "#{p.environment.top_url}#{p.image.public_filename}" if p.image }, title: proc{ |p, plugin| if p.nickname.present? then p.nickname else p.name end }, url: proc do |p, plugin| diff --git a/plugins/metadata/lib/ext/uploaded_file.rb b/plugins/metadata/lib/ext/uploaded_file.rb index 399eec2..cb42251 100644 --- a/plugins/metadata/lib/ext/uploaded_file.rb +++ b/plugins/metadata/lib/ext/uploaded_file.rb @@ -6,9 +6,12 @@ class UploadedFile metadata_spec namespace: :og, tags: { type: proc do |u, plugin| type = if u.image? then :image else :uploaded_file end - MetadataPlugin.og_types[type] || type + plugin.context.params[:og_type] || MetadataPlugin.og_types[type] || type + end, + url: proc do |u, plugin| + url = u.url.merge! profile: u.profile.identifier, view: true, og_type: plugin.context.params[:og_type] + plugin.og_url_for url end, - url: proc{ |u, plugin| plugin.og_url_for u.url.merge(view: true) }, title: proc{ |u, plugin| u.title }, image: proc{ |u, plugin| "#{u.environment.top_url}#{u.public_filename}" if u.image? }, description: proc{ |u, plugin| u.abstract || u.title }, diff --git a/plugins/metadata/lib/metadata_plugin.rb b/plugins/metadata/lib/metadata_plugin.rb index 5a9e64b..49285e7 100644 --- a/plugins/metadata/lib/metadata_plugin.rb +++ b/plugins/metadata/lib/metadata_plugin.rb @@ -1,5 +1,7 @@ -class MetadataPlugin < Noosfero::Plugin +module MetadataPlugin + + extend Noosfero::Plugin::ParentMethods def self.plugin_name _('Export metadata') @@ -13,89 +15,14 @@ class MetadataPlugin < Noosfero::Plugin @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 - - CONTROLLERS = { - manage_products: { - variable: :@product, - }, - content_viewer: { - variable: proc do - if profile and @page and profile.home_page_id == @page.id - @profile - elsif @page.respond_to? :encapsulated_file - @page.encapsulated_file - else - @page - end - end, - }, - profile: { - variable: :@profile, - }, - # fallback - environment: { - variable: :@environment, - }, - } - - def head_ending - plugin = self - lambda do - options = MetadataPlugin::CONTROLLERS[controller.controller_path.to_sym] - options ||= MetadataPlugin::CONTROLLERS[:profile] if controller.is_a? ProfileController - options ||= MetadataPlugin::CONTROLLERS[:environment] - return unless options - - return unless object = case variable = options[:variable] - when Proc then instance_exec(&variable) - else instance_variable_get variable - end - return if object.respond_to? :public? and not object.public? - return unless specs = (object.class.metadata_specs rescue nil) - - r = [] - specs.each do |namespace, spec| - namespace = "#{namespace}:" if namespace.present? - key_attr = spec[:key_attr] || :property - value_attr = spec[:value_attr] || :content - tags = spec[:tags] - - tags.each do |key, values| - key = "#{namespace}#{key}" - values = values.call(object, plugin) if values.is_a? Proc - next if values.blank? - - Array(values).each do |value| - value = value.call(object, plugin) if value.is_a? Proc - next if value.blank? - r << tag(:meta, key_attr => key, value_attr => value) - end - end - end - r.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 + def self.og_config + @og_config ||= self.config[:open_graph] rescue {} end - - def helpers - self.context.class.helpers + def self.og_types + @og_types ||= self.og_config[:types] rescue {} end - protected - -end + mattr_accessor :controllers + self.controllers = MetadataPlugin::Controllers.new -ActiveSupport.run_load_hooks :metadata_plugin, MetadataPlugin -ActiveSupport.on_load :active_record do - ActiveRecord::Base.extend MetadataPlugin::Specs::ClassMethods end - diff --git a/plugins/metadata/lib/metadata_plugin/base.rb b/plugins/metadata/lib/metadata_plugin/base.rb new file mode 100644 index 0000000..a54fee4 --- /dev/null +++ b/plugins/metadata/lib/metadata_plugin/base.rb @@ -0,0 +1,80 @@ + +class MetadataPlugin::Base < 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 + + class_attribute :controllers + self.controllers = MetadataPlugin::Controllers.new + + def head_ending + plugin = self + lambda do + variable = plugin.class.controllers.send controller.controller_path rescue nil + variable ||= plugin.class.controllers.send :profile if controller.is_a? ProfileController + variable ||= plugin.class.controllers.send :home + return unless variable + + return unless object = case variable + when Proc then instance_exec(&variable) + else instance_variable_get variable + end + return if object.respond_to? :public? and not object.public? + return unless specs = (object.class.metadata_specs rescue nil) + + r = [] + specs.each do |namespace, spec| + namespace = "#{namespace}:" if namespace.present? + key_attr = spec[:key_attr] || :property + value_attr = spec[:value_attr] || :content + tags = spec[:tags] + + tags.each do |key, values| + key = "#{namespace}#{key}" + values = values.call(object, plugin) if values.is_a? Proc rescue nil + next if values.blank? + + Array(values).each do |value| + value = value.call(object, plugin) if value.is_a? Proc rescue nil + next if value.blank? + r << tag(:meta, key_attr => key, value_attr => CGI.escape_html(value.to_s)) + end + end + end + r.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 + + def helpers + self.context.class.helpers + end + + protected + +end + +ActiveSupport.run_load_hooks :metadata_plugin, MetadataPlugin +ActiveSupport.on_load :active_record do + ActiveRecord::Base.extend MetadataPlugin::Specs::ClassMethods +end + diff --git a/plugins/metadata/lib/metadata_plugin/controllers.rb b/plugins/metadata/lib/metadata_plugin/controllers.rb new file mode 100644 index 0000000..63db241 --- /dev/null +++ b/plugins/metadata/lib/metadata_plugin/controllers.rb @@ -0,0 +1,27 @@ +class MetadataPlugin::Controllers + + def manage_products + :@product + end + + def content_viewer + lambda do + if profile and @page and profile.home_page_id == @page.id + @profile + elsif @page.respond_to? :encapsulated_file + @page.encapsulated_file + else + @page + end + end + end + + def profile + :@profile + end + + def home + :@environment + end + +end -- libgit2 0.21.2