Module: StandardAPI::Includes
- Defined in:
- lib/standard_api/includes.rb
Class Method Summary collapse
-
.normalize(includes) ⇒ Object
:x => { x: {} } [:x, :y] => { x: {}, y: {} } [ { x: true }, { y: true } ] => { x: {}, y: {} } { x: true, y: true } => { x: {}, y: {} } { x: { y: true } } => { x: { y: {} } } { x: [:y] } => { x: { y: {} } } { x: { where: { y: false } } } => { x: { where: { y: false } } } { x: { order: { y: :asc } } } => { x: { order: { y: :asc } } }.
-
.sanitize(includes, permit, normalized = false) ⇒ Object
sanitize(=> {}, [:key]) # => => {} sanitize(=> {}, => true) # => => {} sanitize(=> {}, :value => {}}, [:key]) => # Raises ParseError sanitize(=> {}, :value => {}}, => true) => # Raises ParseError sanitize(=> {:value => {}}, => [:value]) # => => {:value => {}} sanitize(=> {:value => {}}, => {:value => true}) # => => {:value => {}} sanitize(=> {:value => {}}, [:key]) => # Raises ParseError.
Class Method Details
.normalize(includes) ⇒ Object
:x => { x: {} }
- :x, :y
-
> { x: {}, y: {} }
- { x: true }, { y: true }
-
> { x: {}, y: {} }
{ x: true, y: true } => { x: {}, y: {} } { x: { y: true } } => { x: { y: {} } } { x: [:y] } => { x: { y: {} } } { x: { where: { y: false } } } => { x: { where: { y: false } } } { x: { order: { y: :asc } } } => { x: { order: { y: :asc } } }
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 |
# File 'lib/standard_api/includes.rb', line 12 def self.normalize(includes) normalized = ActiveSupport::HashWithIndifferentAccess.new case includes when Array includes.flatten.compact.each { |v| normalized.merge!(normalize(v)) } when Hash, ActionController::Parameters includes.each_pair do |k, v| normalized[k] = case k.to_s when 'when', 'where', 'order' case v when Hash then v.to_h when ActionController::Parameters then v.to_unsafe_h end when 'limit' case v when String then v.to_i when Integer then v end when 'distinct' case v when 'true' then true when 'false' then false end when 'distinct_on' case v when String then v when Array then v end else normalize(v) end end when nil {} else if ![true, 'true'].include?(includes) normalized[includes] = {} end end normalized end |
.sanitize(includes, permit, normalized = false) ⇒ Object
sanitize(=> {}, [:key]) # => => {} sanitize(=> {}, => true) # => => {} sanitize(=> {}, :value => {}}, [:key]) => # Raises ParseError sanitize(=> {}, :value => {}}, => true) => # Raises ParseError sanitize(=> {:value => {}}, => [:value]) # => => {:value => {}} sanitize(=> {:value => {}}, => {:value => true}) # => => {:value => {}} sanitize(=> {:value => {}}, [:key]) => # Raises ParseError
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/standard_api/includes.rb', line 63 def self.sanitize(includes, permit, normalized=false) includes = normalize(includes) if !normalized permitted = ActiveSupport::HashWithIndifferentAccess.new if permit.is_a?(Array) permit = permit.inject({}) { |acc, v| acc[v] = true; acc } end permit = normalize(permit.with_indifferent_access) includes.each do |k, v| permitted[k] = if permit.has_key?(k) sanitize(v, permit[k] || {}, true) elsif ['limit', 'when', 'where', 'order', 'distinct', 'distinct_on'].include?(k.to_s) v else raise StandardAPI::UnpermittedParameters.new([k]) end end permitted end |