Class: CustomId::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/custom_id/installer.rb

Overview

Manages the Rails initializer file that auto-includes Concern into every ActiveRecord model via ActiveSupport.on_load(:active_record).

This class is intentionally decoupled from Rails so it can be exercised in unit tests without booting a full Rails application.

Constant Summary collapse

INITIALIZER_PATH =

Path of the generated initializer, relative to the Rails root.

Pathname.new("config/initializers/custom_id.rb")
INITIALIZER_CONTENT =

Content written to the initializer file.

<<~RUBY
  # frozen_string_literal: true

  # Auto-include CustomId::Concern into every ActiveRecord model so that
  # the +cid+ class macro is available application-wide.
  #
  # Generated by: rails custom_id:install
  ActiveSupport.on_load(:active_record) do
    include CustomId::Concern
  end
RUBY

Class Method Summary collapse

Class Method Details

.install!(rails_root) ⇒ :created, :skipped

Creates the initializer file under rails_root.



33
34
35
36
37
38
39
40
# File 'lib/custom_id/installer.rb', line 33

def self.install!(rails_root)
  target = Pathname(rails_root).join(INITIALIZER_PATH)
  return :skipped if target.exist?

  target.dirname.mkpath
  target.write(INITIALIZER_CONTENT)
  :created
end

.uninstall!(rails_root) ⇒ :removed, :skipped

Removes the initializer file from rails_root.



47
48
49
50
51
52
53
# File 'lib/custom_id/installer.rb', line 47

def self.uninstall!(rails_root)
  target = Pathname(rails_root).join(INITIALIZER_PATH)
  return :skipped unless target.exist?

  target.delete
  :removed
end