Module: Extjsizable::CoreExt::Array::ExtJs

Extended by:
ActiveSupport::Concern
Defined in:
lib/extjsizable/core_ext/array/extjs.rb

Instance Method Summary collapse

Instance Method Details

#dasherize_hash_keys(h, dash_key = '') ⇒ Object

Dasherize keys that => {:b => :c } becomes to { ‘a_b’ => :c }



33
34
35
36
37
38
39
# File 'lib/extjsizable/core_ext/array/extjs.rb', line 33

def dasherize_hash_keys(h, dash_key = '')
  return { dash_key => h } unless h.is_a?(Hash)

  h.reduce({}) do |nh, (k, v)|
    nh.merge(dasherize_hash_keys(v, dash_key.blank? ? k.to_s : "#{dash_key}_#{k.to_s}"))
  end
end

#to_extjs(options = {}) ⇒ Object

Creates a JSON object by specifying which attributes we want to be shown. Ej: { ‘total’ : 2,

'data' : [
   { 'id' : 1, :nombre : 'Juan'  },
   { 'id' : 2, :nombre : 'Pedro' }
 ]

}



22
23
24
25
26
27
28
29
30
# File 'lib/extjsizable/core_ext/array/extjs.rb', line 22

def to_extjs(options = {})
  array_json_data = self.as_json(options)

  if ::Array.dasherize_keys?
    array_json_data.map! { |h| dasherize_hash_keys(h) }
  end

  { :total => (options.delete(:total) || self.length), :data => array_json_data }.with_indifferent_access
end