app/models/theme.rb in knitkit-2.0.0 vs app/models/theme.rb in knitkit-2.0.1

- old
+ new

@@ -13,11 +13,11 @@ def root_dir @@root_dir ||= "#{Rails.root}/public" end def base_dir(website) - "#{root_dir}/sites/site-#{website.id}/themes" + "#{root_dir}/sites/#{website.iid}/themes" end def import(file, website) name_and_id = file.original_filename.to_s.gsub(/(^.*(\\|\/))|(\.zip$)/, '') theme_name = name_and_id.split('[').first @@ -27,12 +27,11 @@ theme.import(file) end end def make_tmp_dir - random = Time.now.to_i.to_s.split('').sort_by { rand } - Pathname.new(Rails.root.to_s + "/tmp/themes/tmp_#{random}/").tap do |dir| + Pathname.new(Rails.root.to_s + "/tmp/themes/tmp_#{Time.now.to_i.to_s}/").tap do |dir| FileUtils.mkdir_p(dir) unless dir.exist? end end def valid_theme?(file) @@ -61,11 +60,11 @@ def path "#{self.class.base_dir(website)}/#{theme_id}" end def url - "sites/site-#{website.id}/themes/#{theme_id}" + "/public/sites/#{website.iid}/themes/#{theme_id}" end def activate! update_attributes! :active => true end @@ -73,41 +72,38 @@ def deactivate! update_attributes! :active => false end def themed_widgets - widgets = [] - ErpApp::Widgets::Base.installed_widgets.each do |widget_name| - widgets << widget_name unless self.files.where("directory like '/#{File.join(self.url, 'widgets', widget_name)}%'").empty? - end - widgets + Rails.application.config.erp_app.widgets.select do |widget_hash| + !(self.files.where("directory like '#{File.join(self.url, 'widgets', widget_hash[:name])}%'").all.empty?) + end.collect{|item| item[:name]} end def non_themed_widgets already_themed_widgets = self.themed_widgets - widgets = [] - ErpApp::Widgets::Base.installed_widgets.each do |widget_name| - widgets << widget_name unless already_themed_widgets.include?(widget_name) - end - widgets + Rails.application.config.erp_app.widgets.select do |widget_hash| + !already_themed_widgets.include?(widget_hash[:name]) + end.collect{|item| item[:name]} end def create_layouts_for_widget(widget) - file_support = ErpTechSvcs::FileSupport::Base.new - views_location = "::Widgets::#{widget.camelize}::Base".constantize.views_location - create_theme_files_for_directory_node(file_support.build_tree(views_location, :preload => true), :widgets, :path_to_replace => views_location, :widget_name => widget) + widget_hash = Rails.application.config.erp_app.widgets.find{|item| item[:name] == widget} + widget_hash[:view_files].each do |view_file| + save_theme_file(view_file[:path], :widgets, {:path_to_replace => view_file[:path].split('/views')[0], :widget_name => widget}) + end end def about %w(name author version homepage summary).inject({}) do |result, key| result[key] = send(key) result end end def import(file) - file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::FileSupport.options[:storage]) + file_support = ErpTechSvcs::FileSupport::Base.new(:storage => Rails.application.config.erp_tech_svcs.file_storage) file = ActionController::UploadedTempfile.new("uploaded-theme").tap do |f| f.puts file.read f.original_filename = file.original_filename f.read # no idea why we need this here, otherwise the zip can't be opened end unless file.path @@ -131,34 +127,34 @@ name = entry.name.sub(/__MACOSX\//, '') name = Theme.strip_path(entry.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 + 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 + 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 => ErpTechSvcs::FileSupport.options[:storage]) + file_support = ErpTechSvcs::FileSupport::Base.new(:storage => Rails.application.config.erp_tech_svcs.file_storage) tmp_dir = Theme.make_tmp_dir (tmp_dir + "#{name}[#{theme_id}].zip").tap do |file_name| file_name.unlink if file_name.exist? Zip::ZipFile.open(file_name, Zip::ZipFile::CREATE) do |zip| files.each {|file| contents = file_support.get_contents(File.join(file_support.root,file.directory,file.name)) - relative_path = file.directory.sub("/#{url}",'') + relative_path = file.directory.sub("#{url}",'') path = FileUtils.mkdir_p(File.join(tmp_dir,relative_path)) full_path = File.join(path,file.name) - File.open(full_path, 'w+') {|f| f.puts(contents) } + File.open(full_path, 'w+:ASCII-8BIT') {|f| f.puts(contents) } zip.add(File.join(relative_path[1..relative_path.length],file.name), full_path) if ::File.exists?(full_path) } ::File.open(tmp_dir + 'about.yml', 'w') { |f| f.puts(about.to_yaml) } zip.add('about.yml', tmp_dir + 'about.yml') end @@ -200,11 +196,11 @@ file_name.sub(path, '') end end def delete_theme_files! - file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::FileSupport.options[:storage]) + file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::Config.file_storage) file_support.delete_file(File.join(file_support.root,self.url), :force => true) end def create_theme_files! file_support = ErpTechSvcs::FileSupport::Base.new @@ -226,12 +222,12 @@ 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? path = case type when :widgets - path.gsub(options[:path_to_replace], "/#{self.url}/widgets/#{options[:widget_name]}") + path.gsub(options[:path_to_replace], "#{self.url}/widgets/#{options[:widget_name]}") else - path.gsub(options[:path_to_replace], "/#{self.url}/#{type.to_s}") + path.gsub(options[:path_to_replace], "#{self.url}/#{type.to_s}") end self.add_file(contents, path) end