From c427e2af1461718cbb857f2d64633a9f8f451183 Mon Sep 17 00:00:00 2001 From: chris2tof Date: Thu, 25 Aug 2011 08:13:38 +0800 Subject: [PATCH] Page form with drop down. Modifications for Designs --- app/assets/javascripts/page.js | 3 ++ app/controllers/admin/designs_controller.rb | 16 +++---- app/controllers/admin/pages_controller.rb | 13 +++++- app/controllers/gridfs_controller.rb | 40 ++++++++++++++++++ app/models/design/layout_part.rb | 8 +++- app/models/design/theme.rb | 2 +- app/models/page.rb | 10 +---- app/uploaders/asset_uploader.rb | 8 ++++ app/views/admin/pages/_form.html.erb | 14 ++++-- app/views/admin/pages/reload_themes.js.erb | 1 + config/routes.rb | 5 ++- lib/parser.rb | 47 ++++++++++++--------- 12 files changed, 118 insertions(+), 49 deletions(-) create mode 100644 app/assets/javascripts/page.js create mode 100644 app/views/admin/pages/reload_themes.js.erb diff --git a/app/assets/javascripts/page.js b/app/assets/javascripts/page.js new file mode 100644 index 000000000..4b413ddf0 --- /dev/null +++ b/app/assets/javascripts/page.js @@ -0,0 +1,3 @@ +$("#page_design_id").live('change', function() { + $.getScript($(this).attr('rel') + '/' + $(this).val() + '/reload_themes'); +}); \ No newline at end of file diff --git a/app/controllers/admin/designs_controller.rb b/app/controllers/admin/designs_controller.rb index 775989f0d..1b2586005 100644 --- a/app/controllers/admin/designs_controller.rb +++ b/app/controllers/admin/designs_controller.rb @@ -10,10 +10,9 @@ class Admin::DesignsController < ApplicationController def upload_package if !params[:design].nil? temp_file = Tempfile.new("temp_file") - temp_file.binmode original_file = params[:design][:package_file] #if original_file.content_type == 'application/zip' - temp_file.write(original_file.read) + temp_file.write(original_file.read.force_encoding('UTF-8')) temp_file.rewind filename = File.basename(original_file.original_filename,".zip") unzip_design(temp_file, filename) @@ -36,16 +35,14 @@ class Admin::DesignsController < ApplicationController zip_file.entries.each do |entry| case (path = entry.to_s) when /\A(#{zip_name})\/[^\/]*(\.css)\z/ #for structure css - filename = File.basename(entry.to_s,".css") + filename = File.basename(entry.to_s) temp_file4structure = File.new(dir + '/' + filename, 'w+') - temp_file4structure.binmode - temp_file4structure.write (zip_file.read entry ) + temp_file4structure.write (zip_file.read entry ).force_encoding('UTF-8') design.structure_css = temp_file4structure when /\A(#{zip_name})\/[^\/]*(\.html)\z/ #for layout html - filename = File.basename(entry.to_s,".css") + filename = File.basename(entry.to_s) temp_file4layout = File.new(dir + '/' + filename, 'w+') - temp_file4layout.binmode - temp_file4layout.write (zip_file.read entry ) + temp_file4layout.write (zip_file.read entry ).force_encoding('UTF-8') design.layout.file = temp_file4layout design.layout.to_save=true when /\A(#{zip_name})\/(themes)\/.*(\.css)\z/ @@ -63,8 +60,7 @@ class Admin::DesignsController < ApplicationController eval("#{type}_entries").each do |entry| filename = File.basename entry.to_s temp_file = File.new(dir + '/' + filename, 'w+') - temp_file.binmode - temp_file.write zip_file.read entry + temp_file.write (zip_file.read entry).force_encoding('UTF-8') build_and_store = eval("design.#{type}").build build_and_store.file = temp_file build_and_store.file_filename = filename diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb index 1fcc7bded..e3fdbb202 100644 --- a/app/controllers/admin/pages_controller.rb +++ b/app/controllers/admin/pages_controller.rb @@ -23,6 +23,7 @@ class Admin::PagesController < ApplicationController @item.parent_id = @parent_item.id rescue nil @designs = Design.all.entries @default_design = @designs.detect {|d| d.title.to_s == 'Bob' } + @default_theme = @default_design.themes.detect {|t| t.name.to_s == 'default' } end def edit @@ -30,7 +31,6 @@ class Admin::PagesController < ApplicationController @i18n_variable = @item.i18n_variable @designs = Design.all.entries @design = @item.design - @default_design = @design.themes.detect {|t| t.id == @item.theme_id } end def create @@ -54,7 +54,7 @@ class Admin::PagesController < ApplicationController def update @item = Page.find(params[:id]) - if @item.update_attributes(params[:item]) + if @item.update_attributes(params[:page]) flash[:notice] = t('admin.update_success_page') respond_to do |format| format.html { @@ -87,4 +87,13 @@ class Admin::PagesController < ApplicationController end end + def reload_themes + @design = Design.find(params[:id]) + @themes = @design.themes + @default_theme = @themes.detect {|t| t.name.to_s == 'default' } + respond_to do |format| + format.js {} + end + end + end diff --git a/app/controllers/gridfs_controller.rb b/app/controllers/gridfs_controller.rb index be276673f..89b1e69b0 100644 --- a/app/controllers/gridfs_controller.rb +++ b/app/controllers/gridfs_controller.rb @@ -1,8 +1,37 @@ require 'mongo' class GridfsController < ActionController::Metal + def serve gridfs_path = env["PATH_INFO"].gsub("/gridfs/", "") + gridfs_file = Mongo::GridFileSystem.new(Mongoid.database).open(gridfs_path, 'r') rescue nil + if gridfs_file + self.response_body = gridfs_file.read + self.content_type = gridfs_file.content_type + else + serve_rescue(gridfs_path.split('/').pop) + end + end + + def serve_images + gridfs_path = env["PATH_INFO"] + serve_rescue(gridfs_path.split('/').pop) + end + + def serve_rescue(file_name) + referer = request.env['HTTP_REFERER'] + if referer.include?('admin') + page = Page.find(referer.split('/').pop) + else + vars = request.env['HTTP_REFERER'].split('/') + names = get_names(vars, []) + page = Page.find_by_name(names.pop) + names.reverse_each do |name| + page = page.children.find_by_name(name) + end + end + image = page.design.images.detect{|i| i.file_filename == file_name} + gridfs_path = image.file.url.gsub("/gridfs/", "") begin gridfs_file = Mongo::GridFileSystem.new(Mongoid.database).open(gridfs_path, 'r') self.response_body = gridfs_file.read @@ -13,4 +42,15 @@ class GridfsController < ActionController::Metal self.response_body = '' end end + + def get_names(vars, names) + page = Page.all(:conditions => {:name => vars.last}) + names << vars.pop + if page.count > 1 + get_names(vars, names) + else + names + end + end + end \ No newline at end of file diff --git a/app/models/design/layout_part.rb b/app/models/design/layout_part.rb index c0d8c09aa..29392ccd6 100644 --- a/app/models/design/layout_part.rb +++ b/app/models/design/layout_part.rb @@ -3,12 +3,16 @@ class LayoutPart include Mongoid::Document include Mongoid::Timestamps - field :name_tag + field :name field :id_tag - field :editable_tag + field :editable field :class_tag field :content embedded_in :layout + + def editable? + self.editable.eql?('true') + end end diff --git a/app/models/design/theme.rb b/app/models/design/theme.rb index d9888c372..e69741818 100644 --- a/app/models/design/theme.rb +++ b/app/models/design/theme.rb @@ -7,7 +7,7 @@ class Theme < Stylesheet protected def set_name - self.name = self.file_filename.split('.')[0] + self.name = File.basename(self.file_filename,".css") end end diff --git a/app/models/page.rb b/app/models/page.rb index 6cbc00679..be51c35c0 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -3,15 +3,13 @@ class Page < Item field :content field :theme_id - validates_presence_of :design_id - belongs_to :design has_many :page_parts def create_parts - page_design = self.get_design + page_design = self.design page_design.layout.layout_parts.each do |p| - self.page_parts.create( :name => p.name, :i18n_variable_id => I18nVariable.create.id, :kind => 'text' ) if p['editable'] + self.page_parts.create( :name => p.name, :i18n_variable_id => I18nVariable.create.id, :kind => 'text' ) if p.editable? end end @@ -23,8 +21,4 @@ class Page < Item end end - def get_design - Design.find(design_id) - end - end diff --git a/app/uploaders/asset_uploader.rb b/app/uploaders/asset_uploader.rb index 32b14a936..170a87492 100644 --- a/app/uploaders/asset_uploader.rb +++ b/app/uploaders/asset_uploader.rb @@ -1,6 +1,14 @@ # encoding: utf-8 class AssetUploader < CarrierWave::Uploader::Base + require 'mime/types' + + process :set_content_type + + def set_content_type(*args) + content_type = file.content_type == 'binary/octet-stream' || 'application/octet-stream' || file.content_type.blank? ? MIME::Types.type_for(original_filename).first.to_s : file.content_type + self.file.instance_variable_set(:@content_type, content_type) + end # Include RMagick or ImageScience support: # include CarrierWave::RMagick diff --git a/app/views/admin/pages/_form.html.erb b/app/views/admin/pages/_form.html.erb index 208a92f30..1c8d11578 100644 --- a/app/views/admin/pages/_form.html.erb +++ b/app/views/admin/pages/_form.html.erb @@ -16,13 +16,19 @@

