Start building some selects

This commit is contained in:
Rohan Mitchell
2012-11-22 11:03:28 +11:00
parent d6802d9ae5
commit 216125c7a1
2 changed files with 32 additions and 6 deletions

View File

@@ -1,4 +1,6 @@
class AngularFormBuilder < ActionView::Helpers::FormBuilder
# TODO: Use ng_ prefix, like ng_fields_for
def angular_fields_for(record_name, *args, &block)
# TODO: Handle nested angular_fields_for
@fields_for_record_name = record_name
@@ -13,8 +15,30 @@ class AngularFormBuilder < ActionView::Helpers::FormBuilder
name = "#{@object_name}[#{@fields_for_record_name}_attributes][{{ $index }}][#{method}]"
id = "#{@object_name}_#{@fields_for_record_name}_attributes_{{ $index }}_#{method}"
value = "{{ #{@object.send(@fields_for_record_name).first.class.to_s.underscore}.#{method} }}"
@template.text_field_tag name, :id => id
@template.text_field_tag name, value, :id => id
end
def angular_select(method, choices, options = {}, html_options = {})
# ...
end
def angular_options_for_select(container, selected = nil)
return container if String === container
selected, disabled = extract_selected_and_disabled(selected).map do | r |
Array.wrap(r).map { |item| item.to_s }
end
container.map do |element|
html_attributes = option_html_attributes(element)
text, value = option_text_and_value(element).map { |item| item.to_s }
selected_attribute = %Q( ng-selected="#{selected}") if selected
disabled_attribute = ' disabled="disabled"' if disabled && option_value_selected?(value, disabled)
%(<option value="#{ERB::Util.html_escape(value)}"#{selected_attribute}#{disabled_attribute}#{html_attributes}>#{ERB::Util.html_escape(text)}</option>)
end.join("\n").html_safe
end
end

View File

@@ -17,25 +17,27 @@
%th Calculator values
%th
%tbody
= enterprise_fee_set_form.angular_fields_for :collection do |f|
-# -- Finished product
= enterprise_fee_set_form.angular_fields_for :collection do |f| # regular fields_for collection.first ??
%tr{'ng-repeat' => 'enterprise_fee in enterprise_fees'}
%td=# f.collection_select :enterprise_id, Enterprise.all, :id, :name, :include_blank => true
%td=# f.select :fee_type, enterprise_fee_options
%td=# f.angular_select :fee_type, enterprise_fee_options
%td= f.angular_text_field :name
%td=# f.collection_select :calculator_type, @calculators, :name, :description, {}, {:class => 'calculator_type'}
-# -- Manual HTML / form_tag
%tr{'ng-repeat' => 'enterprise_fee in enterprise_fees'}
%td
%select{:id => 'enterprise_fee_set_collection_attributes_{{ $index }}_enterprise_id', :name => "enterprise_fee_set[collection_attributes][{{ $index }}][enterprise_id]", 'ng-model' => 'enterprise_fee'}
%option{:value => ""}
- Enterprise.all.each do |enterprise|
%option{:value => enterprise.id, 'ng-selected' => "enterprise_fee.enterprise_id == #{enterprise.id}"}= enterprise.name
%td {{ enterprise_fee.fee_type }}
%td
{{ enterprise_fee.fee_type }}
%td= text_field_tag 'enterprise_fee_set[collection_attributes][{{ $index }}][name]', '{{ enterprise_fee.name }}', :id => 'enterprise_fee_set_collection_attributes_{{ $index }}_name'
%td {{ enterprise_fee.calculator_type }}
-# -- Plain old Rails
= enterprise_fee_set_form.fields_for :collection do |f|
- enterprise_fee = f.object
%tr