locomotive_cms 0.0.2.8 → 0.0.2.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,13 +20,14 @@ module Admin::PagesHelper
20
20
  list
21
21
  end
22
22
 
23
- def options_for_page_cache_expiration
23
+ def options_for_page_cache_strategy
24
24
  [
25
- [t('.expiration.never'), 0],
26
- [t('.expiration.hour'), 1.hour],
27
- [t('.expiration.day'), 1.day],
28
- [t('.expiration.week'), 1.week],
29
- [t('.expiration.month'), 1.month]
25
+ [t('.cache_strategy.none'), 'none'],
26
+ [t('.cache_strategy.simple'), 'simple'],
27
+ [t('.cache_strategy.hour'), 1.hour.to_s],
28
+ [t('.cache_strategy.day'), 1.day.to_s],
29
+ [t('.cache_strategy.week'), 1.week.to_s],
30
+ [t('.cache_strategy.month'), 1.month.to_s]
30
31
  ]
31
32
  end
32
33
 
@@ -23,7 +23,6 @@ module Models
23
23
  before_destroy :remove_from_list
24
24
 
25
25
  # Fixme (Didier L.): Instances methods are defined before the include itself
26
- alias :ancestors :hacked_ancestors
27
26
  alias :fix_position :hacked_fix_position
28
27
  end
29
28
 
@@ -42,12 +41,7 @@ module Models
42
41
  self.fix_position(false)
43
42
  self.instance_variable_set :@_will_move, true
44
43
  end
45
-
46
- def hacked_ancestors
47
- return [] if root?
48
- self.class.find(self.path.clone << nil) # bug in mongoid (it does not handle array with one element)
49
- end
50
-
44
+
51
45
  protected
52
46
 
53
47
  def change_parent
data/app/models/page.rb CHANGED
@@ -12,7 +12,7 @@ class Page
12
12
  field :slug
13
13
  field :fullpath
14
14
  field :published, :type => Boolean, :default => false
15
- field :cache_expires_in, :type => Integer, :default => 0
15
+ field :cache_strategy, :default => 'none'
16
16
 
17
17
  ## associations ##
18
18
  belongs_to_related :site
@@ -35,7 +35,6 @@ class Page
35
35
  named_scope :not_found, :where => { :slug => '404', :depth => 0, :published => true }
36
36
 
37
37
  ## behaviours ##
38
- # liquid_methods :title, :fullpath
39
38
  liquify_template :joined_parts
40
39
 
41
40
  ## methods ##
@@ -66,6 +65,10 @@ class Page
66
65
  "http://#{self.site.domains.first}/#{self.fullpath}.html"
67
66
  end
68
67
 
68
+ def with_cache?
69
+ self.cache_strategy != 'none'
70
+ end
71
+
69
72
  def to_liquid(options = {})
70
73
  Locomotive::Liquid::Drops::Page.new(self)
71
74
  end
@@ -73,9 +76,6 @@ class Page
73
76
  protected
74
77
 
75
78
  def do_not_remove_index_and_404_pages
76
- # safe_site = self.site rescue nil
77
-
78
- # return if safe_site.nil?
79
79
  return if (self.site rescue nil).nil?
80
80
 
81
81
  if self.index? || self.not_found?
@@ -16,7 +16,7 @@
16
16
  = f.custom_input :published, :css => 'toggle' do
17
17
  = f.check_box :published
18
18
 
19
- = f.input :cache_expires_in, :as => :select, :collection => options_for_page_cache_expiration, :include_blank => false
19
+ = f.input :cache_strategy, :as => :select, :collection => options_for_page_cache_strategy, :include_blank => false
20
20
 
21
21
  #page-parts
22
22
  .nav
@@ -1,6 +1,8 @@
1
1
  - title t('.title')
2
2
 
3
3
  = semantic_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f|
4
+ = f.hidden_field :remember_me, :value => "true"
5
+
4
6
  .inner
5
7
  = login_flash_message
6
8
 
@@ -25,8 +25,7 @@ en:
25
25
  site: Site
26
26
  theme_assets: Theme files
27
27
  footer:
28
- developed_by: Developed by
29
- powered_by: and Powered by
28
+ who_is_behind: "Service developed by {{development}} and designed by <a href=\"http://www.sachagreif.com\">Sacha Greif</a>"
30
29
  form_actions:
31
30
  back: Back without saving
32
31
  create: Create
@@ -97,8 +96,9 @@ en:
97
96
  failed_create: "Page was not created."
98
97
  failed_update: "Page was not updated."
99
98
  form:
100
- expiration:
101
- never: Never
99
+ cache_strategy:
100
+ none: None
101
+ simple: Simple
102
102
  hour: 1 hour
103
103
  day: 1 day
104
104
  week: 1 week
@@ -331,7 +331,7 @@ en:
331
331
  hints:
332
332
  page:
333
333
  published: "Only authenticated accounts can view unpublished pages."
