linkify 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +73 -0
- data/Rakefile +2 -0
- data/lib/linkify.rb +18 -0
- data/lib/linkify/models/active_record.rb +27 -0
- data/lib/linkify/models/base.rb +58 -0
- data/lib/linkify/railtie.rb +23 -0
- data/lib/linkify/version.rb +3 -0
- data/lib/linkify/view_helper.rb +45 -0
- data/linkify.gemspec +17 -0
- metadata +59 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Henk Meijer, Eskes Media
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Linkify
|
2
|
+
|
3
|
+
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 by default, but instead gives you the model name and identifier (usually ID) of a record, so you have'll to create your own logic for generating links from this information afterwards.
|
4
|
+
|
5
|
+
This gem integrates easily with any Rails app using ActiveRecord.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'Linkify'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install Linkify
|
20
|
+
|
21
|
+
## Example output
|
22
|
+
<select>
|
23
|
+
<optgroup label="Artikel">
|
24
|
+
<option value="["Article", 207]">Voluptatem In Sit Ut Fugit Totam Suscipit</option>
|
25
|
+
<option value="["Article", 208]">Minima Id Laboriosam Itaque Minus</option>
|
26
|
+
<option value="["Article", 209]">Aut Et Est</option>
|
27
|
+
<option value="["Article", 210]">Sunt Commodi Itaque</option>
|
28
|
+
</optgroup>
|
29
|
+
</select>
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
Want to add a 'link to page' drop down in a form, simply call the `internal_links` method for the options_for_select parameter.
|
33
|
+
|
34
|
+
Example:
|
35
|
+
|
36
|
+
<%= form_for @link do |f| %>
|
37
|
+
<%= f.select :url_options, internal_links %>
|
38
|
+
...
|
39
|
+
<%= f.submit %>
|
40
|
+
<% end %>
|
41
|
+
|
42
|
+
## Add models to the list
|
43
|
+
|
44
|
+
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:
|
45
|
+
|
46
|
+
linkable_by :title, :id
|
47
|
+
|
48
|
+
It takes two parameters:
|
49
|
+
|
50
|
+
1. the name you'll see in the `<option>`
|
51
|
+
2. the value that will be in the `<option>`
|
52
|
+
3. the find method to select records from this model (defaults to `all` but could also be a (named) scope or custom method)
|
53
|
+
|
54
|
+
## Add extra pages (static pages for example)
|
55
|
+
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:
|
56
|
+
|
57
|
+
<%= select_tag "url", internal_links([["contact", "/contact"], ["about", "/about"]]) %>
|
58
|
+
|
59
|
+
I don't like to link to URL's directly but prefer to link to a Rails helper method instead. For example:
|
60
|
+
|
61
|
+
# in the form
|
62
|
+
<%= select_tag "path", internal_links([["contact", "contact_path"], ["about", "about_path"]]) %>
|
63
|
+
|
64
|
+
# in the place I need to show this link
|
65
|
+
<%= link_to @link.name, send(@link.path)%>
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
1. Fork it
|
70
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
71
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
72
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
73
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/linkify.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "linkify/version"
|
2
|
+
require "linkify/models/active_record" if defined?(::ActiveRecord)
|
3
|
+
require 'linkify/view_helper'
|
4
|
+
module Linkify
|
5
|
+
module Rails
|
6
|
+
class Engine < ::Rails::Engine
|
7
|
+
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
ActionView::Base.send :include, Linkify::ViewHelper
|
14
|
+
|
15
|
+
if defined?(Rails)
|
16
|
+
require "linkify/railtie"
|
17
|
+
Linkify::Railtie.insert
|
18
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'linkify/models/base'
|
2
|
+
|
3
|
+
module Linkify
|
4
|
+
module Model
|
5
|
+
module ActiveRecord
|
6
|
+
include Base
|
7
|
+
|
8
|
+
##
|
9
|
+
# Set attribute names and include the Linky module.
|
10
|
+
#
|
11
|
+
def linkable_by(title, path, finder_method=:all, options = {}, &block)
|
12
|
+
linkable_init(
|
13
|
+
:linkable => true,
|
14
|
+
:user_title => title,
|
15
|
+
:user_path => path,
|
16
|
+
:finder_method => finder_method
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
private # --------------------------------------------------------------
|
22
|
+
|
23
|
+
def geocoder_file_name; "active_record"; end
|
24
|
+
def geocoder_module_name; "ActiveRecord"; end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'linkify'
|
2
|
+
|
3
|
+
module Linkify
|
4
|
+
|
5
|
+
##
|
6
|
+
# Methods for invoking Linky in a model.
|
7
|
+
#
|
8
|
+
module Model
|
9
|
+
module Base
|
10
|
+
def linky_options
|
11
|
+
if defined?(@linky_options)
|
12
|
+
@linky_options
|
13
|
+
elsif superclass.respond_to?(:linky_options)
|
14
|
+
superclass.linky_options || { }
|
15
|
+
else
|
16
|
+
{ }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def linkable_by
|
21
|
+
fail
|
22
|
+
end
|
23
|
+
|
24
|
+
def linkable
|
25
|
+
linky_options[:linkable]
|
26
|
+
end
|
27
|
+
|
28
|
+
def linkable_title
|
29
|
+
linky_options[:user_title]
|
30
|
+
end
|
31
|
+
|
32
|
+
def linkable_path
|
33
|
+
linky_options[:user_path]
|
34
|
+
end
|
35
|
+
|
36
|
+
def linkable_method
|
37
|
+
linky_options[:finder_method]
|
38
|
+
end
|
39
|
+
|
40
|
+
def linkable_records
|
41
|
+
if linkable_method == :all
|
42
|
+
all
|
43
|
+
else
|
44
|
+
linkable_method
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def linkable_init(options)
|
51
|
+
unless @linky_options
|
52
|
+
@linky_options = {}
|
53
|
+
end
|
54
|
+
@linky_options.merge! options
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'linkify'
|
2
|
+
require 'linkify/models/active_record'
|
3
|
+
|
4
|
+
module Linkify
|
5
|
+
if defined? Rails::Railtie
|
6
|
+
require 'rails'
|
7
|
+
class Railtie < Rails::Railtie
|
8
|
+
initializer 'railtie.insert_into_active_record' do
|
9
|
+
ActiveSupport.on_load :active_record do
|
10
|
+
Linkify::Railtie.insert
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Railtie
|
17
|
+
def self.insert
|
18
|
+
if defined?(::ActiveRecord)
|
19
|
+
::ActiveRecord::Base.extend(Model::ActiveRecord)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Linkify
|
2
|
+
# Contains methods to use in views and helpers.
|
3
|
+
|
4
|
+
module ViewHelper
|
5
|
+
|
6
|
+
|
7
|
+
# renders a select
|
8
|
+
def internal_links(additional_pages=[])
|
9
|
+
grouped_options_for_select(grouped_options(additional_pages))
|
10
|
+
end
|
11
|
+
|
12
|
+
def all_models
|
13
|
+
list = []
|
14
|
+
Dir.foreach("#{::Rails.root}/app/models") do |model_path|
|
15
|
+
# only take .rb files
|
16
|
+
if model_path[-2,2] == "rb"
|
17
|
+
mod = model_path.gsub('.rb', '').camelize.constantize
|
18
|
+
list << mod if mod.linkable
|
19
|
+
end
|
20
|
+
end
|
21
|
+
return list
|
22
|
+
end
|
23
|
+
|
24
|
+
def grouped_options(additional_pages)
|
25
|
+
options = []
|
26
|
+
# add additional pages to the list first
|
27
|
+
pages =[]
|
28
|
+
for page in additional_pages
|
29
|
+
pages << page
|
30
|
+
end
|
31
|
+
options << ["-", pages] if pages.any?
|
32
|
+
# then, add records from the database
|
33
|
+
for mod in all_models
|
34
|
+
model_array = []
|
35
|
+
mod.linkable_records.each do |record|
|
36
|
+
model_array << [record.send(mod.linkable_title), [mod.to_s, record.send(mod.linkable_path)]]
|
37
|
+
end
|
38
|
+
options << [I18n.t("activerecord.models.#{mod.to_s.underscore}"), model_array]
|
39
|
+
end
|
40
|
+
return options
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
data/linkify.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/linkify/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Henk Meijer"]
|
6
|
+
gem.email = ["meijerhenk@gmail.com"]
|
7
|
+
gem.description = %q{Lists all the available routes in your rails app, to add internal linking to admin pages}
|
8
|
+
gem.summary = %q{Linky lists all the possible routes in your Rails app in a drop-down menu}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "linkify"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Linkify::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: linkify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Henk Meijer
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-12 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Lists all the available routes in your rails app, to add internal linking
|
15
|
+
to admin pages
|
16
|
+
email:
|
17
|
+
- meijerhenk@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/linkify.rb
|
28
|
+
- lib/linkify/models/active_record.rb
|
29
|
+
- lib/linkify/models/base.rb
|
30
|
+
- lib/linkify/railtie.rb
|
31
|
+
- lib/linkify/version.rb
|
32
|
+
- lib/linkify/view_helper.rb
|
33
|
+
- linkify.gemspec
|
34
|
+
homepage: ''
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.24
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Linky lists all the possible routes in your Rails app in a drop-down menu
|
58
|
+
test_files: []
|
59
|
+
has_rdoc:
|