From f4b1d1e1827593fff7b6e1443ab68cb65982218d Mon Sep 17 00:00:00 2001 From: chris2tof Date: Mon, 5 Sep 2011 01:14:49 +0800 Subject: [PATCH] Modifications in Layout In the layout file we can have "contents" (for editable content) and "layout_parts" (for when the layout is created from scratch) --- app/models/design/design.rb | 7 +++-- app/models/design/layout.rb | 11 ++++--- app/models/design/layout_part.rb | 4 +-- lib/fraisier/layout.html | 13 +++----- lib/parser.rb | 54 ++++++++++++++------------------ lib/tasks/dev.rake | 2 +- 6 files changed, 43 insertions(+), 48 deletions(-) diff --git a/app/models/design/design.rb b/app/models/design/design.rb index 9ad94146..9d3bc5c4 100644 --- a/app/models/design/design.rb +++ b/app/models/design/design.rb @@ -70,12 +70,15 @@ class Design end def procs_embedded_objects - [[self.layout], self.javascripts, self.images].each do |objects| + [self.javascripts, self.images].each do |objects| objects.each do |object| process_object(object) end end - self.layout.parse_urls + if self.layout.to_save + self.layout.save + self.layout.parse_urls + end self.themes.each do |theme| to_parse_url = theme.to_save ? true : false process_object(theme) diff --git a/app/models/design/layout.rb b/app/models/design/layout.rb index c8436db6..cff82575 100644 --- a/app/models/design/layout.rb +++ b/app/models/design/layout.rb @@ -12,19 +12,20 @@ class Layout < DesignFile after_save :parse_layout def content - self.file.read + self.file.read.force_encoding("UTF-8") end def self.exist_one? Layout.count > 0 end - def parse_layout - parse_layout_layout_part(self) + def parse_layout + debugger + parse_layout_contents(self) end def parse_urls - orig_content = content = self.file.read.force_encoding("UTF-8") + orig_content = content = self.content self.remove_file! self.remove_file_orig! names = [] @@ -52,7 +53,7 @@ class Layout < DesignFile temp_file.write content.force_encoding("UTF-8") self.file = temp_file - Layout.without_callback(:save) do + Layout.without_callback(:save, :after, :parse_layout) do self.save end } diff --git a/app/models/design/layout_part.rb b/app/models/design/layout_part.rb index 29392ccd..3b870908 100644 --- a/app/models/design/layout_part.rb +++ b/app/models/design/layout_part.rb @@ -5,14 +5,14 @@ class LayoutPart field :name field :id_tag - field :editable + field :editable, :type => Boolean field :class_tag field :content embedded_in :layout def editable? - self.editable.eql?('true') + self.editable end end diff --git a/lib/fraisier/layout.html b/lib/fraisier/layout.html index 1056f3a4..e8a8531e 100644 --- a/lib/fraisier/layout.html +++ b/lib/fraisier/layout.html @@ -15,22 +15,19 @@
- - + This is the first level of header
- - + This is the second level of header
- - + +
diff --git a/lib/parser.rb b/lib/parser.rb index b8caa7c3..ddfa07a6 100644 --- a/lib/parser.rb +++ b/lib/parser.rb @@ -88,11 +88,11 @@ module Parser end c.define_tag 'layout_part' do |tag| part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s } rescue nil - if part - part.content - else - tag.expand - end + part.content + end + c.define_tag 'content' do |tag| + part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s } rescue nil + part.content end c.define_tag 'link' do |tag| item = Item.first(:conditions => { :full_name => tag.attr['name'] }) @@ -186,20 +186,20 @@ module Parser end c.define_tag 'layout_part' do |tag| part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s } - if part - ret = '' - ret << "
" - ret << "' - ret << part.i18n_variable[I18n.locale.to_s] rescue '' - ret << '
' - else - tag.expand - end + ret = '' + ret << "
" + ret << tag.expand + ret << '
' + end + c.define_tag 'content' do |tag| + part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s } + ret = '' + ret << "
" + ret << "' + ret << part.i18n_variable[I18n.locale.to_s] rescue '' + ret << '
' end c.define_tag 'link' do |tag| item = Item.first(:conditions => { :full_name => tag.attr['name'] }) @@ -250,14 +250,14 @@ module Parser end #============= - def parse_layout_layout_part(layout) + def parse_layout_contents(layout) content = layout.content.force_encoding('UTF-8') - context = parser_layout_layout_part(layout) + context = parser_layout_contents(layout) parser = Radius::Parser.new(context, :tag_prefix => 'r') parser.parse(content) end - def parser_layout_layout_part(layout ) + def parser_layout_contents(layout ) Radius::Context.new do |c| c.define_tag 'javascripts' do |tag| end @@ -271,14 +271,8 @@ module Parser layout.menu.save tag.expand end - c.define_tag 'layout_part' do |tag| - data={} - data[:class_tag]=tag.attr['class'].to_s - data[:id_tag]=tag.attr['id'].to_s - data[:name]=tag.attr['name'].to_s - data[:editable]=tag.attr['editable'].to_s - data[:content] = tag.expand.force_encoding('UTF-8') if !(tag.attr['editable'].eql?('true')) - layout.layout_parts.create(data) + c.define_tag 'content' do |tag| + layout.layout_parts.create(:name => tag.attr['name'], :editable => true) end end end diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index caba0fa6..b3d539ea 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -115,7 +115,7 @@ namespace :dev do home = Page.create!( :i18n_variable_id => var_10.id, :design_id => design_1.id, :name => 'home', :is_published => true, :theme_id => theme.id ) - home.page_parts.create!( :name => 'header_1', :content => File.open("#{Rails.root}/lib/template/home.page").read, :kind => 'text', :i18n_variable_id => var_13.id ) + home.page_parts.create!( :name => 'content_1', :content => File.open("#{Rails.root}/lib/template/home.page").read, :kind => 'text', :i18n_variable_id => var_13.id ) end