Module: ChinaRegionFu::Utils
- Included in:
- Helpers::FormHelper, SimpleForm::RegionInput, RegionInput
- Defined in:
- lib/china_region_fu/helpers/utils.rb
Instance Method Summary collapse
- #append_html_class_option(html_options) ⇒ Object
- #append_html_data_option(current_region, sub_region, html_options) ⇒ Object
- #append_html_options(current_region, sub_region, html_options) ⇒ Object
- #append_prompt(current_region, options) ⇒ Object
- #china_region_fu_js ⇒ Object
Instance Method Details
#append_html_class_option(html_options) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/china_region_fu/helpers/utils.rb', line 49 def append_html_class_option() = .deep_dup [:class] ||= '' [:class] << ' china-region-select' unless [:class].include?('china-region-select') end |
#append_html_data_option(current_region, sub_region, html_options) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/china_region_fu/helpers/utils.rb', line 56 def append_html_data_option(current_region, sub_region, ) = .deep_dup [:data] ||= {} [:data][:region_name] = current_region if sub_region [:data][:sub_region] = sub_region else [:data].delete(:sub_region) end end |
#append_html_options(current_region, sub_region, html_options) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/china_region_fu/helpers/utils.rb', line 68 def (current_region, sub_region, ) = .deep_dup = append_html_class_option() = append_html_data_option(current_region, sub_region, ) end |
#append_prompt(current_region, options) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/china_region_fu/helpers/utils.rb', line 75 def append_prompt(current_region, ) current_name = current_region.to_s.sub(/_id\Z/, '') = .except(:province_prompt, :city_prompt, :district_prompt) [:prompt] = ["#{current_name}_prompt".to_sym] end |
#china_region_fu_js ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/china_region_fu/helpers/utils.rb', line 6 def china_region_fu_js js = <<-JAVASCRIPT <script type="text/javascript"> //<![CDATA[ window.chinaRegionFu = window.chinaRegionFu || {}; $(function(){ $('body').off('change', '.china-region-select').on('change', '.china-region-select', function(event) { var $self = $(event.currentTarget), $subRegionDom = $('[data-region-name="' + $self.data('sub-region') + '"]'), subName = $self.data('sub-region'), parentName = $self.data('region-name'), parentId = $self.val(); if ($subRegionDom.size() > 0 && subName && parentName && parentId) { $.getJSON('/china_region_fu/fetch_options', { columns: window.chinaRegionFu.fetchColumns || 'id,name', sub_name: subName, parent_name: parentName, parent_id: parentId }, function(json) { if (window.chinaRegionFu.ajaxDone) { window.chinaRegionFu.ajaxDone(json); } else { var options = []; $self.parent().nextAll().find('.china-region-select > option[value!=""]').remove() $.each(json.data, function(_, value) { options.push("<option value='" + value.id + "'>" + value.name + "</option>"); }); $subRegionDom.append(options.join('')); } }).fail(function(xhr, textStatus, error) { window.chinaRegionFu.ajaxFail && window.chinaRegionFu.ajaxFail(xhr, textStatus, error); }).always(function(event, xhr, settings) { window.chinaRegionFu.ajaxAlways && window.chinaRegionFu.ajaxAlways(event, xhr, settings); }); } }); }); //]]> </script> JAVASCRIPT js.html_safe end |