comfortable_mexican_sofa 1.2.3 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/VERSION +1 -1
  2. data/app/controllers/cms_admin/base_controller.rb +1 -1
  3. data/app/controllers/cms_admin/snippets_controller.rb +1 -0
  4. data/app/controllers/cms_content_controller.rb +1 -1
  5. data/app/models/cms/layout.rb +6 -11
  6. data/app/models/cms/page.rb +8 -3
  7. data/app/models/cms/site.rb +10 -2
  8. data/app/models/cms/snippet.rb +7 -2
  9. data/app/views/cms_admin/layouts/edit.html.erb +4 -0
  10. data/app/views/cms_admin/layouts/index.html.erb +4 -0
  11. data/app/views/cms_admin/pages/_form_blocks.html.erb +13 -3
  12. data/app/views/cms_admin/pages/edit.html.erb +4 -0
  13. data/app/views/cms_admin/pages/index.html.erb +4 -0
  14. data/app/views/cms_admin/sites/_mirrors.html.erb +20 -0
  15. data/app/views/cms_admin/snippets/edit.html.erb +4 -0
  16. data/app/views/cms_admin/snippets/index.html.erb +4 -0
  17. data/comfortable_mexican_sofa.gemspec +6 -2
  18. data/config/initializers/comfortable_mexican_sofa.rb +6 -1
  19. data/lib/comfortable_mexican_sofa.rb +1 -0
  20. data/lib/comfortable_mexican_sofa/configuration.rb +4 -0
  21. data/lib/comfortable_mexican_sofa/fixtures.rb +100 -18
  22. data/lib/comfortable_mexican_sofa/is_mirrored.rb +83 -0
  23. data/lib/tasks/comfortable_mexican_sofa.rake +15 -9
  24. data/public/javascripts/comfortable_mexican_sofa/cms.js +7 -0
  25. data/public/stylesheets/comfortable_mexican_sofa/content.css +6 -1
  26. data/public/stylesheets/comfortable_mexican_sofa/form.css +18 -0
  27. data/public/stylesheets/comfortable_mexican_sofa/structure.css +2 -7
  28. data/public/stylesheets/comfortable_mexican_sofa/typography.css +3 -0
  29. data/test/functional/cms_admin/layouts_controller_test.rb +2 -2
  30. data/test/functional/cms_admin/sites_controller_test.rb +2 -2
  31. data/test/functional/cms_admin/snippets_controller_test.rb +2 -2
  32. data/test/integration/mirrors_test.rb +75 -0
  33. data/test/test_helper.rb +1 -0
  34. data/test/unit/configuration_test.rb +1 -0
  35. data/test/unit/fixtures_test.rb +133 -43
  36. data/test/unit/mirrors_test.rb +153 -0
  37. data/test/unit/models/layout_test.rb +4 -20
  38. data/test/unit/models/page_test.rb +14 -4
  39. data/test/unit/models/site_test.rb +8 -2
  40. data/test/unit/models/snippet_test.rb +8 -0
  41. metadata +7 -3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.3
1
+ 1.2.4
@@ -35,7 +35,7 @@ protected
35
35
 
36
36
  def load_fixtures
37
37
  return unless ComfortableMexicanSofa.config.enable_fixtures
38
- ComfortableMexicanSofa::Fixtures.sync(@cms_site)
38
+ ComfortableMexicanSofa::Fixtures.import_all(@cms_site.hostname)
39
39
  flash.now[:error] = 'CMS Fixtures are enabled. All changes done here will be discarded.'
40
40
  end
41
41
  end
@@ -17,6 +17,7 @@ class CmsAdmin::SnippetsController < CmsAdmin::BaseController
17
17
  end
18
18
 
19
19
  def create
20
+ # raise @cms_snippet.to_yaml
20
21
  @cms_snippet.save!
21
22
  flash[:notice] = 'Snippet created'
22
23
  redirect_to :action => :edit, :id => @cms_snippet
@@ -28,7 +28,7 @@ protected
28
28
 
29
29
  def load_fixtures
30
30
  return unless ComfortableMexicanSofa.config.enable_fixtures
31
- ComfortableMexicanSofa::Fixtures.sync(@cms_site)
31
+ ComfortableMexicanSofa::Fixtures.import_all(@cms_site.hostname)
32
32
  end
