Last active
April 18, 2018 10:49
-
-
Save kashif-umair/19dd697ea7248ec0bc303536993b63e8 to your computer and use it in GitHub Desktop.
Extract Logic to a Separate Class - Rails
This file contains hidden or 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
# I have the following classes in my project | |
class CustomReport < ActiveRecord::Base | |
def foo | |
print 'hello' | |
end | |
end | |
class CustomReportService | |
def bar | |
print 'foobar' | |
end | |
def lorem | |
print 'lorem ipsum' | |
end | |
end | |
# I have the following code written at many places throughout my project but this code doesn't work. | |
# I don't want to change the following code because this will require changes at too many places. | |
# I am allowed to change the above implementaion to make the following code work. | |
# What can I change above? | |
# You can suggest as many ways as possible to achieve this behaviour. | |
# ASSUME THAT THIS CODE IS WRITTEN IN A RAILS 4.0+ project. | |
cr = CustomReport.new | |
cr.foo # => hello | |
cr.bar # => foobar | |
cr.lorem # => lorem ipsum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
module CustomReportService
Include this module in custom report model.