Page form with drop down. Modifications for Designs

This commit is contained in:
chris2tof 2011-08-25 08:13:38 +08:00
parent 27d47215e9
commit c427e2af14
12 changed files with 118 additions and 49 deletions

View File

@ -0,0 +1,3 @@
$("#page_design_id").live('change', function() {
$.getScript($(this).attr('rel') + '/' + $(this).val() + '/reload_themes');
});

View File

@ -10,10 +10,9 @@ class Admin::DesignsController < ApplicationController
def upload_package def upload_package
if !params[:design].nil? if !params[:design].nil?
temp_file = Tempfile.new("temp_file") temp_file = Tempfile.new("temp_file")
temp_file.binmode
original_file = params[:design][:package_file] original_file = params[:design][:package_file]
#if original_file.content_type == 'application/zip' #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 temp_file.rewind
filename = File.basename(original_file.original_filename,".zip") filename = File.basename(original_file.original_filename,".zip")
unzip_design(temp_file, filename) unzip_design(temp_file, filename)
@ -36,16 +35,14 @@ class Admin::DesignsController < ApplicationController
zip_file.entries.each do |entry| zip_file.entries.each do |entry|
case (path = entry.to_s) case (path = entry.to_s)
when /\A(#{zip_name})\/[^\/]*(\.css)\z/ #for structure css 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 = File.new(dir + '/' + filename, 'w+')
temp_file4structure.binmode temp_file4structure.write (zip_file.read entry ).force_encoding('UTF-8')
temp_file4structure.write (zip_file.read entry )
design.structure_css = temp_file4structure design.structure_css = temp_file4structure
when /\A(#{zip_name})\/[^\/]*(\.html)\z/ #for layout html 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 = File.new(dir + '/' + filename, 'w+')
temp_file4layout.binmode temp_file4layout.write (zip_file.read entry ).force_encoding('UTF-8')
temp_file4layout.write (zip_file.read entry )
design.layout.file = temp_file4layout design.layout.file = temp_file4layout
design.layout.to_save=true design.layout.to_save=true
when /\A(#{zip_name})\/(themes)\/.*(\.css)\z/ when /\A(#{zip_name})\/(themes)\/.*(\.css)\z/
@ -63,8 +60,7 @@ class Admin::DesignsController < ApplicationController
eval("#{type}_entries").each do |entry| eval("#{type}_entries").each do |entry|
filename = File.basename entry.to_s filename = File.basename entry.to_s
temp_file = File.new(dir + '/' + filename, 'w+') temp_file = File.new(dir + '/' + filename, 'w+')
temp_file.binmode temp_file.write (zip_file.read entry).force_encoding('UTF-8')
temp_file.write zip_file.read entry
build_and_store = eval("design.#{type}").build build_and_store = eval("design.#{type}").build
build_and_store.file = temp_file build_and_store.file = temp_file
build_and_store.file_filename = filename build_and_store.file_filename = filename

View File

@ -23,6 +23,7 @@ class Admin::PagesController < ApplicationController
@item.parent_id = @parent_item.id rescue nil @item.parent_id = @parent_item.id rescue nil
@designs = Design.all.entries @designs = Design.all.entries
@default_design = @designs.detect {|d| d.title.to_s == 'Bob' } @default_design = @designs.detect {|d| d.title.to_s == 'Bob' }
@default_theme = @default_design.themes.detect {|t| t.name.to_s == 'default' }
end end
def edit def edit
@ -30,7 +31,6 @@ class Admin::PagesController < ApplicationController
@i18n_variable = @item.i18n_variable @i18n_variable = @item.i18n_variable
@designs = Design.all.entries @designs = Design.all.entries
@design = @item.design @design = @item.design
@default_design = @design.themes.detect {|t| t.id == @item.theme_id }
end end
def create def create
@ -54,7 +54,7 @@ class Admin::PagesController < ApplicationController
def update def update
@item = Page.find(params[:id]) @item = Page.find(params[:id])
if @item.update_attributes(params[:item]) if @item.update_attributes(params[:page])
flash[:notice] = t('admin.update_success_page') flash[:notice] = t('admin.update_success_page')
respond_to do |format| respond_to do |format|
format.html { format.html {
@ -87,4 +87,13 @@ class Admin::PagesController < ApplicationController
end end
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 end

View File

@ -1,8 +1,37 @@
require 'mongo' require 'mongo'
class GridfsController < ActionController::Metal class GridfsController < ActionController::Metal
def serve def serve
gridfs_path = env["PATH_INFO"].gsub("/gridfs/", "") 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 begin
gridfs_file = Mongo::GridFileSystem.new(Mongoid.database).open(gridfs_path, 'r') gridfs_file = Mongo::GridFileSystem.new(Mongoid.database).open(gridfs_path, 'r')
self.response_body = gridfs_file.read self.response_body = gridfs_file.read
@ -13,4 +42,15 @@ class GridfsController < ActionController::Metal
self.response_body = '' self.response_body = ''
end end
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 end

View File

@ -3,12 +3,16 @@ class LayoutPart
include Mongoid::Document include Mongoid::Document
include Mongoid::Timestamps include Mongoid::Timestamps
field :name_tag field :name
field :id_tag field :id_tag
field :editable_tag field :editable
field :class_tag field :class_tag
field :content field :content
embedded_in :layout embedded_in :layout
def editable?
self.editable.eql?('true')
end
end end

View File

@ -7,7 +7,7 @@ class Theme < Stylesheet
protected protected
def set_name def set_name
self.name = self.file_filename.split('.')[0] self.name = File.basename(self.file_filename,".css")
end end
end end

View File

@ -3,15 +3,13 @@ class Page < Item
field :content field :content
field :theme_id field :theme_id
validates_presence_of :design_id
belongs_to :design belongs_to :design
has_many :page_parts has_many :page_parts
def create_parts def create_parts
page_design = self.get_design page_design = self.design
page_design.layout.layout_parts.each do |p| 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
end end
@ -23,8 +21,4 @@ class Page < Item
end end
end end
def get_design
Design.find(design_id)
end
end end

View File

@ -1,6 +1,14 @@
# encoding: utf-8 # encoding: utf-8
class AssetUploader < CarrierWave::Uploader::Base 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 RMagick or ImageScience support:
# include CarrierWave::RMagick # include CarrierWave::RMagick

View File

@ -16,13 +16,19 @@
<p> <p>
<%= t('admin.design_name') %> <%= t('admin.design_name') %>
<%#= f.select :design_id, @designs.collect { |d| [d.title, d.id] }, :selected => @default_design.id %> <% if @design %>
<%= f.select :design_id, @designs.collect { |d| [d.title, d.id] } %> <%= 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 %>
</p> </p>
<p> <p>
<%= t('admin.theme') %> <%= 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 %> <% if @design %>
<%= f.select :theme_id, @design.themes.collect { |t| [t.name, t.id] }%> <%= 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 %>
</p> </p>
<p> <p>

View File

@ -0,0 +1 @@
$('#page_theme_id').html("<%= escape_javascript(options_from_collection_for_select(@themes, :id, :name, @default_theme.id )) %>");

View File

@ -27,6 +27,7 @@ PrototypeR4::Application.routes.draw do
resources :pages do resources :pages do
member do member do
get 'delete' get 'delete'
get 'reload_themes'
end end
end end
resources :page_parts resources :page_parts
@ -49,7 +50,9 @@ PrototypeR4::Application.routes.draw do
# routes for gridfs files # routes for gridfs files
match "/gridfs/*path" => "gridfs#serve" match "/gridfs/*path" => "gridfs#serve"
match "/images/*path" => "gridfs#serve_images"
match "/admin/pages/images/*path" => "gridfs#serve_images"
# routes for pages to generate # routes for pages to generate
match '*page_name' => 'pages#show', :as => :page, :constraints => lambda{|request| match '*page_name' => 'pages#show', :as => :page, :constraints => lambda{|request|
!request.path.starts_with?("/panel") !request.path.starts_with?("/panel")

View File

@ -89,11 +89,12 @@ module Parser
end end
end end
c.define_tag 'layout_part' do |tag| c.define_tag 'layout_part' do |tag|
ret = '' part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s } rescue nil
ret << "<div id='#{tag.attr['name']}'>" if part
ret << page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s }.content rescue '' part.content
ret << tag.expand else
ret << '</div>' tag.expand
end
end end
c.define_tag 'link' do |tag| c.define_tag 'link' do |tag|
item = Item.first(:conditions => { :full_name => tag.attr['name'] }) item = Item.first(:conditions => { :full_name => tag.attr['name'] })
@ -107,7 +108,7 @@ module Parser
def parse_page(page) def parse_page(page)
if page._type == '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) context = parser_context(page)
parser = Radius::Parser.new(context, :tag_prefix => 'r') parser = Radius::Parser.new(context, :tag_prefix => 'r')
parser.parse(parser.parse(layout_content)) parser.parse(parser.parse(layout_content))
@ -116,7 +117,7 @@ module Parser
def parse_page_edit(page) def parse_page_edit(page)
if page._type == '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) context = parser_context_edit(page)
parser = Radius::Parser.new(context, :tag_prefix => 'r') parser = Radius::Parser.new(context, :tag_prefix => 'r')
parser.parse(parser.parse(layout_content)) parser.parse(parser.parse(layout_content))
@ -184,17 +185,20 @@ module Parser
end end
c.define_tag 'layout_part' do |tag| c.define_tag 'layout_part' do |tag|
part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s } part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s }
ret = '' if part
ret << "<div id='#{tag.attr['name']}'" ret = ''
ret << " part_id='#{part.id}'" if part ret << "<div id='#{tag.attr['name']}'"
ret << " class='editable'" if part ret << " part_id='#{part.id}'"
ret << " style='border:solid 1px; margin:5px; padding:5px;'>" ret << " class='editable'"
ret << "<div class='edit_link' style='display:none'>" if part ret << " style='border:solid 1px; margin:5px; padding:5px;'>"
ret << " <a href='#{edit_admin_page_part_path(part.id)}' class='nav'>#{t(:edit)}</a>" if part ret << "<div class='edit_link' style='display:none'>"
ret << '</div>' if part ret << " <a href='#{edit_admin_page_part_path(part.id)}' class='nav'>#{t(:edit)}</a>"
ret << part.i18n_variable[I18n.locale.to_s] rescue '' ret << '</div>'
ret << tag.expand ret << part.i18n_variable[I18n.locale.to_s] rescue ''
ret << '</div>' ret << '</div>'
else
tag.expand
end
end end
c.define_tag 'link' do |tag| c.define_tag 'link' do |tag|
item = Item.first(:conditions => { :full_name => tag.attr['name'] }) item = Item.first(:conditions => { :full_name => tag.attr['name'] })
@ -210,7 +214,7 @@ module Parser
#============= #=============
def parse_layout_layout_part(layout) def parse_layout_layout_part(layout)
content = layout.content content = layout.content.force_encoding('UTF-8')
context = parser_layout_layout_part(layout) context = parser_layout_layout_part(layout)
parser = Radius::Parser.new(context, :tag_prefix => 'r') parser = Radius::Parser.new(context, :tag_prefix => 'r')
parser.parse(content) parser.parse(content)
@ -226,8 +230,9 @@ module Parser
data={} data={}
data[:class_tag]=tag.attr['class'].to_s data[:class_tag]=tag.attr['class'].to_s
data[:id_tag]=tag.attr['id'].to_s data[:id_tag]=tag.attr['id'].to_s
data[:name_tag]=tag.attr['name'].to_s data[:name]=tag.attr['name'].to_s
data[:editable_tag]=tag.attr['editable'].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) layout.layout_parts.create(data)
end end
end end