Permalink
Browse files

Disable denormalize_from, while updating attributes from denormalize_…

…to. Also, use direct accessors instead of update_attributes, to avoid issues with mongoid attribute accesibility. This fixes #6.
  • Loading branch information...
1 parent 6fcb6ad commit cb18454e63f79132a3269994083ef0fbdb036848 @logandk committed Feb 5, 2012
View
@@ -11,10 +11,11 @@ Jeweler::Tasks.new do |gemspec|
gemspec.name = "mongoid_denormalize"
gemspec.summary = "Mongoid denormalization helper."
gemspec.description = "Helper module for denormalizing association attributes in Mongoid models."
- gemspec.files = Dir.glob("lib/**/*") + %w(LICENSE README.md)
+ gemspec.files = Dir.glob("lib/**/*")
gemspec.require_path = 'lib'
gemspec.email = "logan@logan.dk"
gemspec.homepage = "http://github.com/logandk/mongoid_denormalize"
gemspec.authors = ["Logan Raarup"]
+ gemspec.add_dependency 'mongoid', '>= 2.1.9'
end
Jeweler::GemcutterTasks.new
@@ -36,6 +36,10 @@ def denormalize(*fields)
fields.each { |name| field "#{options[:from]}_#{name}", :type => options[:type] || String }
end
end
+
+ def is_denormalized?
+ true
+ end
end
def denormalized_valid?
@@ -46,7 +50,7 @@ def denormalized_valid?
def repair_denormalized!
self.save! unless denormalized_valid?
end
-
+
private
def denormalize_from
self.denormalize_definitions.each do |definition|
@@ -66,15 +70,28 @@ def denormalize_to
relation = []
reflect = self.class.reflect_on_association(association)
relation = reflect.relation.macro unless reflect.nil? || reflect.relation.nil?
-
+
+ reflect.klass.skip_callback(:save, :before, :denormalize_from) if reflect.klass.try(:is_denormalized?)
+
if [:embedded_in, :embeds_one, :referenced_in, :references_one, :has_one, :belongs_to].include? relation
c = self.send(association)
- c.update_attributes(assigns) unless c.blank?
+ unless c.blank?
+ assigns.each { |assign| c.send("#{assign[0]}=", assign[1]) }
+
+ c.save
+ end
else
c = self.send(association)
- c.to_a.each { |a| a.update_attributes(assigns) }
+
+ c.to_a.each do |a|
+ assigns.each { |assign| a.send("#{assign[0]}=", assign[1]) }
+
+ a.save
+ end
end
+
+ reflect.klass.set_callback(:save, :before, :denormalize_from) if reflect.klass.try(:is_denormalized?)
end
end
end
Binary file not shown.
@@ -5,31 +5,37 @@
Gem::Specification.new do |s|
s.name = %q{mongoid_denormalize}
- s.version = "0.2.2"
+ s.version = "0.3.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Logan Raarup"]
- s.date = %q{2011-08-04}
+ s.date = %q{2012-02-05}
s.description = %q{Helper module for denormalizing association attributes in Mongoid models.}
s.email = %q{logan@logan.dk}
s.extra_rdoc_files = [
"LICENSE",
"README.md"
]
s.files = [
- "LICENSE",
- "README.md",
"lib/mongoid_denormalize.rb",
"lib/railties/denormalize.rake",
"lib/railties/railtie.rb"
]
s.homepage = %q{http://github.com/logandk/mongoid_denormalize}
s.require_paths = ["lib"]
- s.rubygems_version = %q{1.6.2}
+ s.rubygems_version = %q{1.3.6}
s.summary = %q{Mongoid denormalization helper.}
if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<mongoid>, [">= 2.1.9"])
+ else
+ s.add_dependency(%q<mongoid>, [">= 2.1.9"])
+ end
+ else
s.add_dependency(%q<mongoid>, [">= 2.1.9"])
end
end
@@ -7,6 +7,7 @@ class Comment
belongs_to :post, :inverse_of => :comments
belongs_to :user
+ attr_accessible :body, :user
denormalize :location, :type => Array, :from => :user
denormalize :name, :from => :user

0 comments on commit cb18454

Please sign in to comment.