From 2e86d8081fbecf87b621a5fa7e378196e360c84a Mon Sep 17 00:00:00 2001 From: chris2tof Date: Tue, 18 Oct 2011 11:21:24 +0800 Subject: [PATCH] Modifications for images in html code --- .gitignore | 2 + Gemfile | 4 +- Gemfile.lock | 125 ++++++++++++++++---------------- app/models/design/design.rb | 7 +- app/models/design/image.rb | 8 +- app/models/design/layout.rb | 36 --------- app/models/page.rb | 2 + app/uploaders/asset_uploader.rb | 2 +- app/uploaders/image_uploader.rb | 22 +++--- lib/parser.rb | 20 ++++- 10 files changed, 110 insertions(+), 118 deletions(-) diff --git a/.gitignore b/.gitignore index dde823ca..71494e30 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ uploads/**/* public/javascripts public/stylesheets config/application.rb + +.rvmrc diff --git a/Gemfile b/Gemfile index 1e6ce8fc..6b84c911 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source 'http://rubygems.org' -gem 'rails', '>=3.1.0.rc4' +gem 'rails' gem 'yajl-ruby', :require => 'yajl' gem 'bson_ext' @@ -16,7 +16,7 @@ gem 'mongoid' gem 'rake', '>=0.9.2' gem 'jquery-rails', '>= 1.0.3' -gem 'sprockets', '2.0.0.beta.13' +gem 'sprockets' # Gems used only for assets and not required # in production environments by default. diff --git a/Gemfile.lock b/Gemfile.lock index 338256e6..d36a30ac 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,50 +1,50 @@ GEM remote: http://rubygems.org/ specs: - actionmailer (3.1.0.rc6) - actionpack (= 3.1.0.rc6) + actionmailer (3.1.0) + actionpack (= 3.1.0) mail (~> 2.3.0) - actionpack (3.1.0.rc6) - activemodel (= 3.1.0.rc6) - activesupport (= 3.1.0.rc6) + actionpack (3.1.0) + activemodel (= 3.1.0) + activesupport (= 3.1.0) builder (~> 3.0.0) erubis (~> 2.7.0) i18n (~> 0.6) rack (~> 1.3.2) - rack-cache (~> 1.0.2) - rack-mount (~> 0.8.1) - rack-test (~> 0.6.0) - sprockets (~> 2.0.0.beta.12) - activemodel (3.1.0.rc6) - activesupport (= 3.1.0.rc6) - bcrypt-ruby (~> 2.1.4) + rack-cache (~> 1.0.3) + rack-mount (~> 0.8.2) + rack-test (~> 0.6.1) + sprockets (~> 2.0.0) + activemodel (3.1.0) + activesupport (= 3.1.0) + bcrypt-ruby (~> 3.0.0) builder (~> 3.0.0) i18n (~> 0.6) - activerecord (3.1.0.rc6) - activemodel (= 3.1.0.rc6) - activesupport (= 3.1.0.rc6) + activerecord (3.1.0) + activemodel (= 3.1.0) + activesupport (= 3.1.0) arel (~> 2.2.1) tzinfo (~> 0.3.29) - activeresource (3.1.0.rc6) - activemodel (= 3.1.0.rc6) - activesupport (= 3.1.0.rc6) - activesupport (3.1.0.rc6) + activeresource (3.1.0) + activemodel (= 3.1.0) + activesupport (= 3.1.0) + activesupport (3.1.0) multi_json (~> 1.0) archive-tar-minitar (0.5.2) arel (2.2.1) - bcrypt-ruby (2.1.4) - bson (1.3.1) - bson_ext (1.3.1) + bcrypt-ruby (3.0.1) + bson (1.4.0) + bson_ext (1.4.0) builder (3.0.0) carrierwave (0.5.4) activesupport (~> 3.0) - chronic (0.6.2) - ckeditor (3.6.1) + chronic (0.6.4) + ckeditor (3.6.2) mime-types (~> 1.16) orm_adapter (~> 0.0.5) - coffee-rails (3.1.0.rc.6) + coffee-rails (3.1.1) coffee-script (>= 2.2.0) - railties (~> 3.1.0.rc1) + railties (~> 3.1.0) coffee-script (2.2.0) coffee-script-source execjs @@ -52,21 +52,22 @@ GEM columnize (0.3.4) delorean (1.1.0) chronic - devise (1.4.2) - bcrypt-ruby (~> 2.1.2) + devise (1.4.7) + bcrypt-ruby (~> 3.0) orm_adapter (~> 0.0.3) warden (~> 1.0.3) - diff-lcs (1.1.2) + diff-lcs (1.1.3) erubis (2.7.0) - execjs (1.2.4) + execjs (1.2.9) multi_json (~> 1.0) - factory_girl (2.0.4) - factory_girl_rails (1.1.0) - factory_girl (~> 2.0.0) + factory_girl (2.1.2) + activesupport + factory_girl_rails (1.2.0) + factory_girl (~> 2.1.0) railties (>= 3.0.0) hike (1.2.1) i18n (0.6.0) - jquery-rails (1.0.13) + jquery-rails (1.0.14) railties (~> 3.0) thor (~> 0.14) linecache19 (0.5.12) @@ -80,40 +81,40 @@ GEM subexec (~> 0.1.0) mongo (1.3.1) bson (>= 1.3.1) - mongoid (2.2.0) + mongoid (2.2.1) activemodel (~> 3.0) - mongo (~> 1.3) + mongo (>= 1.3, < 1.4) tzinfo (~> 0.3.22) multi_json (1.0.3) orm_adapter (0.0.5) polyglot (0.3.2) - rack (1.3.2) - rack-cache (1.0.2) + rack (1.3.3) + rack-cache (1.0.3) rack (>= 0.4) - rack-mount (0.8.2) + rack-mount (0.8.3) rack (>= 1.0.0) rack-ssl (1.3.2) rack rack-test (0.6.1) rack (>= 1.0) - rails (3.1.0.rc6) - actionmailer (= 3.1.0.rc6) - actionpack (= 3.1.0.rc6) - activerecord (= 3.1.0.rc6) - activeresource (= 3.1.0.rc6) - activesupport (= 3.1.0.rc6) + rails (3.1.0) + actionmailer (= 3.1.0) + actionpack (= 3.1.0) + activerecord (= 3.1.0) + activeresource (= 3.1.0) + activesupport (= 3.1.0) bundler (~> 1.0) - railties (= 3.1.0.rc6) - railties (3.1.0.rc6) - actionpack (= 3.1.0.rc6) - activesupport (= 3.1.0.rc6) + railties (= 3.1.0) + railties (3.1.0) + actionpack (= 3.1.0) + activesupport (= 3.1.0) rack-ssl (~> 1.3.2) rake (>= 0.8.7) rdoc (~> 3.4) thor (~> 0.14.6) rake (0.9.2) rcov (0.9.10) - rdoc (3.9.2) + rdoc (3.9.4) rspec (2.6.0) rspec-core (~> 2.6.0) rspec-expectations (~> 2.6.0) @@ -139,32 +140,34 @@ GEM archive-tar-minitar (>= 0.5.2) rubyzip (0.9.4) sass (3.1.7) - sass-rails (3.1.0.rc.6) - actionpack (~> 3.1.0.rc6) - railties (~> 3.1.0.rc6) + sass-rails (3.1.3) + actionpack (~> 3.1.0) + railties (~> 3.1.0) sass (>= 3.1.4) + sprockets (~> 2.0.0) + tilt (~> 1.3.2) shoulda-matchers (1.0.0.beta3) sinatra (1.2.6) rack (~> 1.1) - tilt (< 2.0, >= 1.2.2) - sprockets (2.0.0.beta.13) + tilt (>= 1.2.2, < 2.0) + sprockets (2.0.0) hike (~> 1.2) rack (~> 1.0) - tilt (!= 1.3.0, ~> 1.1) + tilt (~> 1.1, != 1.3.0) subexec (0.1.0) thor (0.14.6) - tilt (1.3.2) + tilt (1.3.3) treetop (1.4.10) polyglot polyglot (>= 0.3.1) tzinfo (0.3.29) - uglifier (1.0.1) + uglifier (1.0.3) execjs (>= 0.3.0) multi_json (>= 1.0.2) warden (1.0.5) rack (>= 1.0) watchr (0.7) - yajl-ruby (0.8.3) + yajl-ruby (1.0.0) PLATFORMS ruby @@ -180,7 +183,7 @@ DEPENDENCIES jquery-rails (>= 1.0.3) mini_magick mongoid - rails (>= 3.1.0.rc4) + rails rake (>= 0.9.2) rcov rspec (~> 2.0) @@ -190,7 +193,7 @@ DEPENDENCIES sass-rails (~> 3.1.0.rc) shoulda-matchers sinatra - sprockets (= 2.0.0.beta.13) + sprockets uglifier watchr yajl-ruby diff --git a/app/models/design/design.rb b/app/models/design/design.rb index 7d7fac73..f1f676b6 100644 --- a/app/models/design/design.rb +++ b/app/models/design/design.rb @@ -16,6 +16,7 @@ class Design embeds_many :themes embeds_many :javascripts embeds_many :images + embeds_many :custom_images, as: :design_image validates_presence_of :title validates_presence_of :author @@ -75,7 +76,6 @@ class Design if self.layout.to_save self.layout.to_save = false self.layout.save - self.layout.parse_urls end [[self.default_css], [self.reset_css], self.themes].each do |objects| objects.each do |object| @@ -91,11 +91,12 @@ class Design def process_object(object) if object.to_save if object.file_filename.nil? - new_object = self.send(object._type.downcase.pluralize).build - new_object.file = object.file + new_object = self.send(object._type.downcase.pluralize).build(object) object.destroy + new_object.to_save = false new_object.save else + object.to_save = false object.save end end diff --git a/app/models/design/image.rb b/app/models/design/image.rb index c0c4881d..939095e8 100644 --- a/app/models/design/image.rb +++ b/app/models/design/image.rb @@ -1,5 +1,11 @@ class Image < DesignFile - embedded_in :design + + field :in_css, :type => Boolean + field :name + field :html_id + field :html_class + + embedded_in :design_image, polymorphic: true mount_uploader :file, ImageUploader end diff --git a/app/models/design/layout.rb b/app/models/design/layout.rb index e27ca0bb..7ae4ceef 100644 --- a/app/models/design/layout.rb +++ b/app/models/design/layout.rb @@ -1,8 +1,6 @@ class Layout < DesignFile include Parser - mount_uploader :file_orig, AssetUploader - attr_reader :content embeds_one :menu @@ -23,38 +21,4 @@ class Layout < DesignFile parse_layout_contents(self) end - def parse_urls - orig_content = content = self.content - self.remove_file! - self.remove_file_orig! - names = [] - images = self.design.images - html_name = nil - content.scan(/(?<=\)/){ - $1.gsub(' ','').scan(/(?<=src=\")(.*?)(?=\")/){ - html_name = $1 - } - name = File.basename(html_name).gsub(/[\\\"]/, '') - file_name = images.detect{ |i| i.file_filename.eql?(name) }.file_url rescue nil - names << [html_name, file_name] - } - names.each do |name| - content.gsub!(name[0], name[1]) rescue nil - end - Dir.mktmpdir('f_path') { |dir| - orig_file_name = self.file_filename - - temp_file = File.new(dir + '/' + orig_file_name, 'w+') - temp_file.write orig_content.force_encoding("UTF-8") - self.file_orig = temp_file - - temp_file = File.new(dir + '/' + orig_file_name, 'w+') - temp_file.write content.force_encoding("UTF-8") - self.file = temp_file - - Layout.without_callback(:save, :after, :parse_layout) do - self.save - end - } - end end diff --git a/app/models/page.rb b/app/models/page.rb index 54564cc7..dad89219 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -6,6 +6,8 @@ class Page < Item belongs_to :design has_many :page_parts + embeds_many :custom_images, as: :design_image + def create_parts page_design = self.design page_design.layout.layout_parts.each do |p| diff --git a/app/uploaders/asset_uploader.rb b/app/uploaders/asset_uploader.rb index 170a8749..1e07bca6 100644 --- a/app/uploaders/asset_uploader.rb +++ b/app/uploaders/asset_uploader.rb @@ -6,7 +6,7 @@ class AssetUploader < CarrierWave::Uploader::Base 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 + 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 diff --git a/app/uploaders/image_uploader.rb b/app/uploaders/image_uploader.rb index c00fdcb3..29a4d92b 100644 --- a/app/uploaders/image_uploader.rb +++ b/app/uploaders/image_uploader.rb @@ -12,7 +12,7 @@ class ImageUploader < CarrierWave::Uploader::Base include CarrierWave::MiniMagick 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 + 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 @@ -58,14 +58,14 @@ class ImageUploader < CarrierWave::Uploader::Base # "something.jpg" if original_filename # end -# def manipulate! -# raise current_path.inspect -# image = ::MiniMagick::Image.open(current_path) -# image = yield(image) -# image.write(current_path) -# ::MiniMagick::Image.open(current_path) -# rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e -# raise CarrierWave::ProcessingError.new("Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: #{e}") -# end - + def manipulate! + cache_stored_file! if !cached? + image = ::MiniMagick::Image.open(current_path) + image = yield(image) + image.write(current_path) + ::MiniMagick::Image.open(current_path) + rescue + nil + end + end diff --git a/lib/parser.rb b/lib/parser.rb index daf93e2b..378e877c 100644 --- a/lib/parser.rb +++ b/lib/parser.rb @@ -164,9 +164,11 @@ module Parser res end c.define_tag 'image' do |tag| - asset = Asset.find(tag.attr['id']) - if asset - res = " tag.attr['name'], :editable => true) end + c.define_tag 'image' do |tag| + image = layout.design.images.detect{ |i| i.file_filename.eql?(parse_html_image(tag.expand)) } + image.update_attributes(:name => tag.attr['name'], :html_id => tag.attr['id'], :html_class => tag.attr['class']) + end end end + + def parse_html_image(html) + html.scan(/(?<=\)/){ + $1.gsub(' ','').scan(/(?<=src=\")(.*?)(?=\")/){ + return File.basename($1).gsub(/[\\\"]/, '') + } + } + end def self.included(base) base.send :helper_method, :parse_page if base.respond_to? :helper_method