7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/myrails/modules/rails_generator_actions.rb', line 7
def self.included(thor)
thor.class_eval do
desc 'model', "Generates a rails model with the given name along with its related spec file and namespace prefix for table creation. Use '/' to create a namespaced model"
def model
template 'rails/app/models/model.rb', "app/models/#{@name.downcase}.rb"
template 'rails/app/models/namespace_model.rb', "app/models/#{@name.split("/").first.singularize.downcase}.rb" if @name.include?("/")
template 'spec/model.rb', "spec/models/#{@name.downcase}_spec.rb"
end
desc 'controller', "Generates a rails controller with the given name along with related spec file. Use '/' to create a namespaced controller"
def controller
template 'rails/app/controllers/controller.rb', "app/controllers/#{@name.downcase.pluralize}_controller.rb"
if @name.include?("/")
parent, child = name.split("/")
template 'rails/app/controllers/namespace_controller.rb', "app/controllers/#{parent}/#{parent.downcase}_controller.rb"
end
template 'spec/controller.rb', "spec/controllers/#{@name.downcase.pluralize}_controller_spec.rb"
run "mkdir -p app/views/#{@name.downcase.pluralize}"
end
desc 'policy', "Generates a pundit policy with the given name and a related spec file. Use '/' to create a namespaced policy"
def policy
template 'rails/app/policies/pundit.rb', "app/policies/#{@name.downcase}_policy.rb"
template 'spec/pundit.rb', "spec/policies/#{@name.downcase}_policy_spec.rb"
end
desc 'decorator', 'Generates draper decoration with given name and related spec file'
def decorator
copy_file 'rails/app/decorators/application_decorator.rb', 'app/decorators/application_decorator.rb'
template 'rails/app/decorators/decoration.rb', "app/decorators/#{@name.downcase}_decoration.rb"
copy_file 'spec/support/configs/decorator_presenter.rb', 'spec/support/configs/decorator_presenter.rb'
template 'spec/decorator_spec.rb', "spec/decorators/#{@name.downcase}_decorator_spec.rb"
end
desc 'factory', "Generates a factory_bot factory in the spec/factories directory. Use '/' to create a namespaced factory"
def factory
template 'spec/factory.rb', "spec/factories/#{@name.downcase}.rb"
end
desc 'config_env', 'Add code to environment files. Host refers to url options. Name option referes to controller and mailer default_url_options'
def config_env
ENVIRONMENTS.each do |environment|
case environment
when 'development'
inject_into_file 'config/environments/development.rb', after: "config.action_mailer.raise_delivery_errors = false\n" do <<-CODE
config.action_mailer.delivery_method = :letter_opener
config.action_mailer.perform_deliveries = false
config.action_mailer.default_url_options = { host: ENV.fetch('DEFAULT_HOST') }
config.action_controller.default_url_options = { host: ENV.fetch('DEFAULT_HOST') }
CODE
end
when 'test'
inject_into_file 'config/environments/test.rb', after: "config.action_mailer.delivery_method = :test\n" do <<-CODE
config.action_mailer.default_url_options = { host: ENV.fetch('DEFAULT_HOST') }
config.action_controller.default_url_options = { host: ENV.fetch('DEFAULT_HOST') }
CODE
end
when 'production'
inject_into_file 'config/environments/production.rb', after: "config.active_record.dump_schema_after_migration = false\n" do <<-CODE
config.action_mailer.default_url_options = { host: ENV.fetch('DEFAULT_HOST') }
config.action_controller.default_url_options = { host: ENV.fetch('DEFAULT_HOST') }
config.assets.compile = true
CODE
end
end
end
end
desc 'new_ui NAME', 'Create a new ui view'
def new_ui
run "touch app/views/ui/#{@name.downcase}.html.haml"
say "DON'T FORGET: Restart the rails app"
end
end
end
|