Sha256: d48efab6f47c92b0f821390b47f4e2366f3305f1cfe39186e2266e5c66601116

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 KB

Contents

require 'rails/generators'

module Fullcalendar
  module Generators
    class InstallGenerator < ::Rails::Generators::Base
      desc "This generator installs fullcalendar to the rails asset pipeline"

      argument :style, :type => :string, :default => 'default'
      # needed for thor templates
      source_root File.expand_path("../templates", __FILE__)
      
      def add_assets
        js_manifest = 'app/assets/javascripts/application.js'
        css_manifest = 'app/assets/stylesheets/application.css'
        scss_manifest = 'app/assets/stylesheets/application.scss'
        js_strings = "//= require calendar\n"

        insert_into_file js_manifest, js_strings, :after => "turbolinks\n" if File.exists?(js_manifest)
        insert_css_strings(css_manifest) if File.exists?(css_manifest)
        insert_scss_strings(scss_manifest) if File.exists?(scss_manifest)
        copy_core_file
      end

      private
      
      def insert_css_strings(css_manifest)
        content = File.read(css_manifest)
        css_strings = "*/= require calendar\n"

        if requires_tree(content)
          insert_into_file css_manifest, css_strings, :after => "require_tree .\n"
        elsif requires_self(content)
          insert_into_file css_manifest, css_strings, :before => " *= require_self\n"
        else
          insert_into_file css_manifest, css_strings, :before => " */"
        end
          
      end

      def insert_scss_strings(scss_manifest)
        content = File.read(scss_manifest)
        scss_strings = "\n@import \"calendar\";\n"
        append_to_file scss_manifest, scss_strings
      end

      def requires_tree(content)
        content.match(/require_tree\s+\.\s*$/)
      end

      def requires_self(content)
        content.match(/require_self\s*$/)
      end
      
      def copy_core_file
        template "#{style}.js.tt", "app/assets/javascripts/calendar.js"
        template "#{style}.scss.tt", "app/assets/stylesheets/calendar.scss"
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fullcalendar-3.9.0 lib/generators/fullcalendar/install_generator.rb
fullcalendar-3.8.2 lib/generators/fullcalendar/install_generator.rb
fullcalendar-3.8.1 lib/generators/fullcalendar/install_generator.rb
fullcalendar-3.8.0 lib/generators/fullcalendar/install_generator.rb