asset_pipeline_routes 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +35 -0
 - data/Rakefile +6 -0
 - data/lib/asset_pipeline_routes/routes_context.rb +9 -0
 - data/lib/asset_pipeline_routes/routes_helper.rb +21 -0
 - data/lib/asset_pipeline_routes/version.rb +3 -0
 - data/lib/asset_pipeline_routes.rb +16 -0
 - data/spec/asset_pipeline_routes_spec.rb +43 -0
 - data/spec/spec_helper.rb +7 -0
 - metadata +75 -0
 
    
        data/README.md
    ADDED
    
    | 
         @@ -0,0 +1,35 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # asset\_pipeline\_routes
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            Getting your Rails routes into the Rails 3.2 asset pipeline is really easy. Just
         
     | 
| 
      
 4 
     | 
    
         
            +
            `include Rails.application.routes.url_helpers` and you have all your routes available.
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            But except for hard-coded links this won't help you, because all resource links dynamic params at compile-time to work, like `{:id => 42}`. Without supplying them you won't get anywhere.
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
            Heh, you might think! Just call a route helper and pass in a dynamic parameter mapping, like
         
     | 
| 
      
 9 
     | 
    
         
            +
            `user_path('{{id}}')`. Sadly this won't yield the desired result! Instead of `/users/{{id}}`, you'll be presented with `/users/%7B%7Bid%7D%7D` because you're mapping just got html_escaped!
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            This is where handlebars\_routes\_assets comes to the rescue!
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            # What it does
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
            It adds a helper object to your `Sprocket::Context`, called `r`. Using
         
     | 
| 
      
 16 
     | 
    
         
            +
            `r` you can access all your routes you'd normally do, except that all those route fragments
         
     | 
| 
      
 17 
     | 
    
         
            +
            are actually changed into Handlebars attribute bindings.
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
            Here's an example, assuming you got a routes.rb with
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                resources :users # => yields multiple routes, e.g. /users/:id(.:format)
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
            in it. Then, in you're javascript file you'd call `r.users_path` instead of `users_path` to
         
     | 
| 
      
 24 
     | 
    
         
            +
            get the Handlebars-version:
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
                r.user_path # => yields /users/{{id}}
         
     | 
| 
      
 27 
     | 
    
         
            +
                r.users_path # => yields /users
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
            You can even hook up member- or collection routes, whatever you like. Just prefix your routes with `r.` and you can directly use them in your view!
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
            Of course, this assumes you want Handlebars.js compatible attribute-bindings. Luckily, the auto-generated mappings should work for Mustache.JS as well ;)
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
            # Addendum
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
            Now, if you happen to use haml\_assets to be able to use HAML in your asset pipeline, you could easily create forms to be used in Backbone.js or similar - because you can add an url option which correctly binds to your context!
         
     | 
    
        data/Rakefile
    ADDED
    
    
| 
         @@ -0,0 +1,21 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require_relative "routes_context"
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module AssetPipelineRoutes
         
     | 
| 
      
 4 
     | 
    
         
            +
              class RoutesHelper
         
     | 
| 
      
 5 
     | 
    
         
            +
                def initialize(routes, default_block = '{{\1}}')
         
     | 
| 
      
 6 
     | 
    
         
            +
                  routes.each do |route|
         
     | 
| 
      
 7 
     | 
    
         
            +
                    next if route.name.nil? # only handle named_routes
         
     | 
| 
      
 8 
     | 
    
         
            +
                    
         
     | 
| 
      
 9 
     | 
    
         
            +
                    self.class.instance_eval do
         
     | 
| 
      
 10 
     | 
    
         
            +
                      define_method :"#{route.name}_path" do |id_replacement = default_block|
         
     | 
| 
      
 11 
     | 
    
         
            +
                        proc { |route, mapping| build_url route, mapping }.curry[route].call id_replacement
         
     | 
| 
      
 12 
     | 
    
         
            +
                      end
         
     | 
| 
      
 13 
     | 
    
         
            +
                    end
         
     | 
| 
      
 14 
     | 
    
         
            +
                  end
         
     | 
| 
      
 15 
     | 
    
         
            +
                end
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                def build_url route, mapping
         
     | 
| 
      
 18 
     | 
    
         
            +
                  route.path.ast.to_s.gsub(/\(\.:\w+\)/,'').gsub(/:(\w+)/, mapping).to_s
         
     | 
