Skip to content

Instantly share code, notes, and snippets.

@flyingzumwalt
Last active December 16, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flyingzumwalt/5446323 to your computer and use it in GitHub Desktop.
Save flyingzumwalt/5446323 to your computer and use it in GitHub Desktop.
Conventions when writing "plugins" for Hydra Apps

Conventions

  • Implement a Rails Engine with engine_name defined
  • Expect host application to use mount if they want to inherit routes

Implement a Rails Engine with engine_name defined

Example:

module Hydra
  module Collections
    class Engine < ::Rails::Engine
      engine_name "collections"
    end
  end
end

Expect host application to use mount if they want to inherit routes from your engine

# config/routes.rb in the host application
Internal::Application.routes.draw do
  mount Hydra::Collections::Engine => '/'
  root :to => "catalog#index"
  ...
end

Things to remember

Routes in Views & Forms

In all views within the gem, reference routes using the engine name you've declared

Example:

<%= button_to label, collections.new_collection_path, :method=>:get %>

When using form_for, tell it to use the engine.

The form_for helper generates the form's @action url for you. In order to ensure that the form knows how to generate that path, tell it to use the engine by passing an Array rather than just the instance.

Example:

<%= form_for([collections, @collection])` do %>

Routes in Specs

Set your controller specs to use the engine's routes

In your spec_helper.rb, set your controller tests to use the routes from the current engine

Example

# spec/spec_helper.rb
RSpec.configure do |config|
  ...
  config.before(:each, :type=>"controller") { @routes = Hydra::Collections::Engine.routes }
end

How tests can reference routes that aren't defined by this gem

If you've set the routes in your spec_helper using the method above, then tests will only have access to routes defined by the engine you're testing. If you need to reference routes/paths that come from other dependencies, use Rails.application.routes.url_helpers

Example:

response.should redirect_to(Rails.application.routes.url_helpers.catalog_index_path("collection[]"=>assigns[:collection].id))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment