Alternatives to use polymorphism in Ruby on Rails - ruby-on-rails

I'm currently writing some intranet web application where people could submit to admins requests for adding different resources. The example requests would be:
installing programs, in this case user will select which program he wants installed
increasing quota, in this case user will just enter the amount of disk space he needs or maybe he will select the predefined quantities - 1GB, 10GB etc...
create new email alias, in this case user will just type the alias.
...
I was thinking about having just one model UserRequests with the reference to the sender and
two optional attributes one would be reference_id that would refefrence to other tables (for
example the Program that he wants installed) and another would be used for free type fields
like email alias or quota.
So my problem is that based on the type of the request the model should contain either:
reference to other table
integer data
string data
Based on the type of the request the given action should be taken - probably email alias
could be added from rails but the application on users computer will be installed by hand.
Does anyone had similar problem? Do you think using polymorphism for this kind of stuff is a good idea? Do you have any suggestions on how to organize data in the tables?

Single Table Inheritance! This way you can have each type of request have custom validations, while still having every request live in the same table.
class CreateUserRequests < ActiveRecord::Migration
def self.up
create_table :user_requests do |t|
t.string :string_data, :type
t.integer :user_id, :integer_data
t.timestamps
end
end
def self.down
drop_table :user_requests
end
end
class UserRequest < ActiveRecord::Base
belongs_to :user
end
class EmailAliasRequest < UserRequest
validates_presence_of :string_data
validates_format_of :string_data, :with => EMAIL_REGEX
end
class ProgramInstallRequest < UserRequest
belongs_to :program, :class_name => "Program", :foreign_key => "integer_data"
validates_presence_of :integer_data
end
class QuotaIncreaseRequest < UserRequest
validates_presence_of :string_data
validates_inclusion_of :string_data, :in => %w( 1GB 5GB 10GB 15GB )
end
And of course, alias your string_data and integer_data to email or whatnot to make your other code have a little more meaning. Let the model be the little black box that hides it all away.

I would use polymorphic associations, which let a model belong to more than one other model using a single association. Something like this:
class AdminRequest < ActiveRecord::Base
belongs_to :user
belongs_to :requestable, :polymorphic => true
end
class EmailAlias < ActiveRecord::Base
has_many :admin_requests, :as => :requestable
end
class ProgramInstall < ActiveRecord::Base
has_many :admin_requests, :as => :requestable
end
class QuotaIncrease < ActiveRecord::Base
has_many :admin_requests, :as => :requestable
end
As ever, Ryan Bates has an excellent Railscast on the subject.

Related

Using delegate to create associated records in Rails 4 / Class or Multiple Table Inheritance

