Sha256: 834e3206e7a50dd75766efe274dca837cd5b8399c1f27a740f962dfa530591a8

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

module Provideal
  module PluginUtils
    class << self
      #
      # Installs your plugins' assets into the host
      # application by linking public/plugins/[your_plugin] to
      # your plugins' public folder.
      #
      # In your plugins' init.rb:
      #
      #   Provideal::PluginUtils.install_assets('your_plugin', 'path to your plugins' public folder')
      #
      # The host application may access the plugins' assets using Rails
      # asset tag helpers together with the provided :plugin option.
      #
      # Example:
      #
      #   <%= javascript_include_tag 'foo.js', :plugin => 'your_plugin' %>
      #
      # This will create a proper link to '/plugins/your_plugin/javascripts/foo.js'.
      #
      def install_assets(plugin_name, target_path)
        # Precheck conditions
        raise "Plugin name required" unless plugin_name
        raise "Target path required" unless target_path
        raise "Taget does't exists"  unless File.exists?(target_path)

        # Create the plugins base path inside the hosts public folder
        plugins_public_base_path = "#{RAILS_ROOT}/public/plugins"
        FileUtils.mkdir_p(plugins_public_base_path)

        # The source path
        source_path = "#{plugins_public_base_path}/#{plugin_name}"
        
        # Create the symlink if the source_path is not present
        # This allows for manual overrides
        unless File.exists?(source_path)
          system "ln -s #{target_path} #{source_path}"
        end
      end

      #
      # Enables Rails extensions
      #
      def enable #:nodoc:
        # enable our overriden asset helper
        require 'provideal_plugin_utils/asset_tag_helper'
        ::ActionView::Helpers::AssetTagHelper.send :include, Provideal::PluginUtils::AssetTagHelper
      end
    end
  end
end

if defined?(Rails) and defined?(ActionController)
  Provideal::PluginUtils.enable
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
provideal-plugin-utils-0.1.0 lib/provideal_plugin_utils.rb