Commit 3259a393a5afb3d419fa63672a7a1abd5e6ad31d
1 parent
4c75b9bd
Exists in
master
and in
22 other branches
New plugin VariablesPlugin
This plugin add a macro that converts {profile} string by the profile identifier. Also: - fix a bug in the MacrosHelper - fix the usage of the jQuery UI dialog widget (Noosfero uses jQuery UI v1.8) Thanks to Serpro and UnB! (ActionItem2907)
Showing
7 changed files
with
186 additions
and
8 deletions
Show diff stats
app/helpers/macros_helper.rb
@@ -20,14 +20,16 @@ module MacrosHelper | @@ -20,14 +20,16 @@ module MacrosHelper | ||
20 | jQuery('<div>'+#{macro_configuration_dialog(macro).to_json}+'</div>').dialog({ | 20 | jQuery('<div>'+#{macro_configuration_dialog(macro).to_json}+'</div>').dialog({ |
21 | title: #{macro_title(macro).to_json}, | 21 | title: #{macro_title(macro).to_json}, |
22 | modal: true, | 22 | modal: true, |
23 | - buttons: [ | ||
24 | - {text: #{_('Ok').to_json}, click: function(){ | 23 | + buttons: { |
24 | + #{_('Ok').to_json}: function(){ | ||
25 | tinyMCE.activeEditor.execCommand('mceInsertContent', false, | 25 | tinyMCE.activeEditor.execCommand('mceInsertContent', false, |
26 | (function(dialog){ #{macro_generator(macro)} })(this)); | 26 | (function(dialog){ #{macro_generator(macro)} })(this)); |
27 | jQuery(this).dialog('close'); | 27 | jQuery(this).dialog('close'); |
28 | - }}, | ||
29 | - {text: #{_('Cancel').to_json}, click: function(){jQuery(this).dialog('close');}} | ||
30 | - ] | 28 | + }, |
29 | + #{_('Cancel').to_json}: function(){ | ||
30 | + jQuery(this).dialog('close'); | ||
31 | + } | ||
32 | + } | ||
31 | }); | 33 | }); |
32 | }" | 34 | }" |
33 | end | 35 | end |
@@ -57,7 +59,11 @@ module MacrosHelper | @@ -57,7 +59,11 @@ module MacrosHelper | ||
57 | 59 | ||
58 | def macro_generator(macro) | 60 | def macro_generator(macro) |
59 | if macro.configuration[:generator] | 61 | if macro.configuration[:generator] |
60 | - macro.configuration[:generator] | 62 | + if macro.configuration[:generator].respond_to?(:call) |
63 | + macro.configuration[:generator].call(macro) | ||
64 | + else | ||
65 | + macro.configuration[:generator] | ||
66 | + end | ||
61 | else | 67 | else |
62 | macro_default_generator(macro) | 68 | macro_default_generator(macro) |
63 | end | 69 | end |
@@ -66,8 +72,7 @@ module MacrosHelper | @@ -66,8 +72,7 @@ module MacrosHelper | ||
66 | 72 | ||
67 | def macro_default_generator(macro) | 73 | def macro_default_generator(macro) |
68 | code = "var params = {};" | 74 | code = "var params = {};" |
69 | - configuration = macro_configuration(macro) | ||
70 | - configuration[:params].map do |field| | 75 | + macro.configuration[:params].map do |field| |
71 | code += "params.#{field[:name]} = jQuery('*[name=#{field[:name]}]', dialog).val();" | 76 | code += "params.#{field[:name]} = jQuery('*[name=#{field[:name]}]', dialog).val();" |
72 | end | 77 | end |
73 | code + " | 78 | code + " |
@@ -0,0 +1,39 @@ | @@ -0,0 +1,39 @@ | ||
1 | +h1. Variables Plugin | ||
2 | + | ||
3 | +A set of simple variables to be used in a macro context. | ||
4 | + | ||
5 | +h2. Usage | ||
6 | + | ||
7 | +* Create a HTML content using RawHTMLBlock, TinyMceArticle or other | ||
8 | + article with HTML support | ||
9 | +* Add a HTML div tag with css class "macro" (see Example) | ||
10 | +* Add inner that div tag the variable desired, like {profile} | ||
11 | + | ||
12 | +h2. Usage with TinyMceArticle | ||
13 | + | ||
14 | +The Noosfero's macros add a extra button in toolbar of the editor | ||
15 | +to use macros in a single way, that way this plugin add a option | ||
16 | +called "Variables" under this option. | ||
17 | + | ||
18 | +h2. Supported variables | ||
19 | + | ||
20 | +* {profile} - will be replaced by the identifier of the profile | ||
21 | +* {name} - will be replaced by the name of the profile | ||
22 | + | ||
23 | +h2. Example | ||
24 | + | ||
25 | +<pre> | ||
26 | +<div class="macro" data-macro="variables_plugin/profile"> | ||
27 | + the identifier of the profile = {profile} | ||
28 | + the name of the profile = {name} | ||
29 | +</div> | ||
30 | +</pre> | ||
31 | + | ||
32 | +h2. Info | ||
33 | + | ||
34 | +This plugin was inspired by the solution proposed by the Serpro in | ||
35 | +the merge-request #419 on the Gitorious: | ||
36 | + | ||
37 | +* https://gitorious.org/noosfero/noosfero/merge_requests/419 | ||
38 | + | ||
39 | +And improved by the guys from the UnB. |
@@ -0,0 +1,13 @@ | @@ -0,0 +1,13 @@ | ||
1 | +class VariablesPlugin < Noosfero::Plugin | ||
2 | + | ||
3 | + def self.plugin_name | ||
4 | + "Variables Plugin" | ||
5 | + end | ||
6 | + | ||
7 | + def self.plugin_description | ||
8 | + _("A set of simple variables to be used in a macro context") | ||
9 | + end | ||
10 | + | ||
11 | +end | ||
12 | + | ||
13 | +require_dependency 'variables_plugin/macros/profile' |
plugins/variables/lib/variables_plugin/macros/profile.rb
0 → 100644
@@ -0,0 +1,37 @@ | @@ -0,0 +1,37 @@ | ||
1 | +ActionView::Base.sanitized_allowed_attributes += ['data-macro'] | ||
2 | + | ||
3 | +class VariablesPlugin::Profile < Noosfero::Plugin::Macro | ||
4 | + | ||
5 | + def self.configuration | ||
6 | + { | ||
7 | + :title => _('Variables'), | ||
8 | + :skip_dialog => false, | ||
9 | + :generator => method(:macro_default_generator), | ||
10 | + :params => [ | ||
11 | + { | ||
12 | + :name => 'variable', | ||
13 | + :label => _('Select the desired variable'), | ||
14 | + :type => 'select', | ||
15 | + :values => ['{profile}', '{name}'] | ||
16 | + } | ||
17 | + ], | ||
18 | + } | ||
19 | + end | ||
20 | + | ||
21 | + def self.macro_default_generator(macro) | ||
22 | + " | ||
23 | + '<div class=\"macro mceNonEditable\" data-macro=\"#{macro.identifier}\">' | ||
24 | + + jQuery('*[name=variable]', dialog).val() | ||
25 | + + '</div>'; | ||
26 | + " | ||
27 | + end | ||
28 | + | ||
29 | + def parse(params, inner_html, source) | ||
30 | + if context.profile | ||
31 | + inner_html.gsub!(/\{profile\}/, context.profile.identifier) | ||
32 | + inner_html.gsub!(/\{name\}/, context.profile.name) | ||
33 | + end | ||
34 | + inner_html | ||
35 | + end | ||
36 | + | ||
37 | +end |
@@ -0,0 +1,41 @@ | @@ -0,0 +1,41 @@ | ||
1 | +class ProfileTest < ActiveSupport::TestCase | ||
2 | + | ||
3 | + def setup | ||
4 | + @macro = VariablesPlugin::Profile.new | ||
5 | + @macro.context = mock() | ||
6 | + @profile = fast_create(Community) | ||
7 | + @macro.context.stubs(:profile).returns(@profile) | ||
8 | + end | ||
9 | + | ||
10 | + attr_reader :macro, :profile | ||
11 | + | ||
12 | + should 'have a configuration' do | ||
13 | + assert VariablesPlugin::Profile.configuration | ||
14 | + end | ||
15 | + | ||
16 | + should 'substitute the {profile} variable by the profile idenfifier' do | ||
17 | + html = 'the profile identifier is {profile}' | ||
18 | + content = macro.parse({}, html, profile) | ||
19 | + assert_equal "the profile identifier is #{profile.identifier}", content | ||
20 | + end | ||
21 | + | ||
22 | + should 'substitute the {name} variable by the profile name' do | ||
23 | + html = 'the profile name is {name}' | ||
24 | + content = macro.parse({}, html, profile) | ||
25 | + assert_equal "the profile name is #{profile.name}", content | ||
26 | + end | ||
27 | + | ||
28 | + should 'do not change the content if the variable is not supported' do | ||
29 | + html = 'the variable {unsupported} is not supported' | ||
30 | + content = macro.parse({}, html, profile) | ||
31 | + assert_equal html, content | ||
32 | + end | ||
33 | + | ||
34 | + should 'do nothing out of profile context' do | ||
35 | + macro.context.stubs(:profile).returns(nil) | ||
36 | + html = 'there is no {support} out of profile context' | ||
37 | + content = macro.parse({}, html, profile) | ||
38 | + assert_equal html, content | ||
39 | + end | ||
40 | + | ||
41 | +end |
@@ -0,0 +1,20 @@ | @@ -0,0 +1,20 @@ | ||
1 | +require 'test_helper' | ||
2 | + | ||
3 | +class VariablesPluginTest < ActiveSupport::TestCase | ||
4 | + | ||
5 | + def setup | ||
6 | + @environment = Environment.default | ||
7 | + @plugin = VariablesPlugin.new | ||
8 | + end | ||
9 | + | ||
10 | + attr_reader :environment, :plugin | ||
11 | + | ||
12 | + should 'have a name' do | ||
13 | + assert_not_equal Noosfero::Plugin.plugin_name, VariablesPlugin::plugin_name | ||
14 | + end | ||
15 | + | ||
16 | + should 'describe yourself' do | ||
17 | + assert_not_equal Noosfero::Plugin.plugin_description, VariablesPlugin::plugin_description | ||
18 | + end | ||
19 | + | ||
20 | +end |
test/unit/macros_helper_test.rb
@@ -129,4 +129,27 @@ class MacrosHelperTest < ActiveSupport::TestCase | @@ -129,4 +129,27 @@ class MacrosHelperTest < ActiveSupport::TestCase | ||
129 | assert_equal 'macro_generator', macro_generator(Plugin1::Macro) | 129 | assert_equal 'macro_generator', macro_generator(Plugin1::Macro) |
130 | end | 130 | end |
131 | 131 | ||
132 | + should 'get macro default generator' do | ||
133 | + class Plugin1::Macro < Noosfero::Plugin::Macro | ||
134 | + def self.configuration | ||
135 | + { :params => [] } | ||
136 | + end | ||
137 | + end | ||
138 | + assert_nothing_raised NoMethodError do | ||
139 | + assert macro_generator(Plugin1::Macro) | ||
140 | + end | ||
141 | + end | ||
142 | + | ||
143 | + should 'can use a code reference as macro generator' do | ||
144 | + class Plugin1::Macro < Noosfero::Plugin::Macro | ||
145 | + def self.configuration | ||
146 | + { :params => [], :generator => method(:macro_generator_method) } | ||
147 | + end | ||
148 | + def self.macro_generator_method(macro) | ||
149 | + "macro generator method return" | ||
150 | + end | ||
151 | + end | ||
152 | + assert_equal "macro generator method return", macro_generator(Plugin1::Macro) | ||
153 | + end | ||
154 | + | ||
132 | end | 155 | end |