I'm trying to build one model on top of another:
# This is a 'base' class which...
class Company < ActiveRecord::Base
has_many :managers
end
# ...has managers, but they are just...
class Manager < ActiveRecord::Base
belongs_to :company
belongs_to :person
delegate :name, :email,
to: :person
validates_presence_of :position
end
# ...a 'layer' on top of a Person
class Person < ActiveRecord::Base
has_one :manager
validates_presence_of :name, :email
end
Because when you have managers, contact persons, clients, employees and so on, it's natural to make Person model to take care of the names, phones, emails and all the attributes that logically belong to person. It's like subclassing:
class PlanetDesigner < ActiveRecord::Base
# if it wasn't AR, we would use PlanetDesigner < Person...
belongs_to :person
belongs_to :what_not
delegate :name, :email, #...but it's AR and we're using this instead
to: :person
# here go lots of PlanetDesigner-specific methods
end
class SaviourOfTheUniverse < ActiveRecord::Base
# same scenario here
end
# and a few similar classes more
But how?
When I Company.find(42).managers.create(name: 'Slartibartfast', email: 'vip_planets#magrathea.com', position: 'Designer'), I expectedly get ActiveRecord::UnknownAttributeError: unknown attribute: name.
I sure can do it with callbacks and stuff, but tell me, can't Rails do it itself?
To me this is not the case of a polymorphic association. It's having several different models with different behaviour and their own attributes built on top of Person.
Thank you in advance for your time.
class Manager < ActiveRecord::Base
belongs_to :company
belongs_to :person
delegate :name, :email, to: :person
validates_presence_of :position
accepts_nested_attribute_for :person
end
Company.find(42).managers.create(position: 'Designer', person_attributes: {
name: 'Slartibartfast', email: 'vip_planets#magrathea.com',
})
After some additional research I figured that the answer to my question is obvious and it's called Multiple Table Inheritance (MTI) or Class Table Inheritance (CTI).
One particular solution is ActiveRecord::ActsAs gem written by Hassan Zamani, which is probably the most elegant and up-to-date. I've checked out every other alternative from Ruby Toolbox and they all seem either abandoned or outdated.
It bugs me though why there's no native support for CTI in Rails yet, as the problem is so common. A few searches in Google and ruby-forum.com showed no active discussions about implementing the feature.
I thank everyone for their answers and the time taken.
If you'd like to extract naming logic from Manager, you can do smth like that:
class Manager < ActiveRecord::Base
belongs_to :company
def person
#person ||= Person.new(name, email)
end
end
# plain Ruby class
class Person
def initialize(name, email)
#name, #email = name, email
end
# person methods
end
# and then use it:
Manager.find(42).person.name_with_mail

Adding belongs to relationship to Ruby Gem Mailboxer

I am building an e-com application and would like to implement something like a messaging system. In the application, all conversation will be related to either a Product model or an Order model. In that case, I would like to store the relating object (type + id, I supposed) to the Conversation object.
To add the fields, of course I can generate and run a migration, however, since the Model and Controller are included within the gem, how can I declare the relationship? (belongs_to :linking_object, :polymorphic) and the controller? Any idea?
Thank you.
I ended up customizing the Mailboxer gem to allow for a conversationable object to be attached to a conversation.
In models/mailboxer/conversation.rb
belongs_to :conversationable, polymorphic: true
Add the migration to make polymorphic associations work:
add_column :mailboxer_conversations, :conversationable_id, :integer
add_column :mailboxer_conversations, :conversationable_type, :string
In lib/mailboxer/models/messageable.rb you add the conversationable_object to the parameters for send_message:
def send_message(recipients, msg_body, subject, sanitize_text=true, attachment=nil, message_timestamp = Time.now, conversationable_object=nil)
convo = Mailboxer::ConversationBuilder.new({
:subject => subject,
:conversationable => conversationable_object,
:created_at => message_timestamp,
:updated_at => message_timestamp
}).build
message = Mailboxer::MessageBuilder.new({
:sender => self,
:conversation => convo,
:recipients => recipients,
:body => msg_body,
:subject => subject,
:attachment => attachment,
:created_at => message_timestamp,
:updated_at => message_timestamp
}).build
message.deliver false, sanitize_text
end
Then you can have conversations around objects:
class Pizza < ActiveRecord::Base
has_many :conversations, as: :conversationable, class_name: "::Mailboxer::Conversation"
...
end
class Photo < ActiveRecord::Base
has_many :conversations, as: :conversationable, class_name: "::Mailboxer::Conversation"
...
end
Assuming you have some users set up to message each other
bob = User.find(1)
joe = User.find(2)
pizza = Pizza.create(:name => "Bacon and Garlic")
bob.send_message(joe, "My Favorite", "Let's eat this", true, nil, Time.now, pizza)
Now inside your Message View you can refer to the object:
Pizza Name: <%= #message.conversation.conversationable.name %>
Although rewriting a custom Conversation system will be the best long-term solution providing the customization requirement (Like linking with other models for instance), to save some time at the moment I have implement the link with a ConversationLink Model. I hope it would be useful for anyone in the future who are at my position.
Model: conversation_link.rb
class ConversationLink < ActiveRecord::Base
belongs_to :conversation
belongs_to :linkingObject, polymorphic: true
end
then in each models I target to link with the conversation, I just add:
has_many :conversation_link, as: :linkingObject
This will only allow you to get the related conversation from the linking object, but the coding for reverse linking can be done via functions defined in a Module.
This is not a perfect solution, but at least I do not need to monkey patch the gem...
The gem automatically take care of this for you, as they have built a solution that any model in your own domain logic can act as a messagble object.
Simply declaring
acts_as_messagable
In your Order or Product model will accomplish what you are looking for.
You could just use something like:
form_helper :products
and add those fields to the message form
but mailboxer comes with attachment functionality(carrierwave) included
this might help if you need something like attachments in your messages:
https://stackoverflow.com/a/12199364/1230075

