Sha256: b0d8c18500da77ee99b1d98511198ee69b72aa3a8f7eba870ebff2cd1d233d9d

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

module JsRoutes

  class Railtie < ::Rails::Railtie

    config.before_configuration do
      config.action_view.javascript_expansions[:defaults] |= %w{ js_routes jquery.pathBuilder.js }
    end

    config.after_initialize do

      JsRoutes.generate_routes_file
      FileUtils.cp File.expand_path('../../assets/javascripts/jquery.pathBuilder.js', __FILE__), File.join(Rails.root, 'public', 'javascripts')

      if Rails.env.development?
        ApplicationController.class_eval do
          before_filter do
            JsRoutes.generate_routes_file
          end
        end
      end
    end

  end

  def self.generate_routes_file
    puts 'GENERATING ROUTES FILE'
    filename = File.join(Rails.root, 'public', 'javascripts', 'js_routes.js')
    FileUtils.rm filename, :force => true
    File.open(filename, 'w') do |f|
      f << 'window.Routes = {};'
      Rails.application.reload_routes!
      Rails.application.routes.routes.each do |route|
        defaults = route.defaults

        unless defaults.present? && defaults.has?(:exclude_from_js) && defaults[:exclude_from_js]
          f << <<-JS.strip
            Routes.#{route.name}_path = function() {
                Array.prototype.unshift.call(arguments, '#{route.path}');
                return $.buildPath.apply($, arguments);
            };
          JS
        end
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
js_routes-0.1.8 lib/js_routes/railtie.rb