Commit 6b037511b4a9541c892fdf559ac4bd81856d0dac
1 parent
2f6c86d9
Exists in
master
and in
23 other branches
named_scope_monkey_patch: move to solr plugin
Showing
2 changed files
with
57 additions
and
57 deletions
Show diff stats
plugins/solr/vendor/plugins/named_scope_with_applied_names/init.rb
0 → 100644
| ... | ... | @@ -0,0 +1,57 @@ |
| 1 | +require_dependency 'active_record/named_scope' | |
| 2 | + | |
| 3 | +if Rails::VERSION::STRING < "2.3.99" | |
| 4 | + | |
| 5 | + module ::ActiveRecord | |
| 6 | + | |
| 7 | + module NamedScope | |
| 8 | + | |
| 9 | + module ClassMethods | |
| 10 | + | |
| 11 | + def named_scope_with_applied_names name, options = {}, &block | |
| 12 | + named_scope_without_applied_names name, options, &block | |
| 13 | + | |
| 14 | + name = name.to_sym | |
| 15 | + scopes[name] = lambda do |parent_scope, *args| | |
| 16 | + scope = Scope.new(parent_scope, case options | |
| 17 | + when Hash | |
| 18 | + options | |
| 19 | + when Proc | |
| 20 | + if self.model_name != parent_scope.model_name | |
| 21 | + options.bind(parent_scope).call(*args) | |
| 22 | + else | |
| 23 | + options.call(*args) | |
| 24 | + end | |
| 25 | + end, &block) | |
| 26 | + scope.scope_name = name | |
| 27 | + scope | |
| 28 | + end | |
| 29 | + end | |
| 30 | + alias_method_chain :named_scope, :applied_names | |
| 31 | + end | |
| 32 | + | |
| 33 | + class Scope | |
| 34 | + attr_accessor :scope_name, :scopes_applied | |
| 35 | + | |
| 36 | + def initialize_with_applied_names proxy_scope, options, &block | |
| 37 | + initialize_without_applied_names proxy_scope, options, &block | |
| 38 | + self.scopes_applied ||= [] | |
| 39 | + self.scopes_applied += proxy_scope.send :scopes_applied if Scope === proxy_scope | |
| 40 | + | |
| 41 | + # unrelated bugfix: use if instead of unless | |
| 42 | + if (Scope === proxy_scope || ActiveRecord::Associations::AssociationCollection === proxy_scope) | |
| 43 | + @current_scoped_methods_when_defined = proxy_scope.send(:current_scoped_methods) | |
| 44 | + end | |
| 45 | + end | |
| 46 | + alias_method_chain :initialize, :applied_names | |
| 47 | + | |
| 48 | + def scope_name= name | |
| 49 | + @scope_name = name | |
| 50 | + self.scopes_applied << @scope_name | |
| 51 | + end | |
| 52 | + | |
| 53 | + end | |
| 54 | + | |
| 55 | + end | |
| 56 | + end | |
| 57 | +end | ... | ... |
vendor/plugins/monkey_patches/named_scope_with_applied_names/init.rb
| ... | ... | @@ -1,57 +0,0 @@ |
| 1 | -require_dependency 'active_record/named_scope' | |
| 2 | - | |
| 3 | -if Rails::VERSION::STRING < "2.3.99" | |
| 4 | - | |
| 5 | - module ::ActiveRecord | |
| 6 | - | |
| 7 | - module NamedScope | |
| 8 | - | |
| 9 | - module ClassMethods | |
| 10 | - | |
| 11 | - def named_scope_with_applied_names name, options = {}, &block | |
| 12 | - named_scope_without_applied_names name, options, &block | |
| 13 | - | |
| 14 | - name = name.to_sym | |
| 15 | - scopes[name] = lambda do |parent_scope, *args| | |
| 16 | - scope = Scope.new(parent_scope, case options | |
| 17 | - when Hash | |
| 18 | - options | |
| 19 | - when Proc | |
| 20 | - if self.model_name != parent_scope.model_name | |
| 21 | - options.bind(parent_scope).call(*args) | |
| 22 | - else | |
| 23 | - options.call(*args) | |
| 24 | - end | |
| 25 | - end, &block) | |
| 26 | - scope.scope_name = name | |
| 27 | - scope | |
| 28 | - end | |
| 29 | - end | |
| 30 | - alias_method_chain :named_scope, :applied_names | |
| 31 | - end | |
| 32 | - | |
| 33 | - class Scope | |
| 34 | - attr_accessor :scope_name, :scopes_applied | |
| 35 | - | |
| 36 | - def initialize_with_applied_names proxy_scope, options, &block | |
| 37 | - initialize_without_applied_names proxy_scope, options, &block | |
| 38 | - self.scopes_applied ||= [] | |
| 39 | - self.scopes_applied += proxy_scope.send :scopes_applied if Scope === proxy_scope | |
| 40 | - | |
| 41 | - # unrelated bugfix: use if instead of unless | |
| 42 | - if (Scope === proxy_scope || ActiveRecord::Associations::AssociationCollection === proxy_scope) | |
| 43 | - @current_scoped_methods_when_defined = proxy_scope.send(:current_scoped_methods) | |
| 44 | - end | |
| 45 | - end | |
| 46 | - alias_method_chain :initialize, :applied_names | |
| 47 | - | |
| 48 | - def scope_name= name | |
| 49 | - @scope_name = name | |
| 50 | - self.scopes_applied << @scope_name | |
| 51 | - end | |
| 52 | - | |
| 53 | - end | |
| 54 | - | |
| 55 | - end | |
| 56 | - end | |
| 57 | -end |