Sha256: f75531825842f73118bba25947c053e9cee1917713f0774de5289753a66bae24

Contents?: true

Size: 1.16 KB

Versions: 9

Compression:

Stored size: 1.16 KB

Contents

require 'rails/generators'

module Staple
  class ImportGenerator < Rails::Generators::Base
    desc 'Copy refills'
    source_root File.expand_path("../../../source", __FILE__)
    argument :snippet, type: :string, required: true

    def copy_html
      copy_file_if_exists(
        partial_name,
        File.join('app', 'views', 'refills', partial_name),
      )
    end

    def copy_styles
      copy_file_if_exists(
        File.join('stylesheets', 'refills', stylesheet_name),
        File.join('app', 'assets', 'stylesheets', 'refills', stylesheet_name),
      )
    end

    def copy_javascripts
      copy_file_if_exists(
        File.join('javascripts', 'refills', javascript_name),
        File.join('app', 'assets', 'javascripts', 'refills', javascript_name),
      )
    end

    private

    def copy_file_if_exists(source, destination)
      if File.exists?(File.join(self.class.source_root, source))
        copy_file source, destination
      end
    end

    def partial_name
      "_#{snippet.underscore}.html.erb"
    end

    def stylesheet_name
      "_#{snippet.dasherize}.scss"
    end

    def javascript_name
      "#{snippet.underscore}.js"
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
staple-0.1.0 lib/staple/import_generator.rb
staple-0.0.9 lib/staple/import_generator.rb
staple-0.0.8 lib/staple/import_generator.rb
staple-0.0.7 lib/staple/import_generator.rb
staple-0.0.6 lib/staple/import_generator.rb
staple-0.0.5 lib/staple/import_generator.rb
staple-0.0.4 lib/staple/import_generator.rb
staple-0.0.3 lib/staple/import_generator.rb
staple-0.0.2 lib/staple/import_generator.rb