Sha256: a5b0cc3d7ef4a772070c31c1b87a5a7d486d78a470b7fbd7a64130cf645d5ed0

Contents?: true

Size: 868 Bytes

Versions: 3

Compression:

Stored size: 868 Bytes

Contents

module Scenic
  # 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 UnaffixedName
    # Gets the unaffixed name for the provided string
    # @return [String]
    #
    # @param name [String] The (potentially) affixed view name
    def self.for(name)
      new(name, config: ActiveRecord::Base).call
    end

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

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

    private

    attr_reader :name, :config
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
scenic-1.8.0 lib/scenic/unaffixed_name.rb
scenic-1.7.0 lib/scenic/unaffixed_name.rb
scenic-1.6.0 lib/scenic/unaffixed_name.rb