33
33
 
34
34
  def load_cms_site
@@ -3,7 +3,7 @@ class Cms::Layout < ActiveRecord::Base
3
3
  set_table_name :cms_layouts
4
4
 
5
5
  acts_as_tree
6
-
6
+ is_mirrored
7
7
  has_revisions_for :content, :css, :js
8
8
 
9
9
  # -- Relationships --------------------------------------------------------
@@ -11,6 +11,7 @@ class Cms::Layout < ActiveRecord::Base
11
11
  has_many :pages, :dependent => :nullify
12
12
 
13
13
  # -- Callbacks ------------------------------------------------------------
14
+ before_validation :assign_label
14
15
  after_save :clear_cache, :clear_cached_page_content
15
16
  after_destroy :clear_cache, :clear_cached_page_content
16
17
 
@@ -23,9 +24,6 @@ class Cms::Layout < ActiveRecord::Base
23
24
  :presence => true,
24
25
  :uniqueness => { :scope => :site_id },
25
26
  :format => { :with => /^\w[a-z0-9_-]*$/i }
26
- validates :content,
27
- :presence => true
28
- validate :check_content_tag_presence
29
27
 
30
28
  # -- Class Methods --------------------------------------------------------
31
29
  # Tree-like structure for layouts
@@ -58,7 +56,7 @@ class Cms::Layout < ActiveRecord::Base
58
56
  if parent
59
57
  regex = /\{\{\s*cms:page:content:?(?:(?::text)|(?::rich_text))?\s*\}\}/
60
58
  if parent.merged_content.match(regex)
61
- parent.merged_content.gsub(regex, content)
59
+ parent.merged_content.gsub(regex, content.to_s)
62
60
  else
63
61
  content
64
62
  end
@@ -69,11 +67,8 @@ class Cms::Layout < ActiveRecord::Base
69
67
 
70
68
  protected
71
69
 
72
- def check_content_tag_presence
73
- ComfortableMexicanSofa::Tag.process_content((test_page = site.pages.new), content)
74
- if test_page.tags.select{|t| t.is_cms_block?}.blank?
75
- self.errors.add(:content, 'No cms page tags defined')
76
- end
70
+ def assign_label
71
+ self.label = self.label.blank?? self.slug.try(:titleize) : self.label
77
72
  end
78
73
 
79
74
  # After saving need to make sure that cached pages for css and js for this
@@ -86,7 +81,7 @@ protected
86
81
  # Forcing page content reload
87
82
  def clear_cached_page_content
88
83
  self.pages.each{ |page| page.save! }
89
- self.children.each{ |child_layout| child_layout.save! }
84
+ self.children.each{ |child_layout| child_layout.clear_cached_page_content }
90
85
  end
91
86
 
92
87
  end
@@ -3,7 +3,7 @@ class Cms::Page < ActiveRecord::Base
3
3
  set_table_name :cms_pages
4
4
 
5
5
  acts_as_tree :counter_cache => :children_count
6
-
6
+ is_mirrored
7
7
  has_revisions_for :blocks_attributes
8
8
 
9
9
  attr_accessor :tags,
@@ -19,7 +19,8 @@ class Cms::Page < ActiveRecord::Base
19
19
  :autosave => true
20
20
 
21
21
  # -- Callbacks ------------------------------------------------------------
22
- before_validation :assign_parent,
22
+ before_validation :assigns_label,
23
+ :assign_parent,
23
24
  :assign_full_path
24
25
  before_validation :assign_position,
25
26
  :on => :create
@@ -34,7 +35,7 @@ class Cms::Page < ActiveRecord::Base
34
35
  validates :slug,
35
36
  :presence => true,
36
37
  :format => /^\w[a-z0-9_-]*$/i,
37
- :unless => lambda{ |p| p == Cms::Page.root || p.site && p.site.pages.count == 0 }
38
+ :unless => lambda{ |p| p.site && (p.site.pages.count == 0 || p.site.pages.root == self) }
38
39
  validates :layout,
39
40
  :presence => true
40
41
  validates :full_path,
@@ -119,6 +120,10 @@ class Cms::Page < ActiveRecord::Base
119
120
 
