refinerycms-pages 2.0.4 → 2.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  module Refinery
2
2
  class PagesController < ::ApplicationController
3
- before_filter :find_page, :except => [:preview]
3
+ before_filter :find_page, :set_canonical, :except => [:preview]
4
4
 
5
5
  # Save whole Page after delivery
6
6
  after_filter { |c| c.write_cache? }
@@ -94,6 +94,10 @@ module Refinery
94
94
  render render_options if render_options.any?
95
95
  end
96
96
 
97
+ def set_canonical
98
+ @canonical = refinery.url_for @page.canonical if @page.present?
99
+ end
100
+
97
101
  def write_cache?
98
102
  if Refinery::Pages.cache_pages_full
99
103
  cache_page(response.body, File.join('', 'refinery', 'cache', 'pages', request.path.sub("//", "/")).to_s)
@@ -10,6 +10,18 @@ module Refinery
10
10
  ActiveRecord::Associations::Preloader.new(pages, :translations).run
11
11
  pages.map {|page| ["#{'-' * page.level} #{page.title}", page.id]}
12
12
  end
13
+
14
+ def template_options(template_type, current_page)
15
+ return {} if current_page.send(template_type)
16
+
17
+ if current_page.parent_id?
18
+ # Use Parent Template by default.
19
+ { :selected => current_page.parent.send(template_type) }
20
+ else
21
+ # Use Default Template (First in whitelist)
22
+ { :selected => Refinery::Pages.send("#{template_type}_whitelist").first }
23
+ end
24
+ end
13
25
  end
14
26
  end
15
27
  end
@@ -26,7 +26,8 @@ module Refinery
26
26
  :skip_to_first_child, :position, :show_in_menu, :draft,
27
27
  :parts_attributes, :browser_title, :meta_description,
28
28
  :parent_id, :menu_title, :created_at, :updated_at,
29
- :page_id, :layout_template, :view_template, :custom_slug
29
+ :page_id, :layout_template, :view_template, :custom_slug,
30
+ :slug
30
31
 
31
32
  attr_accessor :locale, :page_title, :page_menu_title # to hold temporarily
32
33
  validates :title, :presence => true
@@ -173,7 +174,7 @@ module Refinery
173
174
 
174
175
  # Returns how many pages per page should there be when paginating pages
175
176
  def per_page(dialog = false)
176
- dialog ? Pages.pages_per_dialog : Pages.config.pages_per_admin_index
177
+ dialog ? Pages.pages_per_dialog : Pages.pages_per_admin_index
177
178
  end
178
179
 
179
180
  def expire_page_caching
@@ -186,18 +187,25 @@ module Refinery
186
187
  return true # so that other callbacks process.
187
188
  end
188
189
  end
190
+
191
+ def rebuild_with_invalidate_cached_urls!
192
+ rebuild_without_invalidate_cached_urls!
193
+ find_each { |page| page.send :invalidate_cached_urls }
194
+ end
195
+ alias_method_chain :rebuild!, :invalidate_cached_urls
196
+ end
197
+
198
+ # The canonical page for this particular page.
199
+ # Consists of:
200
+ # * The default locale's translated slug
201
+ def canonical
202
+ Globalize.with_locale(::Refinery::I18n.default_frontend_locale){ url }
189
203
  end
190
204
 
191
205
  # Returns in cascading order: custom_slug or menu_title or title depending on
192
206
  # which attribute is first found to be present for this page.
193
207
  def custom_slug_or_title
194
- if custom_slug.present?
195
- custom_slug
196
- elsif menu_title.present?
197
- menu_title
198
- else
199
- title
200
- end
208
+ custom_slug.presence || menu_title.presence || title
201
209
  end
202
210
 
203
211
  # Am I allowed to delete this page?
@@ -291,7 +299,7 @@ module Refinery
291
299
  # For example, this might evaluate to /about for the "About" page.
292
300
  def url_marketable
293
301
  # :id => nil is important to prevent any other params[:id] from interfering with this route.
294
- url_normal.merge(:path => nested_url, :id => nil)
302
+ url_normal.merge :path => nested_url, :id => nil
295
303
  end
296
304
 
297
305
  # Returns a url suitable to be used in url_for in Rails (such as link_to).
@@ -302,26 +310,23 @@ module Refinery
302
310
 
303
311
  # If the current locale is set to something other than the default locale
