splendeo-dependent_select 0.6.4 → 0.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/dependent_select/form_helpers.rb +62 -29
- metadata +1 -1
| @@ -111,7 +111,7 @@ module DependentSelect::FormHelpers | |
| 111 111 | 
             
              # dependent_select generates a javascript array with all the options available to the dependent select.
         | 
| 112 112 | 
             
              # By default, the name of that variable is automatically generated using the following formula:
         | 
| 113 113 | 
             
              #
         | 
| 114 | 
            -
              #    js_array_name = "ds_#{ | 
| 114 | 
            +
              #    js_array_name = "ds_#{dependent_field_id}_array"
         | 
| 115 115 | 
             
              #
         | 
| 116 116 | 
             
              # This can be overriden by using the js_array_name option (its value will be used instead of the previous)
         | 
| 117 117 | 
             
              #
         | 
| @@ -272,11 +272,13 @@ module DependentSelect::FormHelpers | |
| 272 272 | 
             
                initial_collection = dependent_select_initial_collection(object,
         | 
| 273 273 | 
             
                  method, collection, value_method)
         | 
| 274 274 |  | 
| 275 | 
            -
                tag =  | 
| 275 | 
            +
                tag, dependent_field_id = dependent_collection_select_build_tag(
         | 
| 276 | 
            +
                  object_name, object, method, initial_collection, value_method, 
         | 
| 276 277 | 
             
                  text_method, options, html_options)
         | 
| 277 278 |  | 
| 278 279 | 
             
                script = dependent_select_js_for_collection(object_name, object, method, 
         | 
| 279 | 
            -
                  collection, value_method, text_method, filter_method, options,  | 
| 280 | 
            +
                  collection, value_method, text_method, filter_method, options, html_options,
         | 
| 281 | 
            +
                  extra_options, dependent_field_id)
         | 
| 280 282 |  | 
| 281 283 | 
             
                return "#{tag}\n#{script}"
         | 
| 282 284 | 
             
              end
         | 
| @@ -335,10 +337,12 @@ module DependentSelect::FormHelpers | |
| 335 337 |  | 
| 336 338 | 
             
                initial_choices = dependent_select_initial_choices(object, method, choices_with_filter)
         | 
| 337 339 |  | 
| 338 | 
            -
                tag =  | 
| 340 | 
            +
                tag, dependent_field_id = dependent_select_build_tag(
         | 
| 341 | 
            +
                  object_name, object, method, initial_collection, value_method, 
         | 
| 342 | 
            +
                  text_method, options, html_options)
         | 
| 339 343 |  | 
| 340 344 | 
             
                script = dependent_select_js(object_name, method, choices_with_filter,
         | 
| 341 | 
            -
                  filter_method, options, extra_options)
         | 
| 345 | 
            +
                  filter_method, options, html_options, extra_options)
         | 
| 342 346 |  | 
| 343 347 | 
             
                return "#{tag}\n#{script}"
         | 
| 344 348 | 
             
              end
         | 
| @@ -370,10 +374,10 @@ module DependentSelect::FormHelpers | |
| 370 374 | 
             
                #  * An extra observer for custon events. These events are raised by the dependent_select itself.
         | 
| 371 375 | 
             
                #  * An first call to update_dependent_select, that sets up the initial stuff
         | 
| 372 376 | 
             
                def dependent_select_js(object_name, object, method, choices_with_filter, 
         | 
| 373 | 
            -
                  filter_method, options, extra_options)
         | 
| 377 | 
            +
                  filter_method, options, html_options, extra_options, dependent_field_id)
         | 
| 374 378 |  | 
| 375 379 | 
             
                  # the js variable that will hold the array with option values, texts and filters
         | 
| 376 | 
            -
                  js_array_name = extra_options[:array_name] || "ds_#{ | 
| 380 | 
            +
                  js_array_name = extra_options[:array_name] || "ds_#{dependent_field_id}_array"
         | 
| 377 381 |  | 
| 378 382 | 
             
                  js_array_code = ""
         | 
| 379 383 |  | 
| @@ -382,27 +386,27 @@ module DependentSelect::FormHelpers | |
| 382 386 | 
             
                    js_array_code += "#{js_array_name} = #{choices_with_filter.to_json};\n"    
         | 