120
121
  protected
121
122
 
123
+ def assigns_label
124
+ self.label = self.label.blank?? self.slug.try(:titleize) : self.label
125
+ end
126
+
122
127
  def assign_parent
123
128
  return unless site
124
129
  self.parent ||= site.pages.root unless self == site.pages.root || site.pages.count == 0
@@ -8,10 +8,12 @@ class Cms::Site < ActiveRecord::Base
8
8
  has_many :snippets, :dependent => :destroy
9
9
  has_many :uploads, :dependent => :destroy
10
10
 
11
+ # -- Callbacks ------------------------------------------------------------
12
+ before_validation :assign_label
13
+
11
14
  # -- Validations ----------------------------------------------------------
12
15
  validates :label,
13
- :presence => true,
14
- :uniqueness => true
16
+ :presence => true
15
17
  validates :hostname,
16
18
  :presence => true,
17
19
  :uniqueness => true,
@@ -22,4 +24,10 @@ class Cms::Site < ActiveRecord::Base
22
24
  Cms::Site.all.collect{|s| ["#{s.label} (#{s.hostname})", s.id]}
23
25
  end
24
26
 
27
+ protected
28
+
29
+ def assign_label
30
+ self.label = self.label.blank?? self.hostname : self.label
31
+ end
32
+
25
33
  end
@@ -1,13 +1,14 @@
1
1
  class Cms::Snippet < ActiveRecord::Base
2
2
 
3
3
  set_table_name :cms_snippets
4
-
4
+ is_mirrored
5
5
  has_revisions_for :content
6
6
 
7
7
  # -- Relationships --------------------------------------------------------
8
8
  belongs_to :site
9
9
 
10
10
  # -- Callbacks ------------------------------------------------------------
11
+ before_validation :assign_label
11
12
  after_save :clear_cached_page_content
12
13
  after_destroy :clear_cached_page_content
13
14
 
@@ -23,11 +24,15 @@ class Cms::Snippet < ActiveRecord::Base
23
24
 
24
25
  protected
25
26
 
27
+ def assign_label
28
+ self.label = self.label.blank?? self.slug.try(:titleize) : self.label
29
+ end
30
+
26
31
  # Note: This might be slow. We have no idea where the snippet is used, so
27
32
  # gotta reload every single page. Kinda sucks, but might be ok unless there
28
33
  # are hundreds of pages.
29
34
  def clear_cached_page_content
30
- site.pages.all.each{ |page| page.save! }
35
+ site.pages.all.each{ |page| page.save }
31
36
  end
32
37
 
33
38
  end
@@ -1,6 +1,10 @@
1
1
  <%= link_to span_tag(pluralize(@cms_layout.revisions.count, 'revision')), cms_admin_layout_revisions_path(@cms_layout), :class => 'big button' %>
2
2
  <h1> Editing Layout </h1>
3
3
 
4
+ <% content_for :right_column do %>
5
+ <%= render :partial => 'cms_admin/sites/mirrors', :object => @cms_layout %>
6
+ <% end %>
7
+
4
8
  <%= cms_form_for @cms_layout, :url => {:action => :update} do |form| %>
5
9
  <%= render :partial => 'form', :object => form %>
6
10
  <% end %>
@@ -1,6 +1,10 @@
1
1
  <%= link_to span_tag('Create New Layout'), new_cms_admin_layout_path, :class => 'big button' %>
2
2
  <h1>Layouts</h1>
3
3
 
4
+ <% content_for :right_column do %>
5
+ <%= render :partial => 'cms_admin/sites/mirrors' %>
6
+ <% end %>
7
+
4
8
  <ul class='list'>
5
9
  <%= render :partial => 'index_branch', :collection => @cms_layouts %>
6
10
  </ul>
@@ -1,7 +1,17 @@
1
+ <% block_tags = @cms_page.tags(true).select{ |t| t.is_cms_block? } %>
2
+
1
3
  <div id='form_blocks'>