334
- cache_expires_in: "Cache the page for better performance. Pressing shift-reload in the browser will regenerate the page."
334
+ cache_strategy: "Cache the page for better performance. The 'Simple' choice is a good compromise."
335
335
  snippet:
336
336
  slug: "You need to know it in order to insert the snippet inside a page or a layout"
337
337
  site:
@@ -118,8 +118,9 @@ fr:
118
118
  failed_create: "La page n'a pas été créée."
119
119
  failed_update: "La page n'a pas été mise à jour."
120
120
  form:
121
- expiration:
122
- never: Jamais
121
+ cache_strategy:
122
+ none: Aucun
123
+ simple: Simple
123
124
  hour: 1 heure
124
125
  day: 1 jour
125
126
  week: 1 semaine
@@ -352,7 +353,7 @@ fr:
352
353
  hints:
353
354
  page:
354
355
  published: "Seuls les administrateurs authentifiés peuvent voir une page non publiée."
355
- cache_expires_in: "Cache la page pour de meilleure performance. Presser la touche SHIFT et le bouton \"Rafraichir\" dans le navigateur rechargera la page."
356
+ cache_strategy: "Cache la page pour de meilleure performance. L'option 'Simple' est le meilleur compromis."
356
357
  snippet:
357
358
  slug: "Utilisé pour insérer le snippet dans une page ou un gabarit."
358
359
  site:
@@ -57,7 +57,7 @@ fr:
57
57
  parent: Parent
58
58
  slug: Raccourci
59
59
  published: Publiée
60
- cache_expires_in: Cache expire dans
60
+ cache_strategy: Cache
61
61
  content_type:
62
62
  name: Nom
63
63
  description: Description
data/lib/core_ext.rb CHANGED
@@ -29,4 +29,34 @@ class String
29
29
  replace(self.slugify(options))
30
30
  end
31
31
 
32
+ end
33
+
34
+ ## Hash
35
+
36
+ class Hash
37
+
38
+ def underscore_keys
39
+ new_hash = {}
40
+
41
+ self.each_pair do |key, value|
42
+ if value.respond_to?(:collect!) # Array
43
+ value.collect do |item|
44
+ if item.respond_to?(:each_pair) # Hash item within
45
+ item.underscore_keys
46
+ else
47
+ item
48
+ end
49
+ end
50
+ elsif value.respond_to?(:each_pair) # Hash
51
+ value = value.underscore_keys
52
+ end
53
+
54
+ new_key = key.is_a?(String) ? key.underscore : key # only String keys
55
+
56
+ new_hash[new_key] = value
57
+ end
58
+
59
+ self.replace(new_hash)
60
+ end
61
+
32
62
  end
@@ -17,8 +17,9 @@ module Locomotive
17
17
 
18
18
  puts "[WebService] consuming #{path}, #{options.inspect}"
19
19
 
20
- self.get(path, options)
20
+ self.get(path, options).try(:underscore_keys)
21
21
  end
22
+
22
23
  end
23
24
  end
24
25
  end
@@ -14,7 +14,7 @@ module Locomotive
14
14
 
15
15
  output = @page.render(locomotive_context)
16
16
 
17
- prepare_and_set_response(output, @page.cache_expires_in || 0)
17
+ prepare_and_set_response(output)
18
18
  end
19
19
 
20
20
  def locomotive_page
@@ -48,9 +48,17 @@ module Locomotive
48
48
  ::Liquid::Context.new(assigns, registers)
49
49
  end
50
50
 
51
- def prepare_and_set_response(output, cache_expiration = 0)
52
- response.headers['Cache-Control'] = "public, max-age=#{cache_expiration}" if cache_expiration > 0
51
+ def prepare_and_set_response(output)
53
52
  response.headers['Content-Type'] = 'text/html; charset=utf-8'
53
+
54
+ if @page.with_cache?
55
+ fresh_when :etag => @page, :last_modified => @page.updated_at.utc, :public => true
56
+
57
+ if @page.cache_strategy != 'simple' # varnish
58
+ response.cache_control[:max_age] = @page.cache_strategy
59
+ end
60
+ end
61
+
54
62
  render :text => output, :layout => false, :status => :ok
55
63
  end
56
64
 
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Core extensions' do
4
+
5
+ describe 'Adding new methods for Hash items' do
6
+
7
+ describe 'defining underscore_keys' do
8
+
9
+ context 'from simple plain hash' do
10
+
11
+ it 'underscores each key' do
12
+ { 'foo-bar' => 42, :foo => 42, 'foo' => 42 }.underscore_keys.should == { 'foo_bar' => 42, :foo => 42, 'foo' => 42 }
13
+ end
14
+
15
+ end
16
+
17
+ context 'from nested hashes' do
18
+
19
+ it 'underscores each key' do
20
+ { 'foo-bar' => { 'bar-foo' => 42, :test => { 'bar-foo' => 42 } } }.underscore_keys.should == { 'foo_bar' => { 'bar_foo' => 42, :test => { 'bar_foo' => 42 } } }
21
+ end
22
+
23
+ end
24
+
25
+ context 'from hash with arrays of hashes' do
26
+
27
+ it 'underscores each key' do
28
+ { 'foo-bar' => [{ 'bar-foo' => 42 }, 42, { 'bar-foo' => 42 }] }.underscore_keys.should == { 'foo_bar' => [{ 'bar_foo' => 42 }, 42, { 'bar_foo' => 42 }] }
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -8,11 +8,13 @@ describe 'Locomotive rendering system' do
8
8
  @site = Factory.build(:site)