304
312
  # then the :locale attribute will be set on the url hash, otherwise it won't be.
305
- def with_locale_param(url_hash)
306
- if self.class.different_frontend_locale?
307
- url_hash.update(:locale => ::Refinery::I18n.current_frontend_locale)
308
- end
313
+ def with_locale_param(url_hash, locale = nil)
314
+ locale ||= ::Refinery::I18n.current_frontend_locale if self.class.different_frontend_locale?
315
+ url_hash.update :locale => locale if locale
309
316
  url_hash
310
317
  end
311
318
 
319
+ def uncached_nested_url
320
+ [parent.try(:uncached_nested_url), to_param.to_s].compact.flatten
321
+ end
322
+
312
323
  # Returns an array with all ancestors to_param, allow with its own
313
324
  # Ex: with an About page and a Mission underneath,
314
325
  # ::Refinery::Page.find('mission').nested_url would return:
315
326
  #
316
327
  # ['about', 'mission']
317
328
  #
318
- def nested_url
319
- Rails.cache.fetch(url_cache_key) { uncached_nested_url }
320
- end
321
-
322
- def uncached_nested_url
323
- [parent.try(:nested_url), to_param.to_s].compact.flatten
324
- end
329
+ alias_method :nested_url, :uncached_nested_url
325
330
 
326
331
  # Returns the string version of nested_url, i.e., the path that should be generated
327
332
  # by the router
@@ -444,8 +449,7 @@ module Refinery
444
449
  end
445
450
  alias_method_chain :normalize_friendly_id, :marketable_urls
446
451
 
447
- private
448
-
452
+ private
449
453
  def invalidate_cached_urls
450
454
  return true unless Refinery::Pages.marketable_urls
451
455
 
@@ -26,11 +26,8 @@
26
26
  <%= f.label :layout_template, t('.layout_template') %>
27
27
  <%= refinery_help_tag t('.layout_template_help') %>
28
28
  </span>
29
- <% if @page.parent_id? %>
30
- <%= f.select(:layout_template, @valid_layout_templates, {:selected => @page.parent.layout_template}) %>
31
- <% else %>
32
- <%= f.select(:layout_template, @valid_layout_templates) %>
33
- <% end %>
29
+ <%= f.select :layout_template, @valid_layout_templates,
30
+ template_options(:layout_template, @page) %>
34
31
  </div>
35
32
  <% end %>
36
33
  <% if Refinery::Pages.use_view_templates %>
@@ -39,11 +36,8 @@
39
36
  <%= f.label :view_template, t('.view_template') %>
40
37
  <%= refinery_help_tag t('.view_template_help') %>
41
38
  </span>
42
- <% if @page.parent_id? %>
43
- <%= f.select(:view_template, @valid_view_templates.map{|t| [t.titleize, t]}, {:selected => @page.parent.view_template }) %>
44
- <% else %>
45
- <%= f.select(:view_template, @valid_view_templates.map{|t| [t.titleize, t]}) %>
46
- <% end %>
39
+ <%= f.select :view_template, @valid_view_templates.map { |t| [t.titleize, t] },
40
+ template_options(:view_template, @page) %>
47
41
  </div>
48
42
  <% end %>
49
43
 
@@ -8,22 +8,20 @@
8
8
 
9
9
  <span class='title <%= 'toggle' if page.children.present? %>'>
10
10
  <%= page.title_with_meta.html_safe %>
11
- <% if Refinery.i18n_enabled? and Refinery::I18n.frontend_locales.many? %>
12
- <span class='preview'>
13
- <% page.translations.each do |translation| %>
14
- <% if translation.title.present? %>
15
- <%= link_to refinery_icon_tag("flags/#{translation.locale}.png", :size => '16x11'),
16
- refinery.edit_admin_page_path(page, :switch_locale => translation.locale),
17
- :class => 'locale' %>
18
- <% end %>
19
- <% end %>
20
- </span>
21
- <% end %>
22
11
  </span>
12
+ <% if Refinery.i18n_enabled? and Refinery::I18n.frontend_locales.many? %>
13
+ <span class='locales'>
14
+ <% page.translations.each do |translation| %>
15
+ <%= link_to refinery_icon_tag("flags/#{translation.locale}.png", :size => '16x11'),
16
+ refinery.edit_admin_page_path(page.nested_url, :switch_locale => translation.locale),
17
+ :class => 'locale' if translation.title.present? %>
18
+ <% end %>
19
+ </span>
20
+ <% end %>
23
21
 