2
- <%= fields_for :blocks, nil, :builder => ComfortableMexicanSofa::FormBuilder do |cms_blocks| %>
3
- <% @cms_page.tags(true).select{|t| t.is_cms_block?}.each do |tag| %>
4
- <%= cms_blocks.send(tag.class.to_s.demodulize.underscore, tag)%>
4
+ <% if block_tags.empty? %>
5
+ <div class='no_tags'>
6
+ <%= link_to @cms_page.layout.label, edit_cms_admin_layout_path(@cms_page.layout) %>
7
+ Layout has no content tags defined. <br/>
8
+ Edit the content to include a page or field tag, for example: <code>{{cms:page:content}}</code>
9
+ </div>
10
+ <% else %>
11
+ <%= fields_for :blocks, nil, :builder => ComfortableMexicanSofa::FormBuilder do |cms_blocks| %>
12
+ <% block_tags.each do |tag| %>
13
+ <%= cms_blocks.send(tag.class.to_s.demodulize.underscore, tag)%>
14
+ <% end %>
5
15
  <% end %>
6
16
  <% end %>
7
17
  </div>
@@ -1,6 +1,10 @@
1
1
  <%= link_to span_tag(pluralize(@cms_page.revisions.count, 'revision')), cms_admin_page_revisions_path(@cms_page), :class => 'big button' %>
2
2
  <h1> Editing Page </h1>
3
3
 
4
+ <% content_for :right_column do %>
5
+ <%= render :partial => 'cms_admin/sites/mirrors', :object => @cms_page %>
6
+ <% end %>
7
+
4
8
  <%= cms_form_for @cms_page, :url => {:action => :update} do |form| %>
5
9
  <%= render :partial => 'form', :object => form %>
6
10
  <% end %>
@@ -1,6 +1,10 @@
1
1
  <%= link_to span_tag('Create New Page'), new_cms_admin_page_path, :class => 'big button' %>
2
2
  <h1>Pages</h1>
3
3
 
4
+ <% content_for :right_column do %>
5
+ <%= render :partial => 'cms_admin/sites/mirrors' %>
6
+ <% end %>
7
+
4
8
  <ul class='list sortable'>
5
9
  <%= render :partial => 'index_branch', :collection => @cms_pages %>
6
10
  </ul>
@@ -0,0 +1,20 @@
1
+ <%
2
+ return unless ComfortableMexicanSofa.config.enable_mirror_sites
3
+ object ||= mirrors
4
+
5
+ options = case object
6
+ when Cms::Layout
7
+ object.mirrors.collect{|m| [m.site.label, edit_cms_admin_layout_url(m, :host => m.site.hostname)]}
8
+ when Cms::Page
9
+ object.mirrors.collect{|m| [m.site.label, edit_cms_admin_page_url(m, :host => m.site.hostname)]}
10
+ when Cms::Snippet
11
+ object.mirrors.collect{|m| [m.site.label, edit_cms_admin_snippet_url(m, :host => m.site.hostname)]}
12
+ else
13
+ (Cms::Site.all - [@cms_site]).collect{|s| [s.label, url_for(:host => s.hostname)]}
14
+ end
15
+ options = [[@cms_site.label, request.fullpath]] + options
16
+ %>
17
+
18
+ <div id='mirrors' class='box'>
19
+ <%= select_tag :mirror, options_for_select(options) %>
20
+ </div>
@@ -1,6 +1,10 @@
1
1
  <%= link_to span_tag(pluralize(@cms_snippet.revisions.count, 'revision')), cms_admin_snippet_revisions_path(@cms_snippet), :class => 'big button' %>
2
2
  <h1> Editing Snippet </h1>
3
3
 
4
+ <% content_for :right_column do %>
5
+ <%= render :partial => 'cms_admin/sites/mirrors', :object => @cms_snippet %>
6
+ <% end %>
7
+
4
8
  <%= cms_form_for @cms_snippet, :url => {:action => :update} do |form| %>
5
9
  <%= render :partial => 'form', :object => form %>
6
10
  <% end %>
@@ -1,6 +1,10 @@
1
1
  <%= link_to span_tag('Create New Snippet'), new_cms_admin_snippet_path, :class => 'big button' %>
2
2
  <h1>Snippets</h1>
3
3
 
4
+ <% content_for :right_column do %>
5
+ <%= render :partial => 'cms_admin/sites/mirrors' %>
6
+ <% end %>
7
+
4
8
  <ul class='list'>
