Class: SparkEngine::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/spark_engine/plugin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Plugin

Returns a new instance of Plugin.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/spark_engine/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(:@spark_plugin_name, name)
  add_assets
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



3
4
5
# File 'lib/spark_engine/plugin.rb', line 3

def destination
  @destination
end

#engineObject (readonly)

Returns the value of attribute engine.



3
4
5
# File 'lib/spark_engine/plugin.rb', line 3

def engine
  @engine
end

#gemObject (readonly)

Returns the value of attribute gem.



3
4
5
# File 'lib/spark_engine/plugin.rb', line 3

def gem
  @gem
end

#javascriptsObject (readonly)

Returns the value of attribute javascripts.



3
4
5
# File 'lib/spark_engine/plugin.rb', line 3

def javascripts
  @javascripts
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/spark_engine/plugin.rb', line 3

def name
  @name
end

#stylesheetsObject (readonly)

Returns the value of attribute stylesheets.



3
4
5
# File 'lib/spark_engine/plugin.rb', line 3

def stylesheets
  @stylesheets
end

#svgsObject (readonly)

Returns the value of attribute svgs.



3
4
5
# File 'lib/spark_engine/plugin.rb', line 3

def svgs
  @svgs
end

Instance Method Details

#add_assetsObject



89
90
91
92
93
# File 'lib/spark_engine/plugin.rb', line 89

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) ⇒ Object



165
166
167
168
169
# File 'lib/spark_engine/plugin.rb', line 165

def asset_path(file=nil)
  dest = destination
  dest = File.join(dest, file) if file
  dest
end

#asset_rootObject



127
128
129
130
# File 'lib/spark_engine/plugin.rb', line 127

def asset_root
  asset_prefix = Rails.application.config.assets.prefix || '/assets'
  File.join asset_prefix, name
end

#asset_url(file = nil) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/spark_engine/plugin.rb', line 171

def asset_url(file=nil)

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

  path = File.join(path, file) if file
  path
end

#assets(options = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/spark_engine/plugin.rb', line 95

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 = {}) ⇒ Object



112
113
114
115
116
117
# File 'lib/spark_engine/plugin.rb', line 112

def build(options={})
  FileUtils.mkdir_p(asset_path)
  assets(options).each do |asset|
    asset.build
  end
end

#config(options) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/spark_engine/plugin.rb', line 136

def config(options)

  options = {
    production_asset_root: nil,
    destination:   "public/",
    root:          @gem.full_gem_path,
    version:       @gem.version.to_s,
    gem_name:      @gem.name,
    paths: {
      stylesheets: "app/assets/stylesheets/#{name}",
      javascripts: "app/assets/javascripts/#{name}",
      components:  "app/components/#{name}",
      images:      "app/assets/images/#{name}",
      svgs:        "app/assets/svgs/#{name}",
    }
  }.merge(options)

  options.each do |k,v|
    set_instance(k.to_s,v)
  end
end

#create_engine(&block) ⇒ Object

Create a new Rails::Engine



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/spark_engine/plugin.rb', line 23

def create_engine(&block)
  @engine = parent_module.const_set('Engine', Class.new(Rails::Engine) do

    def spark_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(spark_plugin_path)
    end

    engine_name SparkEngine.plugin.name

    require 'spark_engine/middleware'

    # Ensure compiled assets in /public are served
    initializer "#{name}.static_assets" do |app|
      if app.config.public_file_server.enabled
        app.middleware.insert_after ::ActionDispatch::Static, SparkEngine::StaticAssets, "#{root}/public", engine_name: SparkEngine.plugin.name
        app.middleware.insert_before ::ActionDispatch::Static, Rack::Deflater
      end
    end

    initializer "#{name}.view_paths" do |app|
      # Ensure Components are readable from engine paths
      ActiveSupport.on_load :action_controller do
        append_view_path "#{SparkEngine.plugin.paths[:components]}"
      end

    end

    initializer "#{name}.asset_paths" do |app|
      app.config.assets.paths << SparkEngine.plugin.paths[:components]
    end

  end)

  # Autoload engine lib and components path
  @engine.config.autoload_paths.concat [
    File.join(@engine.spark_plugin_path, "lib"),
    SparkEngine.plugin.paths[:components]
  ]

  @engine.config.after_initialize do |app|
    if defined?(SassC) && defined?(SassC::Rails)
      # Inject Sass importer for yaml files
      require "spark_engine/sassc/importer"
      SassC::Rails::Importer::EXTENSIONS << SassC::SparkEngine::Importer::SassYamlExtension.new
    elsif defined?(Sass)
      # Overwrite Sass engine with Yaml support
      require "spark_engine/sass/engine"
    end
  end

  # Takes a block passed an evaluates it in the context of a Rails engine
  # This allows plugins to modify engines when created.
  @engine.instance_eval(&block) if block_given?
end

#debug?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/spark_engine/plugin.rb', line 119

def debug?
  ENV['SPARK_DEBUG']
end

#engine_nameObject



18
19
20
# File 'lib/spark_engine/plugin.rb', line 18

def engine_name
  @engine.name.sub(/::Engine/,'')
end

#expand_asset_pathsObject



158
159
160
161
162
163
# File 'lib/spark_engine/plugin.rb', line 158

def expand_asset_paths
  @paths.each do |type, path|
    @paths[type] = File.join(root, path)
  end
  @destination = File.join(root, @destination)
end

#parent_moduleObject



83
84
85
86
87
# File 'lib/spark_engine/plugin.rb', line 83

def parent_module
  mods = self.class.to_s.split('::')
  mods.pop
  Object.const_get(mods.join('::'))
end

#production_rootObject



132
133
134
# File 'lib/spark_engine/plugin.rb', line 132

def production_root
  @production_asset_root ||= asset_root
end

#svgs?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/spark_engine/plugin.rb', line 108

def svgs?
  @svgs.icons.nil?
end

#watch(options) ⇒ Object



123
124
125
# File 'lib/spark_engine/plugin.rb', line 123

def watch(options)
  assets(options).map(&:watch)
end