Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/criteria.rb
Overview
FIXME: Argh! ruby’s class variables are not per-class… so setting a class variable in a subclass overrides the parent class… in order to get around this, we need to store the associations per class, and then iterate over a classes parent classes to find all applicable associations
Class Method Summary collapse
-
.[](column) ⇒ Object
Access the column object by using the class as a hash.
- .__ar_belongs_to ⇒ Object
- .__ar_count ⇒ Object
- .__ar_find ⇒ Object
- .__ar_has_many ⇒ Object
-
.__ar_method_missing ⇒ Object
alias_method :__ar_inherited, :inherited.
- .belongs_to(id, opts = {}) ⇒ Object
- .count(*a) ⇒ Object
-
.criteria_columns ⇒ Object
puts “enhancing #self < #selfself.superclass” @@one_to_many_associations = {} @@many_to_one_associations = {} @@criteria_columns = nil.
-
.find(*a) ⇒ Object
Provide the ability to search by criteria using the normal find() syntax.
-
.find_associations_with(clazz) ⇒ Object
Search the associations defined on this class for one that would map to the given class This returns an array of symbols which are equal to the symbols used in the has_many and belongs_to definitions.
- .has_many(id, opts = {}, &block) ⇒ Object
- .many_to_one_associations ⇒ Object
- .method_missing(*a) ⇒ Object
- .new_criteria(c = nil, operator = :AND) ⇒ Object
- .one_to_many_associations ⇒ Object
-
.rewrite_criteria_in_args(args) ⇒ Object
protected.
Class Method Details
.[](column) ⇒ Object
Access the column object by using the class as a hash. This is useful if there are other static methods that have the same name as your field, or the field name is not, for some reason, allowed as a ruby method name
Usually, you can just call Class.column_name to access this column object
771 772 773 |
# File 'lib/criteria.rb', line 771 def [](column) criteria_columns[column.to_s] end |
.__ar_belongs_to ⇒ Object
685 |
# File 'lib/criteria.rb', line 685 alias_method :__ar_belongs_to, :belongs_to |
.__ar_count ⇒ Object
684 |
# File 'lib/criteria.rb', line 684 alias_method :__ar_count, :count |
.__ar_find ⇒ Object
683 |
# File 'lib/criteria.rb', line 683 alias_method :__ar_find, :find |
.__ar_has_many ⇒ Object
686 |
# File 'lib/criteria.rb', line 686 alias_method :__ar_has_many, :has_many |
.__ar_method_missing ⇒ Object
alias_method :__ar_inherited, :inherited
682 |
# File 'lib/criteria.rb', line 682 alias_method :__ar_method_missing, :method_missing |
.belongs_to(id, opts = {}) ⇒ Object
795 796 797 798 799 800 |
# File 'lib/criteria.rb', line 795 def belongs_to(id, opts={}) # self.__criteria_enhance puts "#{self} belongs to #{id} with #{opts.inspect}" if Criteria.debug many_to_one_associations[id] = opts __ar_belongs_to(id, opts) end |
.count(*a) ⇒ Object
698 699 700 701 |
# File 'lib/criteria.rb', line 698 def count(*a) a = rewrite_criteria_in_args(a) __ar_count(*a) end |
.criteria_columns ⇒ Object
puts “enhancing #self < #selfself.superclass” @@one_to_many_associations = {} @@many_to_one_associations = {} @@criteria_columns = nil
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 |
# File 'lib/criteria.rb', line 737 def criteria_columns if static_get(:criteria_columns).nil? static_set(:criteria_columns, {}) # do some more initialization self.columns.each do |c| static_get(:criteria_columns)[c.name.to_s] = Criteria::Column.new(self, c, {}, self.connection) end self.one_to_many_associations.each do |k,v| puts "found 1->m #{k} => #{v.inspect}" if Criteria.debug static_get(:criteria_columns)[k.to_s] = Criteria::OneToManyAssociation.new(self, k, v, self.connection) end self.many_to_one_associations.each do |k,v| puts "found m->1 #{k} => #{v.inspect}" if Criteria.debug static_get(:criteria_columns)[k.to_s] = Criteria::ManyToOneAssociation.new(self, k, v, self.connection) end end puts "call criteria_columns on #{self} (#{static_get(:criteria_columns).keys.inspect})" if Criteria.debug static_get(:criteria_columns) end |
.find(*a) ⇒ Object
Provide the ability to search by criteria using the normal find() syntax. Basically we just turn the criteria object into a normal query hash (:conditions, :order, :limit, etc)
Since this can come as the first or second argument in the find() method, we just scan for any instances of Criteria and then call to_hash
693 694 695 696 |
# File 'lib/criteria.rb', line 693 def find(*a) a = rewrite_criteria_in_args(a) __ar_find(*a) end |
.find_associations_with(clazz) ⇒ Object
Search the associations defined on this class for one that would map to the given class This returns an array of symbols which are equal to the symbols used in the has_many and belongs_to definitions
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 |
# File 'lib/criteria.rb', line 778 def find_associations_with(clazz) associations = [] pluralized = Inflector.pluralize(clazz.table_name.to_s).intern singularized = clazz.table_name.to_s.singularize.intern puts "does #{self} 1->m #{one_to_many_associations.inspect} contain #{pluralized}" if Criteria.debug if !one_to_many_associations[pluralized].nil? associations << pluralized associations << one_to_many_associations[pluralized][:through] if !one_to_many_associations[pluralized][:through].nil? end puts "does #{self} m->1 #{many_to_one_associations.inspect} contain #{singularized}" if Criteria.debug if !many_to_one_associations[singularized].nil? associations << singularized # associations << many_to_one_associations[singularized][:through] if !many_to_one_associations[singularized][:through].nil? end associations #+clazz.find_associations_with(self) end |
.has_many(id, opts = {}, &block) ⇒ Object
802 803 804 805 806 807 808 809 |
# File 'lib/criteria.rb', line 802 def has_many(id, opts={}, &block) # self.__criteria_enhance puts "#{self} has many #{id} with #{opts.inspect}" if Criteria.debug puts "#{self.one_to_many_associations.inspect}" if Criteria.debug one_to_many_associations[id] = opts puts "#{self.one_to_many_associations.inspect}" if Criteria.debug __ar_has_many(id, opts, &block) end |
.many_to_one_associations ⇒ Object
762 763 764 |
# File 'lib/criteria.rb', line 762 def many_to_one_associations static_get(:many_to_one_associations, {}) end |
.method_missing(*a) ⇒ Object
703 704 705 706 707 708 709 710 |
# File 'lib/criteria.rb', line 703 def method_missing(*a) puts "seeking method #{a[0]}: #{criteria_columns[a[0]]}" if Criteria.debug if !criteria_columns[a[0].to_s].nil? criteria_columns[a[0].to_s] else __ar_method_missing(*a) end end |
.new_criteria(c = nil, operator = :AND) ⇒ Object
670 671 672 673 674 675 676 677 678 |
# File 'lib/criteria.rb', line 670 def self.new_criteria(c=nil, operator=:AND) if c==:AND or c==:OR Criteria.new(self, c) else crit = Criteria.new(self, operator) crit << c if !c.nil? crit end end |
.one_to_many_associations ⇒ Object
758 759 760 |
# File 'lib/criteria.rb', line 758 def one_to_many_associations static_get(:one_to_many_associations, {}) end |
.rewrite_criteria_in_args(args) ⇒ Object
protected
714 715 716 717 718 719 720 721 722 723 |
# File 'lib/criteria.rb', line 714 def rewrite_criteria_in_args(args) args.collect do |arg| if arg.is_a? Criteria or arg.is_a? Criteria::Criterion # puts "#{arg.to_hash.inspect}" arg.to_hash else arg end end end |