Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 15 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,37 @@ Here are some of the things you can do with RailsUrlShortener:

## Installation

Follow these steps to install and configure RailsUrlShortener:
Follow these steps to install and configure rails_url_shortener in your Rails application.

1. Add this line to your application's Gemfile:
1. Add the Gem
Add the following line to your application's Gemfile:

```ruby
gem "rails_url_shortener"
```

2. Install the gem by running:
2. Install the Gem
Run the following command to install the gem:

```bash
bundle install
```

3. Install and run the migrations:

```bash
bin/rails rails_url_shortener:install:migrations db:migrate
```

4. Generate the initializer for configuration:

3. Run the Generator
Run the generator to set up the necessary files:
```bash
rails generate rails_url_shortener
```
This will:
✅ Install and run the required migrations

## Usage

1. Mount the engine

Mount the engine on your app adding the next code on your config/routes.rb:
✅ Mount the engine
An entry will be added to the bottom of your config/routes.rb file, mounting the engine at the root of your application.

**If you want to mount this on the root of your app, this should be on the bottom of your routes file.**
✅ Generate an initializer for further configuration`

```ruby
mount RailsUrlShortener::Engine, at: "/"

```
## Usage

2. Generate the short link
1. Generate the short link

And generate the short links like you want:

Expand All @@ -80,7 +71,7 @@ short_url("https://www.github.com/a-chacon/rails-url-shortener")
RailsUrlShortener::Url.generate("https://www.github.com/a-chacon/rails-url-shortener")
```

3. Share the short link
2. Share the short link

**Then share the short link to your users or wherever you want.**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@
class RailsUrlShortenerGenerator < Rails::Generators::Base
source_root File.expand_path('templates', __dir__)

def install_and_run_migrations
if Rails.env.test?
puts 'Skipping migrations in test environment'
else
rake 'rails_url_shortener:install:migrations'
rake 'db:migrate'
end
end

def add_route_to_routes_file
# Mount the engine at the bottom of the routes file in the host application.
inject_into_file 'config/routes.rb', before: /\nend\s*\Z/ do
"\n mount RailsUrlShortener::Engine, at: '/'"
end
end

def copy
copy_file 'initializer.rb', 'config/initializers/rails_url_shortener.rb'
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,26 @@ module RailsUrlShortener
class RailsUrlShortenerGeneratorTest < Rails::Generators::TestCase
tests RailsUrlShortenerGenerator
destination Rails.root.join('tmp/generators')

setup :prepare_destination

test 'generator runs without errors' do
assert_nothing_raised do
# create config/routes.rb in tmp/generators
routes_file = File.join(destination_root, 'config', 'routes.rb')
FileUtils.mkdir_p(File.dirname(routes_file))
File.write(routes_file, "Rails.application.routes.draw do\nend")

run_generator ['arguments']
end

# Verify initializer file is created at config/initializers/rails_url_shortener.rb
assert_file 'config/initializers/rails_url_shortener.rb'

# Verify correct entry is added to config/routes.rb
assert_file 'config/routes.rb' do |content|
assert_match(%r{mount RailsUrlShortener::Engine, at: '/}, content)
end
end
end
end
Loading