Fix mongoid deeply nested polimorphic

Got the commit from github. The roles and sub_roles save.
This commit is contained in:
chris2tof 2011-12-19 21:22:26 +08:00
parent d27d7a623c
commit 4108ebf06d
11 changed files with 45 additions and 41 deletions

View File

@ -10,7 +10,7 @@ gem 'mini_magick'
gem 'rubyzip' gem 'rubyzip'
gem 'sinatra' gem 'sinatra'
gem 'execjs' gem 'execjs'
gem 'mongoid' gem 'mongoid', :git => 'git://github.com/mongoid/mongoid.git', :ref => '7a915395db50e2bc6071a503a11530e644879e49'
gem 'rake' gem 'rake'
gem 'jquery-rails' gem 'jquery-rails'

View File

@ -1,3 +1,13 @@
GIT
remote: git://github.com/mongoid/mongoid.git
revision: 7a915395db50e2bc6071a503a11530e644879e49
ref: 7a915395db50e2bc6071a503a11530e644879e49
specs:
mongoid (2.4.0)
activemodel (~> 3.1)
mongo (~> 1.3)
tzinfo (~> 0.3.22)
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
specs: specs:
@ -32,7 +42,7 @@ GEM
archive-tar-minitar (0.5.2) archive-tar-minitar (0.5.2)
arel (2.2.1) arel (2.2.1)
bcrypt-ruby (3.0.1) bcrypt-ruby (3.0.1)
bson (1.5.1) bson (1.5.2)
bson_ext (1.5.1) bson_ext (1.5.1)
builder (3.0.0) builder (3.0.0)
carrierwave (0.5.4) carrierwave (0.5.4)
@ -80,12 +90,8 @@ GEM
mime-types (1.17.2) mime-types (1.17.2)
mini_magick (3.3) mini_magick (3.3)
subexec (~> 0.1.0) subexec (~> 0.1.0)
mongo (1.5.1) mongo (1.5.2)
bson (= 1.5.1) bson (= 1.5.2)
mongoid (2.3.4)
activemodel (~> 3.1)
mongo (~> 1.3)
tzinfo (~> 0.3.22)
multi_json (1.0.4) multi_json (1.0.4)
orm_adapter (0.0.5) orm_adapter (0.0.5)
polyglot (0.3.3) polyglot (0.3.3)
@ -190,7 +196,7 @@ DEPENDENCIES
factory_girl_rails factory_girl_rails
jquery-rails jquery-rails
mini_magick mini_magick
mongoid mongoid!
rails rails
rake rake
rcov rcov

View File