<%= t('admin.design_name') %> - <%#= f.select :design_id, @designs.collect { |d| [d.title, d.id] }, :selected => @default_design.id %> - <%= f.select :design_id, @designs.collect { |d| [d.title, d.id] } %> + <% if @design %> + <%= f.collection_select :design_id, @designs, :id, :title, {}, :rel => admin_pages_path %> + <% else %> + <%= f.select :design_id, @designs.collect { |d| [d.title, d.id] }, {:selected => @default_design.id}, {:rel => admin_pages_path} %> + <% end %>

<%= t('admin.theme') %> - <%#= f.select :theme_id, @default_design.themes.collect { |t| [t.name, t.id] }, :selected => @default_design.themes.detect {|t| t.file_filename.to_s == 'default.css' }.id %> - <%= f.select :theme_id, @design.themes.collect { |t| [t.name, t.id] }%> + <% if @design %> + <%= f.select :theme_id, @design.themes.collect { |t| [t.name.capitalize, t.id] }%> + <% else %> + <%= f.select :theme_id, @default_design.themes.collect { |t| [t.name.capitalize, t.id] }, :selected => @default_theme.id %> + <% end %>

diff --git a/app/views/admin/pages/reload_themes.js.erb b/app/views/admin/pages/reload_themes.js.erb new file mode 100644 index 000000000..e3d111b4b --- /dev/null +++ b/app/views/admin/pages/reload_themes.js.erb @@ -0,0 +1 @@ +$('#page_theme_id').html("<%= escape_javascript(options_from_collection_for_select(@themes, :id, :name, @default_theme.id )) %>"); \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index ceb4aab7a..5971900ac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -27,6 +27,7 @@ PrototypeR4::Application.routes.draw do resources :pages do member do get 'delete' + get 'reload_themes' end end resources :page_parts @@ -49,7 +50,9 @@ PrototypeR4::Application.routes.draw do # routes for gridfs files match "/gridfs/*path" => "gridfs#serve" - + match "/images/*path" => "gridfs#serve_images" + match "/admin/pages/images/*path" => "gridfs#serve_images" + # routes for pages to generate match '*page_name' => 'pages#show', :as => :page, :constraints => lambda{|request| !request.path.starts_with?("/panel") diff --git a/lib/parser.rb b/lib/parser.rb index a5299af39..d55b3cdf8 100644 --- a/lib/parser.rb +++ b/lib/parser.rb @@ -89,11 +89,12 @@ module Parser end end c.define_tag 'layout_part' do |tag| - ret = '' - ret << "

