class Cyborg::Plugin

Attributes

destination[R]
engine[R]
gem[R]
javascripts[R]
name[R]
stylesheets[R]
svgs[R]

Public Class Methods

new(options) click to toggle source
# File lib/cyborg/plugin.rb, line 6
def initialize(options)
  @name            = options.delete(:engine).downcase
  @gem             = Gem.loaded_specs[options.delete(:gem)]
  config(options)
  expand_asset_paths

  # Store the gem path for access later when overriding root
  parent_module.instance_variable_set(:@gem_path, root)
  parent_module.instance_variable_set(:@cyborg_plugin_name, name)
  add_assets
end

Public Instance Methods

add_assets() click to toggle source
# File lib/cyborg/plugin.rb, line 57
def add_assets
  @javascripts = Assets::Javascripts.new(self, paths[:javascripts])
  @stylesheets = Assets::Stylesheets.new(self, paths[:stylesheets])
  @svgs        = Assets::Svgs.new(self, paths[:svgs])
end
asset_path(file=nil) click to toggle source
# File lib/cyborg/plugin.rb, line 129
def asset_path(file=nil)
  dest = destination
  dest = File.join(dest, file) if file
  dest
end
asset_root() click to toggle source
# File lib/cyborg/plugin.rb, line 92
def asset_root
  asset_prefix = Rails.application.config.assets.prefix || '/assets'
  File.join asset_prefix, name
end
asset_url(file=nil) click to toggle source
# File lib/cyborg/plugin.rb, line 135
def asset_url(file=nil)

  path = if Cyborg.production? && !ENV[name.upcase + '_FORCE_LOCAL_ASSETS']
    production_root
  else
    asset_root
  end

  path = File.join(path, file) if file
  path
end
assets(options={}) click to toggle source
# File lib/cyborg/plugin.rb, line 63
def assets(options={})
  assets = []
  if options[:select_assets]
    assets.push @svgs if options[:svg]
    assets.push @stylesheets if options[:css]
    assets.push @javascripts if options[:js]
  else
    assets = [@svgs, @stylesheets, @javascripts]
  end

  assets
end
build(options={}) click to toggle source
# File lib/cyborg/plugin.rb, line 80
def build(options={})
  FileUtils.mkdir_p(asset_path)
  assets(options).each do |asset|
    asset.build
  end

end
config() click to toggle source
# File lib/cyborg/plugin.rb, line 31
def config
  @config ||= Rails::Engine::Configuration.new(cyborg_plugin_path)
end
create_engine(&block) click to toggle source
# File lib/cyborg/plugin.rb, line 22
def create_engine(&block)
  # Create a new Rails::Engine
  @engine = parent_module.const_set('Engine', Class.new(Rails::Engine) do

    def cyborg_plugin_path
      parent = Object.const_get(self.class.name.sub(/::Engine/,''))
      Pathname.new parent.instance_variable_get("@gem_path")
    end

    def config
      @config ||= Rails::Engine::Configuration.new(cyborg_plugin_path)
    end

    engine_name Cyborg.plugin.name

    require 'cyborg/middleware'

    initializer "#{name}.static_assets" do |app|
      if !Cyborg.rails5? || app.config.public_file_server.enabled
        app.middleware.insert_after ::ActionDispatch::Static, Cyborg::StaticAssets, "#{root}/public", engine_name: Cyborg.plugin.name
        app.middleware.insert_before ::ActionDispatch::Static, Rack::Deflater
      end
    end

  end)

  @engine.instance_eval(&block) if block_given?
end
cyborg_plugin_path() click to toggle source
# File lib/cyborg/plugin.rb, line 26
def cyborg_plugin_path
  parent = Object.const_get(self.class.name.sub(/::Engine/,''))
  Pathname.new parent.instance_variable_get("@gem_path")
end
engine_name() click to toggle source
# File lib/cyborg/plugin.rb, line 18
def engine_name
  @engine.name.sub(/::Engine/,'')
end
expand_asset_paths() click to toggle source
# File lib/cyborg/plugin.rb, line 122
def expand_asset_paths
  @paths.each do |type, path|
    @paths[type] = File.join(root, path)
  end
  @destination = File.join(root, @destination)
end
parent_module() click to toggle source
# File lib/cyborg/plugin.rb, line 51
def parent_module
  mods = self.class.to_s.split('::')
  mods.pop
  Object.const_get(mods.join('::'))
end
production_root() click to toggle source
# File lib/cyborg/plugin.rb, line 97
def production_root
  @production_asset_root ||= asset_root
end
svgs?() click to toggle source
# File lib/cyborg/plugin.rb, line 76
def svgs?
  @svgs.icons.nil?
end
watch(options) click to toggle source
# File lib/cyborg/plugin.rb, line 88
def watch(options)
  assets(options).map(&:watch)
end

Private Instance Methods

add_files(klass) click to toggle source

Find files based on class type and return an array of Classes for each file

# File lib/cyborg/plugin.rb, line 155
def add_files(klass)
  ext = asset_ext klass
  find_files(ext).map do |path|
    klass.new(self, path)
  end
end
asset_ext(klass) click to toggle source
# File lib/cyborg/plugin.rb, line 149
def asset_ext(klass)
  klass.name.split('::').last.downcase
end
asset_glob(type) click to toggle source
# File lib/cyborg/plugin.rb, line 170
def asset_glob(type)
  case type
  when "sass"
    "*.s[ca]ss"
  else
    "*.#{type}"
  end
end
find_files(ext) click to toggle source

Find files by class type and extension

# File lib/cyborg/plugin.rb, line 163
def find_files(ext)
  files = Dir[File.join(paths[ext.to_sym], asset_glob(ext))]

  # Filter out partials
  files.reject { |f| File.basename(f).start_with?('_') }
end
set_instance(name, value) click to toggle source

Convert configuration into instance variables

# File lib/cyborg/plugin.rb, line 180
    def set_instance(name, value)
      instance_variable_set("@#{name}", value)

      instance_eval(<<-EOS, __FILE__, __LINE__ + 1)
        def #{name}
          @#{name}
        end
      EOS
    end