diff --git a/app/helpers/macros_helper.rb b/app/helpers/macros_helper.rb
index 9e9c7d0..8616aa3 100644
--- a/app/helpers/macros_helper.rb
+++ b/app/helpers/macros_helper.rb
@@ -20,14 +20,16 @@ module MacrosHelper
jQuery('
'+#{macro_configuration_dialog(macro).to_json}+'
').dialog({
title: #{macro_title(macro).to_json},
modal: true,
- buttons: [
- {text: #{_('Ok').to_json}, click: function(){
+ buttons: {
+ #{_('Ok').to_json}: function(){
tinyMCE.activeEditor.execCommand('mceInsertContent', false,
(function(dialog){ #{macro_generator(macro)} })(this));
jQuery(this).dialog('close');
- }},
- {text: #{_('Cancel').to_json}, click: function(){jQuery(this).dialog('close');}}
- ]
+ },
+ #{_('Cancel').to_json}: function(){
+ jQuery(this).dialog('close');
+ }
+ }
});
}"
end
@@ -57,7 +59,11 @@ module MacrosHelper
def macro_generator(macro)
if macro.configuration[:generator]
- macro.configuration[:generator]
+ if macro.configuration[:generator].respond_to?(:call)
+ macro.configuration[:generator].call(macro)
+ else
+ macro.configuration[:generator]
+ end
else
macro_default_generator(macro)
end
@@ -66,8 +72,7 @@ module MacrosHelper
def macro_default_generator(macro)
code = "var params = {};"
- configuration = macro_configuration(macro)
- configuration[:params].map do |field|
+ macro.configuration[:params].map do |field|
code += "params.#{field[:name]} = jQuery('*[name=#{field[:name]}]', dialog).val();"
end
code + "
diff --git a/plugins/variables/doc/variables.textile b/plugins/variables/doc/variables.textile
new file mode 100644
index 0000000..c29934d
--- /dev/null
+++ b/plugins/variables/doc/variables.textile
@@ -0,0 +1,39 @@
+h1. Variables Plugin
+
+A set of simple variables to be used in a macro context.
+
+h2. Usage
+
+* Create a HTML content using RawHTMLBlock, TinyMceArticle or other
+ article with HTML support
+* Add a HTML div tag with css class "macro" (see Example)
+* Add inner that div tag the variable desired, like {profile}
+
+h2. Usage with TinyMceArticle
+
+The Noosfero's macros add a extra button in toolbar of the editor
+to use macros in a single way, that way this plugin add a option
+called "Variables" under this option.
+
+h2. Supported variables
+
+* {profile} - will be replaced by the identifier of the profile
+* {name} - will be replaced by the name of the profile
+
+h2. Example
+
+
+
+ the identifier of the profile = {profile}
+ the name of the profile = {name}
+
+
+
+h2. Info
+
+This plugin was inspired by the solution proposed by the Serpro in
+the merge-request #419 on the Gitorious:
+
+* https://gitorious.org/noosfero/noosfero/merge_requests/419
+
+And improved by the guys from the UnB.
diff --git a/plugins/variables/lib/variables_plugin.rb b/plugins/variables/lib/variables_plugin.rb
new file mode 100644
index 0000000..ba8c1ec
--- /dev/null
+++ b/plugins/variables/lib/variables_plugin.rb
@@ -0,0 +1,13 @@
+class VariablesPlugin < Noosfero::Plugin
+
+ def self.plugin_name
+ "Variables Plugin"
+ end
+
+ def self.plugin_description
+ _("A set of simple variables to be used in a macro context")
+ end
+
+end
+
+require_dependency 'variables_plugin/macros/profile'
diff --git a/plugins/variables/lib/variables_plugin/macros/profile.rb b/plugins/variables/lib/variables_plugin/macros/profile.rb
new file mode 100644
index 0000000..02f60d0
--- /dev/null
+++ b/plugins/variables/lib/variables_plugin/macros/profile.rb
@@ -0,0 +1,37 @@
+ActionView::Base.sanitized_allowed_attributes += ['data-macro']
+
+class VariablesPlugin::Profile < Noosfero::Plugin::Macro
+
+ def self.configuration
+ {
+ :title => _('Variables'),
+ :skip_dialog => false,
+ :generator => method(:macro_default_generator),
+ :params => [
+ {
+ :name => 'variable',
+ :label => _('Select the desired variable'),
+ :type => 'select',
+ :values => ['{profile}', '{name}']
+ }
+ ],
+ }
+ end
+
+ def self.macro_default_generator(macro)
+ "
+ ''
+ + jQuery('*[name=variable]', dialog).val()
+ + '
';
+ "
+ end
+
+ def parse(params, inner_html, source)
+ if context.profile
+ inner_html.gsub!(/\{profile\}/, context.profile.identifier)
+ inner_html.gsub!(/\{name\}/, context.profile.name)
+ end
+ inner_html
+ end
+
+end
diff --git a/plugins/variables/test/unit/profile_test.rb b/plugins/variables/test/unit/profile_test.rb
new file mode 100644
index 0000000..efdfc2c
--- /dev/null
+++ b/plugins/variables/test/unit/profile_test.rb
@@ -0,0 +1,41 @@
+class ProfileTest < ActiveSupport::TestCase
+
+ def setup
+ @macro = VariablesPlugin::Profile.new
+ @macro.context = mock()
+ @profile = fast_create(Community)
+ @macro.context.stubs(:profile).returns(@profile)
+ end
+
+ attr_reader :macro, :profile
+
+ should 'have a configuration' do
+ assert VariablesPlugin::Profile.configuration
+ end
+
+ should 'substitute the {profile} variable by the profile idenfifier' do
+ html = 'the profile identifier is {profile}'
+ content = macro.parse({}, html, profile)
+ assert_equal "the profile identifier is #{profile.identifier}", content
+ end
+
+ should 'substitute the {name} variable by the profile name' do
+ html = 'the profile name is {name}'
+ content = macro.parse({}, html, profile)
+ assert_equal "the profile name is #{profile.name}", content
+ end
+
+ should 'do not change the content if the variable is not supported' do
+ html = 'the variable {unsupported} is not supported'
+ content = macro.parse({}, html, profile)
+ assert_equal html, content
+ end
+
+ should 'do nothing out of profile context' do
+ macro.context.stubs(:profile).returns(nil)
+ html = 'there is no {support} out of profile context'
+ content = macro.parse({}, html, profile)
+ assert_equal html, content
+ end
+
+end
diff --git a/plugins/variables/test/unit/variables_plugin_test.rb b/plugins/variables/test/unit/variables_plugin_test.rb
new file mode 100644
index 0000000..d3083b3
--- /dev/null
+++ b/plugins/variables/test/unit/variables_plugin_test.rb
@@ -0,0 +1,20 @@
+require 'test_helper'
+
+class VariablesPluginTest < ActiveSupport::TestCase
+
+ def setup
+ @environment = Environment.default
+ @plugin = VariablesPlugin.new
+ end
+
+ attr_reader :environment, :plugin
+
+ should 'have a name' do
+ assert_not_equal Noosfero::Plugin.plugin_name, VariablesPlugin::plugin_name
+ end
+
+ should 'describe yourself' do
+ assert_not_equal Noosfero::Plugin.plugin_description, VariablesPlugin::plugin_description
+ end
+
+end
diff --git a/test/unit/macros_helper_test.rb b/test/unit/macros_helper_test.rb
index 6aa4f86..7a961c5 100644
--- a/test/unit/macros_helper_test.rb
+++ b/test/unit/macros_helper_test.rb
@@ -129,4 +129,27 @@ class MacrosHelperTest < ActiveSupport::TestCase
assert_equal 'macro_generator', macro_generator(Plugin1::Macro)
end
+ should 'get macro default generator' do
+ class Plugin1::Macro < Noosfero::Plugin::Macro
+ def self.configuration
+ { :params => [] }
+ end
+ end
+ assert_nothing_raised NoMethodError do
+ assert macro_generator(Plugin1::Macro)
+ end
+ end
+
+ should 'can use a code reference as macro generator' do
+ class Plugin1::Macro < Noosfero::Plugin::Macro
+ def self.configuration
+ { :params => [], :generator => method(:macro_generator_method) }
+ end
+ def self.macro_generator_method(macro)
+ "macro generator method return"
+ end
+ end
+ assert_equal "macro generator method return", macro_generator(Plugin1::Macro)
+ end
+
end
--
libgit2 0.21.2