Class: InquiryAttrs::Installer

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

Overview

Manages the lifecycle of the inquiry_attrs initializer inside a Rails app.

This class holds all file-system logic so it can be unit-tested without requiring a full Rails boot or Rake DSL. The rake tasks are thin shells that delegate here.

Examples:

From a Rake task (or Rails generator):

InquiryAttrs::Installer.install!(Rails.root)
InquiryAttrs::Installer.uninstall!(Rails.root)

Constant Summary collapse

INITIALIZER_PATH =
Pathname.new('config/initializers/inquiry_attrs.rb')
INITIALIZER_CONTENT =
<<~RUBY
  # frozen_string_literal: true

  # Auto-include InquiryAttrs::Concern into every ActiveRecord model so that
  # the .inquirer macro is available without an explicit include in each class.
  #
  # Generated by: rails inquiry_attrs:install
  #
  # To opt out of auto-include, remove this file and add
  #   include InquiryAttrs::Concern
  # to whichever models need it.
  ActiveSupport.on_load(:active_record) do
    include InquiryAttrs::Concern
  end
RUBY

Class Method Summary collapse

Class Method Details

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

Write the initializer to rails_root/config/initializers/inquiry_attrs.rb.

Parameters:

  • rails_root (Pathname, String) —

    the root directory of the Rails app

Returns:

  • (:created, :skipped)


37
38
39
40
41
42
43
44
# File 'lib/inquiry_attrs/installer.rb', line 37

def self.install!(rails_root)
  destination = Pathname.new(rails_root).join(INITIALIZER_PATH)

  return :skipped if destination.exist?

  destination.write(INITIALIZER_CONTENT)
  :created
end

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

Remove the initializer if it exists.

Parameters:

  • rails_root (Pathname, String)

Returns:

  • (:removed, :skipped)


50
51
52
53
54
55
56
57
# File 'lib/inquiry_attrs/installer.rb', line 50

def self.uninstall!(rails_root)
  destination = Pathname.new(rails_root).join(INITIALIZER_PATH)

  return :skipped unless destination.exist?

  destination.delete
  :removed
end