Skip to content

Instantly share code, notes, and snippets.

@kashif-umair
Last active April 18, 2018 10:49
Show Gist options
  • Select an option

  • Save kashif-umair/19dd697ea7248ec0bc303536993b63e8 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

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