|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -require 'rails/generators/base' |
4 | | -require 'rails/generators/active_record' |
| 3 | +require "rails/generators/base" |
| 4 | +require "rails/generators/active_record" |
5 | 5 |
|
6 | 6 | module Footprinted |
7 | 7 | module Generators |
8 | 8 | class InstallGenerator < Rails::Generators::Base |
9 | 9 | include ActiveRecord::Generators::Migration |
10 | 10 |
|
11 | | - source_root File.expand_path('templates', __dir__) |
| 11 | + source_root File.expand_path("templates", __dir__) |
12 | 12 |
|
13 | | - def self.next_migration_number(dir) |
14 | | - ActiveRecord::Generators::Base.next_migration_number(dir) |
| 13 | + def self.next_migration_number(dirname) |
| 14 | + next_migration_number = current_migration_number(dirname) + 1 |
| 15 | + ActiveRecord::Migration.next_migration_number(next_migration_number) |
15 | 16 | end |
16 | 17 |
|
17 | 18 | def create_migration_file |
18 | | - migration_template 'create_footprinted_footprints.rb.erb', File.join(db_migrate_path, "create_footprinted_footprints.rb") |
| 19 | + migration_template "create_footprinted_footprints.rb.erb", |
| 20 | + File.join(db_migrate_path, "create_footprinted_footprints.rb") |
19 | 21 | end |
20 | 22 |
|
21 | 23 | def create_initializer |
22 | | - template 'footprinted.rb', 'config/initializers/footprinted.rb' |
| 24 | + template "footprinted.rb", "config/initializers/footprinted.rb" |
| 25 | + end |
| 26 | + |
| 27 | + def display_post_install_message |
| 28 | + say "\n🎉 The `footprinted` gem has been successfully installed!", :green |
| 29 | + say "\nTo complete the setup:" |
| 30 | + say " 1. Run `rails db:migrate` to create the footprints table." |
| 31 | + say " ⚠️ You must run migrations before starting your app!", :yellow |
| 32 | + say "\n 2. Add `include Footprinted::Model` to any model you want to track:" |
| 33 | + say " class Product < ApplicationRecord" |
| 34 | + say " include Footprinted::Model" |
| 35 | + say " end" |
| 36 | + say "\n 3. Create footprints from your controllers or services:" |
| 37 | + say " product.footprints.create!(" |
| 38 | + say " ip: request.remote_ip," |
| 39 | + say " event_type: 'page_view'," |
| 40 | + say " occurred_at: Time.current" |
| 41 | + say " )" |
| 42 | + say "\nSee the footprinted README for detailed usage and examples.", :cyan |
| 43 | + say "Happy tracking! 👣\n", :green |
23 | 44 | end |
24 | 45 |
|
25 | 46 | private |
26 | 47 |
|
27 | 48 | def migration_version |
28 | 49 | "[#{ActiveRecord::VERSION::STRING.to_f}]" |
29 | 50 | end |
30 | | - |
31 | 51 | end |
32 | 52 | end |
33 | 53 | end |
0 commit comments