Module: Extjsizable::ActiveRecord::ExtJs
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/extjsizable/active_record/extjs.rb
Instance Method Summary collapse
- #to_extjs(options = {}) ⇒ Object
-
#wrap_hash_with_brackets(h, model_name) ⇒ Object
Wrap with brackets so that => {:b => :c } becomes to { ‘model’ : : ‘c’ }.
Instance Method Details
#to_extjs(options = {}) ⇒ Object
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/extjsizable/active_record/extjs.rb', line 12 def to_extjs( = {}) success = .delete(:success) underscored_class_name = self.class.to_s.demodulize.underscore if success || (success.nil? && errors.empty?) # returns success/data to load a form: # { # "data": { # "id": 1, # "title": "First Post", # "body": "This is my first post.", # "published": true, ... # }, # "success": true # } # # If ActiveRecord::Base.include_root_in_json is true then, the model name is used instead of data key: # { # "post": { # "id": 1, # "title": "First Post", # "body": "This is my first post.", # "published": true, ... # }, # "success": true # } # If ActiveRecord::Base.wrap_with_brackets is true then, the model name is prefixed into data key and all keys are surrounded with brackets: # { # "data": { # "post[id]": 1, # "post[title]": "First Post", # "post[body]": "This is my first post.", # "post[published]": true, ... # }, # "success": true # } h_json_data = self.as_json() h_json_data = { :data => h_json_data } unless ::ActiveRecord::Base.include_root_in_json? h_json_data[h_json_data.keys.first] = wrap_hash_with_brackets(h_json_data.values.first, underscored_class_name) if ::ActiveRecord::Base.wrap_with_brackets? { :success => true }.merge(h_json_data) else # retrieves no-success/errors to the form: # { # "errors": { "title": "Title can't be blank", ... }, # "success": false # } h_json_data = errors.as_json() h_json_data = wrap_hash_with_brackets(h_json_data, underscored_class_name) if ::ActiveRecord::Base.wrap_with_brackets? { :success => false, :errors => h_json_data.with_indifferent_access } end.with_indifferent_access end |
#wrap_hash_with_brackets(h, model_name) ⇒ Object
Wrap with brackets so that => {:b => :c } becomes to { ‘model’ : : ‘c’ }
69 70 71 72 73 74 |
# File 'lib/extjsizable/active_record/extjs.rb', line 69 def wrap_hash_with_brackets(h, model_name) h.reduce({}) do |nh, (k, v)| nh["#{model_name.to_s}[#{k.to_s}]"] = v nh end end |