From 4108ebf06dc469c155788a2cb981fee863e9e3de Mon Sep 17 00:00:00 2001 From: chris2tof Date: Mon, 19 Dec 2011 21:22:26 +0800 Subject: [PATCH] Fix mongoid deeply nested polimorphic Got the commit from github. The roles and sub_roles save. --- Gemfile | 2 +- Gemfile.lock | 22 ++++++++++++------- app/assets/javascripts/attribute_form.js.erb | 4 ++-- app/controllers/admin/roles_controller.rb | 2 ++ app/models/user/attribute.rb | 1 - app/models/user/attribute_field.rb | 2 +- .../attributes/_attribute_field.html.erb | 3 +-- app/views/admin/infos/_form.html.erb | 10 ++++----- app/views/admin/roles/_form.html.erb | 16 +++++++------- app/views/admin/roles/_form_sub.html.erb | 2 +- app/views/admin/roles/_sub_role.html.erb | 22 +++++++++---------- 11 files changed, 45 insertions(+), 41 deletions(-) diff --git a/Gemfile b/Gemfile index 45b8257e..82636879 100644 --- a/Gemfile +++ b/Gemfile @@ -10,7 +10,7 @@ gem 'mini_magick' gem 'rubyzip' gem 'sinatra' gem 'execjs' -gem 'mongoid' +gem 'mongoid', :git => 'git://github.com/mongoid/mongoid.git', :ref => '7a915395db50e2bc6071a503a11530e644879e49' gem 'rake' gem 'jquery-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 86b786e5..9d030c56 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 remote: http://rubygems.org/ specs: @@ -32,7 +42,7 @@ GEM archive-tar-minitar (0.5.2) arel (2.2.1) bcrypt-ruby (3.0.1) - bson (1.5.1) + bson (1.5.2) bson_ext (1.5.1) builder (3.0.0) carrierwave (0.5.4) @@ -80,12 +90,8 @@ GEM mime-types (1.17.2) mini_magick (3.3) subexec (~> 0.1.0) - mongo (1.5.1) - bson (= 1.5.1) - mongoid (2.3.4) - activemodel (~> 3.1) - mongo (~> 1.3) - tzinfo (~> 0.3.22) + mongo (1.5.2) + bson (= 1.5.2) multi_json (1.0.4) orm_adapter (0.0.5) polyglot (0.3.3) @@ -190,7 +196,7 @@ DEPENDENCIES factory_girl_rails jquery-rails mini_magick - mongoid + mongoid! rails rake rcov diff --git a/app/assets/javascripts/attribute_form.js.erb b/app/assets/javascripts/attribute_form.js.erb index 6109099f..9c2b9099 100644 --- a/app/assets/javascripts/attribute_form.js.erb +++ b/app/assets/javascripts/attribute_form.js.erb @@ -2,12 +2,12 @@ $('.attributes_block a.delete').live('click', function(){ $(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); $("#attribute_" + $(this).prev().attr('value')).hide(); }); -$('#attributes_block a.switch').live('click', function(){ +$('.action a.switch').live('click', function(){ $(this).parent().toggleClass('disable'); var $am = $(this).next('.built_in_state') $am.attr('value', ($am.attr('value') == "false") ? "true" : "false"); diff --git a/app/controllers/admin/roles_controller.rb b/app/controllers/admin/roles_controller.rb index 65c86f36..f0321368 100644 --- a/app/controllers/admin/roles_controller.rb +++ b/app/controllers/admin/roles_controller.rb @@ -26,12 +26,14 @@ class Admin::RolesController < ApplicationController end def create + puts params.to_yaml @attribute = Role.new(params[:role]) @attribute.save redirect_to :action => :index end def update + puts params.to_yaml @attribute = Role.find(params[:id]) @attribute.update_attributes(params[:role]) respond_to do |format| diff --git a/app/models/user/attribute.rb b/app/models/user/attribute.rb index c53adebb..af8a1106 100644 --- a/app/models/user/attribute.rb +++ b/app/models/user/attribute.rb @@ -9,7 +9,6 @@ class Attribute has_many :attribute_fields, :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 def is_built_in? diff --git a/app/models/user/attribute_field.rb b/app/models/user/attribute_field.rb index 8f3167e7..7ea385a9 100644 --- a/app/models/user/attribute_field.rb +++ b/app/models/user/attribute_field.rb @@ -13,8 +13,8 @@ class AttributeField belongs_to :attribute has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy has_many :attribute_values + validates_uniqueness_of :key - accepts_nested_attributes_for :i18n_variable, :allow_destroy => true # Convert the string list_options into an array def select_list_options=(var) diff --git a/app/views/admin/attributes/_attribute_field.html.erb b/app/views/admin/attributes/_attribute_field.html.erb index d8fb5ade..56d0b3ad 100644 --- a/app/views/admin/attributes/_attribute_field.html.erb +++ b/app/views/admin/attributes/_attribute_field.html.erb @@ -3,11 +3,10 @@ <%= f.check_box :locale %> - <%= 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| %> <%= f.text_field locale, :style => "width:130px" %> <% end %> - <%= f.hidden_field :id %> <% end %> <%= f.select :markup, LIST[:markups], {}, {:style => "width:90px"} %> diff --git a/app/views/admin/infos/_form.html.erb b/app/views/admin/infos/_form.html.erb index 75ded177..bf31b652 100644 --- a/app/views/admin/infos/_form.html.erb +++ b/app/views/admin/infos/_form.html.erb @@ -13,11 +13,11 @@ <%= is_new ? (f.text_field :key, {:style => "width:150px"}) : @attribute.key %> - <% @site_valid_locales.each do |locale| %> - - <%= text_field_tag "#{@attribute_type}[i18n_variable][#{locale}]", (@i18n_variable[locale] if @i18n_variable), :style => "width:150px" %> - - <% end %> + <%= f.fields_for :i18n_variable, (@attribute.new_record? ? @attribute.build_i18n_variable : @attribute.i18n_variable) do |f| %> + <% @site_valid_locales.each do |locale| %> + <%= f.text_field locale, :style => "width:150px" %> + <% end %> + <% end %> diff --git a/app/views/admin/roles/_form.html.erb b/app/views/admin/roles/_form.html.erb index 7026db95..11608072 100644 --- a/app/views/admin/roles/_form.html.erb +++ b/app/views/admin/roles/_form.html.erb @@ -13,11 +13,11 @@ <%= is_new ? (f.text_field :key, {:style => "width:150px"}) : @attribute.key %> - <% @site_valid_locales.each do |locale| %> - - <%= text_field_tag "#{@attribute_type}[i18n_variable][#{locale}]", (@i18n_variable[locale] if @i18n_variable), :style => "width:150px" %> - - <% end %> + <%= f.fields_for :i18n_variable, (@attribute.new_record? ? @attribute.build_i18n_variable : @attribute.i18n_variable) do |f| %> + <% @site_valid_locales.each do |locale| %> + <%= f.text_field locale, :style => "width:150px" %> + <% end %> + <% end %> @@ -26,8 +26,8 @@

