From 8da0db2454d4de1588fe436088d8d1642ac1e18a Mon Sep 17 00:00:00 2001 From: did Date: Thu, 28 Jul 2011 00:39:59 +0200 Subject: [PATCH] improve the solution for the issue #127 and write tests (pull request #139) --- Gemfile.lock | 4 +- .../extensions/page/editable_elements.rb | 5 +- lib/locomotive/import/pages.rb | 49 +++++++++--------- spec/fixtures/themes/default.zip | Bin 403470 -> 512342 bytes spec/lib/locomotive/import_spec.rb | 14 ++++- 5 files changed, 43 insertions(+), 29 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 45f8a22a..fa1be25e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -52,7 +52,7 @@ GEM bson (1.3.1) bson_ext (1.3.1) builder (2.1.2) - bushido (0.0.31) + bushido (0.0.34) highline (>= 1.6.1) json (>= 1.4.6) orm_adapter (~> 0.0.3) @@ -289,7 +289,7 @@ DEPENDENCIES actionmailer-with-request autotest bson_ext (~> 1.3.0) - bushido (= 0.0.31) + bushido (= 0.0.34) bushido_stub (= 0.0.3) cancan capybara diff --git a/app/models/extensions/page/editable_elements.rb b/app/models/extensions/page/editable_elements.rb index 2a9022f4..3c588d50 100644 --- a/app/models/extensions/page/editable_elements.rb +++ b/app/models/extensions/page/editable_elements.rb @@ -67,10 +67,13 @@ module Extensions if existing_el.nil? # new one from parents new_attributes = el.attributes.merge(:from_parent => true) + if new_attributes['default_attribute'].present? new_attributes['default_content'] = self.send(new_attributes['default_attribute']) || el.content else - new_attributes['default_content'] = el.content + if el.respond_to?(:content) # only for text + new_attributes['default_content'] = el.content + end end self.editable_elements.build(new_attributes, el.class) diff --git a/lib/locomotive/import/pages.rb b/lib/locomotive/import/pages.rb index 1d3de006..f24d098d 100644 --- a/lib/locomotive/import/pages.rb +++ b/lib/locomotive/import/pages.rb @@ -82,30 +82,9 @@ module Locomotive page.attributes = attributes page.save! - + unless editable_elements_attributes.nil? - page.editable_elements.each do |editable_element| - editable_elements_attributes.each do |attributes| - if editable_element.block == attributes['block'] && editable_element.slug == attributes['slug'] - if editable_element.respond_to?(:source) - # editable_file, update file - unless attributes['content'].blank? - full_path = File.join(File.join(theme_path,'public',attributes['content'])) - if File.exists?(full_path) - file = File.open(full_path) - editable_element.source = file - editable_element.save! - file.close - end - end - else - # update content value - editable_element.update_attribute(:content, attributes['content']) - end - break - end - end - end + self.assign_editable_elements(page, editable_elements_attributes) end self.log "adding #{page.fullpath} (#{template.blank? ? 'without' : 'with'} template) / #{page.position}" @@ -117,6 +96,28 @@ module Locomotive page end + def assign_editable_elements(page, elements) + page.reload # editable elements are not synchronized otherwise + + elements.each do |attributes| + element = page.find_editable_element(attributes['block'], attributes['slug']) + + next if element.nil? + + if element.respond_to?(:source) + asset_path = File.join(theme_path, 'public', attributes['content']) + + if File.exists?(asset_path) + element.source = File.open(asset_path) + end + else + element.content = attributes['content'] + end + end + + page.save! + end + def build_parent_template(template) # just check if the template contains the extends keyword fullpath = template.scan(/\{% extends \'?([\w|\/]+)\'? %\}/).flatten.first @@ -148,7 +149,7 @@ module Locomotive return if template.blank? template.gsub!(/\/samples\/(.*\.[a-zA-Z0-9]{3})/) do |match| - name = $1 + name = File.basename($1) if asset = site.assets.where(:source_filename => name).first asset.source.url diff --git a/spec/fixtures/themes/default.zip b/spec/fixtures/themes/default.zip index b157828b6836b61ff907452b662f30af82f120af..973d2494eb10ab84e7ff4fa8fb110ea5bc5c8cbb 100644 GIT binary patch delta 6611 zcma)A2|QHo_n*xjSwa{y#xg@8+fdf*Nrgg`twNNgY}vi4F(H*Dy030jl9U#SHhU^t zi^>{FBvk4pq!KUv@4dF6dVhaDV;=WBXL-&!&-a{j6YIz?G9{DbDG#DmEYi;lTYSZS z#afx;2|^f7_&2h2JlR-=s28JRQ68<5?~ljgD@w<~5Ud0Rd4_yu7`QOV4??|$mHCA_ zD-Qezt2!eOq|zioW84N*dG_y=gO_mfv%6p7KA_qw@$&y6pGZ)j<(QJM1*Q>1|EvQ# z_X0}+b40knK?3g#>v;ml)KQ6m%6}-c0(53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0tP7vAI2Z75bjNI2WL86 z0VsS@n3LG!WCsx?NsFTnLt{-qpEyB73~PY|>?BL2 z%sFh;jFK7B3*=?U*=8VSjGc7zzpiP-Vj(Q-t-2)2S>_#S0_?*li)mhldx-r)L-;sA znw(UuiT_9k)376D-a0Y-u6suM;CW}KmUN;F(J|Vd%{8k0{5cLYtiv}jdS^jd9&E< z(7?6lG~z{Qc`LqEfdCgP>k07OK@7P-7CFlSg0f@GEJXrg!O&utZ7 z65z9HE51&U*fu~X(Uma75WTO(dmqH;Mc-p|bd-p}_g+&J}qz0CqVk?$}J;oG$ zLO_^8kkr@eGk-arS`Y9qoBOz9C);Ssazoj zC3<2I-p}zwJ%0}+rWIgBX++@~B)nAFE(|>0HK$CpXVDTGhCV5y7eWO3Ao=-h#S2fm z0|=A*dm#cc*a2kl+Qvg(nk8dQ<%bYrAw*GH8nP17{~KDg;SZ6q-+&P>ShG&_ASz-= zxB%jHnDmu{xfmK=LB>!GI{0cG0zXEY#t7D$>QBFIl|C-*Qn!xk7x1$bFzGj}_)zwNUu5B4nVS4H5*xO>u1d Ee**fh^Z)<= diff --git a/spec/lib/locomotive/import_spec.rb b/spec/lib/locomotive/import_spec.rb index 10b29bfa..007900c2 100644 --- a/spec/lib/locomotive/import_spec.rb +++ b/spec/lib/locomotive/import_spec.rb @@ -55,18 +55,28 @@ describe Locomotive::Import::Job do @site.pages.not_found.first.should_not be_nil end + it 'sets the editable text for a page from the site config file' do + page = @site.pages.where(:title => 'Contact').first + page.find_editable_element('content', 'address').content.should == '

Our office address: 215 Vine Street, Scranton, PA 18503

' + end + + it 'sets the editable file for a page from the site config file' do + page = @site.pages.where(:title => 'Contact').first + page.find_editable_element('content', 'office').source_filename.should == 'office.jpg' + end + it 'inserts templatized page' do page = @site.pages.where(:templatized => true).first page.should_not be_nil page.fullpath.should == 'portfolio/content_type_template' end - + it 'inserts redirection page' do page = @site.pages.where(:redirect => true).first page.should_not be_nil page.redirect_url.should == 'http://blog.locomotivecms.com' end - + it 'inserts snippets' do @site.snippets.count.should == 1 end