Class: Rails::Surrender::Render::Resource::Instance
- Inherits:
-
Object
- Object
- Rails::Surrender::Render::Resource::Instance
- Defined in:
- lib/rails/surrender/render/resource/instance.rb
Overview
Renders an instance resource
Instance Attribute Summary collapse
-
#ability ⇒ Object
readonly
Returns the value of attribute ability.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
-
#initialize(resource:, config:, ability:) ⇒ Instance
constructor
A new instance of Instance.
- #render ⇒ Object
Constructor Details
#initialize(resource:, config:, ability:) ⇒ Instance
Returns a new instance of Instance.
11 12 13 14 15 |
# File 'lib/rails/surrender/render/resource/instance.rb', line 11 def initialize(resource:, config:, ability:) @resource = resource @config = config @ability = ability end |
Instance Attribute Details
#ability ⇒ Object (readonly)
Returns the value of attribute ability.
9 10 11 |
# File 'lib/rails/surrender/render/resource/instance.rb', line 9 def ability @ability end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
9 10 11 |
# File 'lib/rails/surrender/render/resource/instance.rb', line 9 def config @config end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
9 10 11 |
# File 'lib/rails/surrender/render/resource/instance.rb', line 9 def resource @resource end |
Instance Method Details
#render ⇒ Object
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 |
# File 'lib/rails/surrender/render/resource/instance.rb', line 17 def render config.history.push history_class result = {} config.locally_included_attributes.each { |attr| result[attr.to_sym] = resource.send(attr) } config..each_key do |key| next if config.exclude_locally?(key) nested_resource_class = nested_class_for(resource, key) next if config.history.include? nested_resource_class nested_config = nested_config_for(nested_resource_class, key) if resource.class.reflections[key.to_s].try(:collection?) collection = resource.send(key.to_sym).select { |i| ability.can? :read, i } result[key] = Collection.new(resource: collection, config: nested_config, ability: ability).render else instance = resource.send(key) next if config.history.include? instance.class if ability.can?(:read, instance) result[key] = Instance.new(resource: instance, config: nested_config, ability: ability).render elsif instance.nil? result[key] = nil # represent an associated element as null if it's missing end end end result end |