5
9
  <% @cms_snippets.each do |cms_snippet| %>
6
10
  <li id='cms_snippet_<%= cms_snippet.id %>'>
@@ -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.2.3"
8
+ s.version = "1.2.4"
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{2011-05-16}
12
+ s.date = %q{2011-05-19}
13
13
  s.description = %q{}
14
14
  s.email = %q{oleg@theworkinggroup.ca}
15
15
  s.extra_rdoc_files = [
@@ -54,6 +54,7 @@ Gem::Specification.new do |s|
54
54
  "app/views/cms_admin/pages/toggle_branch.js.erb",
55
55
  "app/views/cms_admin/revisions/show.html.erb",
56
56
  "app/views/cms_admin/sites/_form.html.erb",
57
+ "app/views/cms_admin/sites/_mirrors.html.erb",
57
58
  "app/views/cms_admin/sites/edit.html.erb",
58
59
  "app/views/cms_admin/sites/index.html.erb",
59
60
  "app/views/cms_admin/sites/new.html.erb",
@@ -113,6 +114,7 @@ Gem::Specification.new do |s|
113
114
  "lib/comfortable_mexican_sofa/form_builder.rb",
114
115
  "lib/comfortable_mexican_sofa/has_revisions.rb",
115
116
  "lib/comfortable_mexican_sofa/http_auth.rb",
117
+ "lib/comfortable_mexican_sofa/is_mirrored.rb",
116
118
  "lib/comfortable_mexican_sofa/rails_extensions.rb",
117
119
  "lib/comfortable_mexican_sofa/tag.rb",
118
120
  "lib/comfortable_mexican_sofa/tags/field_datetime.rb",
@@ -230,6 +232,7 @@ Gem::Specification.new do |s|
230
232
  "test/functional/cms_content_controller_test.rb",
231
233
  "test/integration/authentication_test.rb",
232
234
  "test/integration/fixtures_test.rb",
235
+ "test/integration/mirrors_test.rb",
233
236
  "test/integration/render_cms_test.rb",
234
237
  "test/integration/routing_extensions_test.rb",
235
238
  "test/integration/sites_test.rb",
@@ -237,6 +240,7 @@ Gem::Specification.new do |s|
237
240
  "test/test_helper.rb",
238
241
  "test/unit/configuration_test.rb",
239
242
  "test/unit/fixtures_test.rb",
243
+ "test/unit/mirrors_test.rb",
240
244
  "test/unit/models/block_test.rb",
241
245
  "test/unit/models/layout_test.rb",
242
246
  "test/unit/models/page_test.rb",
@@ -21,7 +21,12 @@ ComfortableMexicanSofa.configure do |config|
21
21
 
22
22
  # If you enable this setting you'll be able to serve completely different set
23
23
  # of sites with their own layouts and pages.
24
- # config.enable_multiple_sites = false
24
+ # config.enable_multiple_sites = true
25
+
26
+ # In cases when you need sites with identical page tree structure, like different
27
+ # language versions. This will automatically create/destroy resources across all sites and
28
+ # will keep slugs/paths synced.
29
+ # config.enable_mirror_sites = true
25
30
 
26
31
  # By default you cannot have irb code inside your layouts/pages/snippets.
27
32
  # Generally this is to prevent putting something like this:
@@ -13,6 +13,7 @@ end
13
13
  'comfortable_mexican_sofa/form_builder',
14
14
  'comfortable_mexican_sofa/acts_as_tree',
15
15
  'comfortable_mexican_sofa/has_revisions',
16
+ 'comfortable_mexican_sofa/is_mirrored',
16
17
  'comfortable_mexican_sofa/tag',
17
18
  'comfortable_mexican_sofa/fixtures'
18
19
  ].each do |path|
@@ -21,6 +21,9 @@ class ComfortableMexicanSofa::Configuration
21
21
  # Are you running multiple sites from single install? Default assumption is 'No'
22
22
  attr_accessor :enable_multiple_sites
23
23
 
24
+ # All resources across sites are kept in sync
25
+ attr_accessor :enable_mirror_sites
26
+
24
27
  # Not allowing irb code to be run inside page content. False by default.
