Skip to content

Commit d8d010f

Browse files
committed
feat(generator): simplify installation by reducing setup steps
1 parent 812a4e5 commit d8d010f

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

README.md

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,37 @@ Here are some of the things you can do with RailsUrlShortener:
2525

2626
## Installation
2727

28-
Follow these steps to install and configure RailsUrlShortener:
28+
Follow these steps to install and configure rails_url_shortener in your Rails application.
2929

30-
1. Add this line to your application's Gemfile:
30+
1. Add the Gem
31+
Add the following line to your application's Gemfile:
3132

3233
```ruby
3334
gem "rails_url_shortener"
3435
```
35-
36-
2. Install the gem by running:
36+
2. Install the Gem
37+
Run the following command to install the gem:
3738

3839
```bash
3940
bundle install
4041
```
41-
42-
3. Install and run the migrations:
43-
44-
```bash
45-
bin/rails rails_url_shortener:install:migrations db:migrate
46-
```
47-
48-
4. Generate the initializer for configuration:
49-
42+
3. Run the Generator
43+
Run the generator to set up the necessary files:
5044
```bash
5145
rails generate rails_url_shortener
5246
```
47+
This will:
48+
✅ Install and run the required migrations
5349

54-
## Usage
55-
56-
1. Mount the engine
57-
58-
Mount the engine on your app adding the next code on your config/routes.rb:
50+
✅ Mount the engine
51+
An entry will be added to the bottom of your config/routes.rb file, mounting the engine at the root of your application.
5952

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

62-
```ruby
63-
mount RailsUrlShortener::Engine, at: "/"
6455

65-
```
56+
## Usage
6657

67-
2. Generate the short link
58+
1. Generate the short link
6859

6960
And generate the short links like you want:
7061

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

83-
3. Share the short link
74+
2. Share the short link
8475

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

lib/generators/rails_url_shortener/rails_url_shortener_generator.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
class RailsUrlShortenerGenerator < Rails::Generators::Base
66
source_root File.expand_path('templates', __dir__)
77

8+
def install_and_run_migrations
9+
if Rails.env.test?
10+
puts 'Skipping migrations in test environment'
11+
else
12+
rake 'rails_url_shortener:install:migrations'
13+
rake 'db:migrate'
14+
end
15+
end
16+
17+
def add_route_to_routes_file
18+
# Mount the engine at the bottom of the routes file in the host application.
19+
inject_into_file 'config/routes.rb', before: /\nend\s*\Z/ do
20+
"\n mount RailsUrlShortener::Engine, at: '/'"
21+
end
22+
end
23+
824
def copy
925
copy_file 'initializer.rb', 'config/initializers/rails_url_shortener.rb'
1026
end

test/lib/generators/rails_url_shortener/rails_url_shortener_generator_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,26 @@ module RailsUrlShortener
55
class RailsUrlShortenerGeneratorTest < Rails::Generators::TestCase
66
tests RailsUrlShortenerGenerator
77
destination Rails.root.join('tmp/generators')
8+
89
setup :prepare_destination
910

1011
test 'generator runs without errors' do
1112
assert_nothing_raised do
13+
# create config/routes.rb in tmp/generators
14+
routes_file = File.join(destination_root, 'config', 'routes.rb')
15+
FileUtils.mkdir_p(File.dirname(routes_file))
16+
File.write(routes_file, "Rails.application.routes.draw do\nend")
17+
1218
run_generator ['arguments']
1319
end
20+
21+
# Verify initializer file is created at config/initializers/shortener.rb
1422
assert_file 'config/initializers/rails_url_shortener.rb'
23+
24+
# Verify correct entry is added to config/routes.rb
25+
assert_file 'config/routes.rb' do |content|
26+
assert_match(%r{mount RailsUrlShortener::Engine, at: '/}, content)
27+
end
1528
end
1629
end
1730
end

0 commit comments

Comments
 (0)