Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactoring out a method generator class

  • Loading branch information...
commit 9951c53bb1f2a35b12f3ad4ff09697c01ca42964 1 parent 2ab7f5c
Ryan Bates authored
1  lib/association_freezer.rb
View
@@ -1,2 +1,3 @@
$:.unshift(File.dirname(__FILE__))
require 'association_freezer/model_additions'
+require 'association_freezer/method_generator'
53 lib/association_freezer/method_generator.rb
View
@@ -0,0 +1,53 @@
+module AssociationFreezer
+ class MethodGenerator
+
+ def initialize(reflection)
+ @reflection = reflection
+ end
+
+ def generate
+ # it is very important that we first make sure this hasn't already been done
+ # because otherwise it will result in an endless loop the way alias method works.
+ return if previously_generated? || !frozen_column_exists?
+
+ association = @reflection
+
+ generate_method "freeze_#{association.name}" do
+ send(association.name, true).freeze
+ write_attribute("frozen_#{association.name}", Marshal.dump(send(association.name).attributes))
+ end
+
+ generate_method "unfreeze_#{association.name}" do
+ end
+
+ generate_method "#{association.name}_with_frozen_check" do |*args|
+ if !instance_variable_defined?("@#{association.name}") || args.first # force reload
+ if read_attribute("frozen_#{association.name}")
+ association.klass.new(Marshal.load(read_attribute("frozen_#{association.name}")).except('id'))
+ else
+ send("#{association.name}_without_frozen_check", *args)
+ end
+ end
+ instance_variable_get("@#{association.name}")
+ end
+ model_class.alias_method_chain association.name, :frozen_check
+ end
+
+ def previously_generated?
+ model_class.instance_methods.include? "freeze_#{@reflection.name}"
+ end
+
+ def frozen_column_exists?
+ model_class.column_names.include? "frozen_#{@reflection.name}"
+ end
+
+ def model_class
+ @reflection.active_record
+ end
+
+ def generate_method(name, &block)
+ model_class.send(:define_method, name, &block)
+ end
+
+ end
+end
31 lib/association_freezer/model_additions.rb
View
@@ -2,35 +2,8 @@ module AssociationFreezer
module ModelAdditions
# REFACTORME !!!
def enable_association_freezer
- reflect_on_all_associations(:belongs_to).each do |association|
-
- # it is very important that we first make sure this hasn't already been done
- # because otherwise it will result in an endless loop the way alias method works.
- unless instance_methods.include? "freeze_#{association.name}"
-
- if column_names.include? "frozen_#{association.name}"
- define_method "freeze_#{association.name}" do
- send(association.name, true).freeze
- write_attribute("frozen_#{association.name}", Marshal.dump(send(association.name).attributes))
- end
-
- define_method "unfreeze_#{association.name}" do
-
- end
-
- define_method "#{association.name}_with_frozen_check" do |*args|
- if !instance_variable_defined?("@#{association.name}") || args.first # force reload
- if read_attribute("frozen_#{association.name}")
- association.klass.new(Marshal.load(read_attribute("frozen_#{association.name}")).except('id'))
- else
- send("#{association.name}_without_frozen_check", *args)
- end
- end
- instance_variable_get("@#{association.name}")
- end
- alias_method_chain association.name, :frozen_check
- end
- end
+ reflect_on_all_associations(:belongs_to).each do |reflection|
+ MethodGenerator.new(reflection).generate
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.