Home > Rails > Me No Blog Hella Ugly!

Me No Blog Hella Ugly!

April 10th, 2009

Welcome to the 2000’s, self!

I’m ever so excited to be blogging at a blog that not only understands code highlighting, but doesn’t look like it was crafted by a mad scientist with cataracts in 1992. Now it looks more like it was crafted by a mad scientist without cataracts circa 2008 — which is an entirely more accurate representation of the truth.

That’s the good news.

The bad news?  That I have don’t anything meaningful to report in this post.

Maybe I’ll just write some highlighted code instead.

# ---------------------------------------------------------------------------
# options[:except_list]: list of symbols that we will exclude form this copy
# options[:dont_overwrite]: if true, all attributes in from_model that aren't #blank? will be preserved
def self.copy_attributes_between_models(from_model, to_model, options = {})
	return unless from_model && to_model
	except_list = options[:except_list] || []
	except_list << :id
	to_model.attributes.each do |attr, val|
		to_model[attr] = from_model[attr] unless except_list.index(attr.to_sym) || (options[:dont_overwrite] &amp;&amp; !to_model[attr].blank?)
	end
	to_model.save if options[:save]
	to_model
end

Hey hey hey code, you’re looking quite sexy this evening — you come around here often?

Bill Rails

  1. Mechelle
    April 11th, 2009 at 23:43 | #1

    You are just too funny! lol

  2. da la la
    January 19th, 2010 at 11:56 | #2

    Hi!

    I spiced up your script, this way one can copy not only the model but all its reflections as well. Only recursion should be added for deeply nested reflections. Let me know if something should be fixed.

    #@report = Main.copy_model(@main_old,Main.new,{
    # :entity => {:except_list => [:resource_id, :main_id]},
    # :roles => {:copy_id => true}, | useful for HABTM
    # :thumbnails => {:skip => true}
    # If no submodel given these setting will valid for main model
    # :except_list => [:report_one,:report_two]
    # :copy_id => true
    # }
    def self.copy_model(from, to, options = {})
    to = copy_attributes_between_models(from, to, options)

    to.class.reflections.keys.each do |rel_model|
    next if !options[rel_model].nil? && options[rel_model][:skip]
    if from.class.reflections[rel_model].macro == :belongs_to || from.class.reflections[rel_model].macro == :has_one
    new_rel_model = copy_attributes_between_models(from.send(rel_model), rel_model.to_s.classify.constantize.new, options[rel_model])
    else
    new_rel_model = []
    from.send(rel_model).each_with_index do |sub_model,idx|
    if options[rel_model].nil?
    new_rel_model << copy_attributes_between_models(from.send(rel_model.to_s)[idx], sub_model.class.new)
    else
    new_rel_model << copy_attributes_between_models(from.send(rel_model.to_s)[idx], sub_model.class.new, options[rel_model])
    end
    end
    end

    to.send(”#{rel_model}=”,new_rel_model)
    end
    to
    end

    def self.copy_attributes_between_models(from_model, to_model, options = {})
    return unless from_model && to_model
    except_list = options[:except_list] || []
    if options[:copy_id]
    to_model[:id] = from_model[:id]
    end
    to_model.attributes.each do |attr, val|
    to_model[attr] = from_model[attr] unless except_list.index(attr.to_sym) || (options[:dont_overwrite] == !to_model[attr].blank?)
    end
    to_model.save if options[:save]
    to_model
    end

  1. No trackbacks yet.