jakewendt-pages 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,35 @@
1
+ <% content_tag_for( :tr, page, :class => 'row' ) do %>
2
+ <td><%= hidden_field_tag( 'pages[]', page.id, :id => nil ) %>
3
+ <%= image_tag 'drag.gif', :class => 'handle' %></td>
4
+ <td class='position'><%= page.position -%></td>
5
+
6
+
7
+ <td><table><tbody><tr>
8
+ <td class='path'>
9
+ <%= link_to page.path,
10
+ ActionController::Base.relative_url_root.to_s + page.path -%>
11
+ </td>
12
+ <td class='menu'>
13
+ <%= link_to page.menu(session[:locale]), page -%>
14
+ </td>
15
+ <td class='title'>
16
+ <%= link_to page.title(session[:locale]), page -%>
17
+ </td>
18
+ <td class='manage'>
19
+ <% if page.children.length > 1 %>
20
+ <%= link_to "Order", pages_path(:parent_id => page.id) -%>&nbsp;
21
+ <% end %>
22
+ <%= link_to "Edit", edit_page_path(page) -%>&nbsp;
23
+ <%= link_to "Destroy", page_path(page),
24
+ :confirm => "Delete '#{page.title(session[:locale])}'?\nAre you sure?",
25
+ :method => :delete -%>
26
+ <%#= destroy_link_to "Destroy", page_path(page) -%>
27
+ </td>
28
+ </tr>
29
+
30
+ <% if page.children.length > 0 %>
31
+ <%= render :partial => 'child', :collection => page.children %>
32
+ <% end %>
33
+
34
+ </tbody></table></td>
35
+ <% end %><!-- class='page' -->
@@ -0,0 +1,9 @@
1
+ <%= error_messages_for :page %>
2
+
3
+ <% form_for(@page) do |f| %>
4
+ <%= render 'form', :f => f %>
5
+ <p>
6
+ <%= f.submit "Update", :id => nil %>&nbsp;
7
+ <%= button_link_to "Cancel and View", @page %>
8
+ </p>
9
+ <% end %>
@@ -0,0 +1,28 @@
1
+ <% javascripts('pages') %>
2
+ <% stylesheets('pages') %>
3
+
4
+ <h4>Pages:</h4>
5
+ <% form_tag(order_pages_path, :method => :post, :id => 'order_pages') do -%>
6
+ <%= hidden_field_tag :parent_id, params[:parent_id] %>
7
+ <% unless @pages.empty? %>
8
+ <table id='pages'><tbody>
9
+ <%= render :partial => 'page', :collection => @pages %>
10
+ </tbody></table><!-- id='pages' -->
11
+ <% else %>
12
+ Sorry, no pages yet.
13
+ <% end %>
14
+
15
+ <p>
16
+ <%= link_to "Create new page",
17
+ new_page_path(:parent_id => params[:parent_id]) -%>
18
+ <%= submit_tag 'Save page order', :name => nil, :id => 'save_order' %>
19
+ </p>
20
+
21
+ <% end %>
22
+ <p>
23
+ The page model is used as a Content Management System
24
+ and for menu control. Some routes are not available
25
+ because the application is using them, but in order to
26
+ have the menu function, a "fake page" is created as a
27
+ placeholder. The staff functionality is an example.
28
+ </p>
@@ -0,0 +1,9 @@
1
+ <%= error_messages_for :page %>
2
+
3
+ <% form_for(@page) do |f| %>
4
+ <%= render 'form', :f => f %>
5
+ <p>
6
+ <%= f.submit "Create", :id => nil %>&nbsp;
7
+ <%= button_link_to "Cancel", pages_path %>
8
+ </p>
9
+ <% end %>
@@ -0,0 +1,29 @@
1
+ <% if @page -%>
2
+ <% content_for :head do -%>
3
+ <% javascript_tag do -%>
4
+ if ( typeof(translatables) == 'undefined' ){
5
+ var translatables = [];
6
+ }
7
+ tmp = { tag: '#page', locales: {} };
8
+ <% %w( en es ).each do |locale| -%>
9
+ tmp.locales["<%=locale%>"]="<%=URI.encode(sanitize(textilize(@page.body(locale))))-%>"
10
+ <% end -%>
11
+ translatables.push(tmp);
12
+
13
+ tmp = { tag: 'title', locales: {} };
14
+ <% %w( en es ).each do |locale| -%>
15
+ tmp.locales["<%=locale%>"]="<%=URI.encode(@page.title(locale))-%>"
16
+ <% end -%>
17
+ translatables.push(tmp);
18
+
19
+ tmp = { tag: '#current_root', locales: {} };
20
+ <% %w( en es ).each do |locale| -%>
21
+ tmp.locales["<%=locale%>"]="<%=URI.encode(@page.menu(locale))-%>"
22
+ <% end -%>
23
+ translatables.push(tmp);
24
+ <% end ; end ; end -%>
25
+ <div id='page'>
26
+ <%# if @page -%>
27
+ <%#= sanitize(textilize(@page.body(session[:locale]))) -%>
28
+ <%# end -%>
29
+ </div>
@@ -0,0 +1,42 @@
1
+ jQuery(function(){
2
+
3
+ locale = '<%=session[:locale] || 'en'%>'
4
+
5
+ set_locale_link = function(l){
6
+ if( l == 'es' ){
7
+ locale_link = '<%=link_to( 'English',
8
+ locale_path('en'),
9
+ :id => 'session_locale' )-%>'
10
+ }else{
11
+ locale_link = '<%=link_to( 'Espa&ntilde;ol',
12
+ locale_path('es'),
13
+ :id => 'session_locale' )-%>'
14
+ }
15
+
16
+ jQuery('#session_locale').replaceWith(locale_link);
17
+
18
+ jQuery('#session_locale').click(function(){
19
+ jQuery.getScript(this.href, function(){
20
+ translate(locale);
21
+ set_locale_link(locale);
22
+ });
23
+ return false;
24
+ });
25
+ }
26
+ set_locale_link(locale);
27
+
28
+ translate = function(l){
29
+ if( typeof(translatables) != 'undefined' ){
30
+ jQuery.each(translatables, function(index,value){
31
+ /*
32
+ jQuery(value.tag).html(decodeURI(value.locales[l]));
33
+ decodeURI mucked up decoding Entities
34
+ */
35
+ jQuery(value.tag).html(decodeURIComponent(value.locales[l]));
36
+ jQuery(value.tag).fadeIn('slow'); /* pointless */
37
+ })
38
+ }
39
+ };
40
+ translate(locale);
41
+
42
+ });
data/config/routes.rb ADDED
@@ -0,0 +1,9 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+
3
+ map.resources :pages, :collection => {
4
+ :translate => :get,
5
+ :order => :post }
6
+
7
+ map.resources :locales, :only => :show
8
+
9
+ end
File without changes
@@ -0,0 +1,66 @@
1
+ class PagesGenerator < Rails::Generator::Base
2
+
3
+ def manifest
4
+ # See Rails::Generator::Commands::Create
5
+ # rails-2.3.10/lib/rails_generator/commands.rb
6
+ # for code methods for record (Manifest)
7
+ record do |m|
8
+
9
+ %w( create_pages ).each do |migration|
10
+ m.migration_template "migrations/#{migration}.rb",
11
+ 'db/migrate', :migration_file_name => migration
12
+ end
13
+
14
+ m.directory('public/javascripts')
15
+ Dir["#{File.dirname(__FILE__)}/templates/javascripts/*js"].each{|file|
16
+ f = file.split('/').slice(-2,2).join('/')
17
+ m.file(f, "public/javascripts/#{File.basename(file)}")
18
+ }
19
+ m.directory('public/stylesheets')
20
+ Dir["#{File.dirname(__FILE__)}/templates/stylesheets/*css"].each{|file|
21
+ f = file.split('/').slice(-2,2).join('/')
22
+ m.file(f, "public/stylesheets/#{File.basename(file)}")
23
+ }
24
+ m.directory('test/functional/pages')
25
+ Dir["#{File.dirname(__FILE__)}/templates/functional/*rb"].each{|file|
26
+ f = file.split('/').slice(-2,2).join('/')
27
+ m.file(f, "test/functional/pages/#{File.basename(file)}")
28
+ }
29
+ m.directory('test/unit/pages')
30
+ Dir["#{File.dirname(__FILE__)}/templates/unit/*rb"].each{|file|
31
+ f = file.split('/').slice(-2,2).join('/')
32
+ m.file(f, "test/unit/pages/#{File.basename(file)}")
33
+ }
34
+ end
35
+ end
36
+
37
+ end
38
+ module Rails::Generator::Commands
39
+ class Create
40
+ def migration_template(relative_source,
41
+ relative_destination, template_options = {})
42
+ migration_directory relative_destination
43
+ migration_file_name = template_options[
44
+ :migration_file_name] || file_name
45
+ if migration_exists?(migration_file_name)
46
+ puts "Another migration is already named #{migration_file_name}: #{existing_migrations(migration_file_name).first}: Skipping"
47
+ else
48
+ template(relative_source, "#{relative_destination}/#{next_migration_string}_#{migration_file_name}.rb", template_options)
49
+ end
50
+ end
51
+ end # Create
52
+ class Base
53
+ protected
54
+ # the loop through migrations happens so fast
55
+ # that they all have the same timestamp which
56
+ # won't work when you actually try to migrate.
57
+ # All the timestamps MUST be unique.
58
+ def next_migration_string(padding = 3)
59
+ @s = (!@s.nil?)? @s.to_i + 1 : if ActiveRecord::Base.timestamped_migrations
60
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
61
+ else
62
+ "%.#{padding}d" % next_migration_number
63
+ end
64
+ end
65
+ end # Base
66
+ end
@@ -0,0 +1,59 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ class Pages::LocalesControllerTest < ActionController::TestCase
4
+ tests LocalesController
5
+
6
+ test "should set locale to en" do
7
+ assert_nil session[:locale]
8
+ get :show, :id => 'en'
9
+ assert_equal 'en', session[:locale]
10
+ assert_response :redirect
11
+ end
12
+
13
+ test "should change locale to es" do
14
+ assert_nil session[:locale]
15
+ session[:locale] = 'en'
16
+ assert_equal 'en', session[:locale]
17
+ get :show, :id => 'es'
18
+ assert_equal 'es', session[:locale]
19
+ assert_response :redirect
20
+ end
21
+
22
+ test "should change locale to en" do
23
+ assert_nil session[:locale]
24
+ session[:locale] = 'es'
25
+ assert_equal 'es', session[:locale]
26
+ get :show, :id => 'en'
27
+ assert_equal 'en', session[:locale]
28
+ assert_response :redirect
29
+ end
30
+
31
+ test "should set locale to en via js" do
32
+ @request.accept = "text/javascript"
33
+ assert_nil session[:locale]
34
+ get :show, :id => 'en'
35
+ assert_equal 'en', session[:locale]
36
+ assert_response :success
37
+ end
38
+
39
+ test "should change locale to es via js" do
40
+ @request.accept = "text/javascript"
41
+ assert_nil session[:locale]
42
+ session[:locale] = 'en'
43
+ assert_equal 'en', session[:locale]
44
+ get :show, :id => 'es'
45
+ assert_equal 'es', session[:locale]
46
+ assert_response :success
47
+ end
48
+
49
+ test "should change locale to en via js" do
50
+ @request.accept = "text/javascript"
51
+ assert_nil session[:locale]
52
+ session[:locale] = 'es'
53
+ assert_equal 'es', session[:locale]
54
+ get :show, :id => 'en'
55
+ assert_equal 'en', session[:locale]
56
+ assert_response :success
57
+ end
58
+
59
+ end
@@ -0,0 +1,248 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ class Pages::PagesControllerTest < ActionController::TestCase
4
+ tests PagesController
5
+
6
+ ASSERT_ACCESS_OPTIONS = {
7
+ :model => 'Page',
8
+ :actions => [:new,:create,:edit,:update,:destroy,:index],
9
+ :method_for_create => :factory_create,
10
+ :attributes_for_create => :factory_attributes
11
+ }
12
+
13
+ def factory_create(options={})
14
+ Factory(:page,options)
15
+ end
16
+ def factory_attributes(options={})
17
+ Factory.attributes_for(:page,options)
18
+ end
19
+
20
+ assert_access_with_http :show, { :actions => nil }
21
+ assert_access_with_https :show
22
+ assert_no_access_with_http
23
+
24
+ assert_access_with_login(:show,{
25
+ :logins => [:super_user,:admin,:editor]})
26
+ assert_access_with_login(:show,{
27
+ :logins => [:interviewer,:reader,:active_user], :actions => nil})
28
+ assert_access_without_login( :show, { :actions => nil })
29
+ assert_no_access_with_login({
30
+ :logins => [:interviewer,:reader,:active_user] })
31
+ assert_no_access_without_login
32
+
33
+ assert_no_access_with_login(
34
+ :attributes_for_create => nil,
35
+ :method_for_create => nil,
36
+ :actions => nil,
37
+ :suffix => " and invalid id",
38
+ :login => :super_user,
39
+ :redirect => :pages_path,
40
+ :edit => { :id => 0 },
41
+ :update => { :id => 0 },
42
+ :destroy => { :id => 0 }
43
+ )
44
+
45
+
46
+ %w( super_user admin editor ).each do |cu|
47
+ #
48
+ # index/new/create/edit/update/destroy
49
+ # should only be visible to admins for editing
50
+ #
51
+
52
+ test "should get index with pages with #{cu} login" do
53
+ 3.times{ factory_create }
54
+ login_as send(cu)
55
+ get :index
56
+ assert_template 'index'
57
+ assert_response :success
58
+ assert_not_nil assigns(:pages)
59
+ assigns(:pages).each { |page| assert_nil page.parent }
60
+ end
61
+
62
+ test "should get index with blank parent with #{cu} login" do
63
+ 3.times{ factory_create }
64
+ login_as send(cu)
65
+ get :index, :parent_id => ''
66
+ assert_template 'index'
67
+ assert_response :success
68
+ assert_not_nil assigns(:pages)
69
+ assigns(:pages).each { |page| assert_nil page.parent }
70
+ end
71
+
72
+ test "should get index with subpages with #{cu} login" do
73
+ parent = factory_create
74
+ 3.times{ factory_create(:parent_id => parent.id) }
75
+ login_as send(cu)
76
+ get :index, :parent_id => parent.id
77
+ assert_template 'index'
78
+ assert_response :success
79
+ assert_not_nil assigns(:pages)
80
+ assigns(:pages).each do |page|
81
+ assert_equal page.parent_id, parent.id
82
+ end
83
+ end
84
+
85
+ test "should create page with parent with #{cu} login" do
86
+ parent = factory_create
87
+ login_as send(cu)
88
+ assert_difference('Page.count') do
89
+ post :create, :page => factory_attributes(
90
+ :parent_id => parent.id)
91
+ end
92
+ assert_equal parent, assigns(:page).parent
93
+ assert_redirected_to page_path(assigns(:page))
94
+ end
95
+
96
+ test "should NOT create page with invalid page with #{cu} login" do
97
+ login_as send(cu)
98
+ assert_no_difference('Page.count') do
99
+ post :create, :page => {}
100
+ end
101
+ assert_template 'new'
102
+ assert_response :success
103
+ end
104
+
105
+ test "should NOT update page with invalid page with #{cu} login" do
106
+ login_as send(cu)
107
+ put :update, :id => factory_create.id,
108
+ :page => { :title => "a" }
109
+ assert_not_nil flash[:error]
110
+ assert_template 'edit'
111
+ assert_response :success
112
+ end
113
+
114
+ # I don't think that this is pertinant anymore
115
+ test "should get index with both help and non-help pages with #{cu} login" do
116
+ # test css menus
117
+ login_as send(cu)
118
+ nonhelp_page = factory_create(:path => "/hello" )
119
+ help_page = factory_create(:path => "/help/test" )
120
+ get :index
121
+ assert_response :success
122
+ assert_template 'index'
123
+ end
124
+
125
+ # action: order
126
+
127
+ test "should order pages with #{cu} login" do
128
+ login_as send(cu)
129
+ # pages = 3.times.collect{|i| factory_create }
130
+ # 3.times.collect doesn't work on
131
+ #> ruby --version
132
+ #ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
133
+ pages = []
134
+ 3.times{ pages.push(factory_create) }
135
+ before_page_ids = Page.all.collect(&:id)
136
+ post :order, :pages => before_page_ids.reverse
137
+ after_page_ids = Page.all.collect(&:id)
138
+ assert_equal after_page_ids, before_page_ids.reverse
139
+ assert_redirected_to pages_path
140
+ end
141
+
142
+ test "should order sub pages with #{cu} login" do
143
+ login_as send(cu)
144
+ parent = factory_create
145
+ pages = []
146
+ 3.times{ pages.push(factory_create(:parent_id => parent.id)) }
147
+ assert_equal [1,2,3], pages.collect(&:position)
148
+ before_page_ids = parent.reload.children.collect(&:id)
149
+ post :order,:parent_id => parent.id, :pages => before_page_ids.reverse
150
+ after_page_ids = parent.reload.children.collect(&:id)
151
+ assert_equal after_page_ids, before_page_ids.reverse
152
+ assert_redirected_to pages_path(:parent_id => parent.id)
153
+ end
154
+
155
+ end
156
+
157
+ %w( interviewer reader active_user ).each do |cu|
158
+
159
+ test "should NOT order pages with #{cu} login" do
160
+ login_as send(cu)
161
+ pages = []
162
+ 3.times{ pages.push(factory_create) }
163
+ before_page_ids = Page.all.collect(&:id)
164
+ post :order, :pages => before_page_ids.reverse
165
+ assert_not_nil flash[:error]
166
+ assert_redirected_to root_path
167
+ end
168
+
169
+ end
170
+
171
+ test "should NOT order pages without login" do
172
+ pages = []
173
+ 3.times{ pages.push(factory_create) }
174
+ before_page_ids = Page.all.collect(&:id)
175
+ post :order, :pages => before_page_ids.reverse
176
+ assert_redirected_to_login
177
+ end
178
+
179
+ #
180
+ # /pages/:id should be visible to anyone ?
181
+ #
182
+ # as the pages controller uses the gateway filter, not being logged in currently
183
+ # requires the addition of the line ...
184
+ # CASClient::Frameworks::Rails::GatewayFilter.stubs(:filter).returns(false)
185
+ #
186
+
187
+ test "should NOT show page with invalid id" do
188
+ get :show, :id => 0
189
+ assert_not_nil flash[:error]
190
+ assert_template 'show'
191
+ assert_response :success
192
+ end
193
+
194
+ test "should NOT show page without matching path" do
195
+ get :show, :path => "/i/do/not/exist".split('/').delete_if{|x|x.blank?}
196
+ assert_not_nil flash[:error]
197
+ assert_template 'show'
198
+ assert_response :success
199
+ end
200
+
201
+ test "should show page by path" do
202
+ page = factory_create
203
+ get :show, :path => page.path.split('/').delete_if{|x|x.blank?}
204
+ assert_equal assigns(:page), page
205
+ assert_template 'show'
206
+ assert_response :success
207
+ assert_select 'title', page.title
208
+ end
209
+
210
+ test "should show page by path with slashes" do
211
+ page = factory_create(:path => "/help/blogs")
212
+ get :show, :path => page.path.split('/').delete_if{|x|x.blank?}
213
+ assert_equal assigns(:page), page
214
+ assert_template 'show'
215
+ assert_response :success
216
+ assert_select 'title', page.title
217
+ end
218
+
219
+ test "should show HOME page without HPP" do
220
+ page = Page.by_path('/')
221
+ get :show, :id => page.id
222
+ assert_nil assigns(:hpp)
223
+ assert_template 'show'
224
+ assert_response :success
225
+ assert_select 'title', page.title
226
+ end
227
+
228
+ test "should get translate via js without login" do
229
+ @request.accept = "text/javascript"
230
+ get :translate
231
+ assert_response :success
232
+ assert_match /jQuery/, @response.body
233
+ end
234
+
235
+ test "should get each page in fixtures in each locale" do
236
+ puts
237
+ Page.all.each do |page|
238
+ [ nil,'en','es' ].each do |locale|
239
+ puts "- Showing page id #{page.id} with locale #{locale}"
240
+ session[:locale] = locale
241
+ get :show, :id => page.id
242
+ assert_response :success
243
+ assert_template 'show'
244
+ end
245
+ end
246
+ end
247
+
248
+ end