locomotive_plugins 1.0.0.beta9 → 1.0.0.beta10
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +52 -15
- data/lib/locomotive/plugin.rb +14 -17
- data/lib/locomotive/plugin/exceptions.rb +3 -0
- data/lib/locomotive/plugin/liquid.rb +6 -3
- data/lib/locomotive/plugin/load_initialization.rb +46 -0
- data/lib/locomotive/plugin/rack_app_helpers.rb +75 -62
- data/lib/version.rb +1 -1
- data/spec/lib/locomotive/plugin/config_ui_spec.rb +13 -5
- data/spec/lib/locomotive/plugin/liquid/context_helpers_spec.rb +1 -2
- data/spec/lib/locomotive/plugin/liquid_spec.rb +8 -2
- data/spec/lib/locomotive/plugin/load_initialization_spec.rb +39 -0
- data/spec/lib/locomotive/plugin/rack_app_helpers_spec.rb +57 -36
- data/spec/lib/locomotive/plugin_spec.rb +9 -19
- data/spec/support/plugins/my_other_plugin.rb +1 -1
- data/spec/support/plugins/my_plugin.rb +11 -5
- metadata +5 -3
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
|
2
|
-
# Locomotive Plugins [![Build Status](https://secure.travis-ci.org/colibri-software/locomotive_plugins.png)](https://
|
2
|
+
# Locomotive Plugins [![Build Status](https://secure.travis-ci.org/colibri-software/locomotive_plugins.png)](https://travis-ci.org/colibri-software/locomotive_plugins)
|
3
3
|
|
4
4
|
This gem is used to develop plugins for [Locomotive
|
5
5
|
CMS](http://locomotivecms.com/). Plugins can be enabled or disabled on each
|
@@ -55,29 +55,41 @@ the
|
|
55
55
|
|
56
56
|
### Initialization
|
57
57
|
|
58
|
-
|
59
|
-
|
60
|
-
|
58
|
+
There are two methods which can be overridden to customize the initialization
|
59
|
+
process. The class method `plugin_loaded` is called after the rails app first
|
60
|
+
loads the plugin. The `initialize` method may also be overridden in order run
|
61
|
+
custom code when the plugin object is constructed. Note that the `initialize`
|
62
|
+
method must not take any arguments.
|
61
63
|
|
62
64
|
class MyPlugin
|
63
65
|
include Locomotive::Plugin
|
64
66
|
|
65
|
-
def
|
66
|
-
#
|
67
|
+
def self.plugin_loaded
|
68
|
+
# Initial initialization code (only called once)
|
69
|
+
end
|
70
|
+
|
71
|
+
def initialize
|
72
|
+
# Plugin object initialization code. This is called before each request
|
73
|
+
# for which this plugin is needed
|
67
74
|
end
|
68
75
|
end
|
69
76
|
|
70
|
-
###
|
77
|
+
### Callbacks
|
71
78
|
|
72
|
-
A plugin may
|
73
|
-
|
74
|
-
|
75
|
-
|
79
|
+
A plugin may use ActiveModel callbacks. Currently, two callbacks are supported:
|
80
|
+
`page_render` and `rack_app_request`, both allowing `before`, `around`, and
|
81
|
+
`after` callbacks. The `page_render` callbacks are called for all enabled
|
82
|
+
plugins when a public-facing liquid page in LocomotiveCMS is rendered. The
|
83
|
+
`rack_app_request` callbacks are called for a specific plugin when a request is
|
84
|
+
about to be handed to its rack app (see the section below on including a Rack
|
85
|
+
app in the plugin). The `page_render` callbacks have access to the controller
|
86
|
+
which is being invoked, and both callback types have access to the config
|
87
|
+
variable which is set within the Locomotive UI.
|
76
88
|
|
77
89
|
class BasicAuth
|
78
90
|
include Locomotive::Plugin
|
79
91
|
|
80
|
-
|
92
|
+
before_page_render :authenticate
|
81
93
|
|
82
94
|
def authenticate
|
83
95
|
if self.config[:use_basic_auth]
|
@@ -107,7 +119,17 @@ Plugin code:
|
|
107
119
|
include Locomotive::Plugin
|
108
120
|
|
109
121
|
def to_liquid
|
110
|
-
|
122
|
+
BasicAuthDrop.new(self.get_authenticated_user_id)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
class BasicAuthDrop < ::Liquid::Drop
|
127
|
+
def initialize(userid)
|
128
|
+
@userid = userid
|
129
|
+
end
|
130
|
+
|
131
|
+
def userid
|
132
|
+
@userid
|
111
133
|
end
|
112
134
|
end
|
113
135
|
|
@@ -283,8 +305,9 @@ single site, since each site will have its own database.
|
|
283
305
|
### Rack App
|
284
306
|
|
285
307
|
Plugins can supply a Rack Application to be used for request handling. Do so by
|
286
|
-
overriding the `rack_app` class method
|
287
|
-
be given some helper methods
|
308
|
+
overriding the `rack_app` class method in the plugin class. The Rack app
|
309
|
+
will be given some helper methods which can be called while it is handling a
|
310
|
+
request:
|
288
311
|
|
289
312
|
* `plugin_object`: retrieve the plugin object.
|
290
313
|
* `full_path(path)`: generate the full url path for `path`. The `path` variable
|
@@ -296,3 +319,17 @@ The `full_path` and `full_url` helpers may be used by the Rack app to generate
|
|
296
319
|
full paths and urls without explicit knowledge of the Rack app's mountpoint.
|
297
320
|
This is important since Locomotive will mount the app to a path based on its
|
298
321
|
`plugin_id`.
|
322
|
+
|
323
|
+
The plugin object also has helpers which can be called whether or not the
|
324
|
+
current request is being handled by a Rack app. These are helpful when a plugin
|
325
|
+
needs to use a path or URL which would be handled by the Rack app, for example,
|
326
|
+
to generate a link.
|
327
|
+
|
328
|
+
* `rack_app_full_path(path)`: generate the full url path for `path`. Same as
|
329
|
+
`full_path(path)` above.
|
330
|
+
* `rack_app_full_url(path)`: generate the full url for `path`. Same as
|
331
|
+
`full_url(path)` above.
|
332
|
+
|
333
|
+
If your `rack_app` method creates and returns a new object instance, call the
|
334
|
+
`mounted_rack_app` method on the plugin object or the plugin class to get the
|
335
|
+
instance which is mounted in the rails app.
|
data/lib/locomotive/plugin.rb
CHANGED
@@ -12,6 +12,7 @@ module Locomotive
|
|
12
12
|
|
13
13
|
include ClassTracker
|
14
14
|
include ConfigUI
|
15
|
+
include LoadInitialization
|
15
16
|
include Liquid
|
16
17
|
include RackAppHelpers
|
17
18
|
|
@@ -22,11 +23,17 @@ module Locomotive
|
|
22
23
|
#
|
23
24
|
# @param base the plugin class
|
24
25
|
def self.included(base)
|
26
|
+
self.add_load_initialization_class_methods(base)
|
25
27
|
self.add_liquid_class_methods(base)
|
26
28
|
|
29
|
+
base.extend RackAppHelpers::ClassMethods
|
30
|
+
|
27
31
|
base.class_eval do
|
28
32
|
extend ActiveModel::Callbacks
|
29
|
-
define_model_callbacks :
|
33
|
+
define_model_callbacks :page_render
|
34
|
+
define_model_callbacks :rack_app_request
|
35
|
+
|
36
|
+
around_rack_app_request :set_plugin_object_on_rack_app
|
30
37
|
end
|
31
38
|
|
32
39
|
base.extend ClassMethods
|
@@ -76,9 +83,14 @@ module Locomotive
|
|
76
83
|
{}
|
77
84
|
end
|
78
85
|
|
86
|
+
# Override this method to provide functionality which will be executed
|
87
|
+
# when the CMS starts up and loads all plugins.
|
88
|
+
def plugin_loaded
|
89
|
+
end
|
90
|
+
|
79
91
|
# Override this method to supply a rack app to be used for handling
|
80
92
|
# requests. Locomotive CMS will mount this app on a path dependent on the
|
81
|
-
#
|
93
|
+
# +plugin_id+. See +RackAppHelpers+ for some helper methods.
|
82
94
|
def rack_app
|
83
95
|
nil
|
84
96
|
end
|
@@ -93,21 +105,6 @@ module Locomotive
|
|
93
105
|
# configuration hash for the plugin.
|
94
106
|
attr_accessor :config
|
95
107
|
|
96
|
-
# Initialize by supplying the current config parameters. Note that this
|
97
|
-
# method should not be overridden for custom initialization of plugin
|
98
|
-
# objects. Instead, override the initialize_plugin method.
|
99
|
-
#
|
100
|
-
# @param config the configuration hash
|
101
|
-
def initialize(config)
|
102
|
-
self.config = config
|
103
|
-
self.initialize_plugin
|
104
|
-
end
|
105
|
-
|
106
|
-
# Override this method to supply custom initialization code for the plugin
|
107
|
-
# object. <b>Do not override the normal +initialize+ method</b>.
|
108
|
-
def initialize_plugin
|
109
|
-
end
|
110
|
-
|
111
108
|
# Override this method to provide a liquid drop which should be available
|
112
109
|
# in the CMS.
|
113
110
|
def to_liquid
|
@@ -129,10 +129,13 @@ module Locomotive
|
|
129
129
|
end
|
130
130
|
|
131
131
|
# Add drop with extension
|
132
|
+
context['plugins'] ||= {}
|
132
133
|
drop = self.to_liquid
|
133
|
-
drop
|
134
|
-
|
135
|
-
|
134
|
+
if drop
|
135
|
+
drop.extend(Locomotive::Plugin::Liquid::DropExtension)
|
136
|
+
drop.set_plugin_id(plugin_id)
|
137
|
+
context['plugins'][plugin_id] = drop
|
138
|
+
end
|
136
139
|
|
137
140
|
# Add filters
|
138
141
|
context.add_filters(self.class.prefixed_liquid_filter_module(plugin_id))
|
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
module Locomotive
|
3
|
+
module Plugin
|
4
|
+
# This module provides initialization methods to the plugin class which Locomotive CMS will call after the plugin has been loaded into the app.
|
5
|
+
module LoadInitialization
|
6
|
+
|
7
|
+
# @private
|
8
|
+
#
|
9
|
+
# Add class methods.
|
10
|
+
#
|
11
|
+
# @param base the class which includes this module
|
12
|
+
def self.included(base)
|
13
|
+
base.extend(ClassMethods)
|
14
|
+
end
|
15
|
+
|
16
|
+
# @api internal
|
17
|
+
module ClassMethods
|
18
|
+
# Adds methods from LoadInitializationClassMethods module.
|
19
|
+
#
|
20
|
+
# @param base the plugin class to extend LoadInitializationClassMethods
|
21
|
+
def add_load_initialization_class_methods(base)
|
22
|
+
base.extend(LoadInitializationClassMethods)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# This module adds class-level initialization methods to the plugin class.
|
27
|
+
module LoadInitializationClassMethods
|
28
|
+
# Performs class-level initialization and ensures that it is only done
|
29
|
+
# once. If a block is given, that block is called before the custom
|
30
|
+
# `plugin_loaded` method is called. LocomotiveCMS calls this method on
|
31
|
+
# all plugins after they are loaded.
|
32
|
+
def do_load_initialization
|
33
|
+
raise InitializationError,
|
34
|
+
'cannot initialize plugin more than once!' if @done_load_inialization
|
35
|
+
|
36
|
+
@done_load_inialization = true
|
37
|
+
|
38
|
+
yield if block_given?
|
39
|
+
|
40
|
+
self.plugin_loaded
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -2,26 +2,12 @@
|
|
2
2
|
module Locomotive
|
3
3
|
module Plugin
|
4
4
|
# This module adds helpers for handling the Rack app supplied by the
|
5
|
-
#
|
5
|
+
# +rack_app+ method.
|
6
6
|
module RackAppHelpers
|
7
7
|
|
8
8
|
# Helper methods to be added to the Rack application.
|
9
9
|
module HelperMethods
|
10
10
|
attr_accessor :plugin_object
|
11
|
-
attr_reader :env
|
12
|
-
|
13
|
-
# Set the env on the Rack app so that it can be retrieved later. This
|
14
|
-
# method will yield, and then set the env back to what it was after the
|
15
|
-
# block returns.
|
16
|
-
#
|
17
|
-
# @param env the Rack environment
|
18
|
-
def with_env(env)
|
19
|
-
old_env = @env
|
20
|
-
@env = env
|
21
|
-
yield
|
22
|
-
ensure
|
23
|
-
@env = old_env
|
24
|
-
end
|
25
11
|
|
26
12
|
# Generate the full absolute path for the given path based on the
|
27
13
|
# mountpoint of this plugin's rack app.
|
@@ -30,10 +16,7 @@ module Locomotive
|
|
30
16
|
# app
|
31
17
|
# @return the absolute path
|
32
18
|
def full_path(path)
|
33
|
-
|
34
|
-
base_uri_object.path.sub(%r{/+$}, ''),
|
35
|
-
path.sub(%r{^/+}, '')
|
36
|
-
].join('/')
|
19
|
+
plugin_object.rack_app_full_path(path)
|
37
20
|
end
|
38
21
|
|
39
22
|
# Generate the full URL for the given path based on the mountpoint of
|
@@ -43,60 +26,90 @@ module Locomotive
|
|
43
26
|
# app
|
44
27
|
# @return the URL
|
45
28
|
def full_url(path)
|
46
|
-
|
47
|
-
base_uri_object.to_s.sub(%r{/+$}, ''),
|
48
|
-
path.sub(%r{^/+}, '')
|
49
|
-
].join('/')
|
50
|
-
end
|
51
|
-
|
52
|
-
protected
|
53
|
-
|
54
|
-
def base_uri_object
|
55
|
-
request_uri = env['REQUEST_URI']
|
56
|
-
base_path = env['SCRIPT_NAME']
|
57
|
-
base_uri = request_uri.sub(/(#{base_path}).*$/, '\1')
|
58
|
-
URI(base_uri)
|
29
|
+
plugin_object.rack_app_full_url(path)
|
59
30
|
end
|
60
31
|
end
|
61
32
|
|
62
|
-
#
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
#
|
33
|
+
# Methods to be added to the plugin class
|
34
|
+
module ClassMethods
|
35
|
+
# Gets the Rack app and sets up additional helper methods. This value is memoized
|
36
|
+
# and should be used to access the rack_app, rather than calling the
|
37
|
+
# +rack_app+ class method directly.
|
67
38
|
#
|
68
|
-
# @
|
69
|
-
def
|
70
|
-
@
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
#
|
75
|
-
# @param env the Rack environment
|
76
|
-
def call(env)
|
77
|
-
@app.with_env(env) do
|
78
|
-
@app.call(env)
|
39
|
+
# @return the Rack app with helper methods or nil if no Rack app is given
|
40
|
+
def mounted_rack_app
|
41
|
+
@mounted_rack_app ||= self.rack_app.tap do |app|
|
42
|
+
if app
|
43
|
+
app.extend(HelperMethods)
|
44
|
+
end
|
79
45
|
end
|
80
46
|
end
|
81
47
|
end
|
82
48
|
|
83
|
-
#
|
84
|
-
# wraps it. This method gets the Rack application ready to be called by
|
85
|
-
# Locomotive. Locomotive CMS calls this method to get the Rack app rather
|
86
|
-
# than calling `rack_app` directly.
|
49
|
+
# The mountpoint of the Rack app.
|
87
50
|
#
|
88
|
-
# @return the
|
89
|
-
def
|
90
|
-
|
51
|
+
# @return the mountpoint
|
52
|
+
def mountpoint
|
53
|
+
@mountpoint ||= '/'
|
54
|
+
end
|
91
55
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
56
|
+
# Set the mountpoint of the Rack app.
|
57
|
+
#
|
58
|
+
# @param mountpoint [String] the new mountpoint
|
59
|
+
def mountpoint=(mountpoint)
|
60
|
+
@mountpoint = mountpoint
|
61
|
+
end
|
97
62
|
|
98
|
-
|
99
|
-
|
63
|
+
# Generate the full absolute path within the plugin's rack app for the
|
64
|
+
# given path based on the mountpoint.
|
65
|
+
#
|
66
|
+
# @param path [String] the path relative to the mountpoint of the rack
|
67
|
+
# app
|
68
|
+
# @return the absolute path
|
69
|
+
def rack_app_full_path(path)
|
70
|
+
[
|
71
|
+
base_uri_object.path.sub(%r{/+$}, ''),
|
72
|
+
path.sub(%r{^/+}, '')
|
73
|
+
].join('/')
|
74
|
+
end
|
75
|
+
|
76
|
+
# Generate the full URL for the given path based on the mountpoint of
|
77
|
+
# this plugin's rack app.
|
78
|
+
#
|
79
|
+
# @param path [String] the path relative to the mountpoint of the rack
|
80
|
+
# app
|
81
|
+
# @return the URL
|
82
|
+
def rack_app_full_url(path)
|
83
|
+
[
|
84
|
+
base_uri_object.to_s.sub(%r{/+$}, ''),
|
85
|
+
path.sub(%r{^/+}, '')
|
86
|
+
].join('/')
|
87
|
+
end
|
88
|
+
|
89
|
+
# Delegates to +ClassMethods#mounted_rack_app+.
|
90
|
+
#
|
91
|
+
# @return the rack app with the helper methods
|
92
|
+
def mounted_rack_app
|
93
|
+
self.class.mounted_rack_app
|
94
|
+
end
|
95
|
+
|
96
|
+
protected
|
97
|
+
|
98
|
+
def base_uri_object
|
99
|
+
URI(mountpoint)
|
100
|
+
end
|
101
|
+
|
102
|
+
def set_plugin_object_on_rack_app
|
103
|
+
if mounted_rack_app
|
104
|
+
old_plugin_object = mounted_rack_app.plugin_object
|
105
|
+
begin
|
106
|
+
mounted_rack_app.plugin_object = self
|
107
|
+
yield
|
108
|
+
ensure
|
109
|
+
mounted_rack_app.plugin_object = old_plugin_object
|
110
|
+
end
|
111
|
+
else
|
112
|
+
yield
|
100
113
|
end
|
101
114
|
end
|
102
115
|
|
data/lib/version.rb
CHANGED
@@ -7,14 +7,22 @@ module Locomotive
|
|
7
7
|
|
8
8
|
before(:each) do
|
9
9
|
@config = {}
|
10
|
-
|
11
|
-
@
|
12
|
-
@
|
13
|
-
|
10
|
+
|
11
|
+
@plugin = MyPlugin.new
|
12
|
+
@plugin.config = @config
|
13
|
+
|
14
|
+
@plugin_with_non_string_path = PluginWithNonStringPath.new
|
15
|
+
@plugin_with_non_string_path.config = @config
|
16
|
+
|
17
|
+
@another_plugin = MyOtherPlugin.new
|
18
|
+
@another_plugin.config = @config
|
19
|
+
|
20
|
+
@useless_plugin = UselessPlugin.new
|
21
|
+
@useless_plugin.config = @config
|
14
22
|
end
|
15
23
|
|
16
24
|
it 'should return the template string of an HTML file' do
|
17
|
-
@plugin = MyPlugin.new
|
25
|
+
@plugin = MyPlugin.new
|
18
26
|
filepath = @plugin.config_template_file
|
19
27
|
@plugin.config_template_string.should == IO.read(filepath)
|
20
28
|
end
|
@@ -8,8 +8,7 @@ module Locomotive
|
|
8
8
|
context '#setup_liquid_context' do
|
9
9
|
|
10
10
|
before(:each) do
|
11
|
-
@
|
12
|
-
@plugin = MyPlugin.new(@config)
|
11
|
+
@plugin = MyPlugin.new
|
13
12
|
@context = ::Liquid::Context.new({}, {}, {}, true)
|
14
13
|
@plugin.setup_liquid_context('my_plugin', @context)
|
15
14
|
end
|
@@ -18,6 +17,13 @@ module Locomotive
|
|
18
17
|
@context['plugins.my_plugin'].class.should == MyPlugin::MyDrop
|
19
18
|
end
|
20
19
|
|
20
|
+
it 'should not add the drop extension to nil' do
|
21
|
+
plugin = UselessPlugin.new
|
22
|
+
plugin.setup_liquid_context('useless_plugin', @context)
|
23
|
+
plugin.to_liquid.should be_nil
|
24
|
+
plugin.to_liquid.respond_to?(:[]).should_not be_true
|
25
|
+
end
|
26
|
+
|
21
27
|
it 'should add a set of enabled liquid tags' do
|
22
28
|
@context.registers[:enabled_plugin_tags].class.should == Set
|
23
29
|
@context.registers[:enabled_plugin_tags].size.should == 1
|
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
module Locomotive
|
5
|
+
module Plugin
|
6
|
+
describe LoadInitialization do
|
7
|
+
|
8
|
+
before(:each) do
|
9
|
+
MyPlugin.instance_variable_set(:@done_load_inialization, false)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should call plugin_loaded only once' do
|
13
|
+
MyPlugin.custom_attribute.should_not == 'Value'
|
14
|
+
|
15
|
+
MyPlugin.do_load_initialization
|
16
|
+
MyPlugin.custom_attribute.should == 'Value'
|
17
|
+
|
18
|
+
-> do
|
19
|
+
MyPlugin.do_load_initialization
|
20
|
+
end.should raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should call the given block' do
|
24
|
+
expects(:initialization_code)
|
25
|
+
|
26
|
+
MyPlugin.do_load_initialization do
|
27
|
+
initialization_code
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should not fail if no plugin_loaded method defined' do
|
32
|
+
lambda do
|
33
|
+
UselessPlugin.do_load_initialization
|
34
|
+
end.should_not raise_error
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -5,38 +5,37 @@ module Locomotive
|
|
5
5
|
module Plugin
|
6
6
|
describe RackAppHelpers do
|
7
7
|
|
8
|
-
let(:
|
8
|
+
let(:plugin) { PluginWithRackApp.new }
|
9
9
|
|
10
|
-
let(:
|
10
|
+
let(:mounted_app) { plugin.class.mounted_rack_app }
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
before(:each) do
|
13
|
+
plugin.mountpoint = 'http://www.example.com/my/path/'
|
14
|
+
plugin.class.instance_variable_set(:@mounted_rack_app, nil)
|
15
|
+
end
|
15
16
|
|
16
17
|
it 'should only supply a Rack app if one has been given' do
|
17
|
-
plugin = UselessPlugin.new
|
18
|
-
plugin.
|
18
|
+
plugin = UselessPlugin.new
|
19
|
+
plugin.class.mounted_rack_app.should be_nil
|
19
20
|
end
|
20
21
|
|
21
|
-
it 'should
|
22
|
-
|
23
|
-
original_app.plugin_object.should == plugin
|
24
|
-
end
|
25
|
-
|
26
|
-
prepared_app.call(default_env)
|
22
|
+
it 'should supply a mounted app which is equal to the supplied app' do
|
23
|
+
mounted_app.should == plugin.class.rack_app
|
27
24
|
end
|
28
25
|
|
29
|
-
it 'should add the
|
26
|
+
it 'should add the plugin object to the Rack app' do
|
30
27
|
stub_app_call do
|
31
|
-
|
28
|
+
mounted_app.plugin_object.should == plugin
|
32
29
|
end
|
33
30
|
|
34
|
-
|
31
|
+
mounted_app.plugin_object.should be_nil
|
32
|
+
mounted_app.call(default_env)
|
33
|
+
mounted_app.plugin_object.should be_nil
|
35
34
|
end
|
36
35
|
|
37
36
|
it 'should add path and url helpers to the Rack app' do
|
38
|
-
|
39
|
-
|
37
|
+
mounted_app.respond_to?(:full_path).should be_true
|
38
|
+
mounted_app.respond_to?(:full_url).should be_true
|
40
39
|
end
|
41
40
|
|
42
41
|
it 'should not add the helper methods if they have already been added' do
|
@@ -45,40 +44,60 @@ module Locomotive
|
|
45
44
|
end
|
46
45
|
end
|
47
46
|
|
48
|
-
plugin = PluginWithRackApp.new
|
47
|
+
plugin = PluginWithRackApp.new
|
49
48
|
rack_app = NewRackAppClass.new
|
50
49
|
plugin.class.stubs(:rack_app).returns(rack_app)
|
51
50
|
|
52
51
|
rack_app.expects(:extend).with(HelperMethods)
|
53
|
-
app = plugin.
|
52
|
+
app = plugin.class.mounted_rack_app
|
54
53
|
|
55
|
-
plugin = PluginWithRackApp.new
|
54
|
+
plugin = PluginWithRackApp.new
|
56
55
|
rack_app = NewRackAppClass.new
|
57
56
|
plugin.class.stubs(:rack_app).returns(rack_app)
|
58
57
|
|
59
|
-
app = plugin.
|
58
|
+
app = plugin.class.mounted_rack_app
|
60
59
|
rack_app.expects(:extend).with(HelperMethods).never
|
61
|
-
app = plugin.
|
60
|
+
app = plugin.class.mounted_rack_app
|
62
61
|
end
|
63
62
|
|
64
|
-
it 'should supply an absolute path' do
|
63
|
+
it 'should supply an absolute path from the plugin object and the Rack app' do
|
64
|
+
path0 = '/plugin/path'
|
65
|
+
path1 = 'another//path'
|
66
|
+
|
67
|
+
full_path0 = '/my/path/plugin/path'
|
68
|
+
full_path1 = '/my/path/another//path'
|
69
|
+
|
70
|
+
plugin.rack_app_full_path(path0).should == full_path0
|
71
|
+
plugin.rack_app_full_path(path1).should == full_path1
|
72
|
+
|
65
73
|
stub_app_call do
|
66
|
-
|
67
|
-
|
74
|
+
mounted_app.full_path(path0).should == full_path0
|
75
|
+
mounted_app.full_path(path1).should == full_path1
|
68
76
|
end
|
69
77
|
|
70
|
-
|
78
|
+
mounted_app.call(default_env)
|
71
79
|
end
|
72
80
|
|
73
|
-
it 'should supply a full url' do
|
81
|
+
it 'should supply a full url from the plugin object and the Rack app' do
|
82
|
+
path0 = '/plugin/path'
|
83
|
+
path1 = 'another//path'
|
84
|
+
|
85
|
+
full_url0 = 'http://www.example.com/my/path/plugin/path'
|
86
|
+
full_url1 = 'http://www.example.com/my/path/another//path'
|
87
|
+
|
88
|
+
plugin.rack_app_full_url(path0).should == full_url0
|
89
|
+
plugin.rack_app_full_url(path1).should == full_url1
|
90
|
+
|
74
91
|
stub_app_call do
|
75
|
-
|
76
|
-
|
77
|
-
original_app.full_url('another//path').should ==
|
78
|
-
'http://www.example.com/my/path/another//path'
|
92
|
+
mounted_app.full_url(path0).should == full_url0
|
93
|
+
mounted_app.full_url(path1).should == full_url1
|
79
94
|
end
|
80
95
|
|
81
|
-
|
96
|
+
mounted_app.call(default_env)
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should supply a mounted_rack_app instance method' do
|
100
|
+
plugin.mounted_rack_app.should == plugin.class.mounted_rack_app
|
82
101
|
end
|
83
102
|
|
84
103
|
protected
|
@@ -92,12 +111,14 @@ module Locomotive
|
|
92
111
|
|
93
112
|
# Sets up an object which expects the method `the_block_has_been_called`
|
94
113
|
# to be invoked. This way, if you stub the app call but do not call
|
95
|
-
# `
|
114
|
+
# `mounted_app.call`, the spec will fail.
|
96
115
|
def stub_app_call(&block)
|
97
116
|
obj = Object.new
|
98
|
-
|
117
|
+
mounted_app.block = Proc.new do
|
99
118
|
obj.the_block_has_been_called
|
100
|
-
|
119
|
+
plugin.run_callbacks(:rack_app_request) do
|
120
|
+
block.call
|
121
|
+
end
|
101
122
|
end
|
102
123
|
obj.expects(:the_block_has_been_called).at_least_once
|
103
124
|
end
|
@@ -5,27 +5,17 @@ module Locomotive
|
|
5
5
|
describe Plugin do
|
6
6
|
|
7
7
|
before(:each) do
|
8
|
-
@
|
9
|
-
@
|
10
|
-
@useless_plugin = UselessPlugin.new(@config)
|
8
|
+
@plugin = MyPlugin.new
|
9
|
+
@useless_plugin = UselessPlugin.new
|
11
10
|
end
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
@plugin.custom_attribute.should_not be_nil
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'should have filter callbacks' do
|
25
|
-
@plugin.expects(:my_method1)
|
26
|
-
@plugin.expects(:my_method2)
|
27
|
-
@plugin.expects(:my_method3)
|
28
|
-
@plugin.run_callbacks(:filter) do
|
12
|
+
%w{page_render rack_app_request}.each do |type|
|
13
|
+
it "should have #{type} callbacks" do
|
14
|
+
@plugin.expects(:my_method1)
|
15
|
+
@plugin.expects(:my_method2)
|
16
|
+
@plugin.expects(:my_method3)
|
17
|
+
@plugin.run_callbacks(:"#{type}") do
|
18
|
+
end
|
29
19
|
end
|
30
20
|
end
|
31
21
|
|
@@ -15,13 +15,19 @@ module Locomotive
|
|
15
15
|
class MyTag < ::Liquid::Tag
|
16
16
|
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
before_page_render :my_method1
|
19
|
+
after_page_render :my_method2
|
20
|
+
around_page_render :my_method3
|
21
21
|
|
22
|
-
|
22
|
+
before_rack_app_request :my_method1
|
23
|
+
after_rack_app_request :my_method2
|
24
|
+
around_rack_app_request :my_method3
|
23
25
|
|
24
|
-
|
26
|
+
class << self
|
27
|
+
attr_accessor :custom_attribute
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.plugin_loaded
|
25
31
|
self.custom_attribute = 'Value'
|
26
32
|
end
|
27
33
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: locomotive_plugins
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.
|
4
|
+
version: 1.0.0.beta10
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-03-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: locomotive_liquid
|
@@ -122,6 +122,7 @@ files:
|
|
122
122
|
- lib/locomotive/plugin/liquid/drop_extension.rb
|
123
123
|
- lib/locomotive/plugin/liquid/tag_subclass_methods.rb
|
124
124
|
- lib/locomotive/plugin/liquid/context_helpers.rb
|
125
|
+
- lib/locomotive/plugin/load_initialization.rb
|
125
126
|
- lib/locomotive/plugin/rack_app_helpers.rb
|
126
127
|
- lib/locomotive/plugin/config_ui.rb
|
127
128
|
- lib/locomotive/plugin.rb
|
@@ -141,6 +142,7 @@ files:
|
|
141
142
|
- spec/fixtures/config_template.html
|
142
143
|
- spec/lib/locomotive/plugin/class_tracker_spec.rb
|
143
144
|
- spec/lib/locomotive/plugin/rack_app_helpers_spec.rb
|
145
|
+
- spec/lib/locomotive/plugin/load_initialization_spec.rb
|
144
146
|
- spec/lib/locomotive/plugin/liquid/context_helpers_spec.rb
|
145
147
|
- spec/lib/locomotive/plugin/liquid_spec.rb
|
146
148
|
- spec/lib/locomotive/plugin/config_ui_spec.rb
|
@@ -167,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
169
|
version: 1.3.1
|
168
170
|
requirements: []
|
169
171
|
rubyforge_project:
|
170
|
-
rubygems_version: 1.8.
|
172
|
+
rubygems_version: 1.8.25
|
171
173
|
signing_key:
|
172
174
|
specification_version: 3
|
173
175
|
summary: Gem for creating plugins for Locomotive
|