25
28
  attr_accessor :allow_irb
26
29
 
@@ -49,6 +52,7 @@ class ComfortableMexicanSofa::Configuration
49
52
  @admin_route_redirect = 'pages'
50
53
  @content_route_prefix = ''
51
54
  @enable_multiple_sites = false
55
+ @enable_mirror_sites = false
52
56
  @allow_irb = false
53
57
  @enable_caching = true
54
58
  @upload_file_options = {}
@@ -1,13 +1,20 @@
1
1
  module ComfortableMexicanSofa::Fixtures
2
2
 
3
- def self.sync(site)
4
- sync_layouts(site)
5
- sync_pages(site)
6
- sync_snippets(site)
3
+ def self.import_all(to_hostname, from_hostname = nil)
4
+ import_layouts to_hostname, from_hostname
5
+ import_pages to_hostname, from_hostname
6
+ import_snippets to_hostname, from_hostname
7
7
  end
8
8
 
9
- def self.sync_layouts(site, path = nil, root = true, parent = nil, layout_ids = [])
10
- return unless path ||= find_path(site, 'layouts')
9
+ def self.export_all(from_hostname, to_hostname = nil)
10
+ export_layouts from_hostname, to_hostname
11
+ export_pages from_hostname, to_hostname
12
+ export_snippets from_hostname, to_hostname
13
+ end
14
+
15
+ def self.import_layouts(to_hostname, from_hostname = nil, path = nil, root = true, parent = nil, layout_ids = [])
16
+ return unless site = Cms::Site.find_by_hostname(to_hostname)
17
+ return unless path ||= find_fixtures_path((from_hostname || to_hostname), 'layouts')
11
18
 
12
19
  Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path|
13
20
  slug = path.split('/').last
@@ -48,7 +55,7 @@ module ComfortableMexicanSofa::Fixtures
48
55
  layout_ids << layout.id
49
56
 
50
57
  # checking for nested fixtures
51
- layout_ids += sync_layouts(site, path, false, layout, layout_ids)
58
+ layout_ids += import_layouts(to_hostname, from_hostname, path, false, layout, layout_ids)
52
59
  end
53
60
 
54
61
  # removing all db entries that are not in fixtures
@@ -58,8 +65,9 @@ module ComfortableMexicanSofa::Fixtures
58
65
  layout_ids
59
66
  end
60
67
 
61
- def self.sync_pages(site, path = nil, root = true, parent = nil, page_ids = [])
62
- return unless path ||= find_path(site, 'pages')
68
+ def self.import_pages(to_hostname, from_hostname = nil, path = nil, root = true, parent = nil, page_ids = [])
69
+ return unless site = Cms::Site.find_by_hostname(to_hostname)
70
+ return unless path ||= find_fixtures_path((from_hostname || to_hostname), 'pages')
63
71
 
64
72
  Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path|
65
73
  slug = path.split('/').last
@@ -101,7 +109,7 @@ module ComfortableMexicanSofa::Fixtures
101
109
  page_ids << page.id
102
110
 
103
111
  # checking for nested fixtures
104
- page_ids += sync_pages(site, path, false, page, page_ids)
112
+ page_ids += import_pages(to_hostname, from_hostname, path, false, page, page_ids)
105
113
  end
106
114
 
107
115
  # removing all db entries that are not in fixtures
@@ -111,8 +119,9 @@ module ComfortableMexicanSofa::Fixtures
111
119
  page_ids
112
120
  end
113
121
 
114
- def self.sync_snippets(site)
115
- return unless path = find_path(site, 'snippets')
122
+ def self.import_snippets(to_hostname, from_hostname = nil)
123
+ return unless site = Cms::Site.find_by_hostname(to_hostname)
124
+ return unless path = find_fixtures_path((from_hostname || to_hostname), 'snippets')
116
125
 
117
126
  snippet_ids = []
118
127
  Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path|
@@ -145,12 +154,85 @@ module ComfortableMexicanSofa::Fixtures
145
154
  site.snippets.where('id NOT IN (?)', snippet_ids).each{ |s| s.destroy }
146
155
  end
147
156
 
