From 2f4128bf8fc09d43f21a3f3089efc0eeb80304f1 Mon Sep 17 00:00:00 2001 From: dinedine Date: Sun, 10 Oct 2010 16:37:34 +0200 Subject: [PATCH] refactoring theme assets in progress --- Gemfile | 3 +- Gemfile.lock | 10 +- .../admin/theme_assets_controller.rb | 8 +- app/models/theme_asset.rb | 107 ++++-------------- app/uploaders/asset_uploader.rb | 9 +- app/uploaders/theme_asset_uploader.rb | 60 +++++----- app/views/admin/theme_assets/_asset.html.haml | 14 --- app/views/admin/theme_assets/_form.html.haml | 22 ++-- app/views/admin/theme_assets/edit.html.haml | 2 +- config/locales/admin_ui_en.yml | 2 +- lib/locomotive/import/job.rb | 2 - lib/locomotive/import/site.rb | 2 +- lib/locomotive/mongoid/patches.rb | 14 ++- public/javascripts/admin/theme_assets.js | 2 +- 14 files changed, 103 insertions(+), 154 deletions(-) diff --git a/Gemfile b/Gemfile index 70092740..e0fa7b06 100644 --- a/Gemfile +++ b/Gemfile @@ -16,7 +16,8 @@ gem 'rmagick', '2.12.2' gem 'aws' gem 'mimetype-fu', :require => 'mimetype_fu' gem 'formtastic-rails3', '1.0.0.beta3', :require => 'formtastic' -gem 'carrierwave', '0.5.0.beta2' +# gem 'carrierwave', '0.5.0.beta2' +gem 'carrierwave', :path => '../gems/carrierwave' gem 'actionmailer-with-request', :require => 'actionmailer_with_request' gem 'heroku' gem 'httparty', '0.6.1' diff --git a/Gemfile.lock b/Gemfile.lock index d3bdcc37..3ba4f3b3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,6 +11,12 @@ GIT specs: liquid (2.1.3) +PATH + remote: /Users/didier/Desktop/NoCoffee/LocomotiveCMS/gems/carrierwave + specs: + carrierwave (0.5.0) + activesupport (~> 3.0) + GEM remote: http://rubygems.org/ specs: @@ -53,8 +59,6 @@ GEM bson (1.0.4) bson_ext (1.0.4) builder (2.1.2) - carrierwave (0.5.0.beta2) - activesupport (>= 3.0.0.beta4) cgi_multipart_eof_fix (2.5.0) configuration (1.1.0) crack (0.1.8) @@ -167,7 +171,7 @@ DEPENDENCIES actionmailer-with-request aws bson_ext (= 1.0.4) - carrierwave (= 0.5.0.beta2) + carrierwave! cgi_multipart_eof_fix custom_fields! delayed_job (= 2.1.0.pre2) diff --git a/app/controllers/admin/theme_assets_controller.rb b/app/controllers/admin/theme_assets_controller.rb index cbdbcd78..b2e6efda 100644 --- a/app/controllers/admin/theme_assets_controller.rb +++ b/app/controllers/admin/theme_assets_controller.rb @@ -12,9 +12,6 @@ module Admin def index @assets = current_site.theme_assets.visible.all.order_by([[:slug, :asc]]).group_by { |a| a.folder.split('/').first.to_sym } @js_and_css_assets = (@assets[:javascripts] || []) + (@assets[:stylesheets] || []) - # @non_image_assets = assets.find_all { |a| a.stylesheet? || a.javascript? } - # @image_assets = assets.find_all { |a| a.image? } - # @flash_assets = assets.find_all { |a| a.movie? } if request.xhr? render :action => 'images', :layout => false and return @@ -23,6 +20,11 @@ module Admin end end + def edit + resource.performing_plain_text = true if resource.stylesheet_or_javascript? + edit! + end + def create params[:theme_asset] = { :source => params[:file] } if params[:file] diff --git a/app/models/theme_asset.rb b/app/models/theme_asset.rb index 79c412a9..ff9146d3 100644 --- a/app/models/theme_asset.rb +++ b/app/models/theme_asset.rb @@ -8,7 +8,6 @@ class ThemeAsset field :width, :type => Integer field :height, :type => Integer field :size, :type => Integer - # field :plain_text field :folder, :default => nil field :hidden, :type => Boolean, :default => false mount_uploader :source, ThemeAssetUploader @@ -24,31 +23,25 @@ class ThemeAsset before_validation :store_plain_text before_validation :sanitize_folder before_validation :build_local_path - # before_validation :sanitize_slug - # before_validation :escape_shortcut_urls - # before_save :set_slug ## validations ## - # validate :extname_can_not_be_changed validates_presence_of :site, :source - # validate :plain_text_name_is_required_if_no_given_source - validates_presence_of :plain_text_name, :if => Proc.new { |a| a.performing_plain_text? } - # validates_presence_of :slug, :if => Proc.new { |a| a.new_record? && a.performing_plain_text? } - # validates_uniqueness_of :slug, :scope => [:site_id, :content_type, :folder] + validates_presence_of :plain_text_name, :if => Proc.new { |a| puts "===> performing_plain_text? = #{a.performing_plain_text?}"; a.performing_plain_text? } validates_uniqueness_of :local_path, :scope => :site_id validates_integrity_of :source + validate :content_type_can_not_changed ## named scopes ## scope :visible, :where => { :hidden => false } ## accessors ## - attr_accessor :plain_text_name, :plain_text, :performing_plain_text + attr_accessor :plain_text_name, :plain_text, :performing_plain_text #, :new_file ## methods ## # def source=(new_file) - # @new_source = true # super + # @new_source = true # end %w{movie image stylesheet javascript font}.each do |type| @@ -66,14 +59,6 @@ class ThemeAsset @plain_text_name ||= self.safe_source_filename end @plain_text_name.gsub(/(\.[a-z0-9A-Z]+)$/, '') rescue nil - # - # @plain_text_name ||= self.safe_source_filename rescue nil if - # - # if @plain_text_name_changed - # - # - # @plain_text_name_changed ? @plain_text_name : - # (@plain_text_name || self.safe_source_filename).gsub(/(\.[a-z0-9A-Z]+)$/, '') rescue nil end def plain_text_name=(name) @@ -82,30 +67,20 @@ class ThemeAsset end def plain_text - if not @plain_text_changed + if not self.performing_plain_text? #not @plain_text_changed @plain_text ||= self.source.read rescue nil end @plain_text - # # self.plain_text = self.source.read if read_attribute(:plain_text).blank? - # # read_attribute(:plain_text) - # else - # nil - # end end - def plain_text=(source) - # @new_source = true - @plain_text_changed = true - self.performing_plain_text = true #if self.performing_plain_text.nil? - @plain_text = source - # write_attribute(:plain_text, source) - end + # def plain_text=(source) + # # @plain_text_changed = true + # # self.performing_plain_text = true unless source.blank? + # @plain_text = source + # end def performing_plain_text? - Boolean.set(self.performing_plain_text) - # self.performing_plain_text == true || self.performing_plain_text == '1' || self.performing_plain_text == - # return true if !self.new_record? && self.stylesheet_or_javascript? && self.errors.empty? - # !(self.performing_plain_text.blank? || self.performing_plain_text == 'false' || self.performing_plain_text == false) + Boolean.set(self.performing_plain_text) || false end def store_plain_text @@ -113,28 +88,21 @@ class ThemeAsset sanitized_source = self.escape_shortcut_urls(self.plain_text) + # puts "================" + # puts self.source.instance_variable_get(:@original_file).inspect + self.source = CarrierWave::SanitizedFile.new({ :tempfile => StringIO.new(sanitized_source), :filename => "#{self.plain_text_name}.#{self.stylesheet? ? 'css' : 'js'}" }) end - # def shortcut_url # ex: /stylesheets/application.css is a shortcut for a theme asset (content_type => stylesheet, slug => 'application') - # File.join(self.content_type.pluralize, "#{self.slug}#{File.extname(self.source_filename)}") - # rescue - # '' - # end - def to_liquid { :url => self.source.url }.merge(self.attributes) end protected - # def new_source? - # @new_source == true - # end - def safe_source_filename self.source_filename || self.source.send(:original_filename) rescue nil end @@ -143,7 +111,7 @@ class ThemeAsset self.folder = self.content_type.pluralize if self.folder.blank? # no accents, no spaces, no leading and ending trails - self.folder = ActiveSupport::Inflector.transliterate(self.folder).gsub(/(\s)+/, '_').gsub(/^\//, '').gsub(/\/$/, '') + self.folder = ActiveSupport::Inflector.transliterate(self.folder).gsub(/(\s)+/, '_').gsub(/^\//, '').gsub(/\/$/, '').downcase # folder should begin by a root folder if (self.folder =~ /^(stylesheets|javascripts|images|media|fonts)/).nil? @@ -152,7 +120,7 @@ class ThemeAsset end def build_local_path - puts "self.source_filename = #{self.source_filename} / #{self.safe_source_filename} / #{File.join(self.folder, self.safe_source_filename)}" + # puts "self.source_filename = #{self.source_filename} / #{self.safe_source_filename} / #{File.join(self.folder, self.safe_source_filename)}" self.local_path = File.join(self.folder, self.safe_source_filename) end @@ -160,54 +128,19 @@ class ThemeAsset return if text.blank? text.gsub(/[("'](\/(stylesheets|javascripts|images|media)\/((.+)\/)*([a-z_\-0-9]+)\.[a-z]{2,3})[)"']/) do |path| - sanitized_path = path.gsub(/[("')]/, '').gsub(/^\//, '') - # puts "\t\t\tfound path = #{sanitized_path}" + sanitized_path = path.gsub(/[("')]/, '').gsub(/^\//, '') if asset = self.site.theme_assets.where(:local_path => sanitized_path).first "#{path.first}#{asset.source.url}#{path.last}" else path end - - # content_type, slug = url.split('/')[1..-1] - - # content_type = content_type.singularize - # slug = slug.split('.').first - - # if asset = self.site.theme_assets.where(:content_type => content_type, :slug => slug).first - # asset.source.url - # else - # url - # end end end - # def plain_text_name_is_required_if_no_given_source - # puts "@performing_plain_text = #{self.performing_plain_text?} / #{@plain_text_name} / new_file = #{self.source.inspect}" - # if self.performing_plain_text? && @plain_text_name.blank? - # self.errors.add(:plain_text_name, :blank) - # end - # end + def content_type_can_not_changed + self.errors.add(:source, :extname_changed) if !self.new_record? && self.content_type_changed? + end - # def sanitize_slug - # self.slug.parameterize! if self.slug.present? - # end - # - # def set_slug - # if self.slug.blank? - # self.slug = File.basename(self.source_filename, File.extname(self.source_filename)) - # self.sanitize_slug - # end - # end - # - # def extname_can_not_be_changed - # return if self.new_record? || self.source.file.original_filename.nil? - # - # puts "filename => #{self.source_filename} / source file => #{self.source.file.inspect}" - # - # if File.extname(self.source.file.original_filename) != File.extname(self.source_filename) - # self.errors.add(:source, :extname_changed) - # end - # end end diff --git a/app/uploaders/asset_uploader.rb b/app/uploaders/asset_uploader.rb index 5be936b7..baad1077 100644 --- a/app/uploaders/asset_uploader.rb +++ b/app/uploaders/asset_uploader.rb @@ -27,11 +27,12 @@ class AssetUploader < CarrierWave::Uploader::Base process :convert => 'png' end - process :set_content_type - process :set_size process :set_width_and_height - def set_content_type + after :cache, :set_size + after :cache, :set_content_type + + def set_content_type(*args) value = :other content_type = file.content_type == 'application/octet-stream' ? File.mime_type?(original_filename) : file.content_type @@ -48,7 +49,7 @@ class AssetUploader < CarrierWave::Uploader::Base model.content_type = value end - def set_size + def set_size(*args) model.size = file.size end diff --git a/app/uploaders/theme_asset_uploader.rb b/app/uploaders/theme_asset_uploader.rb index 58c9b2f9..d6a9b5b6 100644 --- a/app/uploaders/theme_asset_uploader.rb +++ b/app/uploaders/theme_asset_uploader.rb @@ -6,38 +6,46 @@ class ThemeAssetUploader < AssetUploader process :set_size process :set_width_and_height - # version :thumb do - # process :resize_to_fill => [50, 50] - # process :convert => 'png' - # end + # after :store, :foo # - # version :medium do - # process :resize_to_fill => [80, 80] - # process :convert => 'png' - # end + # def filename_was + # column = model.send(:_mounter, self.mounted_as).send(:serialization_column) + # model.send("#{column}_was") + # end # - # version :preview do - # process :resize_to_fit => [880, 1100] - # process :convert => 'png' - # end + # def filename_changed? + # column = model.send(:_mounter, self.mounted_as).send(:serialization_column) + # model.send("#{column}_changed?") + # end def store_dir - File.join('sites', model.site_id.to_s, 'theme', model.folder) - # File.join('sites', model.site_id.to_s, 'theme', model.content_type.pluralize, model.subfolder || '') - - # base = File.join('sites', model.site_id.to_s, 'theme') - # # puts "base = #{base} / #{model.subfolder.inspect}" - # if model.subfolder.blank? - # File.join(base, model.content_type.pluralize) - # else - # File.join(base, model.subfolder) - # end - - # - # # "sites/#{model.site_id}/themes/#{model.id}/#{model.content_type.pluralize}" - # File.join("sites/#{model.site_id}/theme", model.subfolder || '', model.content_type.pluralize) + File.join('sites', model.site_id.to_s, 'theme', model.folder_was || model.folder) end + # def store_dir_was + # File.join('sites', model.site_id.to_s, 'theme', model.folder_was) + # end + # + def stale_model? + !model.new_record? && model.folder_changed? + end + # + # def store_dir_was + # File.join('sites', model.site_id.to_s, 'theme', model.folder_was) rescue nil + # end + # + # def store_dir_changed? + # self.store_dir_was && self.store_dir_was != self.store_dir + # end + # + # def store_path_was(for_file=filename) + # File.join([store_dir_was, full_filename(for_file)].compact) + # end + # + # def foo + # "store_dir_was = #{store_dir_was.inspect} / #{store_path_was}" + # end + def extension_white_list %w(jpg jpeg gif png css js swf flv eot svg ttf woff) end diff --git a/app/views/admin/theme_assets/_asset.html.haml b/app/views/admin/theme_assets/_asset.html.haml index 87b24fce..1d079af1 100644 --- a/app/views/admin/theme_assets/_asset.html.haml +++ b/app/views/admin/theme_assets/_asset.html.haml @@ -1,17 +1,3 @@ -/ - per_row = local_assigns[:per_row] || 6 -/ - asset_counter = local_assigns[:asset_counter] || 0 -/ - edit = local_assigns.key?(:edit) ? edit : true -/ -/ %li{ :class => "#{asset.new_record? ? 'new-asset' : 'asset'} #{'last' if (asset_counter + 1) % per_row == 0}"} -/ %h4= link_to truncate(asset.slug, :length => 18), edit ? edit_admin_theme_asset_path(asset) : asset.source.url, :"data-slug" => asset.slug, :"data-shortcut-url" => asset.shortcut_url -/ .image -/ .inside -/ = vignette_tag(asset) -/ - if edit -/ .actions -/ = link_to image_tag('admin/list/icons/cross.png'), admin_theme_asset_path(asset), :class => 'remove', :confirm => t('admin.messages.confirm'), :method => :delete - - %li %em %strong= link_to asset.local_path, edit_admin_theme_asset_path(asset) diff --git a/app/views/admin/theme_assets/_form.html.haml b/app/views/admin/theme_assets/_form.html.haml index 1f1f5a5e..839cf537 100644 --- a/app/views/admin/theme_assets/_form.html.haml +++ b/app/views/admin/theme_assets/_form.html.haml @@ -7,7 +7,6 @@ #file-selector{ :class => "selector #{'hidden' if @theme_asset.performing_plain_text?}" } = f.inputs :name => :information do = f.input :source - = f.input :slug - if @theme_asset.new_record? || @theme_asset.stylesheet_or_javascript? %span.alt @@ -18,10 +17,10 @@ = f.inputs :name => :code, :class => 'inputs code' do - if @theme_asset.new_record? - = f.input :slug + = f.input :plain_text_name = f.custom_input :content_type do - = f.select :content_type, ["stylesheet", "javascript"] + = f.select :content_type, %w(stylesheet javascript) = f.custom_input :plain_text, :css => 'full', :with_label => false do %code{ :class => (@theme_asset.size && @theme_asset.size > 40000 ? 'nude' : (@theme_asset.content_type || 'stylesheet')) } @@ -32,9 +31,14 @@ %span.alt != t('admin.theme_assets.form.choose_file') -- if @theme_asset.image? - = f.foldable_inputs :name => "#{t('formtastic.titles.preview')} #{image_dimensions_and_size(@theme_asset)}", :class => 'preview' do - %li - .image - .inside - = image_tag(@theme_asset.source.url(:preview)) +/ - if @theme_asset.image? +/ = f.foldable_inputs :name => "#{t('formtastic.titles.preview')} #{image_dimensions_and_size(@theme_asset)}", :class => 'preview' do +/ %li +/ .image +/ .inside +/ = image_tag(@theme_asset.source.url(:preview)) + += f.foldable_inputs :name => :options do + = f.input :folder + = f.custom_input :hidden, :css => 'toggle' do + = f.check_box :hidden diff --git a/app/views/admin/theme_assets/edit.html.haml b/app/views/admin/theme_assets/edit.html.haml index dbb500c0..8b8e7cf4 100644 --- a/app/views/admin/theme_assets/edit.html.haml +++ b/app/views/admin/theme_assets/edit.html.haml @@ -6,7 +6,7 @@ - content_for :buttons do = admin_button_tag t('admin.theme_assets.index.new'), new_admin_theme_asset_url, :class => 'new' -%p!= t('.help', :url => @theme_asset.source.url, :shortcut_url => @theme_asset.shortcut_url) +%p!= t('.help', :url => @theme_asset.source.url) = semantic_form_for @theme_asset, :url => admin_theme_asset_url(@theme_asset), :html => { :multipart => true, :class => 'save-with-shortcut' } do |form| diff --git a/config/locales/admin_ui_en.yml b/config/locales/admin_ui_en.yml index 6778d5ea..c0f3634c 100644 --- a/config/locales/admin_ui_en.yml +++ b/config/locales/admin_ui_en.yml @@ -173,7 +173,7 @@ en: help: "You have the choice to either upload any file or to copy/paste a stylesheet or a javascript in plain text." edit: title: "Editing %{file}" - help: "You can insert the following shortcut url in your stylesheets: %{shortcut_url} OR use the direct url %{url}" + help: "This asset is accessible from the following url: %{url}" form: picker_link: Insert a file into the code choose_file: Choose file diff --git a/lib/locomotive/import/job.rb b/lib/locomotive/import/job.rb index d7344b46..19fc7c77 100644 --- a/lib/locomotive/import/job.rb +++ b/lib/locomotive/import/job.rb @@ -4,8 +4,6 @@ module Locomotive module Import class Job - # attr_accessor :theme_file, :site, :enabled - def initialize(theme_file, site = nil, enabled = {}) raise "Theme zipfile not found" unless File.exists?(theme_file) diff --git a/lib/locomotive/import/site.rb b/lib/locomotive/import/site.rb index d6ad7405..1236c765 100644 --- a/lib/locomotive/import/site.rb +++ b/lib/locomotive/import/site.rb @@ -5,7 +5,7 @@ module Locomotive def self.process(context) site, database = context[:site], context[:database] - attributes = database['site'].clone.delete_if { |name, value| %w{pages content_types asset_collections}.include?(name) } + attributes = database['site'].clone.delete_if { |name, value| %w{pages assets content_types asset_collections}.include?(name) } site.attributes = attributes diff --git a/lib/locomotive/mongoid/patches.rb b/lib/locomotive/mongoid/patches.rb index 67614da1..155ef1f8 100644 --- a/lib/locomotive/mongoid/patches.rb +++ b/lib/locomotive/mongoid/patches.rb @@ -1,8 +1,8 @@ require 'mongoid' +# require 'mongoid/document' ## various patches module Mongoid #:nodoc: - module Document def update_child_with_noname(child, clear = false) @@ -13,9 +13,21 @@ module Mongoid #:nodoc: alias_method_chain :update_child, :noname + # module ClassMethods + # + # def instantiate(attrs = nil, allocating = false) # used by carrierwave to back up the original file + # document = super + # document.send(:run_callbacks, :initialize) do + # document + # end + # end + # + # end + end end + module Mongoid #:nodoc: module Validations #:nodoc: class AssociatedValidator < ActiveModel::EachValidator diff --git a/public/javascripts/admin/theme_assets.js b/public/javascripts/admin/theme_assets.js index af533c37..0c65c4b2 100644 --- a/public/javascripts/admin/theme_assets.js +++ b/public/javascripts/admin/theme_assets.js @@ -37,7 +37,7 @@ $(document).ready(function() { $('a#image-picker-link').imagepicker({ insertFn: function(link) { - return link.attr('data-shortcut-url'); + return link.attr('data-url'); } }); });