24
22
  <span class='actions'>
25
23
  <%= link_to refinery_icon_tag('application_go.png'),
26
- refinery.page_path(page),
24
+ page.url,
27
25
  :target => "_blank",
28
26
  :title => t('.view_live_html') %>
29
27
  <%= link_to refinery_icon_tag('page_add.png'),
data/db/seeds.rb CHANGED
@@ -39,3 +39,10 @@ if Refinery::Page.by_title("About").empty?
39
39
  :position => 1
40
40
  })
41
41
  end
42
+
43
+ (Refinery.i18n_enabled? ? Refinery::I18n.frontend_locales : [:en]).each do |lang|
44
+ I18n.locale = lang
45
+ Refinery::Page.find_by_title("Home").update_attributes(:slug => "home")
46
+ Refinery::Page.find_by_title("Page not found").update_attributes(:slug => "page-not-found")
47
+ Refinery::Page.find_by_title("About").update_attributes(:slug => "about")
48
+ end
@@ -24,7 +24,11 @@ module Refinery
24
24
  plugin.name = 'refinery_pages'
25
25
  plugin.version = %q{2.0.0}
26
26
  plugin.menu_match = %r{refinery/page(_part|s_dialog)?s$}
27
- plugin.activity = { :class_name => :'refinery/page' }
27
+ plugin.activity = {
28
+ :class_name => :'refinery/page',
29
+ :nested_with => [:uncached_nested_url],
30
+ :use_record_in_nesting => false
31
+ }
28
32
  plugin.url = proc { Refinery::Core::Engine.routes.url_helpers.admin_pages_path }
29
33
  end
30
34
  end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ module Refinery
4
+ module Admin
5
+ describe PagesController do
6
+ login_refinery_superuser
7
+
8
+ describe "#update_positions" do
9
+ it "calls .rebuild! on Refinery::Page" do
10
+ Refinery::Page.should_receive(:rebuild!).once
11
+
12
+ post :update_positions, :ul => []
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,54 @@
1
+ require "spec_helper"
2
+
3
+ module Refinery
4
+ module Admin
5
+ describe PagesHelper do
6
+ describe "#template_options" do
7
+ context "when page layout/view templte is set" do
8
+ it "returns empty hash" do
9
+ page = FactoryGirl.create(:page)
10
+
11
+ page.view_template = "rspec_template"
12
+ helper.template_options(:view_template, page).should eq({})
13
+
14
+ page.layout_template = "rspec_layout"
15
+ helper.template_options(:layout_template, page).should eq({})
16
+ end
17
+ end
18
+
19
+ context "when page layout/view template isn't set" do
20
+ context "when page has parent" do
21
+ it "returns option hash based on parent page" do
22
+ parent = FactoryGirl.create(:page, :view_template => "rspec_view",
23
+ :layout_template => "rspec_layout")
24
+ page = FactoryGirl.create(:page, :parent_id => parent.id)
25
+
26
+ expected_view = { :selected => parent.view_template }
27
+ helper.template_options(:view_template, page).should eq(expected_view)
28
+
29
+ expected_layout = { :selected => parent.layout_template }
30
+ helper.template_options(:layout_template, page).should eq(expected_layout)
31
+ end
32
+ end
33
+
34
+ context "when page doesn't have parent page" do
35
+ before do
36
+ Refinery::Pages.stub(:view_template_whitelist).and_return(%w(one two))
37
+ Refinery::Pages.stub(:layout_template_whitelist).and_return(%w(two one))
38
+ end
39
+
40
+ it "returns option hash with first item from configured whitelist" do
41
+ page = FactoryGirl.create(:page)
42
+
43
+ expected_view = { :selected => "one" }
44
+ helper.template_options(:view_template, page).should eq(expected_view)
45
+
46
+ expected_layout = { :selected => "two" }
47
+ helper.template_options(:layout_template, page).should eq(expected_layout)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'spec_helper'
2
3
 
3
4
  module Refinery
@@ -103,6 +104,41 @@ module Refinery
103
104
  end
104
105
  end
105
106
 
