Class: TokenizeAttr::Installer

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

Overview

Manages the lifecycle of the tokenize_attr 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):

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

Constant Summary collapse

INITIALIZER_PATH =
Pathname.new("config/initializers/tokenize_attr.rb")
INITIALIZER_CONTENT =
"# frozen_string_literal: true\n\n# Auto-include TokenizeAttr::Concern into every ActiveRecord model so that\n# the .tokenize macro is available without an explicit include in each class.\n#\n# Generated by: rails tokenize_attr:install\n#\n# To opt out of auto-include, remove this file and add\n#   include TokenizeAttr::Concern\n# to whichever models need it.\nActiveSupport.on_load(:active_record) do\n  include TokenizeAttr::Concern\nend\n"

Class Method Summary collapse

Class Method Details

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

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

Parameters:

  • rails_root (Pathname, String) —

    the root directory of the Rails app

Returns:

  • (:created, :skipped)


39
40
41
42
43
44
45
46
# File 'lib/tokenize_attr/installer.rb', line 39

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)


52
53
54
55
56
57
58
59
# File 'lib/tokenize_attr/installer.rb', line 52

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

  return :skipped unless destination.exist?

  destination.delete
  :removed
end