| 
      
 19 
     | 
    
         
            +
                end
         
     | 
| 
      
 20 
     | 
    
         
            +
              end
         
     | 
| 
      
 21 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,16 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require_relative "asset_pipeline_routes/version"
         
     | 
| 
      
 2 
     | 
    
         
            +
            require_relative "asset_pipeline_routes/routes_helper"
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            module AssetPipelineRoutes
         
     | 
| 
      
 5 
     | 
    
         
            +
              class Railtie < ::Rails::Railtie
         
     | 
| 
      
 6 
     | 
    
         
            +
                initializer "asset_pipeline_routes.environment" do |app|
         
     | 
| 
      
 7 
     | 
    
         
            +
                 	ActiveSupport.on_load(:action_view) do
         
     | 
| 
      
 8 
     | 
    
         
            +
                 	  include ::AssetPipelineRoutes::RoutesContext
         
     | 
| 
      
 9 
     | 
    
         
            +
                    
         
     | 
| 
      
 10 
     | 
    
         
            +
                 	  app.assets.context_class.instance_eval do
         
     | 
| 
      
 11 
     | 
    
         
            +
                 	    include ::AssetPipelineRoutes::RoutesContext
         
     | 
| 
      
 12 
     | 
    
         
            +
                    end
         
     | 
| 
      
 13 
     | 
    
         
            +
                 	end
         
     | 
| 
      
 14 
     | 
    
         
            +
                end
         
     | 
| 
      
 15 
     | 
    
         
            +
              end
         
     | 
| 
      
 16 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,43 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            def build_route name, path
         
     | 
| 
      
 4 
     | 
    
         
            +
              OpenStruct.new({:name => name, :path => OpenStruct.new(:ast => path)})
         
     | 
| 
      
 5 
     | 
    
         
            +
            end
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            describe AssetPipelineRoutes do
         
     | 
| 
      
 8 
     | 
    
         
            +
              # the @route property basically describes a RouteSet#routes object
         
     | 
| 
      
 9 
     | 
    
         
            +
              # as Rails.application.routes.routes[:index] returns it
         
     | 
| 
      
 10 
     | 
    
         
            +
              describe 'resources#index' do
         
     | 
| 
      
 11 
     | 
    
         
            +
                before { @route = build_route 'users', '/users(.:format)' }
         
     | 
| 
      
 12 
     | 
    
         
            +
                subject { AssetPipelineRoutes::RoutesHelper.new [@route] }
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
                it { should respond_to(:users_path) }
         
     | 
| 
      
 15 
     | 
    
         
            +
                its(:users_path) { should eql '/users'}
         
     | 
| 
      
 16 
     | 
    
         
            +
                it { subject.users_path('\d+').should eql('/users') }
         
     | 
| 
      
 17 
     | 
    
         
            +
              end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
              describe 'resources#show' do
         
     | 
| 
      
 20 
     | 
    
         
            +
                before { @route = build_route 'user', '/users/:id(.:format)' }
         
     | 
| 
      
 21 
     | 
    
         
            +
                subject { AssetPipelineRoutes::RoutesHelper.new [@route] }
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                it { should respond_to(:user_path) }
         
     | 
| 
      
 24 
     | 
    
         
            +
                its(:user_path) { should eql('/users/{{id}}') }
         
     | 
| 
      
 25 
     | 
    
         
            +
                it { subject.user_path('\d+').should eql('/users/\d+') }
         
     | 
| 
      
 26 
     | 
    
         
            +
              end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
              describe 'resources#edit' do
         
     | 
| 
      
 29 
     | 
    
         
            +
                before { @route = build_route 'edit_user', '/users/:id/edit(.:format)' }
         
     | 
| 
      
 30 
     | 
    
         
            +
                subject { AssetPipelineRoutes::RoutesHelper.new [@route] }
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
                it { should respond_to(:edit_user_path) }
         
     | 
| 
      
 33 
     | 
    
         
            +
                its(:edit_user_path) { should eql('/users/{{id}}/edit') }
         
     | 
| 
      
 34 
     | 
    
         
            +
                it { subject.edit_user_path('\d+').should eql('/users/\d+/edit') }
         
     | 
| 
      
 35 
     | 
    
         
            +
              end
         
     | 
| 
      
 36 
     | 
    
         
            +
              
         
     | 
| 
      
 37 
     | 
    
         
            +
              describe 'resources without name' do
         
     | 
