lib/dejavu.rb in dejavu-0.3.0 vs lib/dejavu.rb in dejavu-0.3.1

- old
+ new

@@ -1,20 +1,18 @@ require "dejavu/version" module Dejavu module ViewHelpers def has_dejavu?(obj) - obj_name = ActiveRecord::Base === obj ? obj.class.model_name.underscore : obj.to_s - !!flash[:"saved_#{obj_name}_for_redisplay"] + !!flash[:"saved_#{object_name(obj)}_for_redisplay"] end def get_dejavu_for(obj, opts = {}) - is_instance = ActiveRecord::Base === obj - model_name = is_instance ? obj.class.model_name.underscore : obj.to_s + model_name = object_name(obj) if has_dejavu?(obj) - foo = if is_instance + foo = if is_instance?(obj) if Rails::VERSION::MINOR >= 1 obj.assign_attributes(flash[:"saved_#{model_name}_for_redisplay"], :without_protection => true) else obj.attributes = flash[:"saved_#{model_name}_for_redisplay"] end @@ -30,13 +28,21 @@ end end foo else - is_instance ? obj : obj.to_s.classify.constantize.new + is_instance?(obj) ? obj : obj.to_s.classify.constantize.new end end + + def is_instance?(obj) + ActiveRecord::Base === obj + end + + def object_name(obj) + (is_instance?(obj) ? obj.class.model_name : obj).to_s.underscore + end end module ControllerMethods def save_for_dejavu(obj, opts = {}) attrs = if opts[:only] && opts[:only].is_a?(Array) @@ -58,10 +64,10 @@ missing_keys.each do |key| attrs[key] = obj.send(key) end - flash[:"saved_#{obj.class.model_name.underscore}_for_redisplay"] = attrs + flash[:"saved_#{object_name(obj)}_for_redisplay"] = attrs end private def save_nested_for_dejavu(obj, key, attrs)