Skip to content

Instantly share code, notes, and snippets.

@kashif-umair
Last active April 18, 2018 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kashif-umair/19dd697ea7248ec0bc303536993b63e8 to your computer and use it in GitHub Desktop.
Save kashif-umair/19dd697ea7248ec0bc303536993b63e8 to your computer and use it in GitHub Desktop.
Extract Logic to a Separate Class - Rails
# 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
@rizwan-munir-7
Copy link

module CustomReportService
Include this module in custom report model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment