Class: Chili::Generators::FeatureGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_assets_overrideObject



87
88
89
90
91
92
93
94
# File 'lib/generators/chili/feature/feature_generator.rb', line 87

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

#add_dummy_overrideObject



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/generators/chili/feature/feature_generator.rb', line 74

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;'>
  #{feature.name} active - edit/remove this file:<br/>
  <strong>#{feature.path}/#{example_file_path}</strong><br/>
  <%= link_to 'deface docs', 'https://github.com/spree/deface', target: '_blank' %>
</div>
  RUBY
  end
end

#add_gem_to_main_gemfileObject



29
30
31
32
33
34
35
# File 'lib/generators/chili/feature/feature_generator.rb', line 29

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

#add_generator_proxyObject



96
97
98
99
100
101
102
103
# File 'lib/generators/chili/feature/feature_generator.rb', line 96

def add_generator_proxy
  create_file "lib/generators/#{feature.name}_generator.rb" do <<-RUBY
class #{feature.name.camelcase}Generator < Rails::Generators::Base
  include Chili::GeneratorProxy
end
  RUBY
  end
end

#automount_engineObject



58
59
60
# File 'lib/generators/chili/feature/feature_generator.rb', line 58

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

#chilify_application_controllerObject



50
51
52
# File 'lib/generators/chili/feature/feature_generator.rb', line 50

def chilify_application_controller
  gsub_file "app/controllers/#{feature.name}/application_controller.rb", "ActionController::Base", 'Chili::ApplicationController'
end

#clean_up_gitignoreObject



54
55
56
# File 'lib/generators/chili/feature/feature_generator.rb', line 54

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

#edit_gemspecObject



20
21
22
23
24
25
26
27
# File 'lib/generators/chili/feature/feature_generator.rb', line 20

def edit_gemspec
  require File.expand_path('../../../../chili/version', __FILE__)
  gemspec = "#{feature.name}.gemspec"
  gsub_file gemspec, '# s.add_dependency "jquery-rails"', "s.add_dependency 'chili', '~> #{Chili::VERSION.sub(/\.\d+$/,'')}'"
  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



66
67
68
69
70
71
72
# File 'lib/generators/chili/feature/feature_generator.rb', line 66

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

#include_chili_libsObject



62
63
64
# File 'lib/generators/chili/feature/feature_generator.rb', line 62

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

#remove_jquery_stuffObject



45
46
47
48
# File 'lib/generators/chili/feature/feature_generator.rb', line 45

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

#remove_unused_filesObject



37
38
39
40
41
42
43
# File 'lib/generators/chili/feature/feature_generator.rb', line 37

def remove_unused_files
  remove_dir "app/helpers/#{feature.name}"
  remove_dir 'app/views/layouts'
  remove_dir 'script'
  remove_file 'Gemfile'
  remove_file 'Rakefile'
end

#reset_destination_rootObject



16
17
18
# File 'lib/generators/chili/feature/feature_generator.rb', line 16

def reset_destination_root
  self.destination_root = ''
end

#run_plugin_generatorObject



5
6
7
8
9
10
11
12
13
14
# File 'lib/generators/chili/feature/feature_generator.rb', line 5

def run_plugin_generator
  ARGV[0] = feature.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