Setup in a Rails project
-
As always, add a new entry to your Gemfile as the first step.
gem "convenient_service", "~> 0.19.1"
-
Then run bundle to install the gem.
bundle install
-
To mimic the classic Rails folder structure, you may consider creating
app/services
directory like so:mkdir -p app/services
But feel free to select other directory if you like.
app/services
is chosen for a demonstration since it is autoloaded by Rails. -
Place a configuration file into it.
A common name may be
application_service/config.rb
.mkdir -p app/services/application_service && touch app/services/application_service/config.rb
This example utilizes
Standard
orRails
configs, but you can also pick any other predefined configuration, create customization, or even have multiple configs.- Standard
- Rails
app/services/application_service/config.rbmodule ApplicationService
module Config
include ConvenientService::Config
included do
include ConvenientService::Standard::Config
end
end
endapp/services/application_service/config.rbConvenientService::Dependencies.require_assigns_attributes_in_constructor_using_active_model_attribute_assignment
ConvenientService::Dependencies.require_has_attributes_using_active_model_attributes
ConvenientService::Dependencies.require_has_result_params_validations_using_active_model_validations
module ApplicationService
module Config
include ConvenientService::Concern
included do
include ConvenientService::Standard::Config
concerns do
use ConvenientService::Plugins::Common::AssignsAttributesInConstructor::UsingActiveModelAttributeAssignment::Concern
use ConvenientService::Plugins::Common::HasAttributes::UsingActiveModelAttributes::Concern
use ConvenientService::Plugins::Service::HasJSendResultParamsValidations::UsingActiveModelValidations::Concern
end
middlewares :initialize do
use ConvenientService::Plugins::Common::AssignsAttributesInConstructor::UsingActiveModelAttributeAssignment::Middleware
end
middlewares :result do
use ConvenientService::Plugins::Service::HasJSendResultParamsValidations::UsingActiveModelValidations::Middleware
end
end
end
end -
Now you have everything ready to create fancy services for your application.
(Do not forget to include config into them 🙂)
- Standard
- Rails
app/services/print_what_cow_says.rbclass PrintWhatCowSays
include ApplicationService::Config
attr_reader :text
def initialize(text: "Hello World!")
@text = text
end
def result
puts template(text)
success
end
private
##
# Copied with ❤️ from https://github.com/gaborbata/rosetta-cow
#
def template(text)
cloud =
<<~HEREDOC
#{border(text, "_")}
< #{text} >
#{border(text, "-")}
HEREDOC
cow =
<<~'HEREDOC'.split("\n").map { |line| " " * 10 + line }.join("\n")
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
HEREDOC
cloud + cow
end
def border(text, char)
char * (text.length + 2)
end
endapp/services/print_what_cow_says.rbclass PrintWhatCowSays
include ApplicationService::Config
attribute :text, :string, default: "Hello World!"
def result
puts template(text)
success
end
private
##
# Copied with ❤️ from https://github.com/gaborbata/rosetta-cow
#
def template(text)
cloud =
<<~HEREDOC
#{border(text, "_")}
< #{text} >
#{border(text, "-")}
HEREDOC
cow =
<<~'HEREDOC'.split("\n").map { |line| " " * 10 + line }.join("\n")
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
HEREDOC
cloud + cow
end
def border(text, char)
char * (text.length + 2)
end
end -
That's it. Happy coding!