Ruby on Rails associations not taking effect

I'm brand new to Ruby on Rails and I'm having a heck of a time making sense of my associations.
At my company we rent out Scanner Packs that include scanners and servers.
When we receive a request for a scanner pack, ideally I'd create a new scanner package with the customer info and attach however many scanners and servers are needed.
Here is what I have for my three models, scanner_pack, server and scanner:
scanner_pack.rb:
class ScannerPack < ActiveRecord::Base
attr_accessible :producer, :reserved_from, :reserved_to, :scanner_id, :server_id, :scanner_pack_id
has_many :scanners, :foreign_key => "scanner_id"
has_many :servers, :foreign_key => "server_id"
end
server.rb:
class Server < ActiveRecord::Base
attr_accessible :cat5, :power_cable, :router, :name, :status, :location, :id, :notes, :reserved_from, :reserved_to, :scanner_pack_id
belongs_to :scanner_pack, :class_name => "Server", :foreign_key => "server_id"
end
scanner.rb:
class Scanner < ActiveRecord::Base
attr_accessible :id, :location, :name, :notes, :serial, :status
belongs_to :scanner_pack, :class_name => "Scanner", :foreign_key => "scanner_id"
end
I've googled and searched for quite a while now and I've noticed sometimes people say to remove the attr_accessible for scanner_id and server_id in the scanner_pack model because it will overwrite the association. When I do that, I get the error:
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes:
In the rails console when I attempt to create a new ScannerPack I'm doing something like this:
scanner = Scanner.find(1)
ScannerPack.create(:producer => 12345, :scanner_id => scanner.id)
Then if I try something like:
scannerpack = ScannerPack.find(1)
scannerpack.scanner_id
it returns the correct value for scanner_id
When I try: scannerpack.scanner.id It gives me an undefined method error (I've also tried scannerpack.scanners.id). In my mind it should return the id of the scanner from the scanner object
I'm thinking that I'm either missing something very simple or that I'm completely misunderstanding how to do this. Maybe I should be using a has_and_belongs_to_many assocation? I'd appreciate any help anyone can give!
thanks!
Edit: Here is the whole project on github.
Since you want to have many-to-many association, here is how to achieve this, step by step.
We should create model that will contain scanner_id and scanner_pack_id. Let's name it Pack (I'm sure you will be able to name it better).
rails g model Pack scanner_id:integer scanner_pack_id:integer
rake db:migrate
When you run it, you have to write appripriate "declarations" in your models:
class Pack
belongs_to :scanner
belongs_to :scanner_pack
end
class ScannerPack
has_many :packs
has_many :scanners, through: :packs
end
class Scanner
has_many :packs
has_many :scanner_packs, through: :packs
end
When you're done, you can easily bind scanner_packs with scanner (and vice versa) with (for example):
scanner.scanner_packs << scanner_pack
Remove all other params from belongs_to and has_many methods, leaving only association names.
Error is exactly here
:class_name => "Server"
and here
:class_name => "Scanner"
Reference http://guides.rubyonrails.org/association_basics.html#belongs_to-association-reference (scroll down to 4.1.2 Options for belongs_to and 4.1.2.2 :class_name

How do I use has_many :through and in_place_edit?

