link_to_me 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5cd0f4641234663fcfc4c89dfdb95f38b6f920bccb047f7066b955fe62cc1c74
4
+ data.tar.gz: 7ebc82b1a3b53edf2264bdedd895074b38cfc9b36b471949a60846d06b47a8b1
5
+ SHA512:
6
+ metadata.gz: 5f0d0704534d036754b605b5825cb8d58759c3058007f78ffa4b9d8e12fee51d7fd34d2f94d1422c40d3ff8f25e84f7f8acc7eb1a0ac5d6647fe405b6876bead
7
+ data.tar.gz: 0451d2101bfabde0a1c0496c9542e37e75c59a0b84fa82b58c526bf8fdf51d253762c8fe19cdfc510bd195e78bd6fd53d876dcde1c38744516454a28403fb4c9
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.0] - 2021-02-19
10
+
11
+ ### Added
12
+ - Initial release
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at hi@olivierlacan.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in link_to_me.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Olivier Lacan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # LinkToMe
2
+
3
+ Extend Rails Action View's `link_to` to allow it to autolink any
4
+ Active Record model instances with a GET url.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'link_to_me'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install link_to_me
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ link_to @model
26
+ ```
27
+
28
+ Will result in the following code:
29
+ ```
30
+ <a href="/models/1">Model 1</a>
31
+ ```
32
+
33
+ Given the `@model` instance defines the following method:
34
+
35
+ ```ruby
36
+ class Model
37
+ def to_s
38
+ "Model #{id}"
39
+ end
40
+ end
41
+ ```
42
+
43
+ Here, `to_s` can point to an attribute like `name` as well if it exists
44
+ and use a fallback:
45
+
46
+ ```ruby
47
+ class Model
48
+ def to_s
49
+ name || "Model #{id}"
50
+ end
51
+ end
52
+ ```
53
+
54
+ If you don't implement a `to_s` method, `link_to` will instead default
55
+ to the original Ruby definition of `to_s` which prints the name of the
56
+ instance's class and its `object_id` in hexacidemal format:
57
+
58
+ ```ruby
59
+ Object.new.to_s
60
+ => "#<Object:0x00007fc122ad4d10>"
61
+ ```
62
+
63
+ It's not quite as useful. Defining `to_s` on Rails Active Record model
64
+ classes is not at all destructive. You can still obtain the
65
+ non-hexadecimal memory object ID via `Object.new.object_id`:
66
+
67
+ ```ruby
68
+ Object.new.object_id
69
+ => 109720
70
+ ```
71
+
72
+ ## Development
73
+
74
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
75
+
76
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
77
+
78
+ ## Contributing
79
+
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/olivierlacan/link_to_me. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/olivierlacan/link_to_me/blob/master/CODE_OF_CONDUCT.md).
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
85
+
86
+ ## Code of Conduct
87
+
88
+ Everyone interacting in the LinkToMe project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/olivierlacan/link_to_me/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "link_to_me"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/link_to_me.rb ADDED
@@ -0,0 +1,174 @@
1
+ require "link_to_me/version"
2
+ require "link_to_me/railtie" if defined?(Rails::Railtie)
3
+
4
+ module LinkToMe
5
+ def self.included(klass)
6
+ klass.class_eval do
7
+ alias_method :_rails_link_to, :link_to
8
+ remove_method :link_to
9
+ end
10
+ end
11
+ # Creates an anchor element of the given +name+ using a URL created by the set of +options+.
12
+ # See the valid options in the documentation for +url_for+. It's also possible to
13
+ # pass a \String instead of an options hash, which generates an anchor element that uses the
14
+ # value of the \String as the href for the link. Using a <tt>:back</tt> \Symbol instead
15
+ # of an options hash will generate a link to the referrer (a JavaScript back link
16
+ # will be used in place of a referrer if none exists). If +nil+ is passed as the name
17
+ # the value of the link itself will become the name.
18
+ #
19
+ # ==== Signatures
20
+ #
21
+ # link_to(body, url, html_options = {})
22
+ # # url is a String; you can use URL helpers like
23
+ # # posts_path
24
+ #
25
+ # link_to(body, url_options = {}, html_options = {})
26
+ # # url_options, except :method, is passed to url_for
27
+ #
28
+ # link_to(options = {}, html_options = {}) do
29
+ # # name
30
+ # end
31
+ #
32
+ # link_to(url, html_options = {}) do
33
+ # # name
34
+ # end
35
+ #
36
+ # link_to(active_record_model)
37
+ #
38
+ # ==== Options
39
+ # * <tt>:data</tt> - This option can be used to add custom data attributes.
40
+ # * <tt>method: symbol of HTTP verb</tt> - This modifier will dynamically
41
+ # create an HTML form and immediately submit the form for processing using
42
+ # the HTTP verb specified. Useful for having links perform a POST operation
43
+ # in dangerous actions like deleting a record (which search bots can follow
44
+ # while spidering your site). Supported verbs are <tt>:post</tt>, <tt>:delete</tt>, <tt>:patch</tt>, and <tt>:put</tt>.
45
+ # Note that if the user has JavaScript disabled, the request will fall back
46
+ # to using GET. If <tt>href: '#'</tt> is used and the user has JavaScript
47
+ # disabled clicking the link will have no effect. If you are relying on the
48
+ # POST behavior, you should check for it in your controller's action by using
49
+ # the request object's methods for <tt>post?</tt>, <tt>delete?</tt>, <tt>patch?</tt>, or <tt>put?</tt>.
50
+ # * <tt>remote: true</tt> - This will allow the unobtrusive JavaScript
51
+ # driver to make an Ajax request to the URL in question instead of following
52
+ # the link. The drivers each provide mechanisms for listening for the
53
+ # completion of the Ajax request and performing JavaScript operations once
54
+ # they're complete
55
+ #
56
+ # ==== Data attributes
57
+ #
58
+ # * <tt>confirm: 'question?'</tt> - This will allow the unobtrusive JavaScript
59
+ # driver to prompt with the question specified (in this case, the
60
+ # resulting text would be <tt>question?</tt>. If the user accepts, the
61
+ # link is processed normally, otherwise no action is taken.
62
+ # * <tt>:disable_with</tt> - Value of this parameter will be used as the
63
+ # name for a disabled version of the link. This feature is provided by
64
+ # the unobtrusive JavaScript driver.
65
+ #
66
+ # ==== Examples
67
+ # Because it relies on +url_for+, +link_to+ supports both older-style controller/action/id arguments
68
+ # and newer RESTful routes. Current Rails style favors RESTful routes whenever possible, so base
69
+ # your application on resources and use
70
+ #
71
+ # link_to "Profile", profile_path(@profile)
72
+ # # => <a href="/profiles/1">Profile</a>
73
+ #
74
+ # or the even pithier
75
+ #
76
+ # link_to "Profile", @profile
77
+ # # => <a href="/profiles/1">Profile</a>
78
+ #
79
+ # in place of the older more verbose, non-resource-oriented
80
+ #
81
+ # link_to "Profile", controller: "profiles", action: "show", id: @profile
82
+ # # => <a href="/profiles/show/1">Profile</a>
83
+ #
84
+ # Similarly,
85
+ #
86
+ # link_to "Profiles", profiles_path
87
+ # # => <a href="/profiles">Profiles</a>
88
+ #
89
+ # is better than
90
+ #
91
+ # link_to "Profiles", controller: "profiles"
92
+ # # => <a href="/profiles">Profiles</a>
93
+ #
94
+ # When name is +nil+ the href is presented instead
95
+ #
96
+ # link_to nil, "http://example.com"
97
+ # # => <a href="http://www.example.com">http://www.example.com</a>
98
+ #
99
+ # Pithier yet, when name is an ActiveRecord model that defines a
100
+ # +to_s+ method returning a name attribute or a default value
101
+ #
102
+ # link_to @profile
103
+ # # => <a href="http://www.example.com/profiles/1">David</a>
104
+ #
105
+ # You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
106
+ #
107
+ # <%= link_to(@profile) do %>
108
+ # <strong><%= @profile.name %></strong> -- <span>Check it out!</span>
109
+ # <% end %>
110
+ # # => <a href="/profiles/1">
111
+ # <strong>David</strong> -- <span>Check it out!</span>
112
+ # </a>
113
+ #
114
+ # Classes and ids for CSS are easy to produce:
115
+ #
116
+ # link_to "Articles", articles_path, id: "news", class: "article"
117
+ # # => <a href="/articles" class="article" id="news">Articles</a>
118
+ #
119
+ # Be careful when using the older argument style, as an extra literal hash is needed:
120
+ #
121
+ # link_to "Articles", { controller: "articles" }, id: "news", class: "article"
122
+ # # => <a href="/articles" class="article" id="news">Articles</a>
123
+ #
124
+ # Leaving the hash off gives the wrong link:
125
+ #
126
+ # link_to "WRONG!", controller: "articles", id: "news", class: "article"
127
+ # # => <a href="/articles/index/news?class=article">WRONG!</a>
128
+ #
129
+ # +link_to+ can also produce links with anchors or query strings:
130
+ #
131
+ # link_to "Comment wall", profile_path(@profile, anchor: "wall")
132
+ # # => <a href="/profiles/1#wall">Comment wall</a>
133
+ #
134
+ # link_to "Ruby on Rails search", controller: "searches", query: "ruby on rails"
135
+ # # => <a href="/searches?query=ruby+on+rails">Ruby on Rails search</a>
136
+ #
137
+ # link_to "Nonsense search", searches_path(foo: "bar", baz: "quux")
138
+ # # => <a href="/searches?foo=bar&baz=quux">Nonsense search</a>
139
+ #
140
+ # The only option specific to +link_to+ (<tt>:method</tt>) is used as follows:
141
+ #
142
+ # link_to("Destroy", "http://www.example.com", method: :delete)
143
+ # # => <a href='http://www.example.com' rel="nofollow" data-method="delete">Destroy</a>
144
+ #
145
+ # You can also use custom data attributes using the <tt>:data</tt> option:
146
+ #
147
+ # link_to "Visit Other Site", "http://www.rubyonrails.org/", data: { confirm: "Are you sure?" }
148
+ # # => <a href="http://www.rubyonrails.org/" data-confirm="Are you sure?">Visit Other Site</a>
149
+ #
150
+ # Also you can set any link attributes such as <tt>target</tt>, <tt>rel</tt>, <tt>type</tt>:
151
+ #
152
+ # link_to "External link", "http://www.rubyonrails.org/", target: "_blank", rel: "nofollow"
153
+ # # => <a href="http://www.rubyonrails.org/" target="_blank" rel="nofollow">External link</a>
154
+ def link_to(name = nil, options = nil, html_options = nil, &block)
155
+ html_options, options, name = options, name, block if block_given?
156
+ options ||= {}
157
+
158
+ html_options = convert_options_to_data_attributes(options, html_options)
159
+
160
+ url = url_for(url_target(name, options))
161
+ html_options["href"] ||= url
162
+
163
+ content_tag("a", name || url, html_options, &block)
164
+ end
165
+
166
+ private
167
+ def url_target(name, options)
168
+ if name.respond_to?(:model_name) && options.empty?
169
+ url_for(name)
170
+ else
171
+ url_for(options)
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,9 @@
1
+ module LinkToMe
2
+ class Railtie < Rails::Railtie
3
+ initializer "link_to_me.configure_rails_initialization" do
4
+ ActiveSupport.on_load(:action_view) do
5
+ ActionView::Helpers::UrlHelper.send(:include, LinkToMe)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module LinkToMe
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/link_to_me/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "link_to_me"
5
+ spec.version = LinkToMe::VERSION
6
+ spec.authors = ["Olivier Lacan"]
7
+ spec.email = ["hi@olivierlacan.com"]
8
+
9
+ spec.summary = %q{Extend link_to to autolink Active Record model instances}
10
+ spec.description = %q{Give link_to the ability to point to the show page of an Active Record model instance resource.}
11
+ spec.homepage = "https://github.com/olivierlacan/link_to_me"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/olivierlacan/link_to_me"
17
+ spec.metadata["changelog_uri"] = "https://github.com/olivierlacan/link_to_me/blob/main/CHANGELOG.md"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: link_to_me
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Olivier Lacan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-02-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Give link_to the ability to point to the show page of an Active Record
14
+ model instance resource.
15
+ email:
16
+ - hi@olivierlacan.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".travis.yml"
24
+ - CHANGELOG.md
25
+ - CODE_OF_CONDUCT.md
26
+ - Gemfile
27
+ - LICENSE.txt
28
+ - README.md
29
+ - Rakefile
30
+ - bin/console
31
+ - bin/setup
32
+ - lib/link_to_me.rb
33
+ - lib/link_to_me/railtie.rb
34
+ - lib/link_to_me/version.rb
35
+ - link_to_me.gemspec
36
+ homepage: https://github.com/olivierlacan/link_to_me
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ homepage_uri: https://github.com/olivierlacan/link_to_me
41
+ source_code_uri: https://github.com/olivierlacan/link_to_me
42
+ changelog_uri: https://github.com/olivierlacan/link_to_me/blob/main/CHANGELOG.md
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 2.7.0
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.1.2
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Extend link_to to autolink Active Record model instances
62
+ test_files: []