| 383 387 | 
             
                  end
         | 
| 384 388 |  | 
| 385 | 
            -
                   | 
| 386 | 
            -
             | 
| 389 | 
            +
                  observed_field_id = dependent_select_calculate_observed_field_id(object_name, object,
         | 
| 390 | 
            +
                    filter_method, html_options, extra_options)
         | 
| 387 391 | 
             
                  initial_value = dependent_select_initial_value(object, method)
         | 
| 388 392 | 
             
                  include_blank = options[:include_blank] || false
         | 
| 389 393 | 
             
                  collapse_spaces = extra_options[:collapse_spaces] || false
         | 
| 390 394 |  | 
| 391 395 | 
             
                  js_callback =
         | 
| 392 | 
            -
                    "function(e) { update_dependent_select( '#{ | 
| 396 | 
            +
                    "function(e) { update_dependent_select( '#{dependent_field_id}', '#{observed_field_id}', #{js_array_name}, " +
         | 
| 393 397 | 
             
                    "'#{initial_value}', #{include_blank}, #{collapse_spaces}, false); }"
         | 
| 394 398 |  | 
| 395 399 | 
             
                  javascript_tag(js_array_code +
         | 
| 396 | 
            -
                    "$('#{ | 
| 397 | 
            -
                    "$('#{ | 
| 398 | 
            -
                    "update_dependent_select( '#{ | 
| 400 | 
            +
                    "$('#{observed_field_id}').observe ('change', #{js_callback});\n" +
         | 
| 401 | 
            +
                    "$('#{observed_field_id}').observe ('DependentSelectFormBuilder:change', #{js_callback}); \n" +
         | 
| 402 | 
            +
                    "update_dependent_select( '#{dependent_field_id}', '#{observed_field_id}', #{js_array_name}, " +
         | 
| 399 403 | 
             
                    "'#{initial_value}', #{include_blank}, #{collapse_spaces}, true);"
         | 
| 400 404 | 
             
                  )
         | 
| 401 405 | 
             
                end
         | 
| 402 406 |  | 
| 403 407 | 
             
                # generates the js script for a dependent_collection_select. See +dependent_select_js+
         | 
| 404 408 | 
             
                def dependent_select_js_for_collection(object_name, object, method, collection, 
         | 
| 405 | 
            -
                  value_method, text_method, filter_method, options, extra_options)
         | 
| 409 | 
            +
                  value_method, text_method, filter_method, options, html_options, extra_options, dependent_field_id)
         | 
| 406 410 |  | 
| 407 411 | 
             
                  # the array that, converted to javascript, will be assigned values_var variable,
         | 
| 408 412 | 
             
                  # so it can be used for updating the select
         | 
| @@ -411,7 +415,29 @@ module DependentSelect::FormHelpers | |
| 411 415 | 
             
                  end
         | 
| 412 416 |  | 
| 413 417 | 
             
                  dependent_select_js(object_name, object, method, choices_with_filter, 
         | 
| 414 | 
            -
                    filter_method, options, extra_options)
         | 
| 418 | 
            +
                    filter_method, options, html_options, extra_options, dependent_field_id)
         | 
| 419 | 
            +
                end
         | 
| 420 | 
            +
                
         | 
| 421 | 
            +
                # returns a collection_select html string and the id of the generated field
         | 
| 422 | 
            +
                def dependent_collection_select_build_tag(object_name, object, method, collection, value_method, 
         | 
| 423 | 
            +
                  text_method, options, html_options)
         | 
| 424 | 
            +
                  dependent_field_id, it = dependent_select_calculate_field_id_and_it(object_name, 
         | 
| 425 | 
            +
                    object, method, html_options)
         | 
| 426 | 
            +
                  
         | 
| 427 | 
            +
                  tag = it.to_collection_select_tag(collection, value_method, text_method, options, html_options)
         | 
| 428 | 
            +
                  
         | 
| 429 | 
            +
                  return [tag, dependent_field_id]
         | 
| 430 | 
            +
                end
         | 
| 431 | 
            +
                
         | 
