Class: Scenic::UnaffixedName

Inherits:
Object
  • Object
show all
Defined in:
lib/scenic/unaffixed_name.rb

Overview

The name of a view or table according to rails.

This removes any table name prefix or suffix that is configured via ActiveRecord. This allows, for example, the SchemaDumper to dump a view with its unaffixed name, consistent with how rails handles table dumping.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config:) ⇒ UnaffixedName

Returns a new instance of UnaffixedName.



16
17
18
19
# File 'lib/scenic/unaffixed_name.rb', line 16

def initialize(name, config:)
  @name = name
  @config = config
end

Class Method Details

.for(name) ⇒ String

Gets the unaffixed name for the provided string

Parameters:

  • name (String)

    The (potentially) affixed view name

Returns:

  • (String)


12
13
14
# File 'lib/scenic/unaffixed_name.rb', line 12

def self.for(name)
  new(name, config: ActiveRecord::Base).call
end

Instance Method Details

#callObject



21
22
23
24
25
# File 'lib/scenic/unaffixed_name.rb', line 21

def call
  prefix = Regexp.escape(config.table_name_prefix)
  suffix = Regexp.escape(config.table_name_suffix)
  name.sub(/\A#{prefix}(.+)#{suffix}\z/, "\\1")
end