I have two Models: Campaign and Contact.
A Campaign has_many Contacts.
A Contact has_many Campaigns.
Currently, each Contact has a contact.date_entered attribute. A Campaign uses that date as the ate to count down to the different Events that belong_to the Campaign.
However, there are situations where a Campaign for a specific Contact may need to be delayed by X number of days. In this instance, the campaigncontact.delaydays = 10.
In some cases, the Campaign must be stopped altogether for the specific Contact, so for now I set campaigncontact.delaydays = 1. (Are there major problems with that?)
By default, I am assuming that no campaigncontact exists (but not sure how that works?)
So here's what I've tried to do:
class Contact < ActiveRecord::Base
has_many :campaigncontacts
has_many :campaigns, :through => :campaigncontacts
end
class Campaign < ActiveRecord::Base
has_many :campaigncontacts
has_many :contacts, :through => :campaigncontacts
end
script/generate model campaigncontact campaign_id:integer contact_id:integer delaydays:integer
class Campaigncontact < ActiveRecord::Base
belongs_to :campaign
belongs_to :contact
end
So, here's the question: Is the above correct? If so, how do I allow a user to edit the delay of a campaign for a specific Contact.
For now, I want to do so from the Contact View.
This is what I tried:
In the Contact controller (?)
in_place_edit_for :campaigncontact, column.delaydays
And in the View
<%= in_place_editor_field :campaigncontact, :delaydays %>
How can I get it right?
I would add an integer field to your Campaigncontacts resource called days_to_delay_communication_by, since this information relates to the association of a campaign and a contact rather than a contact itself.
in your migration:
def self.up
add_column(:campaigncontacts, :days_to_delay_communication_by, :integer)
end
def self.down
remove_column(:campaigncontacts, :days_to_delay_communication_by)
end
Now you can set that value by:
campaigncontact = Campaigncontacts.find(:first, :conditions => { :campaign_id => campaign_id, :contact_id => contact_id })
campaigncontact.days_to_delay_communication_by = 10
Then in the admin side of your application you can have a controller and a view for campaign communications that lets you set the days_to_delay_communication_by field for campaigncontacts. I can expand on this further for you if you're interested, but I think you get the idea.
Then you'll need to run a background process of some sort (probably a cron job, or use the delayed_job plugin), to find communications that haven't happened yet, and make them happen when the date has passed. You could do this in a rake task like so:
namespace :communications do
namespace :monitor do
desc 'Monitor and send communications for campaigns'
task :erma => :environment do
Rails.logger.info "-----BEGIN COMMUNICATION MONITORING-----"
unsent_communications = Communication.all(:conditions => { :date_sent => nil})
unsent_communications.each do |communication|
Rails.logger.info "**sending communication**"
communication.send if communication.time_to_send < Time.now
Rails.logger.info "**communication sent**"
end
Rails.logger.info "-----END COMMUNICATION MONITORING-----"
end #end erma task
end #end sync namespace
end #end db namespace
Then your cron job would do something like:
cd /path/to/application && rake communications:monitor RAILS_ENV=production
Also, I'd consider changing the name of your join model to something more descriptive of it's purpose, for instance memberships, a campaign has many memberships and a contact has many memberships. Then a membership has a days_to_delay_communication field.
A good way to do this is use a "fake" attribute on your Contact model like so:
class Contact < ActiveRecord::Base
has_many :campaigncontacts
has_many :campaigns, :through => :campaigncontacts
attr_accessor :delay
def delay #edit
self.campaigncontacts.last.delaydays
end
def delay=(val)
self.campaigncontacts.each do |c|
c.delaydays = val
end
end
end
Then just set the in_place_editor for this fake field:
in_place_edit_for :contact, :delay
and
<%= in_place_editor_field :contact, :delay %>
I'm not sure I understood exactly what you wanted to accomplish, but I hope this at least points you into the right direction.

How to model has_many with polymorphism?