" - ret << page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s }.content rescue '' - ret << tag.expand - ret << '
' + 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 end c.define_tag 'link' do |tag| item = Item.first(:conditions => { :full_name => tag.attr['name'] }) @@ -107,7 +108,7 @@ module Parser def parse_page(page) if page._type == 'Page' - layout_content = page.design.layout.content + layout_content = page.design.layout.content.force_encoding('UTF-8') context = parser_context(page) parser = Radius::Parser.new(context, :tag_prefix => 'r') parser.parse(parser.parse(layout_content)) @@ -116,7 +117,7 @@ module Parser def parse_page_edit(page) if page._type == 'Page' - layout_content = page.design.layout.content + layout_content = page.design.layout.content.force_encoding('UTF-8') context = parser_context_edit(page) parser = Radius::Parser.new(context, :tag_prefix => 'r') parser.parse(parser.parse(layout_content)) @@ -184,17 +185,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 } - ret = '' - ret << "
" - ret << "' if part - ret << part.i18n_variable[I18n.locale.to_s] rescue '' - ret << tag.expand - ret << '
' + if part + ret = '' + ret << "
" + ret << "' + ret << part.i18n_variable[I18n.locale.to_s] rescue '' + ret << '
' + else + tag.expand + end end c.define_tag 'link' do |tag| item = Item.first(:conditions => { :full_name => tag.attr['name'] }) @@ -210,7 +214,7 @@ module Parser #============= def parse_layout_layout_part(layout) - content = layout.content + content = layout.content.force_encoding('UTF-8') context = parser_layout_layout_part(layout) parser = Radius::Parser.new(context, :tag_prefix => 'r') parser.parse(content) @@ -226,8 +230,9 @@ module Parser data={} data[:class_tag]=tag.attr['class'].to_s data[:id_tag]=tag.attr['id'].to_s - data[:name_tag]=tag.attr['name'].to_s - data[:editable_tag]=tag.attr['editable'].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) end end