lib/scaffolding_extensions/meta_controller.rb
# Scaffolds a habtm association for a class and an association using two select boxes, or
# a select box for removing associations and an autocompleting text box for
# adding associations.    
Scaffolds a habtm association for a class and an association using two select boxes, or
a select box for removing associations and an autocompleting text box for
adding associations.
Scaffolds a habtm association for a class and an association using two select boxes or a select box for removing associations and an autocompleting text box for adding associations .
def scaffold_habtm(klass, association)
        scaffold_setup
        sn = klass.scaffold_name
        shn = klass.scaffold_human_name
        scaffold_auto_complete_for(klass, association) if auto_complete = klass.scaffold_association_use_auto_complete(association)
        if klass.scaffold_habtm_with_ajax
          suffix = "_#{sn}"
          records_list = "#{sn}_associated_records_list"
          element_id = "#{sn}_#{association}_id"
          add_meth = "add_#{association}_to_#{sn}"
          scaffold_define_nonidempotent_method(add_meth) do
            @record = klass.scaffold_find_object(:habtm, scaffold_request_id, :session=>scaffold_session)
            @associated_record = klass.scaffold_add_associated_objects(association, @record, {:session=>scaffold_session}, scaffold_request_param(element_id))
            if scaffold_xhr?
              @klass = klass
              @association = association
              @records_list = records_list
              @auto_complete = auto_complete
              @element_id = element_id
              @scaffold_javascript = true
              scaffold_render_template(add_meth, {}, :inline=>'<%=scaffold_raw scaffold_add_habtm_element %>')
            else
              scaffold_redirect_to(scaffold_url("edit#{suffix}", :id=>@record.scaffold_id))
            end
          end
          remove_meth = "remove_#{association}_from_#{sn}"
          scaffold_define_nonidempotent_method(remove_meth) do
            @record = klass.scaffold_find_object(:habtm, scaffold_request_id, :session=>scaffold_session)
            @associated_record = klass.scaffold_remove_associated_objects(association, @record, {:session=>scaffold_session}, scaffold_request_param(element_id))
            @auto_complete = auto_complete
            if scaffold_xhr?
              @remove_element_id = "#{sn}_#{@record.scaffold_id}_#{association}_#{@associated_record.scaffold_id}"
              @select_id = element_id
              @select_value = @associated_record.scaffold_id
              @select_text = @associated_record.scaffold_name
              @scaffold_javascript = true
              scaffold_render_template(remove_meth, {}, :inline=>'<%=scaffold_raw scaffold_remove_existing_habtm_element %>')
            else
              scaffold_redirect_to(scaffold_url("edit#{suffix}", :id=>@record.scaffold_id))
            end
          end
        else
          suffix = "_#{sn}_#{association}"
          scaffold_options={:aph_name=>association.to_s.humanize, :singular_name=>sn, :singular_human_name=>shn, :association=>association, :class=>klass, :suffix=>suffix}
          scaffold_options[:ash_name] = scaffold_options[:aph_name].downcase
          scaffold_options[:aplch_name] = scaffold_options[:aph_name].downcase
          scaffold_options[:aslhc_name] = scaffold_options[:aplch_name].singularize
          scaffold_define_method("edit#{suffix}") do
            @scaffold_object = klass.scaffold_find_object(:habtm, scaffold_request_id, :session=>scaffold_session, :association=>association)
            @items_to_remove = klass.scaffold_associated_objects(association, @scaffold_object, :session=>scaffold_session)
            @items_to_add = klass.scaffold_unassociated_objects(association, @scaffold_object, :session=>scaffold_session) unless klass.scaffold_association_use_auto_complete(association)
            scaffold_render_template(:habtm, scaffold_options)
          end
          scaffold_define_nonidempotent_method("update#{suffix}") do
            @scaffold_object = klass.scaffold_find_object(:habtm, scaffold_request_id, :session=>scaffold_session, :association=>association)
            klass.scaffold_add_associated_objects(association, @scaffold_object, {:session=>scaffold_session}, *scaffold_select_ids(scaffold_request_param(:add)))
            klass.scaffold_remove_associated_objects(association, @scaffold_object, {:session=>scaffold_session}, *scaffold_select_ids(scaffold_request_param(:remove)))
            scaffold_redirect(:edit, suffix, "Updated #{@scaffold_object.scaffold_name}'s #{scaffold_options[:aplch_name]} successfully", @scaffold_object.scaffold_id)
          end
        end
      end
