Class: Chili::Generators::ExtensionGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/chili/extension_generator.rb

Constant Summary collapse

NAME =
"#{ARGV[0]}_extension"
PATH =
"vendor/chili/#{NAME}"

Instance Method Summary collapse

Instance Method Details

#add_assets_overrideObject



102
103
104
105
106
107
108
109
# File 'lib/generators/chili/extension_generator.rb', line 102

def add_assets_override
  create_file 'app/overrides/layouts/application/assets.html.erb.deface' do <<-RUBY
<!-- insert_bottom 'head' -->
<%= stylesheet_link_tag '#{NAME}/application' %>
<%= javascript_include_tag '#{NAME}/application' %>
  RUBY
  end
end

#add_dummy_overrideObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/generators/chili/extension_generator.rb', line 89

def add_dummy_override
  example_file_path = "app/overrides/layouts/application/example.html.erb.deface"
  create_file example_file_path do <<-RUBY
<!-- insert_bottom 'body' -->
<div style='background: #FFF;text-align: center; padding: 4px 0;position: fixed;width: 100%;z-index: 9999;top: 0;'>
  #{NAME} active - edit/remove this file:<br/>
  <strong>#{PATH}/#{example_file_path}</strong><br/>
  <%= link_to 'deface docs', 'https://github.com/railsdog/deface', target: '_blank' %>
</div>
  RUBY
  end
end

#add_gem_to_main_gemfileObject



31
32
33
34
35
36
37
# File 'lib/generators/chili/extension_generator.rb', line 31

def add_gem_to_main_gemfile
  gemfile =  "../../../Gemfile"
  group = "group :chili do\n"
  append_to_file gemfile, group
  append_to_file gemfile, "  gem '#{NAME}', path: '#{PATH}'\nend", after: group
  gsub_file gemfile, 'end  gem', '  gem' #nasty cleanup
end

#automount_engineObject



73
74
75
# File 'lib/generators/chili/extension_generator.rb', line 73

def automount_engine
  prepend_to_file 'config/routes.rb', "#{NAME.camelcase}::Engine.automount!\n"
end

#clean_up_gitignoreObject



69
70
71
# File 'lib/generators/chili/extension_generator.rb', line 69

def clean_up_gitignore
  gsub_file ".gitignore", /test\/dummy.*\n/, ''
end

#edit_gemspecObject



22
23
24
25
26
27
28
29
# File 'lib/generators/chili/extension_generator.rb', line 22

def edit_gemspec
  require File.expand_path('../../../chili/version', __FILE__)
  gemspec = "#{NAME}.gemspec"
  gsub_file gemspec, '# s.add_dependency "jquery-rails"', "s.add_dependency 'chili', '~> #{Chili::VERSION}'"
  gsub_file gemspec, 'TODO: Your name', `git config user.NAME`.chomp
  gsub_file gemspec, 'TODO: Your email', `git config user.email`.chomp
  gsub_file gemspec, /TODO(:\s)?/, ''
end

#include_active_ifObject



81
82
83
84
85
86
87
# File 'lib/generators/chili/extension_generator.rb', line 81

def include_active_if
  inject_into_file "lib/#{NAME}.rb", after: "module #{NAME.camelcase}\n" do <<-RUBY
  extend Chili::Activatable
  active_if { true } # edit this to activate/deactivate extension at runtime
  RUBY
  end
end

#include_chili_libsObject



77
78
79
# File 'lib/generators/chili/extension_generator.rb', line 77

def include_chili_libs
  prepend_to_file "lib/#{NAME}.rb", "require \"chili\"\n"
end

#remove_jquery_stuffObject



47
48
49
50
# File 'lib/generators/chili/extension_generator.rb', line 47

def remove_jquery_stuff
  gsub_file "app/assets/javascripts/#{NAME}/application.js", "//= require jquery_ujs\n", ''
  gsub_file "app/assets/javascripts/#{NAME}/application.js", "//= require jquery\n", ''
end

#remove_unused_filesObject



39
40
41
42
43
44
45
# File 'lib/generators/chili/extension_generator.rb', line 39

def remove_unused_files
  remove_dir "app/controllers/#{NAME}"
  remove_dir "app/helpers/#{NAME}"
  remove_dir 'app/views/layouts'
  remove_file 'Gemfile'
  remove_file 'Rakefile'
end

#reset_destination_rootObject



18
19
20
# File 'lib/generators/chili/extension_generator.rb', line 18

def reset_destination_root
  self.destination_root = ''
end

#run_plugin_generatorObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/generators/chili/extension_generator.rb', line 7

def run_plugin_generator
  ARGV[0] = PATH
  ARGV[1] = "--mountable"
  ARGV[2] = '--skip-test-unit'
  ARGV[3] = '--skip-bundle'

  require 'rails/generators'
  require 'rails/generators/rails/plugin_new/plugin_new_generator'
  Rails::Generators::PluginNewGenerator.start
end

#set_up_custom_generatorObject



52
53
54
55
56
57
58
59
# File 'lib/generators/chili/extension_generator.rb', line 52

def set_up_custom_generator
  inject_into_file "lib/#{NAME}/engine.rb", after: "isolate_namespace #{NAME.camelcase}\n" do <<-RUBY
    config.generators do |g|
g.scaffold_controller :chili
    end
  RUBY
  end
end

#set_up_rspecObject



61
62
63
64
65
66
67
# File 'lib/generators/chili/extension_generator.rb', line 61

def set_up_rspec
  inject_into_file "lib/#{NAME}/engine.rb", :after => " g.scaffold_controller :chili\n" do <<-RUBY
g.test_framework :rspec, view_specs: false, routing_specs: false, controller_specs: false
g.integration_tool :rspec
  RUBY
  end
end