Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

move finder methods on ActiveRecord::Base to ActiveRecord::Relation

  • Loading branch information...
commit ea66d48c72a70f925120a9f441aec69edefe7452 1 parent 601ba81
@relax4u relax4u authored
View
2  init.rb
@@ -1 +1 @@
-require 'lib/record_with_operator'
+require 'record_with_operator'
View
2  lib/record_with_operator.rb
@@ -1,4 +1,6 @@
+require 'record_with_operator/relation_methods'
require 'record_with_operator/associations/extension'
require 'record_with_operator/extension'
ActiveRecord::Base.send :include, RecordWithOperator::Extension unless ActiveRecord::Base.include?(RecordWithOperator::Extension)
+ActiveRecord::Relation.send :include, RecordWithOperator::RelationMethods unless ActiveRecord::Relation.include?(RecordWithOperator::RelationMethods)
View
37 lib/record_with_operator/associations/extension.rb
@@ -1,37 +1,24 @@
-module AssociationWithOperator
+require 'record_with_operator/utility'
+
+module RecordWithOperator
module Associations
module Extension
+ include RecordWithOperator::Utility
+
def find(*args)
- results = super
- if results.kind_of? Array
- results.each{|r| r.operator = proxy_owner.operator}
- else
- results.operator = proxy_owner.operator if results
- end
- results
+ set_operator_to_records(super, @owner.operator)
end
-
def method_missing(method, *args)
- results = super
- unless ActiveRecord::Relation === results
- if results.respond_to?(:operator=)
- results.operator= proxy_owner.operator
- elsif results.kind_of? Array
- results.each{|r| r.operator = proxy_owner.operator if r.respond_to?(:operator=)}
- end
- end
- results
+ set_operator_to_records(super, @owner.operator)
end
- def construct_join_attributes(associate)
- join_attributes = super
- if @owner.operator || associate.operator
- join_attributes[:operator] ||= @owner.operator
- join_attributes[:operator] ||= associate.operator
+ protected
+ def construct_scope
+ scoping = super
+ scoping[:find][:for] = @owner.operator
+ scoping
end
- join_attributes
- end
end
end
end
View
32 lib/record_with_operator/extension.rb
@@ -37,9 +37,9 @@ def has_many_with_operator(*args, &extension)
# add AssociationWithOprator to :extend
if options[:extend]
options[:extend] = [options[:extend]] unless options[:extend].kind_of? Array
- options[:extend] << AssociationWithOperator::Associations::Extension
+ options[:extend] << RecordWithOperator::Associations::Extension
else
- options[:extend] = AssociationWithOperator::Associations::Extension
+ options[:extend] = RecordWithOperator::Associations::Extension
end
# add :set_operator to :before_add
if options[:before_add]
@@ -53,34 +53,6 @@ def has_many_with_operator(*args, &extension)
end
alias_method_chain :has_many, :operator
- def find_with_for(*args)
- options = args.extract_options!
- operator = options.delete(:for)
- args << options
- results = find_without_for(*args)
- if operator
- if results.kind_of? Array
- results.each{|r| r.operator = operator}
- elsif results
- results.operator = operator
- end
- end
- results
- end
-
- alias_method_chain :find, :for
-
- # No longer valid in Rails 3
- # def validate_find_options_with_for(options)
- # if options
- # options = options.dup
- # options.delete(:for)
- # end
- # validate_find_options_without_for(options)
- # end
- #
- # alias_method :validate_find_options, :validate_find_options_with_for
-
private
# define_method association, association= ...
def association_accessor_methods_with_operator(reflection, association_proxy_class)
View
66 lib/record_with_operator/relation_methods.rb
@@ -0,0 +1,66 @@
+require 'record_with_operator/utility'
+
+module RecordWithOperator
+ module RelationMethods
+ extend ActiveSupport::Concern
+
+ included do
+ attr_accessor :operator
+ end
+
+ module InstanceMethods
+ include RecordWithOperator::Utility
+
+ def merge(r)
+ merged_relation = super
+ merged_relation.operator ||= r.operator
+ merged_relation
+ end
+
+ def except(*skips)
+ result = super
+ result.operator ||= operator
+ result
+ end
+
+ def only(*onlies)
+ result = super
+ result.operator ||= operator
+ result
+ end
+
+ def for(operator)
+ relation = clone
+ relation.operator ||= operator
+ relation
+ end
+
+ def apply_finder_options(options)
+ finders = options.dup
+ operator = finders.delete(:for)
+ relation = super(finders)
+ relation.for(operator)
+ end
+
+ def find(*args)
+ set_operator_to_records(super, operator)
+ end
+
+ def first(*args)
+ set_operator_to_records(super, operator)
+ end
+
+ def last(*args)
+ set_operator_to_records(super, operator)
+ end
+
+ def all(*args)
+ set_operator_to_records(super, operator)
+ end
+
+ def to_a
+ set_operator_to_records(super, operator)
+ end
+ end
+ end
+end
View
19 lib/record_with_operator/utility.rb
@@ -0,0 +1,19 @@
+module RecordWithOperator
+ module Utility
+ private
+ def set_operator_to_records(records, operator)
+ return records unless operator
+
+ unless ActiveRecord::Relation === records
+ if records.respond_to?(:operator=)
+ records.operator = operator
+ elsif Array === records
+ records.each do |record|
+ record.operator = operator if record.respond_to?(:operator=)
+ end
+ end
+ end
+ records
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.