Class: Jstreamer::RailsJson
- Defined in:
- lib/jstreamer/rails_json.rb
Overview
Rails extensions class - provides some helpers methods:
- 
view methods delegation 
- 
view variables access 
- 
render helpers 
Instance Attribute Summary
Attributes inherited from BaseJson
#current_model, #current_stream, #index, #options
Class Method Summary collapse
- 
  
    
      .render(obj = nil, **opts)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Helper to call controller’s render method on a single object. 
- 
  
    
      .render_collection(collection, **opts)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Helper to call controller’s render method on a collection. 
Instance Method Summary collapse
- 
  
    
      #view_context  ⇒ view_context 
    
    
  
  
  
  
  
  
  
  
  
    Returns view_context provided in options. 
- 
  
    
      #view_context_get(var_name)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Gets instance variable value from view_context. 
Methods inherited from BaseJson
#array, #call, #call_collection, delegated_options, #encode_value, #from, generate, generate_collection, #initialize, #merge_json, #object, #partial, #prop, #to_s, #transform_key, #view?
Constructor Details
This class inherits a constructor from Jstreamer::BaseJson
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method) ⇒ Object (private)
Automatically delegates methods to view_context provided in options
| 62 63 64 65 66 67 68 69 70 71 | # File 'lib/jstreamer/rails_json.rb', line 62 def method_missing(method, ...) return super if view_context.nil? || !view_context.respond_to?(method) # TODO: analogue of self.class.delegate(method, to: :view_context) self.class.define_method(method) do |*args| view_context.__send__(method, *args) end public_send(method, ...) end | 
Class Method Details
.render(obj = nil, **opts) ⇒ Object
Helper to call controller’s render method on a single object
| 33 34 35 | # File 'lib/jstreamer/rails_json.rb', line 33 def self.render(obj = nil, **opts) opts.fetch(:view_context).controller.render(json: generate(obj, **opts)) end | 
.render_collection(collection, **opts) ⇒ Object
Helper to call controller’s render method on a collection
| 40 41 42 | # File 'lib/jstreamer/rails_json.rb', line 40 def self.render_collection(collection, **opts) opts.fetch(:view_context).controller.render(json: generate_collection(collection, **opts)) end | 
Instance Method Details
#view_context ⇒ view_context
Returns view_context provided in options
| 46 47 48 49 50 | # File 'lib/jstreamer/rails_json.rb', line 46 def view_context # NOTE: useful view_context injection discussion # https://github.com/drapergem/draper/issues/124 .fetch(:view_context) end | 
#view_context_get(var_name) ⇒ Object
Gets instance variable value from view_context
| 55 56 57 | # File 'lib/jstreamer/rails_json.rb', line 55 def view_context_get(var_name) view_context.instance_variable_get(var_name) end |