require 'rubygems' module Six module Import module Controller def self.included(base) base.extend(ClassMethods) end module ClassMethods public def copy t = copy_ll(Kernel.const_get(self.controller_name.singularize.camelize)) @original_record = t[0] @record = t[1] end def six_local_auto_login if request.remote_ip == "127.0.0.1" && defined?(SixUpdaterWeb::SIX_ADMIN) if SixUpdaterWeb::SIX_ADMIN Goldberg::AuthController.set_user(session, 2) else Goldberg::AuthController.set_user(session, 3) end end end private def copy_ll(cl, include = []) record = cl.find(params[:id]) new_record = record.clone begin new_record.name = "CUST - #{record.name}" if record.name rescue end #new_record.id = nil new_record.updated_at = nil new_record.save [record, new_record] end end end module Model def self.included(base) base.extend(ClassMethods) end module ClassMethods def blabla(h) l = [] h.each_pair do |key, value| case key when self.to_s.downcase c = self.from_hash value l << c end end l end def imp Six::Network::Panel.setlogger(logger) begin Six::Network::Panel.login("", "") rescue ScriptError end path = "/#{self.to_s.pluralize.downcase}/exp/1.json" logger.debug "Path: #{path}" res = Six::Network::Panel.get(path) h = ActiveSupport::JSON.decode res.body k = [] return k unless h case h when Array h.each do |b| k += blabla(b) end when Hash k += blabla h end k end def from_hash( hash ) h = hash.dup h.each do |key, value| case value.class.to_s when 'Array' h[key].map! { |e| Object.const_get(key.camelize.singularize).from_hash e } when 'Hash' h[key] = Object.const_get(key.camelize).from_hash value end end n = nil previous = nil if h["id"] id = h["id"] h.delete "id" r = nil begin r = self.find(id) rescue end if r previous = r.inspect r.attributes = h n = r else n = self.new h n.id = id end else n = self.new h end logger.debug "#{n.class} #{n.id} #{n.changed?}: #{"#{n.inspect} - #{previous}" if n.changed?}" n.save if n.changed?# || n.new? n end def from_json( json ) hash = ActiveSupport::JSON.decode json self.from_hash hash end # The xml has a surrounding class tag (e.g. ship-to), # but the hash has no counterpart (e.g. 'ship_to' => {} ) def from_xml( xml ) hash = Hash.from_xml xml self.from_hash hash[self.to_s.underscore] end end end end end ActiveRecord::Base.send(:include, Six::Import::Model)