Methods
Public Instance methods
[ show source ]
# File kirbybase_adapter.rb, line 852
852: def construct_sql
853: if @options[:finder_sql]
854: raise ArgumentError, "KirbyBase does not support SQL! #{@options[:finder_sql].inspect}" unless @options[:finder_sql].is_a? Proc
855: @finder_sql = lambda{|rec| @options[:finder_sql][rec, @owner] }
856: else
857: extra_conditions = @options[:conditions] ? @association_class.build_conditions_from_options(@options) : nil
858: @finder_sql = if extra_conditions
859: lambda{ |rec| rec.send(@association_class_primary_key_name) == @owner.id and extra_conditions[rec] }
860: else
861: lambda{ |rec| rec.send(@association_class_primary_key_name) == @owner.id }
862: end
863: end
864:
865: if @options[:counter_sql]
866: raise ArgumentError, "KirbyBase does not support SQL! #{@options[:counter_sql].inspect}" unless @options[:counter_sql].is_a? Proc
867: @counter_sql = lambda{|rec| @options[:counter_sql][rec, @owner] }
868: elsif @options[:finder_sql] && @options[:finder_sql].is_a?(Proc)
869: @counter_sql = @finder_sql
870: else
871: extra_conditions = @options[:conditions] ? @association_class.build_conditions_from_options(@options) : nil
872: @counter_sql = if @options[:conditions]
873: lambda{|rec| rec.send(@association_class_primary_key_name) == @owner.id and extra_conditions[rec]}
874: else
875: lambda{|rec| rec.send(@association_class_primary_key_name) == @owner.id}
876: end
877: end
878: end
Count the number of associated records. All arguments are optional.
[ show source ]
# File kirbybase_adapter.rb, line 927
927: def count(runtime_conditions = nil)
928: if @options[:counter_sql]
929: @association_class.count(@counter_sql)
930: elsif @options[:finder_sql]
931: @association_class.count(@finder_sql)
932: else
933: sql = if runtime_conditions
934: runtime_conditions = @association_class.build_conditions_from_options(:conditions => runtime_conditions)
935: lambda{|rec| runtime_conditions[rec] && @finder_sql[rec, @owner] }
936: else
937: @finder_sql
938: end
939: @association_class.count(sql)
940: end
941: end
[ show source ]
# File kirbybase_adapter.rb, line 943
943: def count_records
944: count = if has_cached_counter?
945: @owner.send(:read_attribute, cached_counter_attribute_name)
946: else
947: @association_class.count(@counter_sql)
948: end
949:
950: @target = [] and loaded if count == 0
951:
952: return count
953: end
[ show source ]
# File kirbybase_adapter.rb, line 880
880: def delete_records(records)
881: case @options[:dependent]
882: when true
883: records.each { |r| r.destroy }
884:
885: # when :delete_all
886: # ids = records.map{|rec| rec.id}
887: # @association_class.table.delete do |rec|
888: # rec.send(@association_class_primary_key_name) == @owner.id && ids.include?(rec.recno)
889: # end
890:
891: else
892: ids = records.map{|rec| rec.id}
893: @association_class.table.update do |rec|
894: rec.send(@association_class_primary_key_name) == @owner.id && ids.include?(rec.recno)
895: end.set { |rec| rec.send "#@association_class_primary_key_name=", nil}
896: end
897: end
[ show source ]
# File kirbybase_adapter.rb, line 812
812: def find(*args)
813: options = Base.send(:extract_options_from_args!, args)
814:
815: # If using a custom finder_sql, scan the entire collection.
816: if @options[:finder_sql]
817: expects_array = args.first.kind_of?(Array)
818: ids = args.flatten.compact.uniq
819:
820: if ids.size == 1
821: id = ids.first
822: record = load_target.detect { |record| id == record.id }
823: expects_array? ? [record] : record
824: else
825: load_target.select { |record| ids.include?(record.id) }
826: end
827: else
828: options[:conditions] = if options[:conditions]
829: selector = @association_class.build_conditions_from_options(options)
830: if @finder_sql
831: lambda{|rec| selector[rec] && @finder_sql[rec]}
832: else
833: selector
834: end
835: elsif @finder_sql
836: @finder_sql
837: end
838:
839:
840: if options[:order] && @options[:order]
841: options[:order] = "#{options[:order]}, #{@options[:order]}"
842: elsif @options[:order]
843: options[:order] = @options[:order]
844: end
845:
846: # Pass through args exactly as we received them.
847: args << options
848: @association_class.find(*args)
849: end
850: end
DEPRECATED, but still covered by the AR tests
[ show source ]
# File kirbybase_adapter.rb, line 911
911: def find_all(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil)
912: if @options[:finder_sql]
913: @association_class.find(@finder_sql)
914: else
915: selector = if runtime_conditions
916: runtime_conditions_block = @association_class.build_conditions_from_options(:conditions => runtime_conditions)
917: lambda{|rec| runtime_conditions_block[rec] && @finder_sql[rec] }
918: else
919: @finder_sql
920: end
921: orderings ||= @options[:order]
922: @association_class.find_all(selector, orderings, limit, joins)
923: end
924: end
[ show source ]
# File kirbybase_adapter.rb, line 899
899: def find_target
900: @association_class.find(:all,
901: :conditions => @finder_sql,
902: :order => @options[:order],
903: :limit => @options[:limit],
904: :joins => @options[:joins],
905: :include => @options[:include],
906: :group => @options[:group]
907: )
908: end