locomotive_cms 2.5.6 → 2.5.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +7 -3
- data/app/assets/javascripts/locomotive/utils/tinymce_settings.js.coffee +4 -3
- data/app/assets/javascripts/locomotive/views/pages/_form_view.js.coffee +2 -2
- data/app/assets/stylesheets/locomotive/backoffice/application.css.scss +34 -1
- data/app/controllers/locomotive/api/base_controller.rb +8 -0
- data/app/controllers/locomotive/api/content_entries_controller.rb +15 -2
- data/app/controllers/locomotive/api/my_account_controller.rb +15 -0
- data/app/controllers/locomotive/api/version_controller.rb +17 -0
- data/app/controllers/locomotive/base_controller.rb +1 -1
- data/app/controllers/locomotive/content_entries_controller.rb +9 -10
- data/app/helpers/locomotive/content_types_helper.rb +16 -1
- data/app/helpers/locomotive/pages_helper.rb +13 -1
- data/app/models/locomotive/content_type.rb +15 -8
- data/app/models/locomotive/extensions/page/layout.rb +42 -0
- data/app/models/locomotive/extensions/page/templatized.rb +1 -1
- data/app/models/locomotive/page.rb +1 -0
- data/app/presenters/locomotive/content_entry_presenter.rb +5 -1
- data/app/presenters/locomotive/page_presenter.rb +1 -1
- data/app/services/locomotive/content_entry_service.rb +81 -0
- data/app/uploaders/locomotive/theme_asset_uploader.rb +1 -1
- data/app/views/locomotive/content_entries/index.html.haml +8 -0
- data/app/views/locomotive/content_types/_form.html.haml +2 -0
- data/app/views/locomotive/notifications/new_content_entry.html.haml +2 -2
- data/app/views/locomotive/pages/_form.html.haml +8 -1
- data/config/locales/admin_ui.en.yml +2 -0
- data/config/locales/admin_ui.fr.yml +1 -0
- data/config/locales/formtastic.en.yml +3 -0
- data/config/routes.rb +6 -2
- data/features/api/content_entries.feature +22 -9
- data/features/backoffice/site.feature +1 -1
- data/features/public/content_entries.feature +20 -5
- data/features/step_definitions/content_types_steps.rb +6 -0
- data/lib/generators/locomotive/install/templates/devise.rb +0 -2
- data/lib/locomotive/action_controller/responder.rb +1 -1
- data/lib/locomotive/carrierwave.rb +1 -0
- data/lib/locomotive/carrierwave/asset.rb +1 -1
- data/lib/locomotive/httparty/webservice.rb +17 -11
- data/lib/locomotive/liquid/drops/content_types.rb +7 -1
- data/lib/locomotive/liquid/drops/page.rb +51 -3
- data/lib/locomotive/liquid/tags/consume.rb +15 -10
- data/lib/locomotive/liquid/tags/editable/base.rb +1 -1
- data/lib/locomotive/liquid/tags/with_scope.rb +6 -1
- data/lib/locomotive/presentable.rb +1 -1
- data/lib/locomotive/regexps.rb +1 -1
- data/lib/locomotive/version.rb +1 -1
- data/lib/tasks/locomotive.rake +1 -1
- data/spec/dummy/config/application.rb +1 -1
- data/spec/dummy/config/mongoid.yml +2 -2
- data/spec/lib/locomotive/httparty/webservice_spec.rb +8 -3
- data/spec/lib/locomotive/liquid/drops/page_spec.rb +20 -5
- data/spec/lib/locomotive/liquid/tags/consume_spec.rb +66 -48
- data/spec/lib/locomotive/liquid/tags/with_scope_spec.rb +6 -0
- data/spec/mailers/locomotive/notifications_spec.rb +15 -8
- data/spec/models/locomotive/content_entry_spec.rb +1 -1
- data/spec/models/locomotive/editable_control_spec.rb +2 -2
- data/spec/models/locomotive/extensions/page/layout_spec.rb +50 -0
- data/spec/models/locomotive/site_spec.rb +1 -1
- metadata +26 -7
@@ -41,11 +41,17 @@ module Locomotive
|
|
41
41
|
protected
|
42
42
|
|
43
43
|
def collection
|
44
|
+
options = {}
|
45
|
+
|
44
46
|
if @context['with_scope']
|
45
47
|
self.modify_with_scope
|
48
|
+
|
49
|
+
options = { where: @context['with_scope'] }
|
50
|
+
|
51
|
+
options[:order_by] = options[:where].delete(:order_by)
|
46
52
|
end
|
47
53
|
|
48
|
-
@collection ||= @content_type.ordered_entries(
|
54
|
+
@collection ||= @content_type.ordered_entries(options).visible
|
49
55
|
end
|
50
56
|
|
51
57
|
# Modify the attributes of the with_scope tag so that
|
@@ -3,14 +3,24 @@ module Locomotive
|
|
3
3
|
module Drops
|
4
4
|
class Page < Base
|
5
5
|
|
6
|
-
delegate :seo_title, :meta_keywords, :meta_description, :redirect_url, :handle, to: :@_source
|
6
|
+
delegate :seo_title, :meta_keywords, :meta_description, :redirect_url, :handle, :layout, to: :@_source
|
7
7
|
|
8
8
|
def title
|
9
|
-
@_source.templatized? ? @context['entry']._label :
|
9
|
+
title = @_source.templatized? ? @context['entry'].try(:_label) : nil
|
10
|
+
title || @_source.title
|
10
11
|
end
|
11
12
|
|
12
13
|
def slug
|
13
|
-
@_source.templatized? ? @context['entry']._slug.singularize :
|
14
|
+
slug = @_source.templatized? ? @context['entry'].try(:_slug).try(:singularize) : nil
|
15
|
+
slug || @_source.slug
|
16
|
+
end
|
17
|
+
|
18
|
+
def original_title
|
19
|
+
@_source.title
|
20
|
+
end
|
21
|
+
|
22
|
+
def original_slug
|
23
|
+
@_source.slug
|
14
24
|
end
|
15
25
|
|
16
26
|
def parent
|
@@ -45,6 +55,10 @@ module Locomotive
|
|
45
55
|
@_source.redirect?
|
46
56
|
end
|
47
57
|
|
58
|
+
def is_layout?
|
59
|
+
@_source.is_layout?
|
60
|
+
end
|
61
|
+
|
48
62
|
def templatized?
|
49
63
|
@_source.templatized?
|
50
64
|
end
|
@@ -57,10 +71,44 @@ module Locomotive
|
|
57
71
|
end
|
58
72
|
end
|
59
73
|
|
74
|
+
def editable_elements
|
75
|
+
@editable_elements_hash ||= build_editable_elements_hash
|
76
|
+
end
|
77
|
+
|
60
78
|
def before_method(meth)
|
79
|
+
# @deprecated
|
61
80
|
@_source.editable_elements.where(slug: meth).try(:first).try(:content)
|
62
81
|
end
|
63
82
|
|
83
|
+
private
|
84
|
+
|
85
|
+
def build_editable_elements_hash
|
86
|
+
{}.tap do |hash|
|
87
|
+
@_source.editable_elements.each do |el|
|
88
|
+
safe_slug = el.slug.parameterize.underscore
|
89
|
+
keys = el.block.try(:split, '/').try(:compact) || []
|
90
|
+
|
91
|
+
_hash = _build_editable_elements_hashes(hash, keys)
|
92
|
+
|
93
|
+
_hash[safe_slug] = el.content
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def _build_editable_elements_hashes(hash, keys)
|
99
|
+
_hash = hash
|
100
|
+
|
101
|
+
keys.each do |key|
|
102
|
+
safe_key = key.parameterize.underscore
|
103
|
+
|
104
|
+
_hash[safe_key] = {} if _hash[safe_key].nil?
|
105
|
+
|
106
|
+
_hash = _hash[safe_key]
|
107
|
+
end
|
108
|
+
|
109
|
+
_hash
|
110
|
+
end
|
111
|
+
|
64
112
|
end
|
65
113
|
end
|
66
114
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module Locomotive
|
2
2
|
module Liquid
|
3
3
|
module Tags
|
4
|
+
|
4
5
|
# Consume web services as easy as pie directly in liquid !
|
5
6
|
#
|
6
7
|
# Usage:
|
@@ -13,14 +14,14 @@ module Locomotive
|
|
13
14
|
#
|
14
15
|
class Consume < ::Liquid::Block
|
15
16
|
|
16
|
-
Syntax = /(#{::Liquid::VariableSignature}+)\s*from\s*(#{::Liquid::QuotedString}|#{::Liquid::VariableSignature}+)
|
17
|
+
Syntax = /(#{::Liquid::VariableSignature}+)\s*from\s*(#{::Liquid::QuotedString}|#{::Liquid::VariableSignature}+)(.*)?/
|
17
18
|
|
18
19
|
def initialize(tag_name, markup, tokens, context)
|
19
20
|
if markup =~ Syntax
|
20
21
|
@target = $1
|
21
22
|
|
22
23
|
self.prepare_url($2)
|
23
|
-
self.
|
24
|
+
self.prepare_api_arguments($3)
|
24
25
|
else
|
25
26
|
raise ::Liquid::SyntaxError.new("Syntax Error in 'consume' - Valid syntax: consume <var> from \"<url>\" [username: value, password: value]")
|
26
27
|
end
|
@@ -31,9 +32,12 @@ module Locomotive
|
|
31
32
|
end
|
32
33
|
|
33
34
|
def render(context)
|
35
|
+
self.set_api_options(context)
|
36
|
+
|
34
37
|
if instance_variable_defined? :@variable_name
|
35
38
|
@url = context[@variable_name]
|
36
39
|
end
|
40
|
+
|
37
41
|
render_all_and_cache_it(context)
|
38
42
|
end
|
39
43
|
|
@@ -49,13 +53,14 @@ module Locomotive
|
|
49
53
|
end
|
50
54
|
end
|
51
55
|
|
52
|
-
def
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
@
|
56
|
+
def prepare_api_arguments(string)
|
57
|
+
string = string.gsub(/^(\s*,)/, '').strip
|
58
|
+
@api_arguments = Solid::Arguments.parse(string)
|
59
|
+
end
|
60
|
+
|
61
|
+
def set_api_options(context)
|
62
|
+
@api_options = @api_arguments ? @api_arguments.interpolate(context).first || {} : {}
|
63
|
+
@expires_in = @api_options.delete(:expires_in) || 0
|
59
64
|
end
|
60
65
|
|
61
66
|
def page_fragment_cache_key(url)
|
@@ -81,7 +86,7 @@ module Locomotive
|
|
81
86
|
def render_all_without_cache(context)
|
82
87
|
context.stack do
|
83
88
|
begin
|
84
|
-
context.scopes.last[@target.to_s] = Locomotive::Httparty::Webservice.consume(@url, @
|
89
|
+
context.scopes.last[@target.to_s] = Locomotive::Httparty::Webservice.consume(@url, @api_options)
|
85
90
|
self.cached_response = context.scopes.last[@target.to_s]
|
86
91
|
rescue Timeout::Error
|
87
92
|
context.scopes.last[@target.to_s] = self.cached_response
|
@@ -12,7 +12,7 @@ module Locomotive
|
|
12
12
|
if markup =~ Syntax
|
13
13
|
@slug = $1.gsub(/[\"\']/, '')
|
14
14
|
@options = { fixed: false }
|
15
|
-
markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/^'/, '').gsub(/'$/, '') }
|
15
|
+
markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/^[\"\']/, '').gsub(/[\"\']$/, '') }
|
16
16
|
else
|
17
17
|
raise ::Liquid::SyntaxError.new("Syntax Error in 'editable_xxx' - Valid syntax: editable_xxx <slug>(, <options>)")
|
18
18
|
end
|
@@ -49,7 +49,12 @@ module Locomotive
|
|
49
49
|
# key to h4s symbol
|
50
50
|
_key = _key.to_s.to_sym.send(_operator.to_sym) if _operator
|
51
51
|
|
52
|
-
hash[_key] = value
|
52
|
+
hash[_key] = (case value
|
53
|
+
# regexp inside a string
|
54
|
+
when /^\/[^\/]*\/$/ then Regexp.new(value[1..-2])
|
55
|
+
else
|
56
|
+
value
|
57
|
+
end)
|
53
58
|
end
|
54
59
|
end
|
55
60
|
end
|
data/lib/locomotive/regexps.rb
CHANGED
@@ -3,7 +3,7 @@ module Locomotive
|
|
3
3
|
|
4
4
|
SUBDOMAIN = /^[a-z][a-z0-9_-]*[a-z0-9]{1}$/
|
5
5
|
|
6
|
-
DOMAIN = /^(([a-z])([a-z\d-]){0,61}([a-z\d]))(\.([a-z])([a-z\d-]){0,61}([a-z\d]))*$/i
|
6
|
+
DOMAIN = /^(([a-z\d])([a-z\d-]){0,61}([a-z\d]))(\.([a-z\d])([a-z\d-]){0,61}([a-z\d]))*$/i
|
7
7
|
|
8
8
|
URL = /((http|https|ftp):\/)?\/\S*/
|
9
9
|
|
data/lib/locomotive/version.rb
CHANGED
data/lib/tasks/locomotive.rake
CHANGED
@@ -2,7 +2,7 @@ require File.expand_path('../boot', __FILE__)
|
|
2
2
|
|
3
3
|
require 'action_controller/railtie'
|
4
4
|
require 'action_mailer/railtie'
|
5
|
-
require 'active_resource/railtie'
|
5
|
+
# require 'active_resource/railtie'
|
6
6
|
require "sprockets/railtie"
|
7
7
|
|
8
8
|
Bundler.require *Rails.groups(:assets) if defined?(Bundler)
|
@@ -5,7 +5,7 @@ development:
|
|
5
5
|
default:
|
6
6
|
# Defines the name of the default database that Mongoid can connect to.
|
7
7
|
# (required).
|
8
|
-
database:
|
8
|
+
database: locomotive_engine_2_5_dev
|
9
9
|
# database: locomotive_mounter_dev
|
10
10
|
# Provides the hosts the default session can connect to. Must be an array
|
11
11
|
# of host:port pairs. (required)
|
@@ -102,4 +102,4 @@ production:
|
|
102
102
|
# production:
|
103
103
|
# <<: *defaults
|
104
104
|
# identity_map_enabled: true
|
105
|
-
# database: locomotive_engine_production
|
105
|
+
# database: locomotive_engine_production
|
@@ -9,17 +9,17 @@ describe Locomotive::Httparty::Webservice do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'sets the base uri from a simple url' do
|
12
|
-
Locomotive::Httparty::Webservice.expects(:get).with('/',
|
12
|
+
Locomotive::Httparty::Webservice.expects(:get).with('/', base_uri: 'http://blog.locomotiveapp.org').returns(@response)
|
13
13
|
Locomotive::Httparty::Webservice.consume('http://blog.locomotiveapp.org')
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'sets the base uri from a much more complex url' do
|
17
|
-
Locomotive::Httparty::Webservice.expects(:get).with('/feed/weather.ashx
|
17
|
+
Locomotive::Httparty::Webservice.expects(:get).with('/feed/weather.ashx', base_uri: 'http://free.worldweatheronline.com', query: { 'key' => 'secretapikey', 'format' => 'json' }).returns(@response)
|
18
18
|
Locomotive::Httparty::Webservice.consume('http://free.worldweatheronline.com/feed/weather.ashx?key=secretapikey&format=json')
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'sets both the base uri and the path from an url with parameters' do
|
22
|
-
Locomotive::Httparty::Webservice.expects(:get).with('/api/read/json
|
22
|
+
Locomotive::Httparty::Webservice.expects(:get).with('/api/read/json', base_uri: 'http://blog.locomotiveapp.org', query: { 'num' => '3' }).returns(@response)
|
23
23
|
Locomotive::Httparty::Webservice.consume('http://blog.locomotiveapp.org/api/read/json?num=3')
|
24
24
|
end
|
25
25
|
|
@@ -28,6 +28,11 @@ describe Locomotive::Httparty::Webservice do
|
|
28
28
|
Locomotive::Httparty::Webservice.consume('http://blog.locomotiveapp.org', { username: 'john', password: 'foo' })
|
29
29
|
end
|
30
30
|
|
31
|
+
it 'sends a post request' do
|
32
|
+
Locomotive::Httparty::Webservice.expects(:post).with('/api/charge.json', { base_uri: 'http://blog.locomotiveapp.org', body: { 'source' => 'abc', 'amount' => '42000' } }).returns(@response)
|
33
|
+
Locomotive::Httparty::Webservice.consume('http://blog.locomotiveapp.org/api/charge.json?source=abc&amount=42000', method: :post)
|
34
|
+
end
|
35
|
+
|
31
36
|
end
|
32
37
|
|
33
38
|
context 'in a real-world' do
|
@@ -3,8 +3,8 @@ require 'spec_helper'
|
|
3
3
|
describe Locomotive::Liquid::Drops::Page do
|
4
4
|
|
5
5
|
before(:each) do
|
6
|
-
@site
|
7
|
-
@home
|
6
|
+
@site = FactoryGirl.build(:site)
|
7
|
+
@home = FactoryGirl.build(:page, site: @site, meta_keywords: 'Libidinous, Angsty', meta_description: "Quite the combination.")
|
8
8
|
end
|
9
9
|
|
10
10
|
context '#rendering tree' do
|
@@ -53,6 +53,19 @@ describe Locomotive::Liquid::Drops::Page do
|
|
53
53
|
|
54
54
|
end
|
55
55
|
|
56
|
+
context '#layout' do
|
57
|
+
before(:each) do
|
58
|
+
@layout = FactoryGirl.build(:page, title: 'Simple playout', is_layout: true)
|
59
|
+
@home.layout = @layout
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'renders title of parent page' do
|
63
|
+
content = render_template '{{ page.layout.title }}', { 'page' => @home }
|
64
|
+
content.should == "Simple playout"
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
56
69
|
context '#breadcrumbs' do
|
57
70
|
before(:each) do
|
58
71
|
@sub_page = FactoryGirl.build(:sub_page, meta_keywords: 'Sub Libidinous, Angsty', meta_description: "Sub Quite the combination.")
|
@@ -77,6 +90,7 @@ describe Locomotive::Liquid::Drops::Page do
|
|
77
90
|
entry = Locomotive::Liquid::Drops::ContentEntry.new(mock('entry', _label: 'Locomotive rocks !'))
|
78
91
|
|
79
92
|
render_template('{{ page.title }}', 'page' => templatized, 'entry' => entry).should == 'Locomotive rocks !'
|
93
|
+
render_template('{{ page.original_title }}', 'page' => templatized, 'entry' => entry).should == 'Lorem ipsum template'
|
80
94
|
end
|
81
95
|
|
82
96
|
end
|
@@ -88,11 +102,12 @@ describe Locomotive::Liquid::Drops::Page do
|
|
88
102
|
end
|
89
103
|
|
90
104
|
it 'renders the content instance slug instead for a templatized page' do
|
91
|
-
templatized = FactoryGirl.build(:page, title: 'Lorem ipsum template', templatized: true)
|
105
|
+
templatized = FactoryGirl.build(:page, title: 'Lorem ipsum template', slug: 'content-type-template', templatized: true)
|
92
106
|
|
93
107
|
entry = Locomotive::Liquid::Drops::ContentEntry.new(mock('entry', _slug: 'my_entry'))
|
94
108
|
|
95
109
|
render_template('{{ page.slug }}', 'page' => templatized, 'entry' => entry).should == 'my_entry'
|
110
|
+
render_template('{{ page.original_slug }}', 'page' => templatized, 'entry' => entry).should == 'content-type-template'
|
96
111
|
end
|
97
112
|
|
98
113
|
end
|
@@ -102,12 +117,12 @@ describe Locomotive::Liquid::Drops::Page do
|
|
102
117
|
before(:each) do
|
103
118
|
@site = FactoryGirl.create(:site)
|
104
119
|
@home = @site.pages.root.first
|
105
|
-
@home.update_attributes raw_template: "{% block body %}{% editable_short_text '
|
120
|
+
@home.update_attributes raw_template: "{% editable_text title %}Hello world{% endeditable_text %}{% block body %}{% editable_short_text 'message' %}Lorem ipsum{% endeditable_short_text %}{% endblock %}"
|
106
121
|
@home.editable_elements.first.content = 'Lorem ipsum'
|
107
122
|
end
|
108
123
|
|
109
124
|
it 'renders the text of the editable field' do
|
110
|
-
render_template('{{ home.body }}').should == 'Lorem ipsum'
|
125
|
+
render_template('{{ home.editable_elements.body.message }}').should == 'Lorem ipsum'
|
111
126
|
end
|
112
127
|
|
113
128
|
end
|
@@ -4,84 +4,97 @@ describe Locomotive::Liquid::Tags::Consume do
|
|
4
4
|
|
5
5
|
context '#validating syntax' do
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
let(:markup) { '' }
|
8
|
+
let(:tag) { Locomotive::Liquid::Tags::Consume.new('consume', markup, ["{% endconsume %}"], {}) }
|
9
|
+
subject { lambda { tag } }
|
10
|
+
|
11
|
+
describe 'validates a basic syntax' do
|
12
|
+
|
13
|
+
let(:markup) { 'blog from "http://blog.locomotiveapp.org"' }
|
14
|
+
it { should_not raise_exception }
|
15
|
+
|
12
16
|
end
|
13
17
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
18
|
+
describe 'validates more complex syntax with attributes' do
|
19
|
+
|
20
|
+
let(:markup) { 'blog from "http://www.locomotiveapp.org", username: "john", password: password_from_context' }
|
21
|
+
it { should_not raise_exception }
|
22
|
+
|
19
23
|
end
|
20
24
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
+
describe 'should parse the correct url with complex syntax with attributes' do
|
26
|
+
|
27
|
+
let(:markup) { 'blog from "http://www.locomotiveapp.org" username: "john", password: "easyone"' }
|
28
|
+
it { should_not raise_exception }
|
29
|
+
it { tag.instance_variable_get(:@url).should eq "http://www.locomotiveapp.org" }
|
30
|
+
|
25
31
|
end
|
26
32
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
33
|
+
describe 'raises an error if the syntax is incorrect' do
|
34
|
+
|
35
|
+
let(:markup) { 'blog http://www.locomotiveapp.org' }
|
36
|
+
it { should raise_exception }
|
37
|
+
|
32
38
|
end
|
33
39
|
|
34
40
|
end
|
35
41
|
|
36
42
|
context '#rendering' do
|
37
43
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
44
|
+
let(:assigns) { {} }
|
45
|
+
let(:template) { '' }
|
46
|
+
let(:api_options) { { base_uri: 'http://blog.locomotiveapp.org' } }
|
47
|
+
let(:response) { { 'title' => 'Locomotive rocks !' } }
|
48
|
+
let(:mocked_response) { mock('response', code: 200, parsed_response: parsed_response(response)) }
|
49
|
+
|
50
|
+
before { Locomotive::Httparty::Webservice.expects(:get).with('/api/read', api_options).returns(mocked_response) }
|
51
|
+
|
52
|
+
subject { render_template(template, assigns) }
|
53
|
+
|
54
|
+
describe 'assign the response into the liquid variable' do
|
55
|
+
|
56
|
+
let(:template) { "{% consume blog from \"http://blog.locomotiveapp.org/api/read\" %}{{ blog.title }}{% endconsume %}" }
|
57
|
+
it { should eq 'Locomotive rocks !' }
|
58
|
+
|
43
59
|
end
|
44
60
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
template
|
49
|
-
|
61
|
+
describe 'assign the response into the liquid variable using a url from a variable' do
|
62
|
+
|
63
|
+
let(:assigns) { { 'url' => 'http://blog.locomotiveapp.org/api/read' } }
|
64
|
+
let(:template) { "{% consume blog from url %}{{ blog.title }}{% endconsume %}" }
|
65
|
+
it { should eq 'Locomotive rocks !' }
|
66
|
+
|
50
67
|
end
|
51
68
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
69
|
+
describe 'accept options for the web service' do
|
70
|
+
|
71
|
+
let(:assigns) { { 'secret_password' => 'bar' } }
|
72
|
+
let(:api_options) { { base_uri: 'http://blog.locomotiveapp.org', basic_auth: { username: 'foo', password: 'bar' } } }
|
73
|
+
let(:template) { "{% consume blog from \"http://blog.locomotiveapp.org/api/read\", username: 'foo', password: secret_password %}{{ blog.title }}{% endconsume %}" }
|
74
|
+
it { should eq 'Locomotive rocks !' }
|
56
75
|
|
57
|
-
[['/api/read', 'Locomotive rocks !'], ['/api/read_again', 'Locomotive still rocks !']].each do |path, title|
|
58
|
-
response = mock('response', code: 200, parsed_response: parsed_response('title' => title))
|
59
|
-
Locomotive::Httparty::Webservice.stubs(:get).with(path, {:base_uri => base_uri}).returns(response)
|
60
|
-
compiled_template.render('url' => base_uri + path).should == title
|
61
|
-
end
|
62
76
|
end
|
77
|
+
|
63
78
|
end
|
64
79
|
|
65
80
|
context 'timeout' do
|
66
81
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
82
|
+
let(:url) { 'http://blog.locomotiveapp.org/api/read' }
|
83
|
+
let(:template) { %{{% consume blog from "#{url}" timeout:5.0 %}{{ blog.title }}{% endconsume %}} }
|
84
|
+
|
85
|
+
subject { render_template(template) }
|
71
86
|
|
72
87
|
it 'should pass the timeout option to httparty' do
|
73
|
-
Locomotive::Httparty::Webservice.expects(:consume).with(
|
74
|
-
|
88
|
+
Locomotive::Httparty::Webservice.expects(:consume).with(url, { timeout: 5.0 })
|
89
|
+
subject
|
75
90
|
end
|
76
91
|
|
77
92
|
it 'should return the previous successful response if a timeout occurs' do
|
78
93
|
Locomotive::Httparty::Webservice.stubs(:consume).returns({ 'title' => 'first response' })
|
79
|
-
|
80
|
-
|
81
|
-
template.render.should == 'first response'
|
94
|
+
subject.should eq 'first response'
|
82
95
|
|
83
96
|
Locomotive::Httparty::Webservice.stubs(:consume).raises(Timeout::Error)
|
84
|
-
|
97
|
+
subject.should eq 'first response'
|
85
98
|
end
|
86
99
|
|
87
100
|
end
|
@@ -89,4 +102,9 @@ describe Locomotive::Liquid::Tags::Consume do
|
|
89
102
|
def parsed_response(attributes)
|
90
103
|
OpenStruct.new(underscore_keys: attributes)
|
91
104
|
end
|
105
|
+
|
106
|
+
def render_template(template, assigns = {})
|
107
|
+
_context = Liquid::Context.new(assigns, {}, {})
|
108
|
+
Liquid::Template.parse(template).render(_context)
|
109
|
+
end
|
92
110
|
end
|