Provide proper names for angular select fields, add angular hidden field

This commit is contained in:
Rohan Mitchell
2012-11-22 16:38:25 +11:00
parent fd0cc93e68
commit 52ebe2c495
2 changed files with 24 additions and 23 deletions

View File

@@ -13,19 +13,35 @@ class AngularFormBuilder < ActionView::Helpers::FormBuilder
# @fields_for_record_name --> :collection
# @object.send(@fields_for_record_name).first.class.to_s.underscore --> enterprise_fee
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, value, :id => id
@template.text_field_tag angular_name(method), value, :id => angular_id(method)
end
def angular_hidden_field(method, options = {})
value = "{{ #{@object.send(@fields_for_record_name).first.class.to_s.underscore}.#{method} }}"
@template.hidden_field_tag angular_name(method), value, :id => angular_id(method)
end
def angular_select(method, choices, angular_field, options = {})
@template.select_tag method, @template.angular_options_for_select(choices, angular_field), options.reverse_merge!({'ng-model' => angular_field})
options.reverse_merge!({'id' => angular_id(method)})
@template.select_tag angular_name(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.reverse_merge!({'ng-model' => angular_field})
options.reverse_merge!({'id' => angular_id(method)})
@template.select_tag angular_name(method), @template.angular_options_from_collection_for_select(collection, value_method, text_method, angular_field), options
end
private
def angular_name(method)
"#{@object_name}[#{@fields_for_record_name}_attributes][{{ $index }}][#{method}]"
end
def angular_id(method)
"#{@object_name}_#{@fields_for_record_name}_attributes_{{ $index }}_#{method}"
end
end

View File

@@ -20,28 +20,13 @@
/ -- Finished product
= enterprise_fee_set_form.angular_fields_for :collection do |f|
%tr{'ng-repeat' => 'enterprise_fee in enterprise_fees | filter:query'}
%td= f.angular_collection_select :enterprise_id, Enterprise.all, :id, :name, 'enterprise_fee.enterprise_id', :include_blank => true
%td
= f.angular_hidden_field :id
= 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.angular_collection_select :calculator_type, @calculators, :name, :description, 'enterprise_fee.calculator_type', {:class => 'calculator_type'}
%td{'ng-bind-html-unsafe' => 'enterprise_fee.calculator_settings'}
%td{'spree-delete-resource' => "1"}
%tr
%td ---
/ -- 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_type_options
%td= f.text_field :name
%td= f.collection_select :calculator_type, @calculators, :name, :description, {}, {:class => 'calculator_type'}
%td
- if !enterprise_fee.new_record?
.calculator-settings
= f.fields_for :calculator do |calculator_form|
= preference_fields(enterprise_fee.calculator, calculator_form)
%td= link_to_delete enterprise_fee unless enterprise_fee.new_record?
= enterprise_fee_set_form.submit 'Update'