9
9
  Site.stubs(:find).returns(@site)
10
10
  @controller.current_site = @site
11
+ @page = Factory.build(:page, :site => nil, :published => true)
11
12
  end
12
13
 
13
14
  context 'setting the response' do
14
15
 
15
16
  before(:each) do
17
+ @controller.instance_variable_set(:@page, @page)
16
18
  @controller.send(:prepare_and_set_response, 'Hello world !')
17
19
  end
18
20
 
@@ -27,21 +29,27 @@ describe 'Locomotive rendering system' do
27
29
  it 'does not set the cache' do
28
30
  @controller.response.headers['Cache-Control'].should be_nil
29
31
  end
32
+
33
+ it 'sets the cache by simply using etag' do
34
+ @page.cache_strategy = 'simple'
35
+ @page.stubs(:updated_at).returns(Time.now)
36
+ @controller.send(:prepare_and_set_response, 'Hello world !')
37
+ @controller.response.to_a # force to build headers
38
+ @controller.response.headers['Cache-Control'].should == 'public'
39
+ end
30
40
 
31
- it 'sets the cache' do
32
- @controller.send(:prepare_and_set_response, 'Hello world !', 3600)
33
- @controller.response.headers['Cache-Control'].should == 'public, max-age=3600'
41
+ it 'sets the cache for Varnish' do
42
+ @page.cache_strategy = '3600'
43
+ @page.stubs(:updated_at).returns(Time.now)
44
+ @controller.send(:prepare_and_set_response, 'Hello world !')
45
+ @controller.response.to_a # force to build headers
46
+ @controller.response.headers['Cache-Control'].should == 'max-age=3600, public'
34
47
  end
35
48
 
36
49
  end
37
50
 
38
51
  context 'when retrieving page' do
39
52
 
40
- before(:each) do
41
- @page = Factory.build(:page, :site => nil, :published => true)
42
- @controller
43
- end
44
-
45
53
  it 'should retrieve the index page /' do
46
54
  @controller.request.fullpath = '/'
47
55
  @controller.current_site.pages.expects(:where).with({ :fullpath => 'index' }).returns([@page])
@@ -74,6 +74,11 @@ describe Page do
74
74
  page.slug.should == 'Valid_ite'
75
75
  end
76
76
 
77
+ it 'has no cache strategy' do
78
+ page = Factory.build(:page, :site => nil)
79
+ page.with_cache?.should == false
80
+ end
81
+
77
82
  end
78
83
 
79
84
  describe 'delete' do
@@ -4,7 +4,7 @@ Locomotive.configure do |config|
4
4
  end
5
5
 
6
6
  module Locomotive
7
- class TestController
7
+ class TestController < ApplicationController
8
8
 
9
9
  include Locomotive::Render
10
10
 
@@ -15,26 +15,20 @@ module Locomotive
15
15
  end
16
16
 
17
17
  def response
18
- @response ||= TestResponse.new
18
+ @_response ||= TestResponse.new
19
19
  end
20
20
 
21
21
  def request
22
- @request ||= TestRequest.new
22
+ @_request ||= TestRequest.new
23
23
  end
24
24
 
25
25
  end
26
26
 
27
- class TestResponse
28
-
29
- attr_accessor :headers
30
-
31
- def initialize
32
- self.headers = {}
33
- end
27
+ class TestResponse < ActionDispatch::TestResponse
34
28
 
35
29
  end
36
30
 
37
- class TestRequest
31
+ class TestRequest < ActionDispatch::TestRequest
38
32
 
39
33
  attr_accessor :fullpath
40
34
 
metadata CHANGED
@@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version
6
6
  - 0
7
7
  - 0
8
8
  - 2
9
- - 8
10
- version: 0.0.2.8
9
+ - 9
10
+ version: 0.0.2.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Didier Lafforgue
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-06 00:00:00 +02:00
18
+ date: 2010-07-09 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -1214,6 +1214,7 @@ specification_version: 3
1214
1214
  summary: Locomotive cms engine
1215
1215
  test_files:
1216
1216
  - spec/factories.rb
1217
+ - spec/lib/core_ext_spec.rb
1217
1218
  - spec/lib/locomotive/heroku_spec.rb
1218
1219
  - spec/lib/locomotive/httparty/patches_spec.rb
1219
1220
  - spec/lib/locomotive/httparty/webservice_spec.rb