app/models/theme.rb in knitkit-2.1.15 vs app/models/theme.rb in knitkit-3.0.0

- old
+ new

@@ -3,19 +3,83 @@ class Theme < ActiveRecord::Base attr_protected :created_at, :updated_at THEME_STRUCTURE = ['stylesheets', 'javascripts', 'images', 'templates'] - class << self; attr_accessor :base_layouts_views_path, :knitkit_website_stylesheets_path, :knitkit_website_images_path end + class << self + attr_accessor :base_layouts_views_path, :knitkit_website_stylesheets_path, + :knitkit_website_images_path, :knitkit_website_javascripts_path + end @base_layouts_views_path = "#{Knitkit::Engine.root.to_s}/app/views" @knitkit_website_stylesheets_path = "#{Knitkit::Engine.root.to_s}/public/stylesheets/knitkit" + @knitkit_website_javascripts_path = "#{Knitkit::Engine.root.to_s}/public/javascripts/knitkit" @knitkit_website_images_path = "#{Knitkit::Engine.root.to_s}/public/images/knitkit" protected_with_capabilities has_file_assets + def import_download_item_file(file) + file_support = ErpTechSvcs::FileSupport::Base.new(:storage => Rails.application.config.erp_tech_svcs.file_storage) + + theme_root = Theme.find_theme_root_from_file(file) + + Zip::ZipFile.open(file) do |zip| + zip.each do |entry| + if entry.name == 'about.yml' + data = '' + entry.get_input_stream { |io| data = io.read } + data = StringIO.new(data) if data.present? + about = YAML.load(data) + self.author = about['author'] if about['author'] + self.version = about['version'] if about['version'] + self.homepage = about['homepage'] if about['homepage'] + self.summary = about['summary'] if about['summary'] + else + name = entry.name.sub(/__MACOSX\//, '') + name = Theme.strip_path(name, theme_root) + data = '' + entry.get_input_stream { |io| data = io.read } + data = StringIO.new(data) if data.present? + theme_file = self.files.where("name = ? and directory = ?", File.basename(name), File.join(self.url,File.dirname(name))).first + unless theme_file.nil? + theme_file.data = data + theme_file.save + else + self.add_file(data, File.join(file_support.root, self.url,name)) rescue next + end + end + end + end + activate! + end + class << self + def import_download_item(tempfile, website) + name_and_id = tempfile.gsub(/(^.*(\\|\/))|(\.zip$)/, '') + theme_name = name_and_id.split('[').first + theme_id = name_and_id.split('[').last.gsub(']','') + Theme.create(:name => theme_name.sub(/-theme/, ''), :theme_id => theme_id, :website_id => website.id).tap do |theme| + theme.import_download_item_file(tempfile) + end + end + + + def find_theme_root_from_file(file) + theme_root = '' + Zip::ZipFile.open(file) do |zip| + zip.each do |entry| + entry.name.sub!(/__MACOSX\//, '') + if theme_root == root_in_path(entry.name) + break + end + end + end + theme_root + end + + + def root_dir @@root_dir ||= "#{Rails.root}/public" end def base_dir(website) @@ -118,12 +182,10 @@ theme_root = Theme.find_theme_root(file) Zip::ZipFile.open(file.path) do |zip| zip.each do |entry| if entry.name == 'about.yml' - #TODO - #FIXME this does not work for some reason data = '' entry.get_input_stream { |io| data = io.read } data = StringIO.new(data) if data.present? about = YAML.load(data) self.author = about['author'] if about['author'] @@ -144,10 +206,11 @@ self.add_file(data, File.join(file_support.root, self.url,name)) rescue next end end end end + end def export file_support = ErpTechSvcs::FileSupport::Base.new(:storage => Rails.application.config.erp_tech_svcs.file_storage) tmp_dir = Theme.make_tmp_dir @@ -211,10 +274,11 @@ def create_theme_files! file_support = ErpTechSvcs::FileSupport::Base.new create_theme_files_for_directory_node(file_support.build_tree(Theme.base_layouts_views_path, :preload => true), :templates, :path_to_replace => Theme.base_layouts_views_path) create_theme_files_for_directory_node(file_support.build_tree(Theme.knitkit_website_stylesheets_path, :preload => true), :stylesheets, :path_to_replace => Theme.knitkit_website_stylesheets_path) + create_theme_files_for_directory_node(file_support.build_tree(Theme.knitkit_website_javascripts_path, :preload => true), :javascripts, :path_to_replace => Theme.knitkit_website_javascripts_path) create_theme_files_for_directory_node(file_support.build_tree(Theme.knitkit_website_images_path, :preload => true), :images, :path_to_replace => Theme.knitkit_website_images_path) end private @@ -223,20 +287,42 @@ child_node[:leaf] ? save_theme_file(child_node[:id], type, options) : create_theme_files_for_directory_node(child_node, type, options) end end def save_theme_file(path, type, options) - contents = IO.read(path) - contents.gsub!("../../images/knitkit","../images") unless path.scan('style.css').empty? - contents.gsub!("<%= static_stylesheet_link_tag('knitkit/style.css') %>","<%= theme_stylesheet_link_tag('#{self.theme_id}','style.css') %>") unless path.scan('base.html.erb').empty? + ignored_css = [ + 'bootstrap.min.css', + 'bootstrap-responsive.min.css', + 'datepicker.css', + 'inline_editing.css', + ] - path = case type - when :widgets - path.gsub(options[:path_to_replace], "#{self.url}/widgets/#{options[:widget_name]}") - else - path.gsub(options[:path_to_replace], "#{self.url}/#{type.to_s}") + ignored_js = [ + 'bootstrap.min.js', + 'bootstrap-datepicker.js', + 'confirm-bootstrap.js', + 'inline_editing.js', + 'jquery.maskedinput.min.js', + 'Main.js', + 'View.js' + ] + + ignored_files = (ignored_css | ignored_js).flatten + + unless ignored_files.any? { |w| path =~ /#{w}/ } + contents = IO.read(path) + contents.gsub!("<%= static_stylesheet_link_tag 'knitkit/custom.css' %>","<%= theme_stylesheet_link_tag '#{self.theme_id}','custom.css' %>") unless path.scan('base.html.erb').empty? + contents.gsub!("<%= static_javascript_include_tag 'knitkit/theme.js' %>","<%= theme_javascript_include_tag '#{self.theme_id}','theme.js' %>") unless path.scan('base.html.erb').empty? + + path = case type + when :widgets + path.gsub(options[:path_to_replace], "#{self.url}/widgets/#{options[:widget_name]}") + else + path.gsub(options[:path_to_replace], "#{self.url}/#{type.to_s}") + end + + self.add_file(contents, path) end - self.add_file(contents, path) end - end +