Class: Cyborg::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/cyborg/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/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

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



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

def destination
  @destination
end

#engineObject (readonly)

Returns the value of attribute engine.



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

def engine
  @engine
end

#gemObject (readonly)

Returns the value of attribute gem.



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

def gem
  @gem
end

#javascriptsObject (readonly)

Returns the value of attribute javascripts.



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

def javascripts
  @javascripts
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#stylesheetsObject (readonly)

Returns the value of attribute stylesheets.



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

def stylesheets
  @stylesheets
end

#svgsObject (readonly)

Returns the value of attribute svgs.



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

def svgs
  @svgs
end

Instance Method Details

#add_assetsObject



57
58
59
60
61
# 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) ⇒ Object



129
130
131
132
133
# File 'lib/cyborg/plugin.rb', line 129

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

#asset_rootObject



92
93
94
95
# 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) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# 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 = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# 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 = {}) ⇒ Object



80
81
82
83
84
85
86
# 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(options) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cyborg/plugin.rb', line 101

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}",
      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



22
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
# 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

#engine_nameObject



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

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

#expand_asset_pathsObject



122
123
124
125
126
127
# 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_moduleObject



51
52
53
54
55
# 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_rootObject



97
98
99
# File 'lib/cyborg/plugin.rb', line 97

def production_root
  @production_asset_root ||= asset_root
end

#svgs?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/cyborg/plugin.rb', line 76

def svgs?
  @svgs.icons.nil?
end

#watch(options) ⇒ Object



88
89
90
# File 'lib/cyborg/plugin.rb', line 88

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