148
- def self.find_path(site, dir)
149
- path = nil
150
- File.exists?(path = File.join(ComfortableMexicanSofa.config.fixtures_path, site.hostname, dir)) ||
151
- !ComfortableMexicanSofa.config.enable_multiple_sites &&
152
- File.exists?(path = File.join(ComfortableMexicanSofa.config.fixtures_path, dir))
153
- return path
157
+ def self.export_layouts(from_hostname, to_hostname = nil)
158
+ return unless site = Cms::Site.find_by_hostname(from_hostname)
159
+ path = File.join(ComfortableMexicanSofa.config.fixtures_path, (to_hostname || site.hostname), 'layouts')
160
+ FileUtils.rm_rf(path)
161
+ FileUtils.mkdir_p(path)
162
+
163
+ site.layouts.each do |layout|
164
+ layout_path = File.join(path, layout.ancestors.reverse.collect{|l| l.slug}, layout.slug)
165
+ FileUtils.mkdir_p(layout_path)
166
+
167
+ open(File.join(layout_path, "_#{layout.slug}.yml"), 'w') do |f|
168
+ f.write({
169
+ 'label' => layout.label,
170
+ 'app_layout' => layout.app_layout,
171
+ 'parent' => layout.parent.try(:slug)
172
+ }.to_yaml)
173
+ end
174
+ open(File.join(layout_path, 'content.html'), 'w') do |f|
175
+ f.write(layout.content)
176
+ end
177
+ open(File.join(layout_path, 'css.css'), 'w') do |f|
178
+ f.write(layout.css)
179
+ end
180
+ open(File.join(layout_path, 'js.js'), 'w') do |f|
181
+ f.write(layout.js)
182
+ end
183
+ end
184
+ end
185
+
186
+ def self.export_pages(from_hostname, to_hostname = nil)
187
+ return unless site = Cms::Site.find_by_hostname(from_hostname)
188
+ path = File.join(ComfortableMexicanSofa.config.fixtures_path, (to_hostname || site.hostname), 'pages')
189
+ FileUtils.rm_rf(path)
190
+ FileUtils.mkdir_p(path)
191
+
192
+ site.pages.each do |page|
193
+ page.slug = 'index' if page.slug.blank?
194
+ page_path = File.join(path, page.ancestors.reverse.collect{|p| p.slug.blank?? 'index' : p.slug}, page.slug)
195
+ FileUtils.mkdir_p(page_path)
196
+
197
+ open(File.join(page_path, "_#{page.slug}.yml"), 'w') do |f|
198
+ f.write({
199
+ 'label' => page.label,
200
+ 'layout' => page.layout.try(:slug),
201
+ 'parent' => page.parent && (page.parent.slug.present?? page.parent.slug : 'index'),
202
+ 'target_page' => page.target_page.try(:slug),
203
+ 'is_published' => page.is_published
204
+ }.to_yaml)
205
+ end
206
+ page.blocks_attributes.each do |block|
207
+ open(File.join(page_path, "#{block[:label]}.html"), 'w') do |f|
208
+ f.write(block[:content])
209
+ end
210
+ end
211
+ end
212
+ end
213
+
214
+ def self.export_snippets(from_hostname, to_hostname = nil)
215
+ return unless site = Cms::Site.find_by_hostname(from_hostname)
216
+ path = File.join(ComfortableMexicanSofa.config.fixtures_path, (to_hostname || site.hostname), 'snippets')
217
+ FileUtils.rm_rf(path)
218
+ FileUtils.mkdir_p(path)
219
+
220
+ site.snippets.each do |snippet|
221
+ FileUtils.mkdir_p(snippet_path = File.join(path, snippet.slug))
222
+ open(File.join(snippet_path, "_#{snippet.slug}.yml"), 'w') do |f|
223
+ f.write({'label' => snippet.label}.to_yaml)
224
+ end
225
+ open(File.join(snippet_path, 'content.html'), 'w') do |f|
226
+ f.write(snippet.content)
227
+ end
228
+ end
229
+ end
230
+
231
+ protected
232
+
233
+ def self.find_fixtures_path(hostname, dir)
234
+ path = File.join(ComfortableMexicanSofa.config.fixtures_path, hostname, dir)
235
+ File.exists?(path) ? path : nil
154
236
  end
155
237
 
156
238
  end