def scaffold_habtm ( klass , association ) scaffold_setup sn = klass . scaffold_name shn = klass . scaffold_human_name scaffold_auto_complete_for ( klass , association ) if auto_complete = klass . scaffold_association_use_auto_complete ( association ) if klass . scaffold_habtm_with_ajax suffix = "_#{sn}" records_list = "#{sn}_associated_records_list" element_id = "#{sn}_#{association}_id" add_meth = "add_#{association}_to_#{sn}" scaffold_define_nonidempotent_method ( add_meth ) do @record = klass . scaffold_find_object ( :habtm , scaffold_request_id , :session => scaffold_session ) @associated_record = klass . scaffold_add_associated_objects ( association , @record , { :session => scaffold_session } , scaffold_request_param ( element_id ) ) if scaffold_xhr? @klass = klass @association = association @records_list = records_list @auto_complete = auto_complete @element_id = element_id @scaffold_javascript = true scaffold_render_template ( add_meth , { } , :inline => '<%=scaffold_raw scaffold_add_habtm_element %>' ) else scaffold_redirect_to ( scaffold_url ( "edit#{suffix}" , :id => @record . scaffold_id ) ) end end remove_meth = "remove_#{association}_from_#{sn}" scaffold_define_nonidempotent_method ( remove_meth ) do @record = klass . scaffold_find_object ( :habtm , scaffold_request_id , :session => scaffold_session ) @associated_record = klass . scaffold_remove_associated_objects ( association , @record , { :session => scaffold_session } , scaffold_request_param ( element_id ) ) @auto_complete = auto_complete if scaffold_xhr? @remove_element_id = "#{sn}_#{@record.scaffold_id}_#{association}_#{@associated_record.scaffold_id}" @select_id = element_id @select_value = @associated_record . scaffold_id @select_text = @associated_record . scaffold_name @scaffold_javascript = true scaffold_render_template ( remove_meth , { } , :inline => '<%=scaffold_raw scaffold_remove_existing_habtm_element %>' ) else scaffold_redirect_to ( scaffold_url ( "edit#{suffix}" , :id => @record . scaffold_id ) ) end end else suffix = "_#{sn}_#{association}" scaffold_options = { :aph_name => association . to_s . humanize , :singular_name => sn , :singular_human_name => shn , :association => association , :class => klass , :suffix => suffix } scaffold_options [ :ash_name ] = scaffold_options [ :aph_name ] . downcase scaffold_options [ :aplch_name ] = scaffold_options [ :aph_name ] . downcase scaffold_options [ :aslhc_name ] = scaffold_options [ :aplch_name ] . singularize scaffold_define_method ( "edit#{suffix}" ) do @scaffold_object = klass . scaffold_find_object ( :habtm , scaffold_request_id , :session => scaffold_session , :association => association ) @items_to_remove = klass . scaffold_associated_objects ( association , @scaffold_object , :session => scaffold_session ) @items_to_add = klass . scaffold_unassociated_objects ( association , @scaffold_object , :session => scaffold_session ) unless klass . scaffold_association_use_auto_complete ( association ) scaffold_render_template ( :habtm , scaffold_options ) end scaffold_define_nonidempotent_method ( "update#{suffix}" ) do @scaffold_object = klass . scaffold_find_object ( :habtm , scaffold_request_id , :session => scaffold_session , :association => association ) klass . scaffold_add_associated_objects ( association , @scaffold_object , { :session => scaffold_session } , * scaffold_select_ids ( scaffold_request_param ( :add ) ) ) klass . scaffold_remove_associated_objects ( association , @scaffold_object , { :session => scaffold_session } , * scaffold_select_ids ( scaffold_request_param ( :remove ) ) ) scaffold_redirect ( :edit , suffix , "Updated #{@scaffold_object.scaffold_name}'s #{scaffold_options[:aplch_name]} successfully" , @scaffold_object . scaffold_id ) end end end
Scaffolds a habtm association for a class and an association using two select boxes, or
a select box for removing associations and an autocompleting text box for
adding associations.
Scaffolds a habtm association for a class and an association using two select boxes or a select box for removing associations and an autocompleting text box for adding associations .
# ash_name = association singular human name # aplch_name = association plural lower case human name # aslhc_name = association singular lower case human name (typo!)
klass
<not_specific>
association
<not_specific>