Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mrkamel/uri_for
Build smart URIs from hashes in your rails code
https://github.com/mrkamel/uri_for
Last synced: 23 days ago
JSON representation
Build smart URIs from hashes in your rails code
- Host: GitHub
- URL: https://github.com/mrkamel/uri_for
- Owner: mrkamel
- License: mit
- Created: 2012-10-07T11:24:26.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2013-02-27T18:07:59.000Z (over 11 years ago)
- Last Synced: 2024-10-09T16:47:18.738Z (about 1 month ago)
- Language: Ruby
- Size: 125 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# UriFor
UriFor helps to generate http and https URIs from simple hashes using ruby stdlib's URI module.
If you don't provide e.g., the host, port, etc. UriFor tries to use the values from your controller, if possible.## Installation
Add this line to your application's Gemfile:
gem "uri_for", :git => "git://github.com/mrkamel/uri_for.git"
And then execute:
$ bundle
## Usage
In your standard or mailer views, controllers and models, simply use:
uri_for :host => "example.org", :path => "/path/to/file" # => "http://example.org/path/to/fileOr the short way:
irb> uri_for :path => "/path/to/file
=> "http://example.org/path/to/file" # example.org is taken from the current requestHowever, usually, you want to provide the uri via your models:
class SomeModel < ActiveRecord::Base
def image_uri
{ :host => APP_CONFIG["image_host"], :path => "/path/to/file" }
end
end# In your views:
uri_for SomeModel.first.image_uri # => "http://example.org/path/to/file"
UriFor even takes control of whether to use http or https:
uri_for :port => 443, :path => "/path/to/file" # => "https://example.org/path/to/file"Again, if you don't provide the port with the hash, UriFor will try to use it from
the current request, if accessible.Within mailer views and models UriFor of course can't use the current
request to set these smart defaults. Smart defaults are only available within
your controllers and standard views. However, you can always provide the
port and host manually.## 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