Skip to content

Commit

Permalink
make disabling the model scope work more universally
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich Cavanaugh committed Jun 10, 2008
1 parent 77192dc commit 91847b6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/acts_as_revisable/acts/common.rb
Expand Up @@ -156,7 +156,7 @@ def #{a.to_s}_with_open_scope(*args, &block)
assoc = self.class.reflect_on_association(#{a.inspect})
models = [self.class]
if assoc.macro == :has_many
if [:has_many, :has_one].member? assoc.macro
models << (assoc.options[:class_name] ? assoc.options[:class_name] : #{a.inspect}.to_s.singularize.camelize).constantize
end
Expand Down
4 changes: 4 additions & 0 deletions lib/acts_as_revisable/acts/revisable.rb
Expand Up @@ -341,6 +341,10 @@ def current_revision
self
end

def for_revision

end

module ClassMethods
# acts_as_revisable's override for with_scope that allows for
# including revisions in the scope.
Expand Down
6 changes: 5 additions & 1 deletion lib/acts_as_revisable/acts/revision.rb
Expand Up @@ -76,7 +76,11 @@ def revision_setup #:nodoc:
self[:revisable_type] = current_revision[:type]
self[:revisable_number] = (self.class.maximum(:revisable_number, :conditions => {:revisable_original_id => self[:revisable_original_id]}) || 0) + 1
end


def from_revisable

end

module ClassMethods
# Returns the +revisable_class_name+ as configured in
# +acts_as_revisable+.
Expand Down
30 changes: 27 additions & 3 deletions lib/acts_as_revisable/acts/scoped_model.rb
Expand Up @@ -8,7 +8,7 @@ module ClassMethods
SCOPED_METHODS = %w(construct_calculation_sql construct_finder_sql update_all delete_all destroy_all).freeze

def call_method_with_static_scope(meth, args)
return send(meth, *args) unless self.scoped_model_enabled
return send(meth, *args) unless self.scoped_model_enabled?

with_scope(self.scoped_model_static_scope) do
send(meth, *args)
Expand Down Expand Up @@ -36,14 +36,38 @@ def without_model_scope
rv
end

def disable_model_scope!
self.scoped_model_disable_count += 1
end

def enable_model_scope!
self.scoped_model_disable_count -= 1
end

def scoped_model_enabled?
self.scoped_model_disable_count == 0
end

def scoped_model_enabled
self.scoped_model_enabled?
end

def scoped_model_enabled=(value)
if value == false
disable_model_scope!
else
enable_model_scope!
end
end

def acts_as_scoped_model(*args)
class << self
attr_accessor :scoped_model_static_scope, :scoped_model_enabled
attr_accessor :scoped_model_static_scope, :scoped_model_disable_count
SCOPED_METHODS.each do |m|
alias_method_chain m.to_sym, :static_scope
end
end
self.scoped_model_enabled = true
self.scoped_model_disable_count = 0
self.scoped_model_static_scope = args.extract_options!
end
end
Expand Down

0 comments on commit 91847b6

Please sign in to comment.