Sha256: 3c78b18172f3e981ef18688e776f02b175ed655fa959d85a43a2b06b87626c3c

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

require "as_json_with_includes/version"

module AsJsonWithIncludes
  module BasePatch
    def as_json(options = {})
      super(options).merge(serializable_hash(options))
    end
  end

  module CalculationsPatch
    def convert_activerecord_includes_to_json_includes(active_record_includes)
      return active_record_includes if active_record_includes.is_a? Symbol
      temp_arr = []
      temp_hash = {}
      if active_record_includes.is_a? Array
        active_record_includes.each do |item|
          temp_arr << convert_activerecord_includes_to_json_includes(item)
        end
        return temp_arr
      end
      if active_record_includes.is_a? Hash
        active_record_includes.each do |key, value|
          temp_hash[key] = {include: convert_activerecord_includes_to_json_includes(value)}
        end
        temp_hash
      end
    end

    def as_json_with_includes(options)
      active_record_includes = options[:includes]
      return as_json(options) if active_record_includes.nil?
      json_inclusions = convert_activerecord_includes_to_json_includes(active_record_includes)
      options[:include] = json_inclusions
      options[:includes] = nil
      includes(active_record_includes).as_json(options)
    end
  end
end

ActiveRecord::Base.send(:include, AsJsonWithIncludes::BasePatch)
ActiveRecord::Calculations.send(:include, AsJsonWithIncludes::CalculationsPatch)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
as_json_with_includes-0.1.2 lib/as_json_with_includes.rb
as_json_with_includes-0.1.1 lib/as_json_with_includes.rb