| 432 | 
            +
                # returns a select html string and the id of the generated field
         | 
| 433 | 
            +
                def dependent_select_build_tag(object_name, object, method, choices, options = {}, html_options = {})
         | 
| 434 | 
            +
                  
         | 
| 435 | 
            +
                  dependent_field_id, it = dependent_select_calculate_field_id_and_it(object_name, 
         | 
| 436 | 
            +
                    object, method, html_options)
         | 
| 437 | 
            +
                  
         | 
| 438 | 
            +
                  tag = it.to_select_tag(choices, options, html_options)
         | 
| 439 | 
            +
                  
         | 
| 440 | 
            +
                  return [tag, dependent_field_id]
         | 
| 415 441 | 
             
                end
         | 
| 416 442 |  | 
| 417 443 | 
             
                # Calculates the dom id of the observed field, usually concatenating object_name and filt. meth.
         | 
| @@ -420,24 +446,31 @@ module DependentSelect::FormHelpers | |
| 420 446 | 
             
                #    it returns the value of that item
         | 
| 421 447 | 
             
                #  * If +extra_options+ has an item with key +:filter_field+,
         | 
| 422 448 | 
             
                #    it uses its value instead of +method+
         | 
| 423 | 
            -
                def dependent_select_calculate_observed_field_id(object_name, method,  | 
| 424 | 
            -
                   | 
| 425 | 
            -
             | 
| 426 | 
            -
                   | 
| 427 | 
            -
             | 
| 428 | 
            -
                  end
         | 
| 449 | 
            +
                def dependent_select_calculate_observed_field_id(object_name, object, method, 
         | 
| 450 | 
            +
                  html_options, extra_options)
         | 
| 451 | 
            +
                  
         | 
| 452 | 
            +
                  return extra_options[:complete_filter_field] if extra_options.has_key? :complete_filter_field
         | 
| 453 | 
            +
                  method = extra_options[:filter_field] if extra_options.has_key? :filter_field
         | 
| 429 454 |  | 
| 430 | 
            -
                   | 
| 455 | 
            +
                  return dependent_select_calculate_field_id(object_name, object, method, html_options)
         | 
| 431 456 | 
             
                end
         | 
| 432 457 |  | 
| 433 | 
            -
                # calculates  | 
| 434 | 
            -
                def  | 
| 435 | 
            -
                   | 
| 436 | 
            -
                   | 
| 437 | 
            -
             | 
| 438 | 
            -
                  return "#{sanitized_object_name}_#{sanitized_method_name}"
         | 
| 458 | 
            +
                # calculates the id of a generated field
         | 
| 459 | 
            +
                def dependent_select_calculate_field_id(object_name, object, method, html_options)
         | 
| 460 | 
            +
                  field_id, it = dependent_select_calculate_field_id_and_it(object_name, object, method, html_options)
         | 
| 461 | 
            +
                  return field_id
         | 
| 439 462 | 
             
                end
         | 
| 440 | 
            -
             | 
| 463 | 
            +
                
         | 
| 464 | 
            +
                # ugly hack used to obtain the generated id from a form_helper
         | 
| 465 | 
            +
                # uses the method ActionView::Helpers::InstanceTag.add_default_name_and_id,
         | 
| 466 | 
            +
                # ...which is a private method of an internal class of rails. filty.
         | 
| 467 | 
            +
                def dependent_select_calculate_field_id_and_it(object_name, object, method, html_options)
         | 
| 468 | 
            +
                  it = ActionView::Helpers::InstanceTag.new(object_name, method, self, object)
         | 
| 469 | 
            +
                  html_options = html_options.stringify_keys
         | 
| 470 | 
            +
                  it.send :add_default_name_and_id, html_options #use send since add_default... is private
         | 
| 471 | 
            +
                  return [ html_options['id'], it]
         | 
| 472 | 
            +
                end
         | 
| 473 | 
            +
                
         | 
| 441 474 | 
             
                # Returns the collection that will be used when the dependent_select is first displayed
         | 
| 442 475 | 
             
                # (before even the first update_dependent_select call is done)
         | 
| 443 476 | 
             
                # The collection is obained by taking all the elements in collection whose value
         |