Sha256: 9ff2d3dbf00f08d1467a9c3d2a83800ba1c938b708bdf8c5a0947bcb8144dd18

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

require 'action_view'
require 'rabl'

module Gon
  module Rabl
    class << self

      def handler(args, global = false)
        options = parse_options_from args, global
        if global && !options[:template]
          raise 'You should provide :template when use rabl with global variables'
        end

        include_helpers

        data = parse_rabl \
          Gon::Base.get_template_path(options, 'rabl'),
          Gon::Base.get_controller(options)

        [data, options]
      end

      private

      def parse_rabl(rabl_path, controller)
        source = File.read(rabl_path)
        rabl_engine = ::Rabl::Engine.new(source, :format => 'json')
        output = rabl_engine.render(controller, {})
        JSON.parse(output)
      end

      def parse_options_from(args, global)
        if old_api? args
          unless global
            text =  "[DEPRECATION] view_path argument is now optional. "
            text << "If you need to specify it, "
            text << "please use gon.rabl(:template => 'path')"
            warn text
          end

          args.extract_options!.merge(:template => args[0])
        elsif new_api? args
          args.first
        else
          {}
        end
      end

      def include_helpers
        unless ::Rabl::Engine.include? ::ActionView::Helpers
          ::Rabl::Engine.send(:include, ::ActionView::Helpers)
        end
      end

      def old_api?(args)
        args.first.is_a? String
      end

      def new_api?(args)
        args.first.is_a? Hash
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gon-3.0.5 lib/gon/rabl.rb
gon-3.0.4 lib/gon/rabl.rb
gon-3.0.3 lib/gon/rabl.rb
gon-3.0.2 lib/gon/rabl.rb
gon-3.0.0 lib/gon/rabl.rb