angular_collection_select

This commit is contained in:
Rohan Mitchell
2012-11-22 11:44:32 +11:00
parent e64a86f5be
commit 873937db47
3 changed files with 18 additions and 4 deletions

View File

@@ -20,7 +20,12 @@ class AngularFormBuilder < ActionView::Helpers::FormBuilder
@template.text_field_tag name, value, :id => id
end
def angular_select(method, choices, angular_field, options = {}, html_options = {})
@template.select_tag method, @template.angular_options_for_select(choices, angular_field)
def angular_select(method, choices, angular_field, options = {})
@template.select_tag method, @template.angular_options_for_select(choices, angular_field), options
end
def angular_collection_select(method, collection, value_method, text_method, angular_field, options = {})
@template.select_tag method, @template.angular_options_from_collection_for_select(collection, value_method, text_method, angular_field), options
end
end

View File

@@ -9,8 +9,17 @@ module AngularFormHelper
%(<option value="#{ERB::Util.html_escape(value)}"#{selected_attribute}#{html_attributes}>#{ERB::Util.html_escape(text)}</option>)
end.join("\n").html_safe
end
def angular_options_from_collection_for_select(collection, value_method, text_method, angular_field)
options = collection.map do |element|
[element.send(text_method), element.send(value_method)]
end
angular_options_for_select(options, angular_field)
end
end
class ActionView::Helpers::InstanceTag
include AngularFormHelper
end

View File

@@ -20,10 +20,10 @@
/ -- 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_collection_select :enterprise_id, Enterprise.all, :id, :name, 'enterprise_fee.enterprise_id', :include_blank => true
%td= f.angular_select :fee_type, enterprise_fee_type_options, 'enterprise_fee.fee_type'
%td= f.angular_text_field :name
%td=# f.collection_select :calculator_type, @calculators, :name, :description, {}, {:class => 'calculator_type'}
%td= f.angular_collection_select :calculator_type, @calculators, :name, :description, 'enterprise_fee.calculator_type', {:class => 'calculator_type'}
/ -- Manual HTML / form_tag
%tr{'ng-repeat' => 'enterprise_fee in enterprise_fees'}