ruby on rails - How to use BlankSlate and Proxy patterns to create a symlink between records? -
I want to add a record that acts like a simulator. In other words, I want to store a record / object I want to be able to convert the most properties into any other record / object.
Similar to a file system, where a symlink indicates another file (it's own data), still sends everything else to another file.
class document & lt; ActiveRecord :: Base & Class Page & lt; Document end class folder & lt; Document and Class Similink & lt; ActiveRecord :: Base set_table_name: document example_translations. I {{m] Undef_method m if (ActiveRecord :: Base.instance_methods (wrong) .include? M) & amp; Amp; ('[Link],' link_id ',' link_id = ']. Include? M}} def method_missing (sym, * args, and block) "# sending {# sym} (#')}) to Obj "Send Link. ____ (SIM, * Args, and Block) Save End Def Save 'Symlink Can Be Saved' Until new_record? Super end personal link link @link || = Document.Find (self.link_id) End End
When I try and create a new record, I get a stack level very deep 'contact'.
I thought it would be a systematic way to solve the problem that can be extracted in a gem ( acts_as_symlink: column = & gt; 'parent_id'
).
Another thought was to use an after_save callback to maintain a complete copy of the original record and to update any symlinks or update the original if the symlink has been changed. Therefore, there is no more real symlink than copies of sync.
Any thoughts or alternatives on my code?
OK, I think it's about to break, the key was to change those databases Using columns in ways that postpone other objects, I can also determine if I save the save
attributes in the symlink or the original record by riding the ID method more. document
in my case is a acts_as_tree
+ acts_as_list
, so I leave parent_id
and status
By postpaying the column, I can assign the symlink to a different folder (otherwise useless) and place it in that folder.
class Symlink & lt; Document attr_accessor: save_toself.columns.map {| C | | C.name} .reject {| C | | % W (position parent_id sync_id). Include? C} .each do | | Colonel | Self.send: define_method, col.to_sym source.send (col) end end def id if save_to ==: symlink returns read_attribute (: id) and return source.send: id end end def copy? ! Sync_id.nil? Sync_id @ source || = Document.fund (sync_id) other documents New end and end
has been tested only in the console so far, but the reason why this should not work is the full stack.
If this succeeds, I will think of converting it into General Purpose Function_as_ Simal Link Mani.
Comments
Post a Comment