Sub roles

- <% @attribute.sub_roles.each do |sub_role| %> - <%= render :partial => 'form_sub', :object => sub_role, :locals => {:f => f} %> + <% @attribute.sub_roles.each_with_index do |sub_role, i| %> + <%= render :partial => 'form_sub', :object => sub_role, :locals => {:f => f, :i => i} %> <% end %>
@@ -38,12 +38,12 @@ <% content_for :page_specific_javascript do %> + <%= javascript_include_tag "attribute_form" %> diff --git a/app/views/admin/roles/_form_sub.html.erb b/app/views/admin/roles/_form_sub.html.erb index 09dac402..796d8469 100644 --- a/app/views/admin/roles/_form_sub.html.erb +++ b/app/views/admin/roles/_form_sub.html.erb @@ -1,3 +1,3 @@ <%= 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 %> \ No newline at end of file diff --git a/app/views/admin/roles/_sub_role.html.erb b/app/views/admin/roles/_sub_role.html.erb index 04c85ff5..e5a5f076 100644 --- a/app/views/admin/roles/_sub_role.html.erb +++ b/app/views/admin/roles/_sub_role.html.erb @@ -12,11 +12,11 @@ <%= sub_role.new_record? ? (f.text_field :key, {:style => "width:150px"}) : sub_role.key %> - <% @site_valid_locales.each do |locale| %> - - <%= f.text_field "i18n_variable[#{locale}]", :value => (@i18n_variable[locale] if @i18n_variable), :style => "width:150px" %> - - <% end %> + <%= 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| %> + <%= f.text_field locale, :style => "width:150px" %> + <% end %> + <% end %> @@ -37,10 +37,10 @@ - + <%= hidden_field_tag 'attribute_field_count', sub_role.attribute_fields.count %> <%= t(:add) %> - <%= hidden_field_tag 'sub_role_temp_id', 'new_sub_roles' %> + <%= hidden_field_tag 'sub_role_temp_id', (i rescue 'new_sub_roles') %> @@ -55,16 +55,14 @@ <% content_for :page_specific_javascript do %> - <%= javascript_include_tag "attribute_form" %> -<% end %> \ No newline at end of file +<% end unless !sub_role.new_record? %> \ No newline at end of file