2011-02-28 16:00:41 +01:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
I18n.load_path << File.join(
|
|
|
|
File.dirname(__FILE__), "validations", "locale", "en.yml"
|
|
|
|
)
|
|
|
|
|
|
|
|
module CouchRest
|
|
|
|
module Model
|
|
|
|
|
|
|
|
# This applies to both Model::Base and Model::CastedModel
|
|
|
|
module Dirty
|
|
|
|
extend ActiveSupport::Concern
|
2011-03-03 08:28:57 +01:00
|
|
|
include CouchRest::Model::CastedBy # needed for base_doc
|
|
|
|
include ActiveModel::Dirty
|
|
|
|
|
2011-02-28 16:00:41 +01:00
|
|
|
included do
|
2011-03-03 08:28:57 +01:00
|
|
|
# internal dirty setting - overrides global setting.
|
|
|
|
# this is used to temporarily disable dirty tracking when setting
|
|
|
|
# attributes directly, for performance reasons.
|
|
|
|
self.send(:attr_accessor, :disable_dirty)
|
2011-02-28 16:00:41 +01:00
|
|
|
end
|
|
|
|
|
2011-03-03 13:52:19 +01:00
|
|
|
def use_dirty?
|
|
|
|
bdoc = base_doc
|
|
|
|
bdoc && !bdoc.disable_dirty && bdoc.use_dirty
|
|
|
|
end
|
|
|
|
|
2011-02-28 16:00:41 +01:00
|
|
|
def couchrest_attribute_will_change!(attr)
|
2011-03-03 08:28:57 +01:00
|
|
|
return if attr.nil? || !use_dirty?
|
|
|
|
attribute_will_change!(attr)
|
2011-03-01 12:36:42 +01:00
|
|
|
couchrest_parent_will_change!
|
2011-02-28 16:00:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def couchrest_parent_will_change!
|
|
|
|
@casted_by.couchrest_attribute_will_change!(casted_by_attribute) if @casted_by
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# return the attribute name this object is referenced by in the parent
|
|
|
|
def casted_by_attribute
|
2011-03-03 08:28:57 +01:00
|
|
|
return @casted_by_attribute if @casted_by_attribute
|
2011-02-28 16:00:41 +01:00
|
|
|
attr = @casted_by.attributes
|
2011-03-01 12:36:42 +01:00
|
|
|
@casted_by_attribute = attr.keys.detect { |k| attr[k] == self }
|
2011-02-28 16:00:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|