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) subexec (~> 0.1.0)
mongo (1.3.1) mongo (1.3.1)
bson (>= 1.3.1) bson (>= 1.3.1)
mongoid (2.1.9) mongoid (2.2.0)
activemodel (~> 3.0) activemodel (~> 3.0)
mongo (~> 1.3) mongo (~> 1.3)
tzinfo (~> 0.3.22) tzinfo (~> 0.3.22)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,7 @@
<% content_for :page_specific_css do %>
<%= stylesheet_link_tag "design_temp" %> <%= stylesheet_link_tag "design_temp" %>
<%= javascript_include_tag "design_temp" %> <% end %>
<p> <p>
<%= f.label :title, t('admin.title') %> <%= f.label :title, t('admin.title') %>
<%= f.text_field :title, :class => 'text' %> <%= f.text_field :title, :class => 'text' %>
@ -34,13 +36,13 @@
<% end %> <% end %>
</p> </p>
<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>
<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>
<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> </p>