comfortable_mexican_sofa 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +83 -1
- data/VERSION +1 -1
- data/app/controllers/cms_admin/base_controller.rb +15 -0
- data/app/controllers/cms_admin/layouts_controller.rb +3 -3
- data/app/controllers/cms_admin/pages_controller.rb +8 -8
- data/app/controllers/cms_admin/sites_controller.rb +58 -0
- data/app/controllers/cms_admin/snippets_controller.rb +18 -14
- data/app/controllers/cms_admin/uploads_controller.rb +7 -5
- data/app/controllers/cms_content_controller.rb +17 -5
- data/app/models/cms_layout.rb +7 -7
- data/app/models/cms_page.rb +18 -4
- data/app/models/cms_site.rb +23 -0
- data/app/models/cms_snippet.rb +17 -12
- data/app/models/cms_upload.rb +4 -7
- data/app/views/cms_admin/layouts/_form.html.erb +1 -1
- data/app/views/cms_admin/layouts/index.html.erb +1 -1
- data/app/views/cms_admin/pages/_form.html.erb +3 -2
- data/app/views/cms_admin/pages/_form_blocks.html.erb +1 -1
- data/app/views/cms_admin/pages/_index_branch.html.erb +1 -1
- data/app/views/cms_admin/pages/index.html.erb +1 -1
- data/app/views/cms_admin/sites/_form.html.erb +2 -0
- data/app/views/cms_admin/sites/edit.html.erb +6 -0
- data/app/views/cms_admin/sites/index.html.erb +22 -0
- data/app/views/cms_admin/sites/new.html.erb +6 -0
- data/app/views/cms_admin/snippets/_form.html.erb +2 -1
- data/app/views/cms_admin/snippets/index.html.erb +20 -1
- data/app/views/layouts/cms_admin.html.erb +2 -1
- data/comfortable_mexican_sofa.gemspec +31 -9
- data/config/initializers/comfortable_mexican_sofa.rb +10 -0
- data/config/routes.rb +3 -1
- data/db/migrate/01_create_cms.rb +18 -4
- data/lib/comfortable_mexican_sofa.rb +28 -22
- data/lib/comfortable_mexican_sofa/acts_as_tree.rb +97 -0
- data/lib/comfortable_mexican_sofa/cms_tag/snippet.rb +4 -0
- data/lib/comfortable_mexican_sofa/configuration.rb +19 -0
- data/lib/comfortable_mexican_sofa/controller_methods.rb +41 -0
- data/lib/comfortable_mexican_sofa/{cms_engine.rb → engine.rb} +1 -1
- data/lib/comfortable_mexican_sofa/{cms_form_builder.rb → form_builder.rb} +1 -1
- data/lib/comfortable_mexican_sofa/http_auth.rb +17 -0
- data/lib/comfortable_mexican_sofa/rails_extensions.rb +11 -0
- data/lib/comfortable_mexican_sofa/view_methods.rb +33 -0
- data/lib/generators/cms_generator.rb +4 -0
- data/public/stylesheets/comfortable_mexican_sofa/structure.css +10 -12
- data/test/fixtures/cms_layouts.yml +3 -1
- data/test/fixtures/cms_pages.yml +6 -2
- data/test/fixtures/cms_sites.yml +3 -0
- data/test/fixtures/cms_snippets.yml +3 -1
- data/test/fixtures/cms_uploads.yml +1 -0
- data/test/functional/cms_admin/layouts_controller_test.rb +3 -1
- data/test/functional/cms_admin/pages_controller_test.rb +4 -2
- data/test/functional/cms_admin/sites_controller_test.rb +92 -0
- data/test/functional/cms_admin/snippets_controller_test.rb +62 -37
- data/test/functional/cms_content_controller_test.rb +32 -5
- data/test/integration/authentication_test.rb +27 -0
- data/test/integration/render_cms_test.rb +57 -0
- data/test/integration/sites_test.rb +30 -0
- data/test/test_helper.rb +46 -3
- data/test/unit/cms_block_test.rb +1 -0
- data/test/unit/cms_configuration_test.rb +16 -0
- data/test/unit/cms_layout_test.rb +3 -3
- data/test/unit/cms_page_test.rb +23 -13
- data/test/unit/cms_site_test.rb +41 -0
- data/test/unit/cms_snippet_test.rb +1 -1
- data/test/unit/cms_tags/snippet_test.rb +1 -1
- data/test/unit/cms_upload_test.rb +7 -6
- metadata +32 -10
- data/lib/comfortable_mexican_sofa/cms_acts_as_tree.rb +0 -101
- data/lib/comfortable_mexican_sofa/cms_rails_extensions.rb +0 -32
- data/test/functional/cms_admin/base_controller_test.rb +0 -9
data/test/unit/cms_block_test.rb
CHANGED
@@ -23,6 +23,7 @@ class CmsBlockTest < ActiveSupport::TestCase
|
|
23
23
|
def test_new_with_cast_via_page_nested_attributes
|
24
24
|
assert_difference ['CmsPage.count', 'CmsBlock.count'] do
|
25
25
|
page = CmsPage.create!(
|
26
|
+
:cms_site => cms_sites(:default),
|
26
27
|
:cms_layout => cms_layouts(:default),
|
27
28
|
:label => 'test page',
|
28
29
|
:slug => 'test_page',
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class CmsConfigurationTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
def test_configuration_presense
|
6
|
+
assert config = ComfortableMexicanSofa.configuration
|
7
|
+
assert_equal 'ComfortableMexicanSofa', config.cms_title
|
8
|
+
assert_equal 'ComfortableMexicanSofa::HttpAuth', config.authentication
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_initialization_overrides
|
12
|
+
ComfortableMexicanSofa.configuration.cms_title = 'New Title'
|
13
|
+
assert_equal 'New Title', ComfortableMexicanSofa.configuration.cms_title
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -15,9 +15,9 @@ class CmsLayoutTest < ActiveSupport::TestCase
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_options_for_select
|
18
|
-
assert_equal ['Default Layout', 'Nested Layout', '. . Child Layout'], CmsLayout.options_for_select.collect{|t| t.first}
|
19
|
-
assert_equal ['Default Layout', 'Nested Layout'], CmsLayout.options_for_select(cms_layouts(:child)).collect{|t| t.first}
|
20
|
-
assert_equal ['Default Layout'], CmsLayout.options_for_select(cms_layouts(:nested)).collect{|t| t.first}
|
18
|
+
assert_equal ['Default Layout', 'Nested Layout', '. . Child Layout'], CmsLayout.options_for_select(cms_sites(:default)).collect{|t| t.first}
|
19
|
+
assert_equal ['Default Layout', 'Nested Layout'], CmsLayout.options_for_select(cms_sites(:default), cms_layouts(:child)).collect{|t| t.first}
|
20
|
+
assert_equal ['Default Layout'], CmsLayout.options_for_select(cms_sites(:default), cms_layouts(:nested)).collect{|t| t.first}
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_app_layouts_for_select
|
data/test/unit/cms_page_test.rb
CHANGED
@@ -26,31 +26,41 @@ class CmsPageTest < ActiveSupport::TestCase
|
|
26
26
|
assert_has_errors_on page, :parent_id
|
27
27
|
end
|
28
28
|
|
29
|
+
def test_validation_of_target_page
|
30
|
+
page = cms_pages(:child)
|
31
|
+
page.target_page = cms_pages(:default)
|
32
|
+
page.save!
|
33
|
+
assert_equal cms_pages(:default), page.target_page
|
34
|
+
page.target_page = page
|
35
|
+
assert page.invalid?
|
36
|
+
assert_has_errors_on page, :target_page_id
|
37
|
+
end
|
38
|
+
|
29
39
|
def test_initialization_of_full_path
|
30
40
|
page = CmsPage.new(new_params)
|
31
41
|
assert page.invalid?
|
32
|
-
assert_has_errors_on page, :
|
42
|
+
assert_has_errors_on page, :cms_site_id
|
33
43
|
|
34
|
-
page =
|
44
|
+
page = cms_sites(:default).cms_pages.new(new_params(:parent => cms_pages(:default)))
|
35
45
|
assert page.valid?
|
36
46
|
assert_equal '/test-page', page.full_path
|
37
47
|
|
38
|
-
page =
|
48
|
+
page = cms_sites(:default).cms_pages.new(new_params(:parent => cms_pages(:child)))
|
39
49
|
assert page.valid?
|
40
50
|
assert_equal '/child-page/test-page', page.full_path
|
41
51
|
|
42
52
|
CmsPage.destroy_all
|
43
|
-
page =
|
53
|
+
page = cms_sites(:default).cms_pages.new(new_params)
|
44
54
|
assert page.valid?
|
45
55
|
assert_equal '/', page.full_path
|
46
56
|
end
|
47
57
|
|
48
58
|
def test_sync_child_pages
|
49
59
|
page = cms_pages(:child)
|
50
|
-
page_1 =
|
51
|
-
page_2 =
|
52
|
-
page_3 =
|
53
|
-
page_4 =
|
60
|
+
page_1 = cms_sites(:default).cms_pages.create!(new_params(:parent => page, :slug => 'test-page-1'))
|
61
|
+
page_2 = cms_sites(:default).cms_pages.create!(new_params(:parent => page, :slug => 'test-page-2'))
|
62
|
+
page_3 = cms_sites(:default).cms_pages.create!(new_params(:parent => page_2, :slug => 'test-page-3'))
|
63
|
+
page_4 = cms_sites(:default).cms_pages.create!(new_params(:parent => page_1, :slug => 'test-page-4'))
|
54
64
|
assert_equal '/child-page/test-page-1', page_1.full_path
|
55
65
|
assert_equal '/child-page/test-page-2', page_2.full_path
|
56
66
|
assert_equal '/child-page/test-page-2/test-page-3', page_3.full_path
|
@@ -78,7 +88,7 @@ class CmsPageTest < ActiveSupport::TestCase
|
|
78
88
|
assert_equal 1, page_1.children_count
|
79
89
|
assert_equal 0, page_2.children_count
|
80
90
|
|
81
|
-
page_3 =
|
91
|
+
page_3 = cms_sites(:default).cms_pages.create!(new_params(:parent => page_2))
|
82
92
|
page_1.reload; page_2.reload
|
83
93
|
assert_equal 1, page_1.children_count
|
84
94
|
assert_equal 1, page_2.children_count
|
@@ -102,12 +112,12 @@ class CmsPageTest < ActiveSupport::TestCase
|
|
102
112
|
end
|
103
113
|
|
104
114
|
def test_options_for_select
|
105
|
-
assert_equal ['Default Page', '. . Child Page'], CmsPage.options_for_select.collect{|t| t.first }
|
106
|
-
assert_equal ['Default Page'], CmsPage.options_for_select(cms_pages(:child)).collect{|t| t.first }
|
107
|
-
assert_equal [], CmsPage.options_for_select(cms_pages(:default))
|
115
|
+
assert_equal ['Default Page', '. . Child Page'], CmsPage.options_for_select(cms_sites(:default)).collect{|t| t.first }
|
116
|
+
assert_equal ['Default Page'], CmsPage.options_for_select(cms_sites(:default), cms_pages(:child)).collect{|t| t.first }
|
117
|
+
assert_equal [], CmsPage.options_for_select(cms_sites(:default), cms_pages(:default))
|
108
118
|
|
109
119
|
page = CmsPage.new(new_params(:parent => cms_pages(:default)))
|
110
|
-
assert_equal ['Default Page', '. . Child Page'], CmsPage.options_for_select(page).collect{|t| t.first }
|
120
|
+
assert_equal ['Default Page', '. . Child Page'], CmsPage.options_for_select(cms_sites(:default), page).collect{|t| t.first }
|
111
121
|
end
|
112
122
|
|
113
123
|
protected
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class CmsSiteTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
def test_fixtures_validity
|
6
|
+
CmsSite.all.each do |site|
|
7
|
+
assert site.valid?, site.errors.full_messages
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_validation
|
12
|
+
site = CmsSite.new
|
13
|
+
assert site.invalid?
|
14
|
+
assert_has_errors_on site, [:label, :hostname]
|
15
|
+
|
16
|
+
site = CmsSite.new(:label => 'My Site', :hostname => 'http://mysite.com')
|
17
|
+
assert site.invalid?
|
18
|
+
assert_has_errors_on site, :hostname
|
19
|
+
|
20
|
+
site = CmsSite.new(:label => 'My Site', :hostname => 'mysite.com')
|
21
|
+
assert site.valid?
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_cascading_destroy
|
25
|
+
assert_difference 'CmsSite.count', -1 do
|
26
|
+
assert_difference 'CmsLayout.count', -3 do
|
27
|
+
assert_difference 'CmsPage.count', -2 do
|
28
|
+
assert_difference 'CmsSnippet.count', -1 do
|
29
|
+
cms_sites(:default).destroy
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_options_for_select
|
37
|
+
assert_equal 1, CmsSite.options_for_select.size
|
38
|
+
assert_equal 'Default Site (test.host)', CmsSite.options_for_select[0][0]
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -11,20 +11,21 @@ class CmsUploadTest < ActiveSupport::TestCase
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_create
|
14
|
-
assert_difference 'CmsUpload.count'
|
15
|
-
upload =
|
16
|
-
|
17
|
-
|
14
|
+
assert_difference 'CmsUpload.count' do
|
15
|
+
upload = cms_sites(:default).cms_uploads.create(
|
16
|
+
:uploaded_file => fixture_file_upload('files/valid_image.jpg')
|
17
|
+
)
|
18
18
|
assert upload.image?
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
def test_create_fails_on_invalid_file
|
23
23
|
assert_no_difference 'CmsUpload.count' do
|
24
|
-
upload =
|
24
|
+
upload = cms_sites(:default).cms_uploads.create(
|
25
|
+
:file => fixture_file_upload('files/invalid_file.gif', 'image/gif')
|
26
|
+
)
|
25
27
|
assert upload.errors.present?
|
26
28
|
assert_has_errors_on upload, :file
|
27
29
|
end
|
28
30
|
end
|
29
|
-
|
30
31
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: comfortable_mexican_sofa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 2
|
10
|
+
version: 1.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Oleg Khabarov
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-10-
|
19
|
+
date: 2010-10-19 00:00:00 -04:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -86,12 +86,14 @@ files:
|
|
86
86
|
- app/controllers/cms_admin/base_controller.rb
|
87
87
|
- app/controllers/cms_admin/layouts_controller.rb
|
88
88
|
- app/controllers/cms_admin/pages_controller.rb
|
89
|
+
- app/controllers/cms_admin/sites_controller.rb
|
89
90
|
- app/controllers/cms_admin/snippets_controller.rb
|
90
91
|
- app/controllers/cms_admin/uploads_controller.rb
|
91
92
|
- app/controllers/cms_content_controller.rb
|
92
93
|
- app/models/cms_block.rb
|
93
94
|
- app/models/cms_layout.rb
|
94
95
|
- app/models/cms_page.rb
|
96
|
+
- app/models/cms_site.rb
|
95
97
|
- app/models/cms_snippet.rb
|
96
98
|
- app/models/cms_upload.rb
|
97
99
|
- app/views/cms_admin/layouts/_form.html.erb
|
@@ -106,6 +108,10 @@ files:
|
|
106
108
|
- app/views/cms_admin/pages/form_blocks.js.erb
|
107
109
|
- app/views/cms_admin/pages/index.html.erb
|
108
110
|
- app/views/cms_admin/pages/new.html.erb
|
111
|
+
- app/views/cms_admin/sites/_form.html.erb
|
112
|
+
- app/views/cms_admin/sites/edit.html.erb
|
113
|
+
- app/views/cms_admin/sites/index.html.erb
|
114
|
+
- app/views/cms_admin/sites/new.html.erb
|
109
115
|
- app/views/cms_admin/snippets/_form.html.erb
|
110
116
|
- app/views/cms_admin/snippets/edit.html.erb
|
111
117
|
- app/views/cms_admin/snippets/index.html.erb
|
@@ -124,6 +130,7 @@ files:
|
|
124
130
|
- config/environments/development.rb
|
125
131
|
- config/environments/production.rb
|
126
132
|
- config/environments/test.rb
|
133
|
+
- config/initializers/comfortable_mexican_sofa.rb
|
127
134
|
- config/initializers/mime_types.rb
|
128
135
|
- config/initializers/paperclip.rb
|
129
136
|
- config/locales/en.yml
|
@@ -132,10 +139,7 @@ files:
|
|
132
139
|
- db/seeds.rb
|
133
140
|
- doc/README_FOR_APP
|
134
141
|
- lib/comfortable_mexican_sofa.rb
|
135
|
-
- lib/comfortable_mexican_sofa/
|
136
|
-
- lib/comfortable_mexican_sofa/cms_engine.rb
|
137
|
-
- lib/comfortable_mexican_sofa/cms_form_builder.rb
|
138
|
-
- lib/comfortable_mexican_sofa/cms_rails_extensions.rb
|
142
|
+
- lib/comfortable_mexican_sofa/acts_as_tree.rb
|
139
143
|
- lib/comfortable_mexican_sofa/cms_tag.rb
|
140
144
|
- lib/comfortable_mexican_sofa/cms_tag/field_datetime.rb
|
141
145
|
- lib/comfortable_mexican_sofa/cms_tag/field_integer.rb
|
@@ -147,6 +151,13 @@ files:
|
|
147
151
|
- lib/comfortable_mexican_sofa/cms_tag/page_text.rb
|
148
152
|
- lib/comfortable_mexican_sofa/cms_tag/partial.rb
|
149
153
|
- lib/comfortable_mexican_sofa/cms_tag/snippet.rb
|
154
|
+
- lib/comfortable_mexican_sofa/configuration.rb
|
155
|
+
- lib/comfortable_mexican_sofa/controller_methods.rb
|
156
|
+
- lib/comfortable_mexican_sofa/engine.rb
|
157
|
+
- lib/comfortable_mexican_sofa/form_builder.rb
|
158
|
+
- lib/comfortable_mexican_sofa/http_auth.rb
|
159
|
+
- lib/comfortable_mexican_sofa/rails_extensions.rb
|
160
|
+
- lib/comfortable_mexican_sofa/view_methods.rb
|
150
161
|
- lib/generators/README
|
151
162
|
- lib/generators/cms_generator.rb
|
152
163
|
- public/404.html
|
@@ -173,20 +184,26 @@ files:
|
|
173
184
|
- test/fixtures/cms_blocks.yml
|
174
185
|
- test/fixtures/cms_layouts.yml
|
175
186
|
- test/fixtures/cms_pages.yml
|
187
|
+
- test/fixtures/cms_sites.yml
|
176
188
|
- test/fixtures/cms_snippets.yml
|
177
189
|
- test/fixtures/cms_uploads.yml
|
178
190
|
- test/fixtures/files/invalid_file.gif
|
179
191
|
- test/fixtures/files/valid_image.jpg
|
180
|
-
- test/functional/cms_admin/base_controller_test.rb
|
181
192
|
- test/functional/cms_admin/layouts_controller_test.rb
|
182
193
|
- test/functional/cms_admin/pages_controller_test.rb
|
194
|
+
- test/functional/cms_admin/sites_controller_test.rb
|
183
195
|
- test/functional/cms_admin/snippets_controller_test.rb
|
184
196
|
- test/functional/cms_admin/uploads_controller_test.rb
|
185
197
|
- test/functional/cms_content_controller_test.rb
|
198
|
+
- test/integration/authentication_test.rb
|
199
|
+
- test/integration/render_cms_test.rb
|
200
|
+
- test/integration/sites_test.rb
|
186
201
|
- test/test_helper.rb
|
187
202
|
- test/unit/cms_block_test.rb
|
203
|
+
- test/unit/cms_configuration_test.rb
|
188
204
|
- test/unit/cms_layout_test.rb
|
189
205
|
- test/unit/cms_page_test.rb
|
206
|
+
- test/unit/cms_site_test.rb
|
190
207
|
- test/unit/cms_snippet_test.rb
|
191
208
|
- test/unit/cms_tag_test.rb
|
192
209
|
- test/unit/cms_tags/field_datetime_test.rb
|
@@ -236,16 +253,21 @@ signing_key:
|
|
236
253
|
specification_version: 3
|
237
254
|
summary: ComfortableMexicanSofa is a Rails Engine CMS gem
|
238
255
|
test_files:
|
239
|
-
- test/functional/cms_admin/base_controller_test.rb
|
240
256
|
- test/functional/cms_admin/layouts_controller_test.rb
|
241
257
|
- test/functional/cms_admin/pages_controller_test.rb
|
258
|
+
- test/functional/cms_admin/sites_controller_test.rb
|
242
259
|
- test/functional/cms_admin/snippets_controller_test.rb
|
243
260
|
- test/functional/cms_admin/uploads_controller_test.rb
|
244
261
|
- test/functional/cms_content_controller_test.rb
|
262
|
+
- test/integration/authentication_test.rb
|
263
|
+
- test/integration/render_cms_test.rb
|
264
|
+
- test/integration/sites_test.rb
|
245
265
|
- test/test_helper.rb
|
246
266
|
- test/unit/cms_block_test.rb
|
267
|
+
- test/unit/cms_configuration_test.rb
|
247
268
|
- test/unit/cms_layout_test.rb
|
248
269
|
- test/unit/cms_page_test.rb
|
270
|
+
- test/unit/cms_site_test.rb
|
249
271
|
- test/unit/cms_snippet_test.rb
|
250
272
|
- test/unit/cms_tag_test.rb
|
251
273
|
- test/unit/cms_tags/field_datetime_test.rb
|
@@ -1,101 +0,0 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module Acts
|
3
|
-
module Tree
|
4
|
-
|
5
|
-
def self.included(base)
|
6
|
-
base.extend(ClassMethods)
|
7
|
-
end
|
8
|
-
|
9
|
-
module ClassMethods
|
10
|
-
def acts_as_tree(options = {})
|
11
|
-
configuration = {
|
12
|
-
:foreign_key => 'parent_id',
|
13
|
-
:order => nil,
|
14
|
-
:counter_cache => nil,
|
15
|
-
:dependent => :destroy,
|
16
|
-
:touch => false }
|
17
|
-
configuration.update(options) if options.is_a?(Hash)
|
18
|
-
|
19
|
-
belongs_to :parent,
|
20
|
-
:class_name => name,
|
21
|
-
:foreign_key => configuration[:foreign_key],
|
22
|
-
:counter_cache => configuration[:counter_cache],
|
23
|
-
:touch => configuration[:touch]
|
24
|
-
|
25
|
-
has_many :children,
|
26
|
-
:class_name => name,
|
27
|
-
:foreign_key => configuration[:foreign_key],
|
28
|
-
:order => configuration[:order],
|
29
|
-
:dependent => configuration[:dependent]
|
30
|
-
|
31
|
-
class_eval <<-EOV
|
32
|
-
include ActiveRecord::Acts::Tree::InstanceMethods
|
33
|
-
|
34
|
-
scope :roots,
|
35
|
-
:conditions => "#{configuration[:foreign_key]} IS NULL",
|
36
|
-
:order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}
|
37
|
-
|
38
|
-
def self.root
|
39
|
-
roots.first
|
40
|
-
end
|
41
|
-
|
42
|
-
validates_each "#{configuration[:foreign_key]}" do |record, attr, value|
|
43
|
-
if value
|
44
|
-
if record.id == value
|
45
|
-
record.errors.add attr, "cannot be it's own id"
|
46
|
-
elsif record.descendants.map {|c| c.id}.include?(value)
|
47
|
-
record.errors.add attr, "cannot be a descendant's id"
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
EOV
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
module InstanceMethods
|
57
|
-
# Returns list of ancestors, starting from parent until root.
|
58
|
-
#
|
59
|
-
# subchild1.ancestors # => [child1, root]
|
60
|
-
def ancestors
|
61
|
-
node, nodes = self, []
|
62
|
-
nodes << node = node.parent while node.parent
|
63
|
-
nodes
|
64
|
-
end
|
65
|
-
|
66
|
-
# Returns all children and children of children
|
67
|
-
def descendants
|
68
|
-
nodes = []
|
69
|
-
self.children.each do |c|
|
70
|
-
nodes << c
|
71
|
-
nodes << c.descendants
|
72
|
-
end
|
73
|
-
nodes.flatten
|
74
|
-
end
|
75
|
-
|
76
|
-
# Returns the root node of the tree.
|
77
|
-
def root
|
78
|
-
node = self
|
79
|
-
node = node.parent while node.parent
|
80
|
-
node
|
81
|
-
end
|
82
|
-
|
83
|
-
# Returns all siblings of the current node.
|
84
|
-
#
|
85
|
-
# subchild1.siblings # => [subchild2]
|
86
|
-
def siblings
|
87
|
-
self_and_siblings - [self]
|
88
|
-
end
|
89
|
-
|
90
|
-
# Returns all siblings and a reference to the current node.
|
91
|
-
#
|
92
|
-
# subchild1.self_and_siblings # => [subchild1, subchild2]
|
93
|
-
def self_and_siblings
|
94
|
-
parent ? parent.children : self.class.roots
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
ActiveRecord::Base.send :include, ActiveRecord::Acts::Tree
|
@@ -1,32 +0,0 @@
|
|
1
|
-
class String
|
2
|
-
# Converts string to something suitable to be used as an element id
|
3
|
-
def idify
|
4
|
-
self.strip.gsub(/\W/, '_').gsub(/\s|^_*|_*$/, '').squeeze('_')
|
5
|
-
end
|
6
|
-
|
7
|
-
# Capitalize all words in the string
|
8
|
-
def capitalize_all(delimiter = ' ')
|
9
|
-
self.split(delimiter).collect{|w| w.capitalize }.join(' ')
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
module CmsViewHelpers
|
14
|
-
|
15
|
-
# Wrapper around CmsFormBuilder
|
16
|
-
def cms_form_for(record_or_name_or_array, *args, &proc)
|
17
|
-
options = args.extract_options!
|
18
|
-
form_for(record_or_name_or_array, *(args << options.merge(:builder => CmsFormBuilder)), &proc)
|
19
|
-
end
|
20
|
-
|
21
|
-
# Wrapper for <span>
|
22
|
-
def span_tag(*args)
|
23
|
-
content_tag(:span, *args)
|
24
|
-
end
|
25
|
-
|
26
|
-
# Rails 3.0 doesn't have this helper defined
|
27
|
-
def datetime_field_tag(name, value = nil, options = {})
|
28
|
-
text_field_tag(name, value, options.stringify_keys.update('type' => 'datetime'))
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
ActionView::Base.send :include, CmsViewHelpers
|