Skip to content

Commit

Permalink
Merge pull request mongomapper#374 from laserlemon/master
Browse files Browse the repository at this point in the history
Stop relying on ActiveSupport::Concern's deprecated inclusion of the InstanceMethods module
  • Loading branch information
bkeepers committed Jan 4, 2012
2 parents 857970d + d2333d9 commit 730512f
Show file tree
Hide file tree
Showing 29 changed files with 480 additions and 535 deletions.
18 changes: 9 additions & 9 deletions Gemfile
Expand Up @@ -2,21 +2,21 @@ source :rubygems
gemspec

group(:development) do
gem 'bson_ext', '~> 1.3.0'
gem 'bson_ext', '~> 1.5'

gem 'SystemTimer', :platform => :mri_18
gem 'ruby-debug', :platform => :mri_18
gem 'ruby-debug19', :platform => :mri_19, :require => 'ruby-debug'
gem 'perftools.rb', :platform => :mri, :require => 'perftools'

gem 'rake'
gem 'tzinfo'
gem 'json'
gem 'log_buddy'
gem 'jnunemaker-matchy', '~> 0.4.0', :require => 'matchy'
gem 'tzinfo', '~> 0.3'
gem 'json', '~> 1.6'
gem 'log_buddy', '~> 0.6'
gem 'jnunemaker-matchy', '~> 0.4', :require => 'matchy'
gem 'shoulda', '~> 2.11'
gem 'timecop', '~> 0.3.1'
gem 'mocha', '~> 0.9.8'
gem 'rack-test'
gem 'rails'
gem 'timecop', '~> 0.3'
gem 'mocha', '~> 0.10'
gem 'rack-test', '~> 0.6'
gem 'rails', '~> 3.0'
end
7 changes: 2 additions & 5 deletions examples/plugins.rb
Expand Up @@ -16,11 +16,8 @@ def foo
end
end

# InstanceMethods module will automatically get included
module InstanceMethods
def foo
'Foo instance method!'
end
def foo
'Foo instance method!'
end

# Any configuration can be done in the #included block, which gets
Expand Down
11 changes: 5 additions & 6 deletions lib/mongo_mapper/extensions/object.rb
Expand Up @@ -2,6 +2,8 @@
module MongoMapper
module Extensions
module Object
extend ActiveSupport::Concern

module ClassMethods
def to_mongo(value)
value
Expand All @@ -12,16 +14,13 @@ def from_mongo(value)
end
end

module InstanceMethods
def to_mongo
self.class.to_mongo(self)
end
def to_mongo
self.class.to_mongo(self)
end
end
end
end

class Object
extend MongoMapper::Extensions::Object::ClassMethods
include MongoMapper::Extensions::Object::InstanceMethods
include MongoMapper::Extensions::Object
end
30 changes: 14 additions & 16 deletions lib/mongo_mapper/plugins/accessible.rb
Expand Up @@ -14,25 +14,23 @@ def attr_accessible(*attrs)
end
end

module InstanceMethods
def attributes=(attrs={})
super(filter_inaccessible_attrs(attrs))
end

def update_attributes(attrs={})
super(filter_inaccessible_attrs(attrs))
end
def attributes=(attrs={})
super(filter_inaccessible_attrs(attrs))
end

def update_attributes!(attrs={})
super(filter_inaccessible_attrs(attrs))
end
def update_attributes(attrs={})
super(filter_inaccessible_attrs(attrs))
end

protected
def filter_inaccessible_attrs(attrs)
return attrs if !accessible_attributes? || attrs.blank?
attrs.dup.delete_if { |key, val| !accessible_attributes.include?(key.to_sym) }
end
def update_attributes!(attrs={})
super(filter_inaccessible_attrs(attrs))
end

protected
def filter_inaccessible_attrs(attrs)
return attrs if !accessible_attributes? || attrs.blank?
attrs.dup.delete_if { |key, val| !accessible_attributes.include?(key.to_sym) }
end
end
end
end
44 changes: 21 additions & 23 deletions lib/mongo_mapper/plugins/associations.rb
Expand Up @@ -56,35 +56,33 @@ def create_association(association)
end
end

module InstanceMethods
def associations
self.class.associations
end
def associations
self.class.associations
end

def embedded_associations
associations.values.select { |assoc| assoc.embeddable? }
end
def embedded_associations
associations.values.select { |assoc| assoc.embeddable? }
end

def build_proxy(association)
proxy = association.proxy_class.new(self, association)
self.instance_variable_set(association.ivar, proxy)
def build_proxy(association)
proxy = association.proxy_class.new(self, association)
self.instance_variable_set(association.ivar, proxy)

proxy
end
proxy
end

def get_proxy(association)
unless proxy = self.instance_variable_get(association.ivar)
proxy = build_proxy(association)
end
proxy
def get_proxy(association)
unless proxy = self.instance_variable_get(association.ivar)
proxy = build_proxy(association)
end
proxy
end

