Sha256: 4dc661e1fe1d293eafbe8ca831c2eaa1d593fd6dbcedef1cd415d0117641620e

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

require "ruby2js"
require "listen"

module Clapton
  class Engine < ::Rails::Engine
    isolate_namespace Clapton

    initializer "clapton.helpers" do
      ActiveSupport.on_load(:action_view) do
        include ClaptonHelper
      end
    end

    initializer "clapton.action_cable_helpers" do
      ActiveSupport.on_load(:action_cable) do
        ActionCable.server.config.logger = Rails.logger
      end

      if Rails.env.development? || Rails.env.test?
        components_path = Rails.root.join("app", "components")
        FileUtils.mkdir_p(components_path) unless components_path.exist?
        FileUtils.touch(components_path.join(".keep"))

        listener = Listen.to(Rails.root.join("app", "components")) do |modified, added, removed|
          compile_components
        end

        listener.start
      end
    end

    def compile_components
      js = File.read(File.join(__dir__, "javascripts", "dist", "components.js"))
      js += "\n"
      js += File.read(File.join(__dir__, "javascripts", "dist", "client.js"))
      js += "\n"
      js += "window.components = [];"
      js += "\n"
      Dir.glob(Rails.root.join("app", "components", "**", "*.rb")).each do |file|
        js += Ruby2JS.convert(File.read(file), preset: true)
        js += "\n"
        js += "window.#{File.basename(file, ".rb").camelize} = #{File.basename(file, ".rb").camelize};"
        js += "\n"
      end
      FileUtils.mkdir_p(Rails.root.join("public", "clapton")) unless Rails.root.join("public", "clapton").exist?
      File.write(Rails.root.join("public", "clapton", "index.js"), js)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
clapton-0.0.12 lib/clapton/engine.rb