107
+ describe '#canonical' do
108
+ before do
109
+ ::Refinery::I18n.stub(:default_frontend_locale).and_return(:en)
110
+ ::Refinery::I18n.stub(:frontend_locales).and_return([Refinery::I18n.default_frontend_locale, :ru])
111
+ ::Refinery::I18n.stub(:current_frontend_locale).and_return(Refinery::I18n.default_frontend_locale)
112
+
113
+ page.save
114
+ end
115
+ let(:page_title) { 'team' }
116
+ let(:child_title) { 'about' }
117
+ let(:ru_page_title) { 'Новости' }
118
+ let!(:default_canonical) {
119
+ Globalize.with_locale(::Refinery::I18n.default_frontend_locale) {
120
+ page.canonical
121
+ }
122
+ }
123
+
124
+ specify 'page returns itself' do
125
+ page.canonical.should == page.url
126
+ end
127
+
128
+ specify 'default canonical matches page#canonical' do
129
+ default_canonical.should == page.canonical
130
+ end
131
+
132
+ specify 'translated page returns master page' do
133
+ Globalize.with_locale(:ru) do
134
+ page.title = ru_page_title
135
+ page.save
136
+
137
+ page.canonical.should == default_canonical
138
+ end
139
+ end
140
+ end
141
+
106
142
  context 'custom slugs' do
107
143
  let(:custom_page_slug) { 'custom-page-slug' }
108
144
  let(:custom_child_slug) { 'custom-child-slug' }
@@ -132,7 +132,7 @@ module Refinery
132
132
  page.body.should =~ /Add a new child page/
133
133
  page.body.should =~ %r{/refinery/pages/new\?parent_id=}
134
134
  page.body.should =~ /View this page live/
135
- page.body.should =~ %r{/pages/my-first-page}
135
+ page.body.should =~ %r{href="/my-first-page"}
136
136
 
137
137
  Refinery::Page.count.should == 1
138
138
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: refinerycms-pages
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.4
4
+ version: 2.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -54,7 +54,7 @@ dependencies:
54
54
  requirements:
55
55
  - - '='
56
56
  - !ruby/object:Gem::Version
57
- version: 2.0.4
57
+ version: 2.0.5
58
58
  type: :runtime
59
59
  prerelease: false
60
60
  version_requirements: !ruby/object:Gem::Requirement
@@ -62,7 +62,7 @@ dependencies:
62
62
  requirements:
63
63
  - - '='
64
64
  - !ruby/object:Gem::Version
65
- version: 2.0.4
65
+ version: 2.0.5
66
66
  - !ruby/object:Gem::Dependency
67
67
  name: babosa
68
68
  requirement: !ruby/object:Gem::Requirement
@@ -168,7 +168,9 @@ files:
168
168
  - lib/refinerycms-pages.rb
169
169
  - license.md
170
170
  - refinerycms-pages.gemspec
171
+ - spec/controllers/refinery/admin/pages_controller_spec.rb
171
172
  - spec/factories/pages.rb
173
+ - spec/helpers/refinery/pages/admin/pages_helper_spec.rb
172
174
  - spec/helpers/refinery/pages/content_pages_helper_spec.rb
173
175
  - spec/lib/generators/refinery/pages/pages_generator_spec.rb
174
176
  - spec/lib/pages/content_page_presenter_spec.rb
@@ -195,7 +197,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
195
197
  version: '0'
196
198
  segments:
197
199
  - 0
198
- hash: 1077792218542240122
200
+ hash: -121281082121598221
199
201
  required_rubygems_version: !ruby/object:Gem::Requirement
200
202
  none: false
201
203
  requirements:
@@ -204,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
204
206
  version: '0'
205
207
  segments:
206
208
  - 0
207
- hash: 1077792218542240122
209
+ hash: -121281082121598221
208
210
  requirements: []
209
211
  rubyforge_project: refinerycms
210
212
  rubygems_version: 1.8.22
@@ -212,7 +214,9 @@ signing_key:
212
214
  specification_version: 3
213
215
  summary: Pages extension for Refinery CMS
214
216
  test_files:
217
+ - spec/controllers/refinery/admin/pages_controller_spec.rb
215
218
  - spec/factories/pages.rb
219
+ - spec/helpers/refinery/pages/admin/pages_helper_spec.rb
216
220
  - spec/helpers/refinery/pages/content_pages_helper_spec.rb
217
221
  - spec/lib/generators/refinery/pages/pages_generator_spec.rb
218
222
  - spec/lib/pages/content_page_presenter_spec.rb