@ -2,12 +2,12 @@ $('.attributes_block a.delete').live('click', function(){
$(this).parents('.list_item').remove(); $(this).parents('.list_item').remove();
}); });
$('#attributes a.remove_existing_record').live('click', function(){ $('.action a.remove_existing_record').live('click', function(){
$(this).next('.should_destroy').attr('value', 1); $(this).next('.should_destroy').attr('value', 1);
$("#attribute_" + $(this).prev().attr('value')).hide(); $("#attribute_" + $(this).prev().attr('value')).hide();
}); });
$('#attributes_block a.switch').live('click', function(){ $('.action a.switch').live('click', function(){
$(this).parent().toggleClass('disable'); $(this).parent().toggleClass('disable');
var $am = $(this).next('.built_in_state') var $am = $(this).next('.built_in_state')
$am.attr('value', ($am.attr('value') == "false") ? "true" : "false"); $am.attr('value', ($am.attr('value') == "false") ? "true" : "false");

View File

@ -26,12 +26,14 @@ class Admin::RolesController < ApplicationController
end end
def create def create
puts params.to_yaml
@attribute = Role.new(params[:role]) @attribute = Role.new(params[:role])
@attribute.save @attribute.save
redirect_to :action => :index redirect_to :action => :index
end end
def update def update
puts params.to_yaml
@attribute = Role.find(params[:id]) @attribute = Role.find(params[:id])
@attribute.update_attributes(params[:role]) @attribute.update_attributes(params[:role])
respond_to do |format| respond_to do |format|

View File

@ -9,7 +9,6 @@ class Attribute
has_many :attribute_fields, :autosave => true, :dependent => :destroy has_many :attribute_fields, :autosave => true, :dependent => :destroy
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :i18n_variable, :allow_destroy => true
accepts_nested_attributes_for :attribute_fields, :allow_destroy => true accepts_nested_attributes_for :attribute_fields, :allow_destroy => true
def is_built_in? def is_built_in?

View File

@ -13,8 +13,8 @@ class AttributeField
belongs_to :attribute belongs_to :attribute
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
has_many :attribute_values has_many :attribute_values
validates_uniqueness_of :key validates_uniqueness_of :key
accepts_nested_attributes_for :i18n_variable, :allow_destroy => true
# Convert the string list_options into an array # Convert the string list_options into an array
def select_list_options=(var) def select_list_options=(var)

View File

@ -3,11 +3,10 @@
<td class='select_mulitlingual' style='text-align:center'> <td class='select_mulitlingual' style='text-align:center'>
<%= f.check_box :locale %> <%= f.check_box :locale %>
</td> </td>
<%= f.fields_for :i18n_variable, (attribute_field.i18n_variable.nil? ? attribute_field.build_i18n_variable : attribute_field.i18n_variable) do |f| %> <%= f.fields_for :i18n_variable, (attribute_field.new_record? ? attribute_field.build_i18n_variable : attribute_field.i18n_variable) do |f| %>
<% @site_valid_locales.each do |locale| %> <% @site_valid_locales.each do |locale| %>
<td><%= f.text_field locale, :style => "width:130px" %></td> <td><%= f.text_field locale, :style => "width:130px" %></td>
<% end %> <% end %>
<%= f.hidden_field :id %>
<% end %> <% end %>
<td class='select_type'> <td class='select_type'>
<%= f.select :markup, LIST[:markups], {}, {:style => "width:90px"} %> <%= f.select :markup, LIST[:markups], {}, {:style => "width:90px"} %>

View File

@ -13,10 +13,10 @@
<tbody> <tbody>
<tr class="list_item"> <tr class="list_item">
<td><%= is_new ? (f.text_field :key, {:style => "width:150px"}) : @attribute.key %></td> <td><%= is_new ? (f.text_field :key, {:style => "width:150px"}) : @attribute.key %></td>
<%= f.fields_for :i18n_variable, (@attribute.new_record? ? @attribute.build_i18n_variable : @attribute.i18n_variable) do |f| %>
<% @site_valid_locales.each do |locale| %> <% @site_valid_locales.each do |locale| %>
<td> <td><%= f.text_field locale, :style => "width:150px" %></td>
<%= text_field_tag "#{@attribute_type}[i18n_variable][#{locale}]", (@i18n_variable[locale] if @i18n_variable), :style => "width:150px" %> <% end %>
</td>
<% end %> <% end %>
</tr> </tr>
</tbody> </tbody>

View File

@ -13,10 +13,10 @@
<tbody> <tbody>
<tr class="list_item"> <tr class="list_item">
<td><%= is_new ? (f.text_field :key, {:style => "width:150px"}) : @attribute.key %></td> <td><%= is_new ? (f.text_field :key, {:style => "width:150px"}) : @attribute.key %></td>
<%= f.fields_for :i18n_variable, (@attribute.new_record? ? @attribute.build_i18n_variable : @attribute.i18n_variable) do |f| %>
<% @site_valid_locales.each do |locale| %> <% @site_valid_locales.each do |locale| %>
<td> <td><%= f.text_field locale, :style => "width:150px" %></td>
<%= text_field_tag "#{@attribute_type}[i18n_variable][#{locale}]", (@i18n_variable[locale] if @i18n_variable), :style => "width:150px" %> <% end %>
</td>
<% end %> <% end %>
</tr> </tr>
</tbody> </tbody>
@ -26,8 +26,8 @@
<div id='sub_roles' class="attributes_block <%= @class %>"> <div id='sub_roles' class="attributes_block <%= @class %>">
<h2>Sub roles</h2> <h2>Sub roles</h2>
<% @attribute.sub_roles.each do |sub_role| %> <% @attribute.sub_roles.each_with_index do |sub_role, i| %>
<%= render :partial => 'form_sub', :object => sub_role, :locals => {:f => f} %> <%= render :partial => 'form_sub', :object => sub_role, :locals => {:f => f, :i => i} %>
<% end %> <% end %>
</div> </div>
</div> </div>
@ -38,12 +38,12 @@
</div> </div>
<% content_for :page_specific_javascript do %> <% content_for :page_specific_javascript do %>
<%= javascript_include_tag "attribute_form" %>
<script> <script>
$('#add_sub_role a.add').live('click', function(){ $('#add_sub_role a.add').live('click', function(){
var new_id = $(this).prev().attr('value'); var new_id = $(this).prev().attr('value');
var old_id = new RegExp("new_sub_roles", "g"); var old_id = new RegExp("new_sub_roles", "g");
$(this).prev().attr('value', parseInt(new_id) + 1); $(this).prev().attr('value', parseInt(new_id) + 1);
$('#sub_role_temp_id').attr('value', new_id);
$('#sub_roles').append(("<%= escape_javascript(add_attribute 'admin/roles/sub_role', f, :sub_roles) %>").replace(old_id, new_id)); $('#sub_roles').append(("<%= escape_javascript(add_attribute 'admin/roles/sub_role', f, :sub_roles) %>").replace(old_id, new_id));
}); });
</script> </script>

View File

@ -1,3 +1,3 @@
<%= f.fields_for :sub_roles, form_sub do |f| %> <%= f.fields_for :sub_roles, form_sub do |f| %>
<%= render :partial => 'sub_role', :object => form_sub, :locals => {:f => f} %> <%= render :partial => 'sub_role', :object => form_sub, :locals => {:f => f, :i => i} %>
<% end %> <% end %>

View File

@ -12,10 +12,10 @@
<tbody> <tbody>
<tr class="list_item"> <tr class="list_item">
<td><%= sub_role.new_record? ? (f.text_field :key, {:style => "width:150px"}) : sub_role.key %></td> <td><%= sub_role.new_record? ? (f.text_field :key, {:style => "width:150px"}) : sub_role.key %></td>
<%= f.fields_for :i18n_variable, (sub_role.new_record? ? sub_role.build_i18n_variable : sub_role.i18n_variable) do |f| %>
<% @site_valid_locales.each do |locale| %> <% @site_valid_locales.each do |locale| %>
<td> <td><%= f.text_field locale, :style => "width:150px" %></td>
<%= f.text_field "i18n_variable[#{locale}]", :value => (@i18n_variable[locale] if @i18n_variable), :style => "width:150px" %> <% end %>
</td>
<% end %> <% end %>
</tr> </tr>
</tbody> </tbody>
@ -37,10 +37,10 @@
</thead> </thead>
<tfoot> <tfoot>
<tr> <tr>
<td colspan="5"> <td id='add_attribute' colspan="5">
<%= hidden_field_tag 'attribute_field_count', sub_role.attribute_fields.count %> <%= hidden_field_tag 'attribute_field_count', sub_role.attribute_fields.count %>
<a href="#" class="add"><%= t(:add) %></a> <a href="#" class="add"><%= t(:add) %></a>
<%= hidden_field_tag 'sub_role_temp_id', 'new_sub_roles' %> <%= hidden_field_tag 'sub_role_temp_id', (i rescue 'new_sub_roles') %>
</td> </td>
</tr> </tr>
</tfoot> </tfoot>
@ -55,16 +55,14 @@
</div> </div>
<% content_for :page_specific_javascript do %> <% content_for :page_specific_javascript do %>
<%= javascript_include_tag "attribute_form" %>
<script> <script>
$('.attributes_block a.add').live('click', function(){ $('#add_attribute a.add').live('click', function(){
var new_parent_id = $(this).next().attr('value'); var new_parent_id = $(this).next().attr('value');
var old_parent_id = new RegExp("new_sub_roles", "g"); var old_parent_id = new RegExp("new_sub_roles", "g");
var new_id = $(this).prev().attr('value'); var new_id = $(this).prev().attr('value');
var old_id = new RegExp("new_attribute_fields", "g"); var old_id = new RegExp("new_attribute_fields", "g");
$(this).prev().attr('value', parseInt(new_id) + 1); $(this).prev().attr('value', parseInt(new_id) + 1);
$(this).next().attr('value', parseInt(new_parent_id) + 1); $(this).parents('table').append(("<%= escape_javascript(add_attribute 'admin/attributes/attribute_field', f, :attribute_fields) %>").replace(old_id, new_id).replace(old_parent_id, new_parent_id));
$(this).parents('.sub_role_attributes').append(("<%= escape_javascript(add_attribute 'admin/attributes/attribute_field', f, :attribute_fields) %>").replace(old_id, new_id).replace(old_parent_id, new_parent_id));
}); });
</script> </script>
<% end %> <% end unless !sub_role.new_record? %>