| 
      
 38 
     | 
    
         
            +
                before { @route = build_route nil, '/foo' }
         
     | 
| 
      
 39 
     | 
    
         
            +
                subject { AssetPipelineRoutes::RoutesHelper.new [@route] }
         
     | 
| 
      
 40 
     | 
    
         
            +
                
         
     | 
| 
      
 41 
     | 
    
         
            +
                it { subject.should_not_receive(:build_url) }
         
     | 
| 
      
 42 
     | 
    
         
            +
              end
         
     | 
| 
      
 43 
     | 
    
         
            +
            end
         
     | 
    
        data/spec/spec_helper.rb
    ADDED
    
    
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,75 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: asset_pipeline_routes
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.1
         
     | 
| 
      
 5 
     | 
    
         
            +
              prerelease: 
         
     | 
| 
      
 6 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 7 
     | 
    
         
            +
            authors:
         
     | 
| 
      
 8 
     | 
    
         
            +
            - Raphael Randschau
         
     | 
| 
      
 9 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 10 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 11 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2012-01-30 00:00:00.000000000 Z
         
     | 
| 
      
 13 
     | 
    
         
            +
            dependencies:
         
     | 
| 
      
 14 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 15 
     | 
    
         
            +
              name: rails
         
     | 
| 
      
 16 
     | 
    
         
            +
              requirement: &70123436899600 !ruby/object:Gem::Requirement
         
     | 
| 
      
 17 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 18 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 19 
     | 
    
         
            +
                - - ~>
         
     | 
| 
      
 20 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 21 
     | 
    
         
            +
                    version: 3.2.0
         
     | 
| 
      
 22 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 23 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 24 
     | 
    
         
            +
              version_requirements: *70123436899600
         
     | 
| 
      
 25 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 26 
     | 
    
         
            +
              name: rspec
         
     | 
| 
      
 27 
     | 
    
         
            +
              requirement: &70123444734540 !ruby/object:Gem::Requirement
         
     | 
| 
      
 28 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 29 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 30 
     | 
    
         
            +
                - - ! '>='
         
     | 
| 
      
 31 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 32 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 33 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 34 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 35 
     | 
    
         
            +
              version_requirements: *70123444734540
         
     | 
| 
      
 36 
     | 
    
         
            +
            description: Add a routes helper for all asset pipeline needs
         
     | 
| 
      
 37 
     | 
    
         
            +
            email:
         
     | 
| 
      
 38 
     | 
    
         
            +
            - nicolai86@me.com
         
     | 
| 
      
 39 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 40 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 41 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 42 
     | 
    
         
            +
            files:
         
     | 
| 
      
 43 
     | 
    
         
            +
            - lib/asset_pipeline_routes/routes_context.rb
         
     | 
| 
      
 44 
     | 
    
         
            +
            - lib/asset_pipeline_routes/routes_helper.rb
         
     | 
| 
      
 45 
     | 
    
         
            +
            - lib/asset_pipeline_routes/version.rb
         
     | 
| 
      
 46 
     | 
    
         
            +
            - lib/asset_pipeline_routes.rb
         
     | 
| 
      
 47 
     | 
    
         
            +
            - spec/asset_pipeline_routes_spec.rb
         
     | 
| 
      
 48 
     | 
    
         
            +
            - spec/spec_helper.rb
         
     | 
| 
      
 49 
     | 
    
         
            +
            - README.md
         
     | 
| 
      
 50 
     | 
    
         
            +
            - Rakefile
         
     | 
| 
      
 51 
     | 
    
         
            +
            homepage: ''
         
     | 
| 
      
 52 
     | 
    
         
            +
            licenses: []
         
     | 
| 
      
 53 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 54 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 55 
     | 
    
         
            +
            require_paths:
         
     | 
| 
      
 56 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 57 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 58 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 59 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 60 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 61 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 62 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 63 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 64 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 65 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 66 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 67 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 68 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 69 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 70 
     | 
    
         
            +
            rubyforge_project: asset_pipeline_routes
         
     | 
| 
      
 71 
     | 
    
         
            +
            rubygems_version: 1.8.15
         
     | 
| 
      
 72 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 73 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 74 
     | 
    
         
            +
            summary: Add a routes helper for all asset pipeline needs
         
     | 
| 
      
 75 
     | 
    
         
            +
            test_files: []
         
     |