Project

linkify

0.0
No commit activity in last 3 years
No release in over 3 years
Lists all the available routes in your rails app, to add internal linking to admin pages
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

Linkify

Linkify makes it possible to list all records from specific models in a grouped_options_for_select array. This can by very useful for internal linking. It doesn't return absolute or relative paths, but instead gives you a pointer to the model name and identifier (usually ID) of a record, so you'll have to create your own logic for saving this information serialized and a helper method to generate links from it afterwards.

Why is this better than saving a path as a string in the database?

Because sometimes your path's change, due to your logic or due to internationlization.

So this won't work right out of the box?

Nope, you'll have to write some code yourself to save the data. We suggest to save it serialized. And perhaps you want to write a helper method te regenerate links from the saved data.

Drop down with internal links

Installation

Add this line to your application's Gemfile:

gem 'linkify'

And then execute:

$ bundle

Or install it yourself as:

$ gem install linkify

Example output

<select>
  <optgroup label="Artikel">
    <option value="["Article", 207]">Voluptatem In Sit Ut Fugit Totam Suscipit</option>
    <option value="["Article", 208]">Minima Id Laboriosam Itaque Minus</option>
    <option value="["Article", 209]">Aut Et Est</option>
    <option value="["Article", 210]">Sunt Commodi Itaque</option>
  </optgroup>
</select>

Usage

Want to add a 'link to page' drop down in a form? Simply call the internal_links method for the options_for_select parameter. It requires the current value as a parameter.

Example:

<%= form_for @link do |f| %>
  <%= f.select :url_options, internal_links(@link.url_options) %>
  ...
  <%= f.submit %>
<% end %>

Add models to the list

By default, no records will return if you call this method. For each model you want to list, you have to add this method to the model:

linkable_by :title, :id

or, to show only a selection of records:

linkable_by :title, :id, limit(10)

It takes three parameters:

  1. the name you'll see in the <option>
  2. the value that will be in the <option>
  3. the collection of records from this model (defaults to all but could also be a (named) scope or custom method)

Add extra pages (static pages for example)

Sometimes you want to not only list records from the database, but also static pages: home, contact, about, etc. To add these to the <select> too, you can pass them as a parameter to the internal_links method:

<%= f.select :url_options, internal_links(@link.url_options, [["contact", "/contact"], ["about", "/about"]]) %>

I don't like to link to URL's directly but prefer to link to a Rails helper method instead. For example:

# in the form
<%= f.select :url_options, internal_links(@link.url_options, [["contact", "contact_path"], ["about", "about_path"]]) %>

# in the place I need to show this link
<%= link_to @link.name, send(@link.path)%>

Sponsor

This gem is made by the smart people of Eskes Media.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request