comfortable_mexican_sofa 1.0.14 → 1.0.15

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.14
1
+ 1.0.15
@@ -15,7 +15,8 @@ class CmsPage < ActiveRecord::Base
15
15
  accepts_nested_attributes_for :cms_blocks
16
16
 
17
17
  # -- Callbacks ------------------------------------------------------------
18
- before_validation :assign_full_path
18
+ before_validation :assign_parent,
19
+ :assign_full_path
19
20
  after_save :sync_child_pages
20
21
 
21
22
  # -- Validations ----------------------------------------------------------
@@ -78,6 +79,19 @@ class CmsPage < ActiveRecord::Base
78
79
  end
79
80
 
80
81
  # -- Instance Methods -----------------------------------------------------
82
+ # Transforms existing cms_block information into a hash that can be used
83
+ # during form processing. That's the only way to modify cms_blocks.
84
+ def cms_blocks_attributes
85
+ self.cms_blocks.inject([]) do |arr, block|
86
+ block_attr = {}
87
+ block_attr[:type] = block.class.name
88
+ block_attr[:label] = block.label
89
+ block_attr[:content] = block.content
90
+ block_attr[:id] = block.id
91
+ arr << block_attr
92
+ end
93
+ end
94
+
81
95
  # Processing content will return rendered content and will populate
82
96
  # self.cms_tags with instances of CmsTag
83
97
  def content
@@ -93,6 +107,10 @@ class CmsPage < ActiveRecord::Base
93
107
 
94
108
  protected
95
109
 
110
+ def assign_parent
111
+ self.parent ||= CmsPage.root unless self == CmsPage.root || CmsPage.count == 0
112
+ end
113
+
96
114
  def assign_full_path
97
115
  self.full_path = self.parent ? "#{self.parent.full_path}/#{self.slug}".squeeze('/') : '/'
98
116
  end
@@ -19,10 +19,7 @@ class CmsSnippet < ActiveRecord::Base
19
19
  end
20
20
 
21
21
  def self.initialize_or_find(cms_page, slug)
22
- if ComfortableMexicanSofa.configuration.seed_data_path
23
- s = CmsTag::Snippet.load_from_file(cms_page.cms_site, slug)
24
- end
25
- s || find_by_slug(slug) || new(:slug => slug)
22
+ load_for_slug(cms_page.cms_site, slug) || new(:slug => slug)
26
23
  end
27
24
 
28
25
  # Attempting to initialize snippet object from yaml file that is found in config.seed_data_path
@@ -34,4 +31,24 @@ class CmsSnippet < ActiveRecord::Base
34
31
  new(attributes)
35
32
  end
36
33
 
34
+ # Wrapper around load_from_file and find_by_slug
35
+ # returns layout object if loaded / found
36
+ def self.load_for_slug!(site, slug)
37
+ if ComfortableMexicanSofa.configuration.seed_data_path
38
+ load_from_file(site, slug)
39
+ else
40
+ # FIX: This a bit odd... Snippet is used as a tag, so sometimes there's no site scope
41
+ # being passed. So we're enforcing this only if it's found. Need to review.
42
+ conditions = site ? {:conditions => {:cms_site_id => site.id}} : {}
43
+ find_by_slug(slug, conditions)
44
+ end || raise(ActiveRecord::RecordNotFound, "CmsSnippet with slug: #{slug} cannot be found")
45
+ end
46
+
47
+ # Non-blowing-up version of the method above
48
+ def self.load_for_slug(site, slug)
49
+ load_for_slug!(site, slug)
50
+ rescue ActiveRecord::RecordNotFound
51
+ nil
52
+ end
53
+
37
54
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{comfortable_mexican_sofa}
8
- s.version = "1.0.14"
8
+ s.version = "1.0.15"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Oleg Khabarov", "The Working Group Inc"]
12
- s.date = %q{2010-11-05}
12
+ s.date = %q{2010-11-08}
13
13
  s.description = %q{}
14
14
  s.email = %q{oleg@theworkinggroup.ca}
15
15
  s.extra_rdoc_files = [
@@ -102,6 +102,7 @@ Gem::Specification.new do |s|
102
102
  "lib/comfortable_mexican_sofa/view_methods.rb",
103
103
  "lib/generators/README",
104
104
  "lib/generators/cms_generator.rb",
105
+ "lib/tasks/comfortable_mexican_sofa.rake",
105
106
  "public/404.html",
106
107
  "public/422.html",
107
108
  "public/500.html",
@@ -144,6 +145,7 @@ Gem::Specification.new do |s|
144
145
  "test/functional/cms_admin/uploads_controller_test.rb",
145
146
  "test/functional/cms_content_controller_test.rb",
146
147
  "test/integration/authentication_test.rb",
148
+ "test/integration/rake_tasks_test.rb",
147
149
  "test/integration/render_cms_seed_test.rb",
148
150
  "test/integration/render_cms_test.rb",
149
151
  "test/integration/sites_test.rb",
@@ -182,6 +184,7 @@ Gem::Specification.new do |s|
182
184
  "test/functional/cms_admin/uploads_controller_test.rb",
183
185
  "test/functional/cms_content_controller_test.rb",
184
186
  "test/integration/authentication_test.rb",
187
+ "test/integration/rake_tasks_test.rb",
185
188
  "test/integration/render_cms_seed_test.rb",
186
189
  "test/integration/render_cms_test.rb",
187
190
  "test/integration/sites_test.rb",
@@ -0,0 +1,157 @@
1
+ namespace :comfortable_mexican_sofa do
2
+
3
+ namespace :import do
4
+
5
+ task :check_for_requirements => :environment do |task, args|
6
+ if !(@seed_path = ComfortableMexicanSofa.config.seed_data_path)
7
+ abort('ComfortableMexicanSofa.config.seed_data_path is not set. Where are those yaml files?')
8
+ end
9
+ if !(@site = CmsSite.find_by_hostname(args[:hostname]))
10
+ abort("Can't find site with HOSTNAME '#{args[:site]}'")
11
+ end
12
+ puts "Starting import into #{@site.label} (#{@site.hostname})..."
13
+ end
14
+
15
+ desc 'Import layouts into database'
16
+ task :layouts => [:environment, :check_for_requirements] do |task, args|
17
+ puts 'Importing Layouts...'
18
+ layouts = Dir.glob(File.expand_path("#{@site.hostname}/layouts/**/*.yml", @seed_path)).collect do |layout_file_path|
19
+ attributes = YAML.load_file(layout_file_path).symbolize_keys!
20
+ @site.cms_layouts.load_for_slug!(@site, attributes[:slug])
21
+ end
22
+
23
+ CmsPage.connection.transaction do
24
+ # Fixtures are not ordered in any particular way. Saving order matters,
25
+ # so we cycle them until there nothing left to save
26
+ while layouts.present?
27
+ layout = layouts.shift
28
+ if !layout.parent || layout.parent && parent = @site.cms_layouts.find_by_slug(layout.parent.slug)
29
+ layout.parent = (parent rescue nil)
30
+ should_write = true
31
+ existing_layout = nil
32
+
33
+ if existing_layout = @site.cms_layouts.find_by_slug(layout.slug)
34
+ print "Found layout in database with slug: #{layout.slug}. Overwrite? (yN): "
35
+ should_write = ($stdin.gets.to_s.strip.downcase == 'y')
36
+ end
37
+ if should_write
38
+ if existing_layout
39
+ existing_layout.attributes = layout.attributes.slice('label', 'content', 'css', 'js')
40
+ layout = existing_layout
41
+ end
42
+ layout.save!
43
+ puts "Saved layout: #{layout.label} (#{layout.slug})"
44
+ else
45
+ puts "Skipping layout: #{layout.label} (#{layout.slug})"
46
+ end
47
+ else
48
+ layouts.push layout
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ desc 'Import pages into database'
55
+ task :pages => [:environment, :check_for_requirements] do |task, args|
56
+ puts 'Importing Pages...'
57
+ pages = Dir.glob(File.expand_path("#{@site.hostname}/pages/**/*.yml", @seed_path)).collect do |page_file_path|
58
+ attributes = YAML.load_file(page_file_path).symbolize_keys!
59
+ @site.cms_pages.load_for_full_path!(@site, attributes[:full_path])
60
+ end
61
+ CmsPage.connection.transaction do
62
+ # Fixtures are not ordered in any particular way. Saving order matters,
63
+ # so we cycle them until there nothing left to save
64
+ while pages.present?
65
+ page = pages.shift
66
+ if !page.parent || page.parent && parent = @site.cms_pages.find_by_full_path(page.parent.full_path)
67
+ page.parent = (parent rescue nil)
68
+ page.cms_layout = @site.cms_layouts.find_by_slug(page.cms_layout.slug)
69
+ should_write = true
70
+ existing_page = nil
71
+
72
+ if existing_page = @site.cms_pages.find_by_full_path(page.full_path)
73
+ print "Found page in database with full_path: #{page.full_path}. Overwrite? (yN): "
74
+ should_write = ($stdin.gets.to_s.strip.downcase == 'y')
75
+ end
76
+
77
+ if should_write
78
+ if existing_page
79
+ # merging cms_blocks_attributes with the existing page
80
+ attrs = page.cms_blocks_attributes.collect do |block_attrs|
81
+ existing_block = existing_page.cms_blocks_attributes.find{|b| b[:label] == block_attrs[:label]}
82
+ block_attrs[:id] = existing_block[:id] if existing_block
83
+ block_attrs.stringify_keys
84
+ end
85
+
86
+ existing_page.attributes = page.attributes.slice('label')
87
+ existing_page.cms_blocks_attributes = attrs
88
+ page = existing_page
89
+ end
90
+ page.save!
91
+ puts "Saved page: #{page.label} (#{page.full_path})"
92
+ else
93
+ puts "Skipping page: #{page.label} (#{page.full_path})"
94
+ end
95
+ else
96
+ pages.push page
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ desc 'Import snippets into database'
103
+ task :snippets => [:environment, :check_for_requirements] do |task, args|
104
+ puts 'Importing Snippets...'
105
+ snippets = Dir.glob(File.expand_path("#{@site.hostname}/snippets/**/*.yml", @seed_path)).collect do |snippet_file_path|
106
+ attributes = YAML.load_file(snippet_file_path).symbolize_keys!
107
+ @site.cms_snippets.load_for_slug!(@site, attributes[:slug])
108
+ end
109
+ CmsSnippet.connection.transaction do
110
+ snippets.each do |snippet|
111
+ should_write = true
112
+ existing_snippet = nil
113
+ if existing_snippet = @site.cms_snippets.find_by_slug(snippet.slug)
114
+ print "Found snippet in database with slug: #{snippet.slug}. Overwrite? (yN): "
115
+ should_write = ($stdin.gets.to_s.strip.downcase == 'y')
116
+ end
117
+ if should_write
118
+ if existing_snippet
119
+ existing_snippet.attributes = snippet.attributes.slice('label', 'content')
120
+ snippet = existing_snippet
121
+ end
122
+ snippet.save!
123
+ puts "Saved snippet: #{snippet.label} (#{snippet.slug})"
124
+ else
125
+ puts "Skipping snippet: #{snippet.label} (#{snippet.slug})"
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ desc 'Import layouts, pages and snippets all in one go'
132
+ task :all => [:layouts, :pages, :snippets]
133
+
134
+ end
135
+
136
+ namespace :export do
137
+
138
+ desc 'Export layouts to yaml files'
139
+ task :layouts => [:environment, :check_for_requirements] do |task, args|
140
+
141
+ end
142
+
143
+ desc 'Export pages to yaml files'
144
+ task :pages => [:environment, :check_for_requirements] do |task, args|
145
+
146
+ end
147
+
148
+ desc 'Export snippets to yaml files'
149
+ task :snippets => [:environment, :check_for_requirements] do |task, args|
150
+
151
+ end
152
+
153
+ desc 'Export layouts, pages and snippets all in one go'
154
+ task :all => [:layouts, :pages, :snippets]
155
+
156
+ end
157
+ end
@@ -1,6 +1,7 @@
1
1
  label: Child Page
2
2
  parent: /
3
3
  cms_layout: default
4
+ full_path: /child
4
5
  slug: child
5
6
  cms_blocks_attributes:
6
7
  -
@@ -1,6 +1,7 @@
1
1
  label: Child Page
2
2
  parent: /child
3
3
  cms_layout: nested
4
+ full_path: /child/subchild
4
5
  slug: subchild
5
6
  cms_blocks_attributes:
6
7
  -
@@ -1,7 +1,8 @@
1
1
  label: Default Page
2
2
  parent:
3
3
  cms_layout: default
4
- slug:
4
+ full_path: /
5
+ slug:
5
6
  cms_blocks_attributes:
6
7
  -
7
8
  label: content
@@ -0,0 +1,59 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ require 'rake'
4
+ require 'rake/rdoctask'
5
+ require 'rake/testtask'
6
+
7
+ Rake.application.rake_require '../lib/tasks/comfortable_mexican_sofa'
8
+
9
+ class RakeTasksTest < ActionDispatch::IntegrationTest
10
+
11
+ def test_layouts_import
12
+ CmsLayout.destroy_all
13
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
14
+
15
+ assert_difference 'CmsLayout.count', 2 do
16
+ capture_rake_output{
17
+ Rake.application['comfortable_mexican_sofa:import:check_for_requirements'].execute(:hostname => 'test.host')
18
+ Rake.application['comfortable_mexican_sofa:import:layouts'].execute(:hostname => 'test.host')
19
+ }
20
+ end
21
+ end
22
+
23
+ def test_pages_import
24
+ CmsPage.destroy_all
25
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
26
+
27
+ assert_difference ['CmsPage.count', 'CmsBlock.count'], 3 do
28
+ capture_rake_output{
29
+ Rake.application['comfortable_mexican_sofa:import:check_for_requirements'].execute(:hostname => 'test.host')
30
+ Rake.application['comfortable_mexican_sofa:import:pages'].execute(:hostname => 'test.host')
31
+ }
32
+ end
33
+ end
34
+
35
+ def test_snippets_import
36
+ CmsSnippet.destroy_all
37
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
38
+
39
+ assert_difference 'CmsSnippet.count', 1 do
40
+ capture_rake_output{
41
+ Rake.application['comfortable_mexican_sofa:import:check_for_requirements'].execute(:hostname => 'test.host')
42
+ Rake.application['comfortable_mexican_sofa:import:snippets'].execute(:hostname => 'test.host')
43
+ }
44
+ end
45
+ end
46
+
47
+ protected
48
+
49
+ def capture_rake_output
50
+ s = StringIO.new
51
+ oldstdout = $stdout
52
+ $stdout = s
53
+ yield
54
+ s.string
55
+ ensure
56
+ $stdout = oldstdout
57
+ end
58
+
59
+ end
@@ -15,6 +15,13 @@ class CmsPageTest < ActiveSupport::TestCase
15
15
  assert_has_errors_on page, [:cms_layout, :slug, :label]
16
16
  end
17
17
 
18
+ def test_validation_of_parent_presence
19
+ page = cms_sites(:default).cms_pages.new(new_params)
20
+ assert !page.parent
21
+ assert page.valid?, page.errors.full_messages
22
+ assert_equal cms_pages(:default), page.parent
23
+ end
24
+
18
25
  def test_validation_of_parent_relationship
19
26
  page = cms_pages(:default)
20
27
  assert !page.parent
@@ -174,6 +181,15 @@ class CmsPageTest < ActiveSupport::TestCase
174
181
  assert !CmsPage.load_for_full_path(cms_sites(:default), '/invalid_page')
175
182
  end
176
183
 
184
+ def test_cms_blocks_attributes_accessor
185
+ page = cms_pages(:default)
186
+ assert_equal page.cms_blocks.count, page.cms_blocks_attributes.size
187
+ assert_equal 'CmsTag::FieldText', page.cms_blocks_attributes.first[:type]
188
+ assert_equal 'default_field_text', page.cms_blocks_attributes.first[:label]
189
+ assert_equal 'default_field_text_content', page.cms_blocks_attributes.first[:content]
190
+ assert page.cms_blocks_attributes.first[:id]
191
+ end
192
+
177
193
  protected
178
194
 
179
195
  def new_params(options = {})
@@ -32,4 +32,29 @@ class CmsSnippetTest < ActiveSupport::TestCase
32
32
  assert_equal 'Content for Default Snippet', snippet.content
33
33
  end
34
34
 
35
+ def test_load_for_slug
36
+ assert snippet = CmsSnippet.load_for_slug!(cms_sites(:default), 'default')
37
+ assert !snippet.new_record?
38
+ db_content = snippet.content
39
+
40
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
41
+ assert snippet = CmsSnippet.load_for_slug!(cms_sites(:default), 'default')
42
+ assert snippet.new_record?
43
+ file_content = snippet.content
44
+ assert_not_equal db_content, file_content
45
+ end
46
+
47
+ def test_load_for_slug_exceptions
48
+ assert_exception_raised ActiveRecord::RecordNotFound, 'CmsSnippet with slug: not_found cannot be found' do
49
+ CmsSnippet.load_for_slug!(cms_sites(:default), 'not_found')
50
+ end
51
+ assert !CmsSnippet.load_for_slug(cms_sites(:default), 'not_found')
52
+
53
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
54
+ assert_exception_raised ActiveRecord::RecordNotFound, 'CmsSnippet with slug: not_found cannot be found' do
55
+ CmsSnippet.load_for_slug!(cms_sites(:default), 'not_found')
56
+ end
57
+ assert !CmsSnippet.load_for_slug(cms_sites(:default), 'not_found')
58
+ end
59
+
35
60
  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: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 14
10
- version: 1.0.14
9
+ - 15
10
+ version: 1.0.15
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-11-05 00:00:00 -04:00
19
+ date: 2010-11-08 00:00:00 -05:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -162,6 +162,7 @@ files:
162
162
  - lib/comfortable_mexican_sofa/view_methods.rb
163
163
  - lib/generators/README
164
164
  - lib/generators/cms_generator.rb
165
+ - lib/tasks/comfortable_mexican_sofa.rake
165
166
  - public/404.html
166
167
  - public/422.html
167
168
  - public/500.html
@@ -204,6 +205,7 @@ files:
204
205
  - test/functional/cms_admin/uploads_controller_test.rb
205
206
  - test/functional/cms_content_controller_test.rb
206
207
  - test/integration/authentication_test.rb
208
+ - test/integration/rake_tasks_test.rb
207
209
  - test/integration/render_cms_seed_test.rb
208
210
  - test/integration/render_cms_test.rb
209
211
  - test/integration/sites_test.rb
@@ -270,6 +272,7 @@ test_files:
270
272
  - test/functional/cms_admin/uploads_controller_test.rb
271
273
  - test/functional/cms_content_controller_test.rb
272
274
  - test/integration/authentication_test.rb
275
+ - test/integration/rake_tasks_test.rb
273
276
  - test/integration/render_cms_seed_test.rb
274
277
  - test/integration/render_cms_test.rb
275
278
  - test/integration/sites_test.rb