Sha256: da5ad8ad14ce3745875f6dc527dfe5872e280fe1d10494d638f8cad64a9cb965

Contents?: true

Size: 1.75 KB

Versions: 29

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

# module PagesGallery
#  class Plugin < PagesCore::Plugin
#    paths['db/migrate'] = 'template/db/migrate'
#  end
# end

module PagesCore
  class Plugin
    class << self
      attr_accessor :called_from, :paths

      def inherited(plugin)
        plugin.paths ||= default_paths
        plugin.called_from = begin
          # Remove the line number from backtraces making sure we
          # don't leave anything behind
          call_stack = caller.map { |p| p.sub(/:\d+.*/, "") }
          File.dirname(
            call_stack.detect do |p|
              p !~ %r{railties[\w.-]*/lib/rails|rack[\w.-]*/lib/rack}
            end
          )
        end
        super
      end

      def admin_menu_item(label, path, group = :custom, options = {})
        PagesCore::AdminMenuItem.register(label, path, group, options)
      end

      def plugins
        @plugins ||= ::PagesCore::Plugin.subclasses.map do |class_name|
          class_name.to_s.split("::").inject(Object) do |klass, m|
            klass.const_get(m)
          end
        end
      end

      private

      def default_paths
        { "db/migrate" => "db/migrate" }
      end
    end

    def root
      @root ||= find_root_with_subfolder("app")
    end

    def paths
      self.class.paths
    end

    protected

    def find_root_with_subfolder(subfolder)
      root_path = self.class.called_from

      while root_path && File.directory?(root_path) &&
            !File.exist?("#{root_path}/#{subfolder}")
        parent = File.dirname(root_path)
        root_path = parent != root_path && parent
      end

      unless File.exist?("#{root_path}/#{subfolder}")
        raise "Could not find root path for #{self}"
      end

      Pathname.new(root_path).realpath
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
pages_core-3.15.5 lib/pages_core/plugin.rb
pages_core-3.15.4 lib/pages_core/plugin.rb
pages_core-3.15.3 lib/pages_core/plugin.rb
pages_core-3.15.2 lib/pages_core/plugin.rb
pages_core-3.15.1 lib/pages_core/plugin.rb
pages_core-3.14.0 lib/pages_core/plugin.rb
pages_core-3.13.0 lib/pages_core/plugin.rb
pages_core-3.12.7 lib/pages_core/plugin.rb
pages_core-3.12.6 lib/pages_core/plugin.rb
pages_core-3.12.5 lib/pages_core/plugin.rb
pages_core-3.12.4 lib/pages_core/plugin.rb
pages_core-3.12.3 lib/pages_core/plugin.rb
pages_core-3.12.2 lib/pages_core/plugin.rb
pages_core-3.12.1 lib/pages_core/plugin.rb
pages_core-3.12.0 lib/pages_core/plugin.rb
pages_core-3.11.3 lib/pages_core/plugin.rb
pages_core-3.11.2 lib/pages_core/plugin.rb
pages_core-3.11.1 lib/pages_core/plugin.rb
pages_core-3.11.0 lib/pages_core/plugin.rb
pages_core-3.10.2 lib/pages_core/plugin.rb