Clean code and workaround mongoid embedded with carrierwave

This commit is contained in:
chris2tof 2011-09-08 18:12:04 +08:00
parent a1391493a3
commit a571d1dcff
7 changed files with 55 additions and 66 deletions

View File

@ -80,7 +80,7 @@ GEM
subexec (~> 0.1.0)
mongo (1.3.1)
bson (>= 1.3.1)
mongoid (2.1.9)
mongoid (2.2.0)
activemodel (~> 3.0)
mongo (~> 1.3)
tzinfo (~> 0.3.22)

View File

@ -59,9 +59,9 @@ $(".r_snapshot").live({
);
$('input.multi_files').live("change", function(){
$(this).parent().prev("ul").append("<li>"+$(this).val()+"</li>");
new_node =$(this).parent().clone();
$(this).css("display","none");
$(this).parent().prev("ul").append("<li>" + $(this).val() + "</li>");
new_node = $(this).parent().clone();
$(this).parent().css("display","none");
new_node.children('input.multi_files').val("");
$(this).parent().before(new_node);
});
@ -69,8 +69,8 @@ $('input.multi_files').live("change", function(){
$('a.remove_mark').live("click",function(){
$(this).prev("input").val("true");
$(this).parent().css("text-decoration","line-through")
$(this).parent("li").removeClass("r_snapshot");
$(this).next("span").remove();
$(this).parent().removeClass("r_snapshot");
$(this).next().remove();
$(this).remove();
return false;
});

View File

@ -32,7 +32,6 @@ class Admin::DesignsController < ApplicationController
end
def update
params[:design][:existing_task_attributes] ||= {}
@design = Design.find(params[:id])
if @design.update_attributes(params[:design])
flash[:notice] = "Successfully updated design and tasks."

View File

@ -38,16 +38,14 @@ class Design
# Update or create the attribute_model records
def files=(attrs)
case attrs.last
when 'layout'
files = self.layout.build
else
files = eval(attrs.last)
when 'layout'
files = self.layout.build
else
files = eval(attrs.last)
end
attrs[0].each do |a|
if(a[:id]=='' && !a[:file].nil? )
if(a[:file]!='')
files.build(:file => a[:file], :to_save => true)
end
if a[:id].blank? && !a[:file].blank?
files.build(:file => a[:file], :to_save => true)
else
files.each do |file|
if file.id.to_s == a[:id]
@ -75,6 +73,7 @@ class Design
end
end
if self.layout.to_save
self.layout.to_save = false
self.layout.save
self.layout.parse_urls
end
@ -86,12 +85,11 @@ class Design
end
def process_object(object)
if object.file.blank?
object.to_save = false
end
if object.to_save
object.to_save = false
object.save
new_object = self.send(object._type.downcase.pluralize).build
new_object.file = object.file
object.destroy
new_object.save
end
if object.to_destroy
object.destroy

View File

@ -39,7 +39,7 @@ class Layout < DesignFile
names << [html_name, file_name]
}
names.each do |name|
content.gsub!(name[0], name[1])
content.gsub!(name[0], name[1]) rescue nil
end
Dir.mktmpdir('f_path') { |dir|
orig_file_name = self.file_filename

View File

@ -1,34 +1,24 @@
<% item_destroy ||= false %>
<% item_snapshot ||= false %>
<% item_editable ||= false %>
<% li_class = ''%>
<% fieldname_p = fieldname.pluralize %>
<%= f.label "fieldname", t('admin.'+fieldname) %>
<%= f.label "field_name", t('admin.' + field_name) %>
<ul>
<% object.send(fieldname_p).each do |t| %>
<% li_class += 'r_destroy ' if item_destroy %>
<% li_class += 'r_snapshot ' if item_snapshot %>
<% li_class += 'r_edit ' if item_editable %>
<li class="<%= li_class %>" <%= "path='#{t.file.url}' filename='#{t.file_filename}'" if (item_snapshot || item_editable) %> >
<% object.send(field_name).each do |t| %>
<li class="<%= classes %>" <%="path='#{t.file.url}' filename='#{t.file_filename}'" %> >
<%= t.file_filename %>
<% if item_destroy %>
<%=fields_for "design["+fieldname_p+"][]",t,:index=>nil do |f| %>
<% if classes.include?('r_destroy') %>
<%= fields_for "design[" + field_name + "][]", t, :index => nil do |f| %>
<%= f.hidden_field :id %>
<%= f.hidden_field :file %>
<%= f.hidden_field :to_destroy %>
<%end%>
<% end %>
<a class="remove_mark" href="#"><%= t('Delete') %></a>
<%end%>
<% if item_editable %>
<% end %>
<% if classes.include?('r_editable') %>
<a class="edit_mark" href="#"><%= t('Edit') %></a>
<% end %>
</li>
<% end %>
</ul>
<p class="new_file">
<%= fields_for "design["+fieldname_p+"][]", object, :index => nil do |f| %>
<%= f.hidden_field :id,{:value=>''} %>
<%= f.file_field :file ,{:class=>"multi_files",:rtype=>rtype}%>
<%= fields_for "design[" + field_name + "][]", object.send(field_name).build, :index => nil do |f| %>
<%= f.file_field :file, :class => "multi_files" %>
</p>
<% end %>
<% end %>

View File

@ -1,46 +1,48 @@
<%= stylesheet_link_tag "design_temp" %>
<%= javascript_include_tag "design_temp" %>
<% content_for :page_specific_css do %>
<%= stylesheet_link_tag "design_temp" %>
<% end %>
<p>
<%= f.label :title, t('admin.title') %>
<%= f.text_field :title, :class => 'text' %>
<%= f.label :title, t('admin.title') %>
<%= f.text_field :title, :class => 'text' %>
</p>
<p>
<%= f.label :author, t('admin.author') %>
<%= f.text_field :author, :class => 'text' %>
<%= f.label :author, t('admin.author') %>
<%= f.text_field :author, :class => 'text' %>
</p>
<p>
<%= f.label :intro, t('admin.intro') %>
<%= f.text_field :intro, :class => 'text' %>
<%= f.label :intro, t('admin.intro') %>
<%= f.text_field :intro, :class => 'text' %>
</p>
<p>
<%= f.label "layout", t('admin.layout') %>
<% if @design.layout.blank? %>
<%= f.label "layout", t('admin.layout') %>
<% if @design.layout.blank? %>
<%= fields_for 'design[layout]' do |f| %>
<%= f.file_field :file %>
<%= f.file_field :file %>
<%= f.hidden_field :to_save, :value => true %>
<% end %>
<% else%>
<%= File.basename (@design.layout.file.url) %>
<% end %>
<% else %>
<%= File.basename (@design.layout.file.url) %>
<% end %>
</p>
<p>
<%= f.label "default_css", t('admin.default_css') %>
<% if @design.default_css.blank? %>
<%= f.file_field :default_css %>
<% else%>
<%= File.basename (@design.default_css.url) %>
<% end %>
<%= f.label "default_css", t('admin.default_css') %>
<% if @design.default_css.blank? %>
<%= f.file_field :default_css %>
<% else %>
<%= File.basename (@design.default_css.url) %>
<% end %>
</p>
<p>
<%= render :partial => 'design_file' ,:locals => { :fieldname=>"themes",:object=>@design ,:f=>f,:rtype=>'stylesheets',:item_destroy=>true,:item_editable=>true } %>
<%= render :partial => 'design_file', :locals => { :object => @design, :field_name => "themes", :f => f, :classes => "r_destroy, r_editable" } %>
</p>
<p>
<%= render :partial => 'design_file' ,:locals => { :fieldname=>"javascript",:object=>@design ,:f=>f,:rtype=>'javascripts' ,:item_destroy=>true,:item_editable=>true} %>
<%= render :partial => 'design_file', :locals => { :object => @design, :field_name => "javascripts", :f => f, :classes => "r_destroy, r_editable" } %>
</p>
<p>
<%= render :partial => 'design_file' ,:locals => { :fieldname=>"image",:object=>@design ,:f=>f,:rtype=>'images' ,:item_destroy=>true,:item_snapshot=>true} %>
<%= render :partial => 'design_file', :locals => { :object => @design, :field_name => "images", :f => f, :classes => "r_destroy, r_snapshot" } %>
</p>