update for desktop widgets

This commit is contained in:
Harry Bomrah authored and chris committed 2012-11-05 11:40:11 +08:00
1 parent 2e809c5c19
commit ca3e06c58b
37 files changed
+284 -179

No files matched your search

+1
View File
@@ -1,5 +1,6 @@
class CssDefault < Stylesheet
belongs_to :design
belongs_to :desktop_widget
end
+1
View File
@@ -11,6 +11,7 @@ class Image
mount_uploader :file, ImageUploader
belongs_to :design
belongs_to :desktop_widget
before_save :set_name
+1
View File
@@ -1,3 +1,4 @@
class Javascript < DesignFile
belongs_to :design
belongs_to :desktop_widget
end
+31
View File
@@ -1,5 +1,6 @@
class Stylesheet < DesignFile
belongs_to :design
belongs_to :desktop_widget
mount_uploader :file_orig, AssetUploader
def parse_urls
@@ -32,5 +33,35 @@ class Stylesheet < DesignFile
self.save
}
end
def parse_widgets_urls
orig_content = content = self.file.read.force_encoding("UTF-8")
# self.remove_file!
# self.remove_file_orig!
names = []
images = self.desktop_widget.images
content.scan(/(?<=url)(.*?)(?=\))/){
css_name = $1.gsub(' ','').gsub('(','')
name = File.basename(css_name).gsub(/[\\\"]/, '')
image = images.detect{ |i| i.file_identifier.eql?(name) } rescue nil
image.update_attribute(:in_css, true) if image
file_name = image.file_url rescue nil
names << [css_name, file_name]
}
names.each do |name|
content.gsub!(name[0], name[1]) if name[1]
end
Dir.mktmpdir('f_path') { |dir|
orig_file_name = self.file_identifier
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
self.save
}
end
end
@@ -9,7 +9,7 @@ class Desktop
belongs_to :user
has_many :sections, :autosave => true, :dependent => :destroy
has_many :desktop_widgets, :autosave => true, :dependent => :destroy
# has_many :desktop_widgets, :autosave => true, :dependent => :destroy
before_create :initialize_section
File renamed without changes.
+31
View File
@@ -0,0 +1,31 @@
class DesktopWidget
include Mongoid::Document
include Mongoid::Timestamps
include ParserLayoutWidget
field :name
field :author
field :shape
field :version, :type => String
has_one :css_default, :autosave => true, :dependent => :destroy
has_one :widget_layout, :autosave => true, :dependent => :destroy
has_many :images, :autosave => true, :dependent => :destroy
has_many :javascripts, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :images, :allow_destroy => true
accepts_nested_attributes_for :javascripts, :allow_destroy => true
after_save :parse_css_for_images
# belongs_to :desktop
protected
def parse_css_for_images
self.css_default.parse_widgets_urls
parse_widget_for_images(self)
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -9,6 +9,5 @@ class Tile
field :title
belongs_to :group
end
+18
View File
@@ -0,0 +1,18 @@
class WidgetLayout < DesignFile
attr_reader :content
field :body
belongs_to :desktop_widget
def content
self.file.read.force_encoding("UTF-8") rescue ''
end
def self.exist_one?
WidgetLayout.count > 0
end
end
-13
View File
@@ -1,13 +0,0 @@
class DesktopWidget
include Mongoid::Document
include Mongoid::Timestamps
field :name
field :author
field :shape
field :desktop_id
field :status
field :section
belongs_to :desktop
end