-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
110 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module RailsServices | ||
module Generators | ||
class CreateGenerator < Rails::Generators::Base | ||
source_root File.expand_path('../templates', __FILE__) | ||
argument :service_name, type: :string | ||
argument :parent_name, type: :string | ||
argument :sub_folder, type: :string, default: '' | ||
|
||
def create_service | ||
template 'base_service.rb.erb', "app/services/#{service_directory}/#{service_file}" | ||
template 'base_service_spec.rb.erb', "spec/services/#{service_directory}/#{spec_file}" | ||
end | ||
|
||
private | ||
def service_directory | ||
file_path = "#{parent_name.underscore}" | ||
file_path += "/#{sub_folder.underscore}" if sub_folder.present? | ||
file_path | ||
end | ||
|
||
def service_file | ||
"#{service_name.underscore}.rb" | ||
end | ||
|
||
def spec_file | ||
"#{service_name.underscore}_spec.rb" | ||
end | ||
|
||
def qualified_name | ||
name = "#{parent_name.camelize}::" | ||
name += "#{sub_folder.camelize}::" if sub_folder.present? | ||
name += "#{service_name.camelize}" | ||
name | ||
end | ||
end | ||
end | ||
end |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Description: | ||
Destroys a service class and accompanying spec file | ||
|
||
Example: | ||
rails generate rails_services:destroy Thing Model|Controller Sub-folder [opt] | ||
|
||
This will remove: | ||
app/services/model|controller/[sub-folder]/thing.rb | ||
spec/services/model|controller/[sub-folder]/thing_spec.rb |
51 changes: 51 additions & 0 deletions
51
lib/generators/rails_services/destroy/destroy_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module RailsServices | ||
module Generators | ||
class DestroyGenerator < Rails::Generators::Base | ||
argument :service_name, type: :string | ||
argument :parent_name, type: :string | ||
argument :sub_folder, type: :string, default: '' | ||
|
||
def destroy_service | ||
remove_file "app/services/#{full_service_directory}/#{service_file}" | ||
remove_file "spec/services/#{full_service_directory}/#{spec_file}" | ||
|
||
manage_containing_directories('app', 'spec') | ||
end | ||
|
||
private | ||
def full_service_directory | ||
file_path = "#{service_parent_directory}" | ||
file_path += "/#{sub_folder.underscore}" if sub_folder.present? | ||
file_path | ||
end | ||
|
||
def service_parent_directory | ||
"#{parent_name.underscore}" | ||
end | ||
|
||
def service_file | ||
"#{service_name.underscore}.rb" | ||
end | ||
|
||
def spec_file | ||
"#{service_name.underscore}_spec.rb" | ||
end | ||
|
||
def manage_containing_directories(*directory_types) | ||
directory_types.each do |directory_type| | ||
remove_dir("#{directory_type}/services/#{full_service_directory}") if directory_empty?(directory_type) | ||
remove_dir("#{directory_type}/services/#{service_parent_directory}") if parent_directory_empty?(directory_type) | ||
end | ||
end | ||
|
||
def directory_empty?(directory_type) | ||
Dir["#{Rails.root}/#{directory_type}/services/#{full_service_directory}/*"].empty? | ||
end | ||
|
||
def parent_directory_empty?(directory_type) | ||
Dir["#{Rails.root}/#{directory_type}/services/#{service_parent_directory}/*"].empty? | ||
end | ||
|
||
end | ||
end | ||
end |
37 changes: 0 additions & 37 deletions
37
lib/generators/rails_services/service/service_generator.rb
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
spec/generators/rails_services/create/create_generator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe 'My behaviour' do | ||
it 'should do something' do | ||
expect(true).to be_falsey | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
spec/generators/rails_services/destroy/destroy_generator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
describe 'My behaviour' do | ||
|
||
it 'should do something' do | ||
expect(true).to be_falsey | ||
end | ||
|
||
end |