I am very new to ruby and rails. I am coming from a C++ background, so ruby and rails are a little vexing.
I have designed a rails application that works as a queue. The web API allows items to be pushed into and popped out of the queue. Currently this lives as a specific application with a Jobs model and JobQueueItems model that belongs_to Jobs. However, the mechanism of the queue is not specific to Job at all. It just relies on a foreign key for the item in the queue. How can I abstract this behavior so the next time I want an AnimalQueue or a CustomerQueue I can just create one in my application?
I know I can make a generator which will create the model code with names and methods like "Job" hardcoded in, then call
rails g MyCoolQueue Job
but this seems quite complicated. There are routes and names and views and strong parameters methods to be set up. Is there another way? Can use inheritance or interfaces to make this work? Something like:
class QueueItem < ActiveRecord::Base
end
class JobQueueItem < QueueItem
belongs_to :Job
end
class QueueItemsController < ApplicationController
# all my queue controls, reference the foreign key and associated model in a generic way...
end
class JobQueueItemsController < QueueItemsController
# suddenly I have the controls to queue jobs?
end
# And also some views that generically reference the necessary fields and models in a QueueItem?
# Suddenly I have views for JobQueueItems?
How does one go about making an abstract data type, tool, or other abstract relationship that is easily inserted in to a rails app? One could imagine any number of scenarios where behavior and relationships can be abstracted and only need the names of models and fields to be filled in. I'm sure experienced rails developers don't recode everything from scratch every time or just copy and paste. As I said before, I am new to ruby and rails, so I would like to learn the standard RoR way to do something like this. Is code generation the only way?
Thanks!