Where should my non-model/non-controller code live?
This is what the 'lib' folder is for.
The lib folder is in the automatically looked up path, so you can have
class MyFoo
end
in lib/my_foo.rb
and then just by calling
MyFoo.new
from a controller the code will be loaded without you needing a require 'my_foo'
Rails: Where do you put non-model code?
You should be able to use lib
folder in your root directory (unless it's changed in Rails 3).
You can refer classes from there without require
statement.
Where to place Rails code that is not a model, view, controller or helper?
If the code is something like modules with utility methods in these could be placed in the lib
folder. Alternatively you could create a common superclass for some of your controllers if they share behaviour.
Please post an example of the kind of code you're thinking of.
Where do I place my non-model classes?
For service-type objects I put them in app/services.
For model-type objects I put them in app/models. I do not think it is necessary to inherit from ActiveRecord to be considered a model.
I think your object classifies as a 'service object' since it is designed to wrap the service of uploading a file, it isn't really a domain object like a model would be.
Rails best practice question: Where should one put shared code and how will it be loaded?
I'd put the shared code in the Rails project's /lib
directory and consider making it a custom Rake task.
Rails XML parsing -- should I put this functionality in the controller or model?
I would place the code in a class which would fetch the xml and parse the xml for you.
This way its easier to Unit Test the class for parsing or any other operations you want to do on the returned XML.
The class can be placed in the lib folder or if its sort of like a Pseudo Model ( with a XML Database ) it can be placed in Models as well. Thats mostly upto what the class is and how you would like structure your codebase.
Update: a Plus of this approach, you code base can remain the same even if you swap out the service for something else, because you code base interacts with this class, the changes will have to be made only to this class. Interface advantages :)
Complex Model-related logic called via Rake: where to keep it?
In my opinion all API-related classes should be separated and lie in app/services/
directory, moreover, I'd create adapters for each of these classes(for example if external API will change version case - I wrote a post about this approach if you are interested). Next, if your logic hits multiple models, I'd put it into app/usecases
, there is a great article about structuring Rails app.
So I suggest you to split everything, according to Single Responsibility Principle. This way it would be easy to test and maintain the code.
Example:
- rake task calls
SomeComplexCreatorUsecase.new(options).run
SomeComplexCreatorUsecase
creates needed objects and relationsSomeComplexCreatorUsecase
callsExternalApi1Service.get_some_data
ExternalApi1Service.get_some_data
callsExternalApi1Adapter.get_some_data
(optional)SomeComplexCreatorUsecase
gets all needed data and perform remaining calculations
Related Topics
How to Generate Links with Trailing Slash in Rails 3
Why Is Enumerable#Each_With_Object Deprecated
Convert String into Array in Rails
HTML Code Inside Buttons with Simple_Form
Ruby Modulo 3 with Negative Numbers Is Unintuitive
Converting a Hexadecimal Digest to Base64 in Ruby
Ruby Difference in Array Including Duplicates
How to Communicate with Threads in Ruby
Add Space After Commas Only If It Doesn't Already
How to Make 'Whenever' Gem Work on Windows
How to Implement a "Callback" in Ruby
Connect to a Locally Built Jekyll Server Using Mobile Devices in the Lan
How to Enable Tls V1.2 in Ruby? If So, How
Ruby: Creating a Sandboxed Eval
How to Copy File Across Buckets Using Aws-S3 or Aws-Sdk Gem in Ruby on Rails