angular_options_for_select

This commit is contained in:
Rohan Mitchell
2012-11-22 11:24:05 +11:00
parent 216125c7a1
commit ad76e66944
4 changed files with 23 additions and 26 deletions

View File

@@ -23,22 +23,4 @@ class AngularFormBuilder < ActionView::Helpers::FormBuilder
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

@@ -0,0 +1,16 @@
module AngularFormHelper
def angular_options_for_select(container, angular_field=nil)
return container if String === container
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="#{angular_field} == '#{value}'") if angular_field
%(<option value="#{ERB::Util.html_escape(value)}"#{selected_attribute}#{html_attributes}>#{ERB::Util.html_escape(text)}</option>)
end.join("\n").html_safe
end
end
class ActionView::Helpers::InstanceTag
include AngularFormHelper
end

View File

@@ -1,5 +1,5 @@
module EnterpriseFeesHelper
def enterprise_fee_options
def enterprise_fee_type_options
EnterpriseFee::FEE_TYPES.map { |f| [f.capitalize, f] }
end
end

View File

@@ -17,32 +17,31 @@
%th Calculator values
%th
%tbody
-# -- Finished product
= enterprise_fee_set_form.angular_fields_for :collection do |f| # regular fields_for collection.first ??
/ -- Finished product
= enterprise_fee_set_form.angular_fields_for :collection do |f|
%tr{'ng-repeat' => 'enterprise_fee in enterprise_fees'}
%td=# f.collection_select :enterprise_id, Enterprise.all, :id, :name, :include_blank => true
%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
/ -- 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= select_tag :fee_type, angular_options_for_select(enterprise_fee_type_options, '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
/ -- Plain old Rails
= enterprise_fee_set_form.fields_for :collection do |f|
- enterprise_fee = f.object
%tr
%td= f.collection_select :enterprise_id, Enterprise.all, :id, :name, :include_blank => true
%td= f.select :fee_type, enterprise_fee_options
%td= f.select :fee_type, enterprise_fee_type_options
%td= f.text_field :name
%td= f.collection_select :calculator_type, @calculators, :name, :description, {}, {:class => 'calculator_type'}
%td