I've run into a situation that I am not quite sure how to model.
EDIT: The code below now represent a working solution. I am still interested in nicer looking solutions, though.
Suppose I have a User class, and a user has many services. However, these services are quite different, for example a MailService and a BackupService, so single table inheritance won't do. Instead, I am thinking of using polymorphic associations together with an abstract base class:
class User < ActiveRecord::Base
has_many :services
end
class Service < ActiveRecord::Base
validates_presence_of :user_id, :implementation_id, :implementation_type
validates_uniqueness_of :user_id, :scope => :implementation_type
belongs_to :user
belongs_to :implementation, :polymorphic => true, :dependent => :destroy
delegate :common_service_method, :name, :to => :implementation
end
#Base class for service implementations
class ServiceImplementation < ActiveRecord::Base
validates_presence_of :user_id, :on => :create
#Virtual attribute, allows us to create service implementations in one step
attr_accessor :user_id
has_one :service, :as => :implementation
after_create :create_service_record
#Tell Rails this class does not use a table.
def self.abstract_class?
true
end
#Name of the service.
def name
self.class.name
end
#Returns the user this service
#implementation belongs to.
def user
unless service.nil?
service.user
else #Service not yet created
#my_user ||= User.find(user_id) rescue nil
end
end
#Sets the user this
#implementation belongs to.
def user=(usr)
#my_user = usr
user_id = usr.id
end
protected
#Sets up a service object after object creation.
def create_service_record
service = Service.new(:user_id => user_id)
service.implementation = self
service.save!
end
end
class MailService < ServiceImplementation
#validations, etc...
def common_service_method
puts "MailService implementation of common service method"
end
end
#Example usage
MailService.create(..., :user => user)
BackupService.create(...., :user => user)
user.services.each do |s|
puts "#{user.name} is using #{s.name}"
end #Daniel is using MailService, Daniel is using BackupService
Notice that I want the Service instance to be implictly created when I create a new service.
So, is this the best solution? Or even a good one? How have you solved this kind of problem?
I don't think your current solution will work. If ServiceImplementation is abstract, what will the associated classes point to? How does the other end of the has_one work, if ServiceImplementation doesn't have a pk persisted to the database? Maybe I'm missing something.
EDIT: Whoops, my original didn't work either. But the idea is still there. Instead of a module, go ahead and use Service with STI instead of polymorphism, and extend it with individual implementations. I think you're stuck with STI and a bunch of unused columns across different implementations, or rethinking the services relationship in general. The delegation solution you have might work as a separate ActiveRecord, but I don't see how it works as abstract if it has to have a has_one relationship.
EDIT: So instead of your original abstract solution, why not persist the delgates? You'd have to have separate tables for MailServiceDelegate and BackupServiceDelegate -- not sure how to get around that if you want to avoid all the null columns with pure STI. You can use a module with the delgate classes to capture the common relationships and validations, etc. Sorry it took me a couple of passes to catch up with your problem:
class User < ActiveRecord::Base
has_many :services
end
class Service < ActiveRecord::Base
validates_presence_of :user_id
belongs_to :user
belongs_to :service_delegate, :polymorphic => true
delegate :common_service_method, :name, :to => :service_delegate
end
class MailServiceDelegate < ActiveRecord::Base
include ServiceDelegate
def name
# implement
end
def common_service_method
# implement
end
end
class BackupServiceDelegate < ActiveRecord::Base
include ServiceDelegate
def name
# implement
end
def common_service_method
# implement
end
end
module ServiceDelegate
def self.included(base)
base.has_one :service, :as => service_delegate
end
def name
raise "Not Implemented"
end
def common_service_method
raise "Not Implemented"
end
end
I think following will work
in user.rb
has_many :mail_service, :class_name => 'Service'
has_many :backup_service, :class_name => 'Service'
in service.rb
belongs_to :mail_user, :class_name => 'User', :foreign_key => 'user_id', :conditions=> is_mail=true
belongs_to :backup_user, :class_name => 'User', :foreign_key => 'user_id', :conditions=> is_mail=false

Resources