I have a Subject model which represents a treeview with parent children nodes.
To move a subject to another branch/node, I need to have a 2 subject id’s that represent from and to values.
I have started by putting all of the logic into the controller but am now wanting to reuse the copy method and would like to set it up in the model.
Here is some of my controller code.
def copy
from = Subject.find(params[:from])
to = Subject.find(params[:to])
if to.is_descendant_of? from
render :json => {:error => ["Can't move branch because the target is a descendant."]}.to_json, :status => :bad_request
return
end
if to.read_only?
render :json => {:error => ["Can't copy to this branch as it is read only." ]}.to_json, :status => :bad_request
return
end
if params[:subjects] == 'copy'
subject = Subject.create(:name => from.name, :description => from.description, :parent_id => to.id)
#recursively walk the tree
copy_tree(from, subject)
else
#move the tree
if !(from.read_only or to.read_only)
to.children << from
end
end
end
Here is what I started doing in my model
class Subject < ActiveRecord::Base
def self.copy(from, to, operations)
from = Subject.find(from)
to = Subject.find(to)
if to.is_descendant_of? from
#how do I add validation errors on this static method?
end
end
end
My first concern is how to add errors to a static method in a model?
I’m not sure if I’m going about it the right way by using a static method or an instance method.
Anyone able to give me a bit of help in refactoring this code?
You have three possible solutions. ( I prefer the 3rd approach)
1) Return nil on success, error string on failure
2) Throw exception on error
3) Add an instance method on
Subjectclass.