def save_to_collection(options={})
super if defined?(super)
associations.each do |association_name, association|
proxy = get_proxy(association)
proxy.save_to_collection(options) if proxy.proxy_respond_to?(:save_to_collection) && association.autosave?
end
def save_to_collection(options={})
super if defined?(super)
associations.each do |association_name, association|
proxy = get_proxy(association)
proxy.save_to_collection(options) if proxy.proxy_respond_to?(:save_to_collection) && association.autosave?
end
end
end
Expand Down
24 changes: 11 additions & 13 deletions lib/mongo_mapper/plugins/caching.rb
Expand Up @@ -4,19 +4,17 @@ module Plugins
module Caching
extend ActiveSupport::Concern

module InstanceMethods
def cache_key(*suffixes)
cache_key = case
when !persisted?
"#{self.class.name}/new"
when timestamp = self[:updated_at]
"#{self.class.name}/#{id}-#{timestamp.to_s(:number)}"
else
"#{self.class.name}/#{id}"
end
cache_key += "/#{suffixes.join('/')}" unless suffixes.empty?
cache_key
end
def cache_key(*suffixes)
cache_key = case
when !persisted?
"#{self.class.name}/new"
when timestamp = self[:updated_at]
"#{self.class.name}/#{id}-#{timestamp.to_s(:number)}"
else
"#{self.class.name}/#{id}"
end
cache_key += "/#{suffixes.join('/')}" unless suffixes.empty?
cache_key
end
end
end
Expand Down
26 changes: 12 additions & 14 deletions lib/mongo_mapper/plugins/callbacks.rb
Expand Up @@ -4,23 +4,21 @@ module Plugins
module Callbacks
extend ActiveSupport::Concern

module InstanceMethods
def destroy
run_callbacks(:destroy) { super }
end
def destroy
run_callbacks(:destroy) { super }
end

private
def create_or_update(*)
run_callbacks(:save) { super }
end
private
def create_or_update(*)
run_callbacks(:save) { super }
end

def create(*)
run_callbacks(:create) { super }
end
def create(*)
run_callbacks(:create) { super }
end

def update(*)
run_callbacks(:update) { super }
end
def update(*)
run_callbacks(:update) { super }
end
end
end
Expand Down
24 changes: 11 additions & 13 deletions lib/mongo_mapper/plugins/clone.rb
Expand Up @@ -4,19 +4,17 @@ module Plugins
module Clone
extend ActiveSupport::Concern

module InstanceMethods
def initialize_copy(other)
@_new = true
@_destroyed = false
@_id = nil
associations.each do |name, association|
instance_variable_set(association.ivar, nil)
end
self.attributes = other.attributes.clone.except(:_id).inject({}) do |hash, entry|
key, value = entry
hash[key] = value.duplicable? ? value.clone : value
hash
end
def initialize_copy(other)
@_new = true
@_destroyed = false
@_id = nil
associations.each do |name, association|
instance_variable_set(association.ivar, nil)
end
self.attributes = other.attributes.clone.except(:_id).inject({}) do |hash, entry|
key, value = entry
hash[key] = value.duplicable? ? value.clone : value
hash
end
end
end
Expand Down
74 changes: 36 additions & 38 deletions lib/mongo_mapper/plugins/dirty.rb
Expand Up @@ -6,56 +6,54 @@ module Dirty

include ::ActiveModel::Dirty

module InstanceMethods
def initialize(*)
# never register initial id assignment as a change
super.tap { changed_attributes.delete('_id') }
end
def initialize(*)
# never register initial id assignment as a change
super.tap { changed_attributes.delete('_id') }
end

def initialize_from_database(*)
super.tap { changed_attributes.clear }
end
def initialize_from_database(*)
super.tap { changed_attributes.clear }
end

def save(*)
clear_changes { super }
end
def save(*)
clear_changes { super }
end

def reload(*)
super.tap { clear_changes }
end
def reload(*)
super.tap { clear_changes }
end

protected
protected

def attribute_method?(attr)
# This overrides ::ActiveSupport::Dirty#attribute_method? to allow attributes to be any key
# in the attributes hash ( default ) or any key defined on the model that may not yet have
# had a value stored in the attributes collection.
super || key_names.include?(attr)
end
def attribute_method?(attr)
# This overrides ::ActiveSupport::Dirty#attribute_method? to allow attributes to be any key
# in the attributes hash ( default ) or any key defined on the model that may not yet have
# had a value stored in the attributes collection.
super || key_names.include?(attr)
end

def clear_changes
previous = changes
(block_given? ? yield : true).tap do |result|
unless result == false #failed validation; nil is OK.
@previously_changed = previous
changed_attributes.clear
end
def clear_changes
previous = changes
(block_given? ? yield : true).tap do |result|
unless result == false #failed validation; nil is OK.
@previously_changed = previous
changed_attributes.clear
end
end
end

private
private

def write_key(key, value)
key = key.to_s
attribute_will_change!(key) unless attribute_changed?(key)
super(key, value).tap do
changed_attributes.delete(key) unless attribute_value_changed?(key)
end
def write_key(key, value)
key = key.to_s
attribute_will_change!(key) unless attribute_changed?(key)
super(key, value).tap do
changed_attributes.delete(key) unless attribute_value_changed?(key)
end
end

def attribute_value_changed?(key_name)
attribute_was(key_name) != read_key(key_name)
end
def attribute_value_changed?(key_name)
attribute_was(key_name) != read_key(key_name)
end
end
end
Expand Down

0 comments on commit 730512f

Please sign in to comment.