Module: ComfortableMexicanSofa::Fixtures
- Defined in:
- lib/comfortable_mexican_sofa/fixtures.rb
Class Method Summary collapse
- .export_all(from_hostname, to_hostname = nil) ⇒ Object
- .export_layouts(from_hostname, to_hostname = nil) ⇒ Object
- .export_pages(from_hostname, to_hostname = nil) ⇒ Object
- .export_snippets(from_hostname, to_hostname = nil) ⇒ Object
- .import_all(to_hostname, from_hostname = nil) ⇒ Object
- .import_layouts(to_hostname, from_hostname = nil, path = nil, root = true, parent = nil, layout_ids = []) ⇒ Object
- .import_pages(to_hostname, from_hostname = nil, path = nil, root = true, parent = nil, page_ids = []) ⇒ Object
- .import_snippets(to_hostname, from_hostname = nil) ⇒ Object
Class Method Details
.export_all(from_hostname, to_hostname = nil) ⇒ Object
9 10 11 12 13 |
# File 'lib/comfortable_mexican_sofa/fixtures.rb', line 9 def self.export_all(from_hostname, to_hostname = nil) export_layouts from_hostname, to_hostname export_pages from_hostname, to_hostname export_snippets from_hostname, to_hostname end |
.export_layouts(from_hostname, to_hostname = nil) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/comfortable_mexican_sofa/fixtures.rb', line 157 def self.export_layouts(from_hostname, to_hostname = nil) return unless site = Cms::Site.find_by_hostname(from_hostname) path = File.join(ComfortableMexicanSofa.config.fixtures_path, (to_hostname || site.hostname), 'layouts') FileUtils.rm_rf(path) FileUtils.mkdir_p(path) site.layouts.each do |layout| layout_path = File.join(path, layout.ancestors.reverse.collect{|l| l.slug}, layout.slug) FileUtils.mkdir_p(layout_path) open(File.join(layout_path, "_#{layout.slug}.yml"), 'w') do |f| f.write({ 'label' => layout.label, 'app_layout' => layout.app_layout, 'parent' => layout.parent.try(:slug) }.to_yaml) end open(File.join(layout_path, 'content.html'), 'w') do |f| f.write(layout.content) end open(File.join(layout_path, 'css.css'), 'w') do |f| f.write(layout.css) end open(File.join(layout_path, 'js.js'), 'w') do |f| f.write(layout.js) end end end |
.export_pages(from_hostname, to_hostname = nil) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/comfortable_mexican_sofa/fixtures.rb', line 186 def self.export_pages(from_hostname, to_hostname = nil) return unless site = Cms::Site.find_by_hostname(from_hostname) path = File.join(ComfortableMexicanSofa.config.fixtures_path, (to_hostname || site.hostname), 'pages') FileUtils.rm_rf(path) FileUtils.mkdir_p(path) site.pages.each do |page| page.slug = 'index' if page.slug.blank? page_path = File.join(path, page.ancestors.reverse.collect{|p| p.slug.blank?? 'index' : p.slug}, page.slug) FileUtils.mkdir_p(page_path) open(File.join(page_path, "_#{page.slug}.yml"), 'w') do |f| f.write({ 'label' => page.label, 'layout' => page.layout.try(:slug), 'parent' => page.parent && (page.parent.slug.present?? page.parent.slug : 'index'), 'target_page' => page.target_page.try(:slug), 'is_published' => page.is_published }.to_yaml) end page.blocks_attributes.each do |block| open(File.join(page_path, "#{block[:label]}.html"), 'w') do |f| f.write(block[:content]) end end end end |
.export_snippets(from_hostname, to_hostname = nil) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/comfortable_mexican_sofa/fixtures.rb', line 214 def self.export_snippets(from_hostname, to_hostname = nil) return unless site = Cms::Site.find_by_hostname(from_hostname) path = File.join(ComfortableMexicanSofa.config.fixtures_path, (to_hostname || site.hostname), 'snippets') FileUtils.rm_rf(path) FileUtils.mkdir_p(path) site.snippets.each do |snippet| FileUtils.mkdir_p(snippet_path = File.join(path, snippet.slug)) open(File.join(snippet_path, "_#{snippet.slug}.yml"), 'w') do |f| f.write({'label' => snippet.label}.to_yaml) end open(File.join(snippet_path, 'content.html'), 'w') do |f| f.write(snippet.content) end end end |
.import_all(to_hostname, from_hostname = nil) ⇒ Object
3 4 5 6 7 |
# File 'lib/comfortable_mexican_sofa/fixtures.rb', line 3 def self.import_all(to_hostname, from_hostname = nil) import_layouts to_hostname, from_hostname import_pages to_hostname, from_hostname import_snippets to_hostname, from_hostname end |
.import_layouts(to_hostname, from_hostname = nil, path = nil, root = true, parent = nil, layout_ids = []) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/comfortable_mexican_sofa/fixtures.rb', line 15 def self.import_layouts(to_hostname, from_hostname = nil, path = nil, root = true, parent = nil, layout_ids = []) return unless site = Cms::Site.find_by_hostname(to_hostname) return unless path ||= find_fixtures_path((from_hostname || to_hostname), 'layouts') Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path| slug = path.split('/').last layout = site.layouts.find_by_slug(slug) || site.layouts.new(:slug => slug) # updating attributes if File.exists?(file_path = File.join(path, "_#{slug}.yml")) if layout.new_record? || File.mtime(file_path) > layout.updated_at attributes = YAML.load_file(file_path).symbolize_keys! layout.label = attributes[:label] || slug.titleize layout.app_layout = attributes[:app_layout] || parent.try(:app_layout) end elsif layout.new_record? layout.label = slug.titleize layout.app_layout = parent.try(:app_layout) end # updating content if File.exists?(file_path = File.join(path, 'content.html')) if layout.new_record? || File.mtime(file_path) > layout.updated_at layout.content = File.open(file_path, 'rb').read end end if File.exists?(file_path = File.join(path, 'css.css')) if layout.new_record? || File.mtime(file_path) > layout.updated_at layout.css = File.open(file_path, 'rb').read end end if File.exists?(file_path = File.join(path, 'js.js')) if layout.new_record? || File.mtime(file_path) > layout.updated_at layout.js = File.open(file_path, 'rb').read end end # saving layout.parent = parent layout.save! if layout.changed? layout_ids << layout.id # checking for nested fixtures layout_ids += import_layouts(to_hostname, from_hostname, path, false, layout, layout_ids) end # removing all db entries that are not in fixtures site.layouts.where('id NOT IN (?)', layout_ids.uniq).each{ |l| l.destroy } if root # returning ids of layouts in fixtures layout_ids end |
.import_pages(to_hostname, from_hostname = nil, path = nil, root = true, parent = nil, page_ids = []) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/comfortable_mexican_sofa/fixtures.rb', line 68 def self.import_pages(to_hostname, from_hostname = nil, path = nil, root = true, parent = nil, page_ids = []) return unless site = Cms::Site.find_by_hostname(to_hostname) return unless path ||= find_fixtures_path((from_hostname || to_hostname), 'pages') Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path| slug = path.split('/').last page = if parent parent.children.find_by_slug(slug) || parent.children.new(:slug => slug, :site => site) else site.pages.root || site.pages.new(:slug => slug) end # updating attributes if File.exists?(file_path = File.join(path, "_#{slug}.yml")) if page.new_record? || File.mtime(file_path) > page.updated_at attributes = YAML.load_file(file_path).symbolize_keys! page.label = attributes[:label] || slug.titleize page.layout = site.layouts.find_by_slug(attributes[:layout]) || parent.try(:layout) page.target_page = site.pages.find_by_full_path(attributes[:target_page]) page.is_published = attributes[:is_published].present?? attributes[:is_published] : true end elsif page.new_record? page.label = slug.titleize page.layout = parent.try(:layout) end # updating content blocks_attributes = [ ] Dir.glob("#{path}/*.html").each do |file_path| if page.new_record? || File.mtime(file_path) > page.updated_at label = file_path.split('/').last.split('.').first blocks_attributes << { :label => label, :content => File.open(file_path, 'rb').read } end end # saving page.blocks_attributes = blocks_attributes if blocks_attributes.present? page.save! if page.changed? || blocks_attributes.present? page_ids << page.id # checking for nested fixtures page_ids += import_pages(to_hostname, from_hostname, path, false, page, page_ids) end # removing all db entries that are not in fixtures site.pages.where('id NOT IN (?)', page_ids.uniq).each{ |p| p.destroy } if root # returning ids of layouts in fixtures page_ids end |
.import_snippets(to_hostname, from_hostname = nil) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/comfortable_mexican_sofa/fixtures.rb', line 122 def self.import_snippets(to_hostname, from_hostname = nil) return unless site = Cms::Site.find_by_hostname(to_hostname) return unless path = find_fixtures_path((from_hostname || to_hostname), 'snippets') snippet_ids = [] Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path| slug = path.split('/').last snippet = site.snippets.find_by_slug(slug) || site.snippets.new(:slug => slug) # updating attributes if File.exists?(file_path = File.join(path, "_#{slug}.yml")) if snippet.new_record? || File.mtime(file_path) > snippet.updated_at attributes = YAML.load_file(file_path).symbolize_keys! snippet.label = attributes[:label] || slug.titleize end elsif snippet.new_record? snippet.label = slug.titleize end # updating content if File.exists?(file_path = File.join(path, 'content.html')) if snippet.new_record? || File.mtime(file_path) > snippet.updated_at snippet.content = File.open(file_path, 'rb').read end end # saving snippet.save! if snippet.changed? snippet_ids << snippet.id end # removing all db entries that are not in fixtures site.snippets.where('id NOT IN (?)', snippet_ids).each{ |s| s.destroy } end |