Skip to content

Commit

Permalink
enhancement: support for choice dropdown extended fields to allow use…
Browse files Browse the repository at this point in the history
…r supplied choices.
  • Loading branch information
Kieran Pilkington committed Mar 10, 2009
1 parent c4ad177 commit b716b92
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 8 additions & 2 deletions app/helpers/extended_fields_helper.rb
Expand Up @@ -349,8 +349,14 @@ def extended_field_choice_select_editor(name, value, options, extended_field, ch
:complete => "Element.hide('#{id_for_extended_field(extended_field)}_#{level}_spinner')")
}

select_tag("#{name}[#{level}]", option_tags, default_options.merge(options)) +
"<img src='/images/indicator.gif' width='16' height='16' alt='Getting choices. ' id='#{id_for_extended_field(extended_field)}_#{level}_spinner' style='display:none;' />"
html = select_tag("#{name}[#{level}][preset]", option_tags, default_options.merge(options))
html += "<img src='/images/indicator.gif' width='16' height='16' alt='Getting choices. ' id='#{id_for_extended_field(extended_field)}_#{level}_spinner' style='display:none;' />"
if extended_field.user_choice_addition?
user_supplied_id = "#{id_for_extended_field(extended_field)}_custom"
html += ' OR add your own '
html += text_field_tag("#{name}[#{level}][custom]", nil, :size => 10, :id => user_supplied_id)
end
html
end

def extended_field_choice_autocomplete_editor(name, value, options, extended_field, choices, level = 1)
Expand Down
14 changes: 13 additions & 1 deletion lib/extended_content_helpers.rb
Expand Up @@ -85,6 +85,18 @@ def extended_content_field_xml_tag(options = {})
xsi_type = options[:xsi_type] || nil
extended_field = options[:extended_field] || nil

# With choices from a dropdown, we can have preset dropdown and custom text field
# So before we go any further, make sure we convert the values from Hash to either
# the preset or custom value depending on which one is filled in
if extended_field.ftype == 'choice'
value.each do |key,choice_value|
next unless choice_value.is_a?(Hash)
choice = choice_value['preset'] # Preset values come from the dropdown
choice = choice_value['custom'] unless choice_value['custom'].blank? # Custom values come from a text field
value[key] = choice
end
end

options = {}
options.merge!(:xml_element_name => xml_element_name) unless xml_element_name.blank?
options.merge!(:xsi_type => xsi_type) unless xsi_type.blank?
Expand Down Expand Up @@ -112,7 +124,7 @@ def extended_content_field_xml_tag(options = {})
matching_choice = Choice.matching(l,v)

# Handle the creation of new choices where the choice is not recognised.
if !matching_choice && extended_field.ftype == 'autocomplete' && extended_field.user_choice_addition?
if !matching_choice && %w(autocomplete choice).include?(extended_field.ftype) && extended_field.user_choice_addition?
index = value.to_a.index([k, v])

to_check = v
Expand Down

0 comments on commit b716b92

Please sign in to comment.