Skip to content

Commit

Permalink
Merge pull request #18 from sobrinho/class_attribute
Browse files Browse the repository at this point in the history
Class attribute
  • Loading branch information
carlosantoniodasilva committed May 31, 2011
2 parents 836cf44 + 0112ebb commit 3d0854a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/has_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.included(base)
base.class_eval do
extend ClassMethods
helper_method :current_scopes
class_inheritable_hash :scopes_configuration, :instance_writer => false
class_attribute :scopes_configuration, :instance_writer => false
end
end

Expand Down Expand Up @@ -79,11 +79,11 @@ def has_scope(*scopes, &block)
options[:only] = Array(options[:only])
options[:except] = Array(options[:except])

self.scopes_configuration ||= {}
self.scopes_configuration = (self.scopes_configuration || {}).dup

scopes.each do |scope|
self.scopes_configuration[scope] ||= { :as => scope, :type => :default, :block => block }
self.scopes_configuration[scope].merge!(options)
self.scopes_configuration[scope] = self.scopes_configuration[scope].merge(options)
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions test/has_scope_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ def default_render
end
end

class BonsaisController < TreesController
has_scope :categories, :if => :categories?

protected
def categories?
false
end
end

class HasScopeTest < ActionController::TestCase
tests TreesController

Expand Down Expand Up @@ -222,6 +231,11 @@ def test_scope_with_other_block_types
assert_equal({ :by_category => 'for' }, current_scopes)
end

def test_overwritten_scope
assert_nil(TreesController.scopes_configuration[:categories][:if])
assert_equal(:categories?, BonsaisController.scopes_configuration[:categories][:if])
end

protected

def mock_tree(stubs={})
Expand Down

0 comments on commit 3d0854a

Please sign in to comment.